Skip to content

Commit

Permalink
add support for groovy and java projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredsburrows committed Dec 28, 2016
1 parent 6efec81 commit 5cff0b2
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 56 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
[![Twitter Follow](https://img.shields.io/twitter/follow/jaredsburrows.svg?style=social)](https://twitter.com/jaredsburrows)

This plugin provides a task to generate a HTML license report based on the
configuration variant. (eg. `licenseDebugReport` for all debug dependencies).
configuration. (eg. `licenseDebugReport` for all debug dependencies in an Android project).

Applying this to an Android App or Library project will generate a the license
file(`open_source_licenses.html`) and copy it to `<project>/src/main/assets/`.
Applying this to an Android or Java project will generate a the license
file(`open_source_licenses.html`) in the `<project>/build/reports/licenses/`.

Also, for Android projects the license HTML file will be copied to `<project>/src/main/assets/`.

## Download

Expand Down Expand Up @@ -48,9 +50,10 @@ apply plugin: "com.jaredsburrows.license"

## Tasks

**`license${variant}Report`**
- **`license${variant}Report`** for Android
- **`licenseReport`** for Java

Generates a HTML report of the all open source licenses. (eg. `licenseDebugReport` for all debug dependencies).
Generates a HTML report of the all open source licenses. (eg. `licenseDebugReport` for all debug dependencies in an Android project).

Example `build.gradle`:

Expand Down Expand Up @@ -85,6 +88,27 @@ HTML:
</html>
```

JSON:
```json
[
{
"project":"Android GIF Drawable Library",
"license":"The MIT License",
"license_url":"http://opensource.org/licenses/MIT"
},
{
"project":"Design",
"license":"The Apache Software License, Version 2.0",
"license_url":"http://www.apache.org/licenses/LICENSE-2.0.txt"
},
{
"project":"LeakCanary for Android",
"license":"The Apache Software License, Version 2.0",
"license_url":"http://www.apache.org/licenses/LICENSE-2.0.txt"
}
]
```

## Developing

### Building
Expand Down
61 changes: 49 additions & 12 deletions src/main/groovy/com/jaredsburrows/license/LicensePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ final class LicensePlugin implements Plugin<Project> {
final static def ANDROID_APPLICATION_PLUGIN = "com.android.application"
final static def ANDROID_LIBRARY_PLUGIN = "com.android.library"
final static def ANDROID_TEST_PLUGIN = "com.android.test"
final static def GROOVY_PLUGIN = "groovy"
final static def JAVA_PLUGIN = "java"

@Override void apply(Project project) {
// Only allow Android projects for now
if ((project.plugins.hasPlugin(ANDROID_APPLICATION_PLUGIN)
|| project.plugins.hasPlugin(ANDROID_LIBRARY_PLUGIN)
|| project.plugins.hasPlugin(ANDROID_TEST_PLUGIN))) {
configureAndroidProject(project)
} else {
throw new IllegalStateException("License report plugin can only be applied to android projects.")
}
if (isAndroidProject(project)) configureAndroidProject(project) else
if (isJavaProject(project)) configureJavaProject(project) else
throw new IllegalStateException("License report plugin can only be applied to android, groovy or java projects.")
}

/**
* Configure project and all variants for Android.
*/
static def configureAndroidProject(project) {
// Get correct plugin
final def variants = (project.plugins.hasPlugin(ANDROID_APPLICATION_PLUGIN)
? project.android.applicationVariants
: project.android.libraryVariants)
// Get correct plugin - Check for android library, default to application variant for application/test plugin
final def variants = (project.plugins.hasPlugin(ANDROID_LIBRARY_PLUGIN)
? project.android.libraryVariants
: project.android.applicationVariants)

// Configure tasks for all variants
variants.all { variant ->
Expand All @@ -36,7 +37,7 @@ final class LicensePlugin implements Plugin<Project> {

// Create tasks based on variant
final def task = project.tasks.create("$taskName", LicenseReportTask)
task.description = "Outputs licenses for ${variantName} variant."
task.description = "Outputs licenses report for ${variantName} variant."
task.group = "Reporting"
task.htmlFile = project.file(path + LicenseReportTask.HTML_EXT)
task.jsonFile = project.file(path + LicenseReportTask.JSON_EXT)
Expand All @@ -50,4 +51,40 @@ final class LicensePlugin implements Plugin<Project> {
variant.assemble.doLast { task.licenseReport() }
}
}

/**
* Configure project for Groovy/Java.
*/
static def configureJavaProject(project) {
final def taskName = "licenseReport"
final def path = "${project.buildDir}/reports/licenses/$taskName"

// Create tasks
final def task = project.tasks.create("$taskName", LicenseReportTask)
task.description = "Outputs licenses report."
task.group = "Reporting"
task.htmlFile = project.file(path + LicenseReportTask.HTML_EXT)
task.jsonFile = project.file(path + LicenseReportTask.JSON_EXT)
task.outputs.upToDateWhen { false } // Make sure to not to use cache license file, update each run

// Run task
project.assemble.doLast { task.licenseReport() }
}

/**
* Check if the project has Android plugins.
*/
static def isAndroidProject(project) {
(project.plugins.hasPlugin(ANDROID_APPLICATION_PLUGIN)
|| project.plugins.hasPlugin(ANDROID_LIBRARY_PLUGIN)
|| project.plugins.hasPlugin(ANDROID_TEST_PLUGIN))
}

/**
* Check if project has Java plugins.
*/
static def isJavaProject(project) {
(project.plugins.hasPlugin(GROOVY_PLUGIN)
|| project.plugins.hasPlugin(JAVA_PLUGIN))
}
}
25 changes: 19 additions & 6 deletions src/main/groovy/com/jaredsburrows/license/LicenseReportTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LicenseReportTask extends DefaultTask {
final static def APACHE_LICENSE_URL = "http://www.apache.org/licenses/LICENSE-2.0.txt"
final static def PLUGIN_REPOSITORY = "https://plugins.gradle.org/m2/"
final static def ANDROID_REPOSITORY = "file://${System.env.ANDROID_HOME}/extras/android/m2repository"
final static def GOOGLE_REPOSITORY = "file://${System.env.ANDROID_HOME}/extras/google/m2repository"
final static def OPEN_SOURCE_LICENSES = "open_source_licenses"
final static def HTML_EXT = ".html"
final static def JSON_EXT = ".json"
Expand All @@ -41,6 +42,7 @@ class LicenseReportTask extends DefaultTask {

// Add repositories for Android repositories
project.repositories {
maven { url GOOGLE_REPOSITORY } // Local dependencies downloaded from Android SDK manager
maven { url ANDROID_REPOSITORY } // Local dependencies downloaded from Android SDK manager
maven { url PLUGIN_REPOSITORY } // Plugin repository Should proxy jcenter()/mavenCentral()
mavenCentral() // If not in plugin repository, try maven central next
Expand All @@ -49,12 +51,20 @@ class LicenseReportTask extends DefaultTask {

// Add POM information to our POM configuration
def configurations = new LinkedHashSet<>()
configurations << project.configurations.compile // Add default compile configuration
configurations << project.configurations."${buildType}Compile" // Add buildType compile configuration
productFlavors.each { flavor -> // Add productFlavors compile configuration
// Works for productFlavors and productFlavors with dimensions
if (variant.capitalize().contains(flavor.name.capitalize()))
configurations << project.configurations."${flavor.name}Compile"

// Add default compile configuration
configurations << project.configurations.compile

// If Android project, add extra configurations
if (variant) {
// Add buildType compile configuration
configurations << project.configurations."${buildType}Compile"
// Add productFlavors compile configuration
productFlavors.each { flavor ->
// Works for productFlavors and productFlavors with dimensions
if (variant.capitalize().contains(flavor.name.capitalize()))
configurations << project.configurations."${flavor.name}Compile"
}
}

// Iterate through all "compile" configurations's dependencies
Expand Down Expand Up @@ -181,6 +191,9 @@ class LicenseReportTask extends DefaultTask {
printStream.println()
}

// If Android project, copy to asset directory
if (!variant) return

// Copy HTML file to the assets directory
assetDirs.each { directory ->
final def licenseFile = new File(directory.path, OPEN_SOURCE_LICENSES + HTML_EXT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ final class License {
new Builder(this)
}

/**
* Build a new {@link License}.
*/
final static class Builder {
def name
def url
Expand All @@ -37,11 +40,17 @@ final class License {
this.url = license.url
}

/**
* Sets the name of the {@link License}.
*/
Builder name(name) {
this.name = name
this
}

/**
* Sets the URL of the {@link License}.
*/
Builder url(url) {
this.url = url
this
Expand Down
18 changes: 18 additions & 0 deletions src/main/groovy/com/jaredsburrows/license/internal/Project.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class Project {
new Builder(this)
}

/**
* Build a new {@link Project}.
*/
final static class Builder {
def name
def license
Expand All @@ -49,26 +52,41 @@ final class Project {
this.year = project.year
}

/**
* Sets the name of the {@link Project}.
*/
Builder name(name) {
this.name = name
this
}

/**
* Sets the {@link License} of the {@link Project}.
*/
Builder license(license) {
this.license = license
this
}

/**
* Sets the URL of the {@link Project}.
*/
Builder url(url) {
this.url = url
this
}

/**
* Sets the developers/authors of the {@link Project}.
*/
Builder authors(authors) {
this.authors = authors
this
}

/**
* Sets the inception year of the {@link Project}.
*/
Builder year(year) {
this.year = year
this
Expand Down
45 changes: 33 additions & 12 deletions src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ final class LicensePluginSpec extends Specification {
project = ProjectBuilder.builder().build()
}

def "test non android project"() {
def "test unsupported project project"() {
when:
new LicensePlugin().apply(project)

then:
def ex = thrown(IllegalStateException)
ex.message == "License report plugin can only be applied to android projects."
ex.message == "License report plugin can only be applied to android, groovy or java projects."
}

def "test groovy project"() {
Expand All @@ -34,8 +34,7 @@ final class LicensePluginSpec extends Specification {
new LicensePlugin().apply(project)

then:
def ex = thrown(IllegalStateException)
ex.message == "License report plugin can only be applied to android projects."
noExceptionThrown()
}

def "test java project"() {
Expand All @@ -46,11 +45,10 @@ final class LicensePluginSpec extends Specification {
new LicensePlugin().apply(project)

then:
def ex = thrown(IllegalStateException)
ex.message == "License report plugin can only be applied to android projects."
noExceptionThrown()
}

def "test application project"() {
def "test android application project"() {
given:
project.apply plugin: "com.android.application"

Expand All @@ -61,7 +59,7 @@ final class LicensePluginSpec extends Specification {
noExceptionThrown()
}

def "test library project"() {
def "test android library project"() {
given:
project.apply plugin: "com.android.library"

Expand All @@ -72,7 +70,30 @@ final class LicensePluginSpec extends Specification {
noExceptionThrown()
}

def "test default all tasks created"() {
def "test android test project"() {
given:
project.apply plugin: "com.android.test"

when:
new LicensePlugin().apply(project)

then:
noExceptionThrown()
}

def "test groovy/java default all tasks created"() {
given:
project.apply plugin: "java"

when:
project.evaluate()
new LicensePlugin().apply(project)

then:
project.tasks.getByName("licenseReport")
}

def "test android default all tasks created"() {
given:
project.apply plugin: "com.android.application"
project.android {
Expand All @@ -92,7 +113,7 @@ final class LicensePluginSpec extends Specification {
project.tasks.getByName("licenseDebugReport")
}

def "test buildTypes all tasks created"() {
def "test android buildTypes all tasks created"() {
given:
project.apply plugin: "com.android.application"
project.android {
Expand All @@ -118,7 +139,7 @@ final class LicensePluginSpec extends Specification {
project.tasks.getByName("licenseReleaseReport")
}

def "test buildTypes productFlavors all tasks created"() {
def "test android buildTypes productFlavors all tasks created"() {
given:
project.apply plugin: "com.android.application"
project.android {
Expand Down Expand Up @@ -151,7 +172,7 @@ final class LicensePluginSpec extends Specification {
project.tasks.getByName("licenseFlavor2ReleaseReport")
}

def "test buildTypes productFlavors flavorDimensions all tasks created"() {
def "test android buildTypes productFlavors flavorDimensions all tasks created"() {
given:
project.apply plugin: "com.android.application"
project.android {
Expand Down

0 comments on commit 5cff0b2

Please sign in to comment.