Skip to content

Commit

Permalink
configure common task (#216)
Browse files Browse the repository at this point in the history
- Introduce extension to configure tasks based on common settings
- Remove `LicenseReportTask`
- Fix misspellings
  • Loading branch information
jaredsburrows authored May 30, 2022
1 parent ec5f8ec commit a441c46
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ package com.jaredsburrows.license

/** Configuration options for the gradle license plugin. */
open class LicenseReportExtension { // extensions can't be final
/** Whether or not the Csv report should be generated. */
/** Whether the Csv report should be generated. */
var generateCsvReport = true

/** Whether or not the HTML report should be generated. */
/** Whether the HTML report should be generated. */
var generateHtmlReport = true

/** Whether or not the JSON report should be generated. */
/** Whether the JSON report should be generated. */
var generateJsonReport = true

/** Whether or not the Text report should be generated. */
/** Whether the Text report should be generated. */
var generateTextReport = true

/**
* Whether or not the Csv report should be copied to the Android assets directory. Ignored if the
* Whether the Csv report should be copied to the Android assets directory. Ignored if the
* project is not an Android project. Has no effect if the Csv report is disabled.
*/
var copyCsvReportToAssets = false

/**
* Whether or not the HTML report should be copied to the Android assets directory. Ignored if the
* Whether the HTML report should be copied to the Android assets directory. Ignored if the
* project is not an Android project. Has no effect if the HTML report is disabled.
*/
var copyHtmlReportToAssets = true

/**
* Whether or not the JSON report should be copied to the Android assets directory. Ignored if the
* Whether the JSON report should be copied to the Android assets directory. Ignored if the
* project is not an Android project. Has no effect if the JSON report is disabled.
*/
var copyJsonReportToAssets = false

/**
* Wheither when copying reports to the Android assets directory, it uses the variant-specific
* Whether when copying reports to the Android assets directory, it uses the variant-specific
* asset directory instead of main. Defaults to false.
*/
var useVariantSpecificAssetDirs = false

/**
* Whether or not the Text report should be copied to the Android assets directory. Ignored if the
* Whether the Text report should be copied to the Android assets directory. Ignored if the
* project is not an Android project. Has no effect if the Text report is disabled.
*/
var copyTextReportToAssets = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.apache.maven.model.Developer
import org.apache.maven.model.License
import org.apache.maven.model.Model
import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.ResolvedArtifact
Expand All @@ -18,6 +19,7 @@ import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.FileReader
Expand All @@ -27,20 +29,64 @@ import java.util.Locale
import java.util.UUID

/** A [org.gradle.api.Task] that creates HTML and JSON reports of the current projects dependencies. */
internal open class LicenseReportTask : BaseLicenseReportTask() { // tasks can't be final
internal open class LicenseReportTask : DefaultTask() { // tasks can't be final

@Input
var assetDirs = emptyList<File>()

@Optional
@Input
var variantName: String? = null

@Input var assetDirs = emptyList<File>()
@Optional @Input var variantName: String? = null
// This input is used by the task indirectly via some project properties (such as "configurations" and "dependencies")
// that affect the task's outcome. When the mentioned project properties change the task should re-run the next time
// it is requested and should *not* be marked as UP-TO-DATE.
@InputFile var buildFile: File? = null
@InputFile
var buildFile: File? = null

// Task annotations cannot be internal
@get:OutputDirectory
lateinit var outputDir: File

@Input
var generateCsvReport = false

@Input
var generateHtmlReport = false

@Input
var generateJsonReport = false

@Input
var generateTextReport = false

@Input
var copyCsvReportToAssets = false

@Input
var copyHtmlReportToAssets = false

@Input
var copyJsonReportToAssets = false

@Input
var copyTextReportToAssets = false

@Input
var useVariantSpecificAssetDirs = false

private val projects = mutableListOf<Model>()
private var pomConfiguration = "poms"
private var tempPomConfiguration = "tempPoms"

@TaskAction fun licenseReport() {
init {
// From DefaultTask
description = "Outputs licenses report for $name."
group = "Reporting"
}

@TaskAction
fun licenseReport() {
val mavenReader = MavenXpp3Reader()
val configurations: ConfigurationContainer = project.configurations
val dependencies: DependencyHandler = project.dependencies
Expand Down Expand Up @@ -207,8 +253,8 @@ internal open class LicenseReportTask : BaseLicenseReportTask() { // tasks can't
/**
* Attempting to getAllModuleArtifacts on a local library project will result
* in AmbiguousVariantSelectionException as there are not enough criteria
* to match a specific variant of the library project. Instead we skip the
* the library project itself and enumerate its dependencies.
* to match a specific variant of the library project. Instead, we skip the
* library project itself and enumerate its dependencies.
*/
"unspecified" -> resolvedArtifacts += getResolvedArtifactsFromResolvedDependencies(
resolvedDependency.children
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
package com.jaredsburrows.license

import org.gradle.api.Project
import org.gradle.api.reporting.ReportingExtension

/** Returns true if plugin exists in project */
/** Returns true if plugin exists in project. */
internal fun Project.hasPlugin(list: List<String>): Boolean {
return list.find { project.plugins.hasPlugin(it) } != null
return list.find { plugins.hasPlugin(it) } != null
}

/** Configure common configuration for both Java and Android tasks. */
internal fun Project.configureCommon(task: LicenseReportTask) {
task.buildFile = buildFile

// Customizing internal task options
task.outputDir = extensions.getByType(ReportingExtension::class.java).file("licenses")

// Customizing internal task options from extension
val extension = extensions.getByType(LicenseReportExtension::class.java)
task.generateCsvReport = extension.generateCsvReport
task.generateHtmlReport = extension.generateHtmlReport
task.generateJsonReport = extension.generateJsonReport
task.generateTextReport = extension.generateTextReport
task.copyCsvReportToAssets = extension.copyCsvReportToAssets
task.copyHtmlReportToAssets = extension.copyHtmlReportToAssets
task.copyJsonReportToAssets = extension.copyJsonReportToAssets
task.copyTextReportToAssets = extension.copyTextReportToAssets
task.useVariantSpecificAssetDirs = extension.useVariantSpecificAssetDirs
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import java.util.Locale

/** Returns true if Android Gradle project */
/** Returns true if Android Gradle project. */
internal fun Project.isAndroidProject(): Boolean {
return hasPlugin(
listOf(
Expand All @@ -41,32 +41,32 @@ internal fun Project.isAndroidProject(): Boolean {
* TestPlugin - "com.android.test"
*/
internal fun Project.configureAndroidProject() {
project.plugins.all {
plugins.all {
when (it) {
is AppPlugin -> {
project.extensions.getByType(AppExtension::class.java).run {
extensions.getByType(AppExtension::class.java).run {
configureVariant(this, applicationVariants)
configureVariant(this, testVariants)
configureVariant(this, unitTestVariants)
}
}
is FeaturePlugin -> {
project.extensions.getByType(FeatureExtension::class.java).run {
extensions.getByType(FeatureExtension::class.java).run {
configureVariant(this, featureVariants)
configureVariant(this, libraryVariants)
configureVariant(this, testVariants)
configureVariant(this, unitTestVariants)
}
}
is LibraryPlugin -> {
project.extensions.getByType(LibraryExtension::class.java).run {
extensions.getByType(LibraryExtension::class.java).run {
configureVariant(this, libraryVariants)
configureVariant(this, testVariants)
configureVariant(this, unitTestVariants)
}
}
is TestPlugin -> {
project.extensions.getByType(TestExtension::class.java).run {
extensions.getByType(TestExtension::class.java).run {
configureVariant(this, applicationVariants)
}
}
Expand All @@ -89,6 +89,10 @@ private fun Project.configureVariant(
}

tasks.register("license${name}Report", LicenseReportTask::class.java) {
// Apply common task configuration first
configureCommon(it)

// Custom for Android tasks
val sourceSetName = if (it.useVariantSpecificAssetDirs) variant.name else "main"
it.assetDirs = baseExtension
.sourceSets
Expand All @@ -97,7 +101,6 @@ private fun Project.configureVariant(
.srcDirs
.toList()
it.variantName = variant.name
it.buildFile = buildFile
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.jaredsburrows.license

import org.gradle.api.Project

/** Returns true if Java Gradle project */
/** Returns true if Java Gradle project. */
internal fun Project.isJavaProject(): Boolean {
return hasPlugin(
listOf(
Expand All @@ -15,10 +15,11 @@ internal fun Project.isJavaProject(): Boolean {
/**
* Configure for Java projects.
*
* All of these plugins will apply the JavaPlugin(relies on JavaBasePlugin) or JavaPlatformPlugin.
* All of these plugins will apply the JavaPlugin(relies on JavaBasePlugin).
*/
internal fun Project.configureJavaProject() {
tasks.register("licenseReport", LicenseReportTask::class.java) {
it.buildFile = buildFile
// Apply common task configuration first
configureCommon(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ final class LicensePluginVersionSpec extends Specification {
'7.4.2',
],
[
'7.2.0-rc01',
'7.2.1',
]
].combinations()
}
Expand Down

0 comments on commit a441c46

Please sign in to comment.