Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #178 from novoda/prepare_for_gradle_5
Browse files Browse the repository at this point in the history
Prepare for gradle 5
  • Loading branch information
mr-archano authored Mar 9, 2019
2 parents 7afa8fd + 3f785ed commit 52e8ea2
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 74 deletions.
2 changes: 0 additions & 2 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'
apply from: rootProject.file('gradle/publish.gradle')

sourceSets {
Expand Down Expand Up @@ -29,5 +28,4 @@ dependencies {
testCompile libraries.findbugs.annotations
testCompile libraries.mockito

testRuntime files(pluginUnderTestMetadata)
}
72 changes: 36 additions & 36 deletions plugin/src/test/groovy/com/novoda/test/DeployRulesTestRule.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ class DeployRulesTestRule implements TestRule {
void evaluate() throws Throwable {
cleanRepo()
File projectDir = createProjectDir("${System.currentTimeMillis()}")
createBuildScript(projectDir)
createBuildScripts(projectDir)
GradleRunner.create()
.withProjectDir(projectDir)
.withDebug(true)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.out))
.withArguments('clean', 'publish')
.forwardOutput()
.build()
base.evaluate()
projectDir.deleteDir()
Expand All @@ -46,40 +45,41 @@ class DeployRulesTestRule implements TestRule {
}
}

