diff --git a/src/main/kotlin/com/jaredsburrows/license/project.kt b/src/main/kotlin/com/jaredsburrows/license/project.kt new file mode 100644 index 00000000..c648bb45 --- /dev/null +++ b/src/main/kotlin/com/jaredsburrows/license/project.kt @@ -0,0 +1,8 @@ +package com.jaredsburrows.license + +import org.gradle.api.Project + +/** Returns true if plugin exists in project */ +internal fun Project.hasPlugin(list: List): Boolean { + return list.find { project.plugins.hasPlugin(it) } != null +} diff --git a/src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt b/src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt index 2b83e756..1ba4e053 100644 --- a/src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt +++ b/src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt @@ -7,6 +7,8 @@ import com.android.build.gradle.FeatureExtension import com.android.build.gradle.FeaturePlugin import com.android.build.gradle.LibraryExtension import com.android.build.gradle.LibraryPlugin +import com.android.build.gradle.TestExtension +import com.android.build.gradle.TestPlugin import com.android.build.gradle.api.BaseVariant import org.gradle.api.DomainObjectSet import org.gradle.api.Project @@ -14,7 +16,20 @@ import java.util.Locale /** Returns true if Android Gradle project */ internal fun Project.isAndroidProject(): Boolean { - return project.plugins.hasPlugin("android") + return hasPlugin( + listOf( + // AppPlugin + "android", + "com.android.application", + // FeaturePlugin + "com.android.feature", + // LibraryPlugin + "android-library", + "com.android.library", + // TestPlugin + "com.android.test", + ) + ) } /** @@ -23,6 +38,7 @@ internal fun Project.isAndroidProject(): Boolean { * AppPlugin - "android", "com.android.application" * FeaturePlugin - "com.android.feature" * LibraryPlugin - "android-library", "com.android.library" + * TestPlugin - "com.android.test" */ internal fun Project.configureAndroidProject() { project.plugins.all { @@ -43,6 +59,11 @@ internal fun Project.configureAndroidProject() { configureVariant(libraryVariants) } } + is TestPlugin -> { + project.extensions.getByType(TestExtension::class.java).run { + configureVariant(applicationVariants) + } + } } } } diff --git a/src/main/kotlin/com/jaredsburrows/license/projectJava.kt b/src/main/kotlin/com/jaredsburrows/license/projectJava.kt index 10f0d7a7..d41306b1 100644 --- a/src/main/kotlin/com/jaredsburrows/license/projectJava.kt +++ b/src/main/kotlin/com/jaredsburrows/license/projectJava.kt @@ -4,7 +4,15 @@ import org.gradle.api.Project /** Returns true if Java Gradle project */ internal fun Project.isJavaProject(): Boolean { - return project.plugins.hasPlugin("java") + return hasPlugin( + listOf( + "application", // JavaApplicationPlugin + "groovy", // GroovyPlugin + "java", // JavaPlugin, also applies JavaBasePlugin, + "java-library", // JavaLibraryPlugin, also applies JavaPlugin + "scala", // ScalaPlugin + ) + ) } /** diff --git a/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy b/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy new file mode 100644 index 00000000..69c244db --- /dev/null +++ b/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy @@ -0,0 +1,185 @@ +package com.jaredsburrows.license + + +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification +import spock.lang.Unroll + +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS +import static test.TestUtils.gradleWithCommand + +final class LicensePluginSpec extends Specification { + @Rule public final TemporaryFolder testProjectDir = new TemporaryFolder() + private List pluginClasspath + private String classpathString + private File buildFile + + def 'setup'() { + def pluginClasspathResource = getClass().classLoader.getResource('plugin-classpath.txt') + if (pluginClasspathResource == null) { + throw new IllegalStateException( + 'Did not find plugin classpath resource, run `testClasses` build task.') + } + + pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) } + classpathString = pluginClasspath + .collect { it.absolutePath.replace('\\', '\\\\') } // escape backslashes in Windows paths + .collect { "'$it'" } + .join(", ") + buildFile = testProjectDir.newFile('build.gradle') + } + + def 'apply with buildscript'() { + given: + buildFile << + """ + buildscript { + repositories { + mavenCentral() + google() + } + + dependencies { + classpath files($classpathString) + } + } + + apply plugin: 'java' + apply plugin: 'com.jaredsburrows.license' + """ + + when: + def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s') + + then: + result.task(':licenseReport').outcome == SUCCESS + } + + def 'apply with plugins'() { + given: + buildFile << + """ + plugins { + id 'java' + id 'com.jaredsburrows.license' + } + """ + + when: + def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s') + + then: + result.task(':licenseReport').outcome == SUCCESS + } + + @Unroll def 'apply with allowed java plugins: #javaPlugin'() { + given: + buildFile << + """ + plugins { + id '${javaPlugin}' + id 'com.jaredsburrows.license' + } + """ + + when: + def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s') + + then: + result.task(':licenseReport').outcome == SUCCESS + + where: + javaPlugin << [ + 'application', // JavaApplicationPlugin + 'groovy', // GroovyPlugin + 'java', // JavaPlugin + 'java-library', // JavaLibraryPlugin + 'scala', // ScalaPlugin + ] + } + + @Unroll def 'apply with allowed android plugins: #androidPlugin'() { + given: + testProjectDir.newFile('settings.gradle') << + """ + include 'subproject' + """ + + testProjectDir.newFolder('subproject') + + testProjectDir.newFile('subproject/build.gradle') << + """ + buildscript { + repositories { + mavenCentral() + google() + } + + dependencies { + classpath "com.android.tools.build:gradle:3.6.4" + classpath files($classpathString) + } + } + + apply plugin: 'com.android.application' + apply plugin: 'com.jaredsburrows.license' + + android { + compileSdkVersion 28 + + defaultConfig { + if (project.plugins.hasPlugin("com.android.application")) applicationId 'com.example' + if (project.plugins.hasPlugin("com.android.test")) targetProjectPath ':subproject' + } + } + """ + + buildFile << + """ + buildscript { + repositories { + mavenCentral() + google() + } + + dependencies { + classpath "com.android.tools.build:gradle:3.6.4" + classpath files($classpathString) + } + } + + apply plugin: '${androidPlugin}' + apply plugin: 'com.jaredsburrows.license' + + android { + compileSdkVersion 28 + + defaultConfig { + if (project.plugins.hasPlugin("com.android.application")) applicationId 'com.example' + if (project.plugins.hasPlugin("com.android.test")) targetProjectPath ':subproject' + } + } + """ + + when: + def result = gradleWithCommand(testProjectDir.root, 'licenseDebugReport', '-s') + + then: + result.task(':licenseDebugReport').outcome == SUCCESS + + where: + androidPlugin << [ + // AppPlugin + 'android', + 'com.android.application', + // FeaturePlugin + 'com.android.feature', + // LibraryPlugin + 'android-library', + 'com.android.library', + // TestPlugin + 'com.android.test', + ] + } +}