From 8dd5ef9664b4e86ad3952613e59773b82a8df7ef Mon Sep 17 00:00:00 2001 From: aalmiray Date: Tue, 9 Oct 2018 16:55:15 +0200 Subject: [PATCH] Let license aliases be defined using the DSL. Fixes #20 --- README.adoc | 16 +- .../gradle/plugin/base/model/License.groovy | 3 + .../plugin/license/LicensePlugin.groovy | 192 +++++++++++------- 3 files changed, 130 insertions(+), 81 deletions(-) diff --git a/README.adoc b/README.adoc index 58173bd9c..58cdd9fe9 100644 --- a/README.adoc +++ b/README.adoc @@ -194,6 +194,7 @@ license { url distribution comments + aliases } } } @@ -425,13 +426,14 @@ This block maps to the `` block in POM. At least one nested `license` .License.licenses.license [header, cols="5*"] |=== -| Name | Type | Required | Default Value | Description -| id | LicenseId | no* | | -| primary | boolean | no* | false | Identifies this as the main license if there are more than one -| name | String | yes | | Maps to the `` block -| url | String | no | | Maps to the `` block -| distribution | String | no | 'repo' | Maps to the `` block -| comments | String | no | | Maps to the `` block +| Name | Type | Required | Default Value | Description +| id | LicenseId | no* | | +| primary | boolean | no* | false | Identifies this as the main license if there are more than one +| name | String | yes | | Maps to the `` block +| url | String | no | | Maps to the `` block +| distribution | String | no | 'repo' | Maps to the `` block +| comments | String | no | | Maps to the `` block +| aliases | List | no | [] | List of license aliases |=== This entry maps to a `` block nested inside `` in POM. diff --git a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/model/License.groovy b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/model/License.groovy index 0dcf65bb1..c412a3de4 100644 --- a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/model/License.groovy +++ b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/model/License.groovy @@ -35,6 +35,7 @@ class License { String distribution = 'repo' String comments boolean primary + List aliases = [] void setId(LicenseId id) { this.id = id @@ -60,6 +61,7 @@ class License { copy.distribution = distribution copy.comments = comments copy.primary + copy.aliases.addAll(aliases) copy } @@ -70,5 +72,6 @@ class License { o1.distribution = o1.distribution ?: o2?.distribution o1.comments = o1.comments ?: o2?.comments o1.primary = o1.primary ?: o2?.primary + o1.aliases = (o1.aliases + o2.aliases).unique() } } diff --git a/plugins/license-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/license/LicensePlugin.groovy b/plugins/license-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/license/LicensePlugin.groovy index 8d3a4a904..72bcc3f70 100644 --- a/plugins/license-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/license/LicensePlugin.groovy +++ b/plugins/license-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/license/LicensePlugin.groovy @@ -42,6 +42,89 @@ import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject class LicensePlugin implements Plugin { private static final String VISITED = LicensePlugin.class.name.replace('.', '_') + '_VISITED' + private static final LicenseMetadata LICENSE_APACHE_TWO = new LicenseMetadata('The Apache Software License, Version 2.0', LicenseId.APACHE_2_0.url()) + private static final LicenseMetadata LICENSE_EPL1 = new LicenseMetadata('Eclipse Public License v1.0', LicenseId.EPL_1_0.url()) + private static final LicenseMetadata LICENSE_EPL2 = new LicenseMetadata('Eclipse Public License v2.0', LicenseId.EPL_2_0.url()) + private static final LicenseMetadata LICENSE_LGPL21 = new LicenseMetadata('GNU Lesser General Public License v2.1 or later', LicenseId.LGPL_2_1_OR_LATER.url()) + private static final LicenseMetadata LICENSE_MIT = new LicenseMetadata('MIT License', LicenseId.MIT.url()) + private static final LicenseMetadata LICENSE_BSD2 = new LicenseMetadata('BSD 2-Clause FreeBSD License', LicenseId.BSD_2_CLAUSE_FREEBSD.url()) + private static final LicenseMetadata LICENSE_BSD3 = new LicenseMetadata('BSD 3-Clause "New" or "Revised" License', LicenseId.BSD_3_CLAUSE.url()) + + private static final Map LICENSES_MAP = [ + (LicenseId.APACHE_2_0) : LICENSE_APACHE_TWO, + (LicenseId.EPL_1_0) : LICENSE_EPL1, + (LicenseId.EPL_2_0) : LICENSE_EPL2, + (LicenseId.LGPL_2_1_OR_LATER) : LICENSE_LGPL21, + (LicenseId.MIT) : LICENSE_MIT, + (LicenseId.BSD_2_CLAUSE_FREEBSD): LICENSE_BSD2, + (LicenseId.BSD_3_CLAUSE) : LICENSE_BSD3 + ] + + private static final Map> DEFAULT_ALIASES = [ + (LICENSE_APACHE_TWO): ['The Apache Software License, Version 2.0', + 'The Apache Software License, version 2.0', + 'Apache Software License - Version 2.0', + 'Apache Software License - version 2.0', + 'the Apache License, ASL Version 2.0', + 'The Apache License, Version 2.0', + 'The Apache License Version 2.0', + 'Apache License, Version 2.0', + 'Apache License, version 2.0', + 'Apache License Version 2.0', + 'Apache License version 2.0', + 'The Apache License 2.0', + 'Apache 2.0 License', + 'Apache License 2.0', + 'Apache 2.0', + 'Apache-2.0', + 'Apache 2', + LICENSE_APACHE_TWO], + (LICENSE_EPL1) : ['Eclipse Public License - Version 1.0', + 'Eclipse Public License v1.0', + 'Eclipse Public License 1.0', + 'Eclipse Public License', + 'EPL v1.0', + 'EPL 1.0', + 'EPL-1.0', + LICENSE_EPL1], + (LICENSE_EPL2) : ['Eclipse Public License v2.0', + 'Eclipse Public License 2.0', + 'EPL v2.0', + 'EPL 2.0', + 'EPL-2.0', + LICENSE_EPL2], + (LICENSE_LGPL21) : ['GNU Library General Public License v2.1 or later', + 'GNU Lesser General Public License v2.1 or later', + 'GNU Lesser General Public License, Version 2.1', + 'LGPL 2.1', + 'LGPL-2.1', + LICENSE_LGPL21], + (LICENSE_MIT) : ['The MIT License', + 'The MIT license', + 'MIT License', + 'MIT license', + 'MIT', + LICENSE_MIT], + (LICENSE_BSD2) : ['BSD 2-Clause FreeBSD License', + 'The BSD License', + 'The BSD license', + LICENSE_BSD2], + (LICENSE_BSD3) : ['BSD 3-Clause "New" or "Revised" License', + '3-Clause BSD License', + '3-Clause BSD license', + 'Revised BSD License', + 'Revised BSD license', + 'BSD Revised License', + 'BSD Revised license', + 'New BSD License', + 'New BSD license', + 'BSD New License', + 'BSD New license', + 'BSD 3-Clause', + 'BSD 3-clause', + LICENSE_BSD3] + ] + Project project void apply(Project project) { @@ -81,10 +164,11 @@ class LicensePlugin implements Plugin { project.plugins.apply(nl.javadude.gradle.plugins.license.LicensePlugin) } - preconfigureDownloadLicensesExtension(project) + preConfigureDownloadLicensesExtension(project) project.afterEvaluate { configureLicenseExtension(project) + postConfigureDownloadLicensesExtension(project) } if (isRootProject(project) && !project.childProjects.isEmpty()) { @@ -132,80 +216,40 @@ class LicensePlugin implements Plugin { licenseExtension.exclude 'META-INF/services/*' } - private void preconfigureDownloadLicensesExtension(Project project) { + private void preConfigureDownloadLicensesExtension(Project project) { + DownloadLicensesExtension extension = project.extensions.findByType(DownloadLicensesExtension) + extension.aliases = new LinkedHashMap<>(DEFAULT_ALIASES) + } + + private void postConfigureDownloadLicensesExtension(Project project) { + ProjectConfigurationExtension mergedConfiguration = project.ext.mergedConfiguration + + Map> defaultAliases = new LinkedHashMap<>(DEFAULT_ALIASES) + mergedConfiguration.license.licenses.licenses.each { license -> + if (license.id && license.aliases) { + LicenseMetadata licenseMetadata = LICENSES_MAP.get(license.id) + if (!licenseMetadata) { + licenseMetadata = new LicenseMetadata(license.name, license.url) + LICENSES_MAP.put(license.id, licenseMetadata) + } + + List aliases = defaultAliases.get(licenseMetadata) + if(!aliases) { + aliases = [] + defaultAliases.put(licenseMetadata, aliases) + } + List combined = aliases + license.aliases + combined = combined.unique() - aliases + aliases.addAll(combined) + } + } + DownloadLicensesExtension extension = project.extensions.findByType(DownloadLicensesExtension) - LicenseMetadata apacheTwo = extension.license('The Apache Software License, Version 2.0', LicenseId.APACHE_2_0.url()) - LicenseMetadata epl1 = extension.license('Eclipse Public License v1.0', LicenseId.EPL_1_0.url()) - LicenseMetadata epl2 = extension.license('Eclipse Public License v2.0', LicenseId.EPL_2_0.url()) - LicenseMetadata lgpl21 = extension.license('GNU Lesser General Public License v2.1 or later', LicenseId.LGPL_2_1_OR_LATER.url()) - LicenseMetadata mit = extension.license('MIT License', LicenseId.MIT.url()) - LicenseMetadata bsd2 = extension.license('BSD 2-Clause FreeBSD License', LicenseId.BSD_2_CLAUSE_FREEBSD.url()) - LicenseMetadata bsd3 = extension.license('BSD 3-Clause "New" or "Revised" License', LicenseId.BSD_3_CLAUSE.url()) - - extension.aliases = [ - (apacheTwo): ['The Apache Software License, Version 2.0', - 'The Apache Software License, version 2.0', - 'Apache Software License - Version 2.0', - 'Apache Software License - version 2.0', - 'the Apache License, ASL Version 2.0', - 'The Apache License, Version 2.0', - 'The Apache License Version 2.0', - 'Apache License, Version 2.0', - 'Apache License, version 2.0', - 'Apache License Version 2.0', - 'Apache License version 2.0', - 'The Apache License 2.0', - 'Apache 2.0 License', - 'Apache License 2.0', - 'Apache 2.0', - 'Apache-2.0', - 'Apache 2', - apacheTwo], - (epl1) : ['Eclipse Public License - Version 1.0', - 'Eclipse Public License v1.0', - 'Eclipse Public License 1.0', - 'Eclipse Public License', - 'EPL v1.0', - 'EPL 1.0', - 'EPL-1.0', - epl1], - (epl2) : ['Eclipse Public License v2.0', - 'Eclipse Public License 2.0', - 'EPL v2.0', - 'EPL 2.0', - 'EPL-2.0', - epl1], - (lgpl21) : ['GNU Library General Public License v2.1 or later', - 'GNU Lesser General Public License v2.1 or later', - 'GNU Lesser General Public License, Version 2.1', - 'LGPL 2.1', - 'LGPL-2.1', - epl1], - (mit) : ['The MIT License', - 'The MIT license', - 'MIT License', - 'MIT license', - 'MIT', - mit], - (bsd2) : ['BSD 2-Clause FreeBSD License', - 'The BSD License', - 'The BSD license', - bsd2], - (bsd3) : ['BSD 3-Clause "New" or "Revised" License', - '3-Clause BSD License', - '3-Clause BSD license', - 'Revised BSD License', - 'Revised BSD license', - 'BSD Revised License', - 'BSD Revised license', - 'New BSD License', - 'New BSD license', - 'BSD New License', - 'BSD New license', - 'BSD 3-Clause', - 'BSD 3-clause', - bsd3] - ] + extension.aliases = new LinkedHashMap<>(defaultAliases) + + project.tasks.withType(DownloadLicenses) { DownloadLicenses task -> + task.aliases = defaultAliases + } } private void configureAggregateLicenseReportTask(Project project) {