-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ae74a2
commit 12dc3a8
Showing
5 changed files
with
102 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 5 additions & 59 deletions
64
src/main/kotlin/com/jaredsburrows/license/LicensePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,19 @@ | ||
package com.jaredsburrows.license | ||
|
||
import com.android.build.gradle.AppExtension | ||
import com.android.build.gradle.AppPlugin | ||
import com.android.build.gradle.BaseExtension | ||
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.api.BaseVariant | ||
import org.gradle.api.DomainObjectSet | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.plugins.JavaPlugin | ||
import java.util.Locale | ||
|
||
/** A [Plugin] which grabs the POM.xml files from maven dependencies. */ | ||
class LicensePlugin : Plugin<Project> { | ||
override fun apply(project: Project) { | ||
project.extensions.add("licenseReport", LicenseReportExtension::class.java) | ||
|
||
project.plugins.all { | ||
when (it) { | ||
is JavaPlugin -> project.configureJavaProject() | ||
is FeaturePlugin -> { | ||
project.extensions.getByType(FeatureExtension::class.java).run { | ||
project.configureAndroidProject(featureVariants) | ||
project.configureAndroidProject(libraryVariants) | ||
} | ||
} | ||
is LibraryPlugin -> { | ||
project.extensions.getByType(LibraryExtension::class.java).run { | ||
project.configureAndroidProject(libraryVariants) | ||
} | ||
} | ||
is AppPlugin -> { | ||
project.extensions.getByType(AppExtension::class.java).run { | ||
project.configureAndroidProject(applicationVariants) | ||
} | ||
} | ||
project.afterEvaluate { | ||
when { | ||
project.isAndroidProject() -> project.configureAndroidProject() | ||
project.isJavaProject() -> project.configureJavaProject() | ||
else -> throw UnsupportedOperationException("'com.jaredsburrows.license' requires Java or Android Gradle Plugins.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Configure for Java projects. */ | ||
private fun Project.configureJavaProject() { | ||
tasks.register("licenseReport", LicenseReportTask::class.java) | ||
} | ||
|
||
/** Configure for Android projects. */ | ||
private fun Project.configureAndroidProject(variants: DomainObjectSet<out BaseVariant>? = null) { | ||
// Configure tasks for all variants | ||
variants?.all { variant -> | ||
val name = variant.name.replaceFirstChar { | ||
if (it.isLowerCase()) { | ||
it.titlecase(Locale.getDefault()) | ||
} else { | ||
it.toString() | ||
} | ||
} | ||
|
||
tasks.register("license${name}Report", LicenseReportTask::class.java) { | ||
it.assetDirs = (extensions.getByName("android") as BaseExtension) | ||
.sourceSets | ||
.getByName("main") | ||
.assets | ||
.srcDirs | ||
.toList() | ||
it.variantName = variant.name | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.jaredsburrows.license | ||
|
||
import com.android.build.gradle.AppExtension | ||
import com.android.build.gradle.AppPlugin | ||
import com.android.build.gradle.BaseExtension | ||
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.api.BaseVariant | ||
import org.gradle.api.DomainObjectSet | ||
import org.gradle.api.Project | ||
import java.util.Locale | ||
|
||
/** Returns true if Android Gradle project */ | ||
internal fun Project.isAndroidProject(): Boolean { | ||
return project.plugins.hasPlugin("android") | ||
} | ||
|
||
/** | ||
* Configure for Android projects. | ||
* | ||
* AppPlugin - "android", "com.android.application" | ||
* FeaturePlugin - "com.android.feature" | ||
* LibraryPlugin - "android-library", "com.android.library" | ||
*/ | ||
internal fun Project.configureAndroidProject() { | ||
project.plugins.all { | ||
when (it) { | ||
is AppPlugin -> { | ||
project.extensions.getByType(AppExtension::class.java).run { | ||
configureVariant(applicationVariants) | ||
} | ||
} | ||
is FeaturePlugin -> { | ||
project.extensions.getByType(FeatureExtension::class.java).run { | ||
configureVariant(featureVariants) | ||
configureVariant(libraryVariants) | ||
} | ||
} | ||
is LibraryPlugin -> { | ||
project.extensions.getByType(LibraryExtension::class.java).run { | ||
configureVariant(libraryVariants) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Project.configureVariant(variants: DomainObjectSet<out BaseVariant>? = null) { | ||
// Configure tasks for all variants | ||
variants?.all { variant -> | ||
val name = variant.name.replaceFirstChar { | ||
if (it.isLowerCase()) { | ||
it.titlecase(Locale.getDefault()) | ||
} else { | ||
it.toString() | ||
} | ||
} | ||
|
||
tasks.register("license${name}Report", LicenseReportTask::class.java) { | ||
it.assetDirs = (extensions.getByName("android") as BaseExtension) | ||
.sourceSets | ||
.getByName("main") | ||
.assets | ||
.srcDirs | ||
.toList() | ||
it.variantName = variant.name | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jaredsburrows.license | ||
|
||
import org.gradle.api.Project | ||
|
||
/** Returns true if Java Gradle project */ | ||
internal fun Project.isJavaProject(): Boolean { | ||
return project.plugins.hasPlugin("java") | ||
} | ||
|
||
/** | ||
* Configure for Java projects. | ||
* | ||
* JavaPlugin - "java" - also applies JavaBasePlugin | ||
* JavaLibraryPlugin - "java-library" - also applies JavaPlugin | ||
*/ | ||
internal fun Project.configureJavaProject() { | ||
tasks.register("licenseReport", LicenseReportTask::class.java) | ||
} |