private void createBuildScript(File projectDir) {
private void createBuildScripts(File projectDir) {
new File(projectDir, 'build.gradle').text = """
buildscript {
repositories {
jcenter()
}
}
version='$version'
apply plugin: 'java'
sourceSets {
main {
resources {
srcDirs = ${resourceDirs.collect { "'$it.path'" }}
}
}
}
apply plugin: 'maven-publish'
publishing {
repositories {
maven { url '${repoDir}' }
}
publications {
mavenJava(MavenPublication) {
groupId '$groupId'
artifactId '$artifactId'
from components.java
}
}
}"""
buildscript {
repositories {
jcenter()
}
}
version='$version'
apply plugin: 'java'
sourceSets {
main {
resources {
srcDirs = ${resourceDirs.collect { "'$it.path'" }}
}
}
}
apply plugin: 'maven-publish'
publishing {
repositories {
maven { url '${repoDir}' }
}
publications {
mavenJava(MavenPublication) {
groupId '$groupId'
artifactId '$artifactId'
from components.java
}
}
}""".stripIndent()
new File(projectDir, 'settings.gradle').text = ''''''
}

private static File createProjectDir(String path) {
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/test/groovy/com/novoda/test/Fixtures.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.common.io.Resources

public final class Fixtures {
private static final Exception NO_INSTANCE_ALLOWED = new UnsupportedOperationException("No instance allowed");
private static final File ROOT_DIR = new File(Resources.getResource('.').file).parentFile.parentFile.parentFile.parentFile
public static final File ROOT_DIR = new File(Resources.getResource('.').file).parentFile.parentFile.parentFile.parentFile
private static final File FIXTURES_DIR = new File(ROOT_DIR, 'src/test/fixtures')
private static final File SOURCES_DIR = new File(FIXTURES_DIR, 'sources')
private static final File REPORTS_DIR = new File(FIXTURES_DIR, 'reports')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.20'
classpath 'com.novoda:gradle-static-analysis-plugin:local'
}
}
plugins {
${formatPlugins(project)}
id 'com.novoda.static-analysis'
}
repositories {
google()
jcenter()
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.novoda.static-analysis'
android {
compileSdkVersion 27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.novoda:gradle-static-analysis-plugin:local'
}
}
plugins {
${formatPlugins(project)}
id 'com.novoda.static-analysis'
}
repositories {
google()
jcenter()
}
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.static-analysis'
android {
compileSdkVersion 27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ final class TestJavaProject extends TestProject<TestJavaProject> {

private static final Closure<String> TEMPLATE = { TestProject project ->
"""
buildscript {
dependencies {
classpath 'com.novoda:gradle-static-analysis-plugin:local'
}
}
plugins {
${formatPlugins(project)}
id 'com.novoda.static-analysis'
}
repositories {
jcenter()
}
apply plugin: 'java'
apply plugin: 'com.novoda.static-analysis'
sourceSets {
${formatSourceSets(project)}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.novoda:gradle-static-analysis-plugin:local'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.10'
}
}
plugins {
${formatPlugins(project)}
id 'com.novoda.static-analysis'
}
apply plugin: 'kotlin'
apply plugin: 'com.novoda.static-analysis'
repositories {
jcenter()
Expand Down
76 changes: 45 additions & 31 deletions plugin/src/test/groovy/com/novoda/test/TestProject.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import javax.annotation.Nullable
abstract class TestProject<T extends TestProject> {
private static final Closure<String> EXTENSION_TEMPLATE = { TestProject project ->
"""
staticAnalysis {
${(project.penalty ?: '').replace(' ', '')}
${(project.toolsConfig ?: '').replace(' ', ' ')}
}
${project.additionalConfiguration}
"""
staticAnalysis {
${(project.penalty ?: '').replace(' ', '')}
${(project.toolsConfig ?: '').replace(' ', ' ')}
}
${project.additionalConfiguration}
""".stripIndent()
}
private static final Closure<String> SETTINGS_GRADLE_TEMPLATE = { String projectName ->
"""
rootProject.name = '$projectName'
includeBuild('$Fixtures.ROOT_DIR/src/test/test-plugin') {
dependencySubstitution {
substitute module('com.novoda:gradle-static-analysis-plugin') with project(':')
}
}
"""
}

private final File projectDir
private final GradleRunner gradleRunner
private final Closure<String> template
String additionalConfiguration = ''
Map<String, List<File>> sourceSets = [main: []]
Expand All @@ -29,11 +38,6 @@ ${project.additionalConfiguration}
TestProject(Closure<String> template) {
this.template = template
this.projectDir = createProjectDir("${System.currentTimeMillis()}")
this.gradleRunner = GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.out))
}

private static File createProjectDir(String path) {
Expand All @@ -44,87 +48,97 @@ ${project.additionalConfiguration}
}

List<String> defaultArguments() {
Collections.emptyList()
['--stacktrace']
}

public T withFile(File source, String path) {
T withFile(File source, String path) {
write(source.text, path)
return this
}

public T withFile(String text, String path) {
T withFile(String text, String path) {
write(text, path)
return this
}

private void write(String text, String path) {
File file = new File(gradleRunner.projectDir, path)
File file = new File(projectDir, path)
file.parentFile.mkdirs()
file.text = text
}

public T copyIntoSourceSet(String sourceSet, File srcDir) {
T copyIntoSourceSet(String sourceSet, File srcDir) {
srcDir.listFiles().each {
withFile(it, "src/${sourceSet}/java/${it.name}")
}
return this
}

public T withSourceSet(String sourceSet, File... srcDirs) {
T withSourceSet(String sourceSet, File... srcDirs) {
sourceSets[sourceSet] = srcDirs
return this
}

public T withPenalty(String penalty) {
T withPenalty(String penalty) {
this.penalty = "penalty $penalty"
return this
}

public T withToolsConfig(String toolsConfig) {
T withToolsConfig(String toolsConfig) {
this.toolsConfig = toolsConfig
return this
}

public T withAdditionalConfiguration(String additionalConfiguration) {
T withAdditionalConfiguration(String additionalConfiguration) {
this.additionalConfiguration = additionalConfiguration
return this
}

public T withPlugin(String plugin, String version = null) {
T withPlugin(String plugin, String version = null) {
this.plugins.add("id '$plugin' ${version ? "version '$version'" : ""}")
return this
}


public Result build(String... arguments) {
Result build(String... arguments) {
BuildResult buildResult = newRunner(arguments).build()
createResult(buildResult)
}

private GradleRunner newRunner(String... arguments) {
new File(projectDir, 'build.gradle').text = template.call(this)
new File(projectDir, 'settings.gradle').text = SETTINGS_GRADLE_TEMPLATE.call(projectDir.name)
List<String> defaultArgs = defaultArguments()
List<String> args = new ArrayList<>(arguments.size() + defaultArgs.size())
args.addAll(defaultArgs)
args.addAll(arguments)
gradleRunner.withArguments(args)
return GradleRunner.create()
.withProjectDir(projectDir)
.withArguments(args)
.forwardOutput()
}

private createResult(BuildResult buildResult) {
new Result(buildResult, new File(projectDir, 'build'))
}

public Result buildAndFail(String... arguments) {
Result buildAndFail(String... arguments) {
BuildResult buildResult = newRunner(arguments).buildAndFail()
createResult(buildResult)
}

public void deleteDir() {
projectDir.deleteDir()
void deleteDir() {
recursiveDelete(projectDir)
}

String projectDir() {
return projectDir
private void recursiveDelete(File file) {
File[] files = file.listFiles()
if (files != null) {
for (File each : files) {
recursiveDelete(each)
}
}
file.delete()
}

protected static String formatExtension(TestProject project) {
Expand All @@ -135,7 +149,7 @@ ${project.additionalConfiguration}
project.plugins.join('\n')
}

public static class Result {
static class Result {
private final BuildResult buildResult
private final File buildDir

Expand All @@ -161,7 +175,7 @@ ${project.additionalConfiguration}
buildResult.task(taskPath).outcome
}

public static class Logs {
static class Logs {
final String output

Logs(String output) {
Expand Down
12 changes: 12 additions & 0 deletions plugin/src/test/test-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: 'groovy'

sourceSets {
main {
groovy.srcDir(project.file('../../main/groovy'))
resources.srcDir(project.file('../../main/resources'))
}
}

dependencies {
implementation gradleApi()
}
Empty file.

0 comments on commit 52e8ea2

Please sign in to comment.