Skip to content

Commit 2d38a61

Browse files
committed
Move LicenseExpressionExtensions to instance functions for better API from Groovy and Java
1 parent 5794df4 commit 2d38a61

File tree

20 files changed

+202
-136
lines changed

20 files changed

+202
-136
lines changed

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/GatherLicenseTask.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.github.vlsi.gradle.license.api.LicenseExpression
2424
import com.github.vlsi.gradle.license.api.LicenseExpressionParser
2525
import com.github.vlsi.gradle.license.api.OsgiBundleLicenseParser
2626
import com.github.vlsi.gradle.license.api.SpdxLicense
27-
import com.github.vlsi.gradle.license.api.asExpression
2827
import com.github.vlsi.gradle.license.api.text
2928
import groovy.util.slurpersupport.GPathResult
3029
import org.gradle.api.Action
@@ -137,8 +136,8 @@ open class GatherLicenseTask @Inject constructor(
137136
objectFactory.setProperty<LicenseExpression>()
138137
.convention(
139138
listOf(
140-
SpdxLicense.Apache_2_0.asExpression(),
141-
SpdxLicense.MPL_2_0.asExpression()
139+
SpdxLicense.Apache_2_0.expression,
140+
SpdxLicense.MPL_2_0.expression
142141
)
143142
)
144143

@@ -152,7 +151,7 @@ open class GatherLicenseTask @Inject constructor(
152151
objectFactory.setProperty<LicenseExpression>()
153152

154153
fun ignoreMissingLicenseFor(license: License) {
155-
ignoreMissingLicenseFor(license.asExpression())
154+
ignoreMissingLicenseFor(license.expression)
156155
}
157156

158157
fun ignoreMissingLicenseFor(license: LicenseExpression) {
@@ -186,7 +185,7 @@ open class GatherLicenseTask @Inject constructor(
186185
private val licenseExpressionParser = LicenseExpressionParser()
187186

188187
fun addDependency(module: String, license: License) {
189-
addDependency(module, license.asExpression())
188+
addDependency(module, license.expression)
190189
}
191190

192191
fun addDependency(module: String, licenseExpression: LicenseExpression) {
@@ -202,7 +201,7 @@ open class GatherLicenseTask @Inject constructor(
202201
}
203202

204203
fun expectLicense(module: String, license: License) {
205-
expectLicense(module, license.asExpression())
204+
expectLicense(module, license.expression)
206205
}
207206

208207
fun expectLicense(module: String, licenseExpression: LicenseExpression) {
@@ -212,7 +211,7 @@ open class GatherLicenseTask @Inject constructor(
212211
}
213212

214213
fun overrideLicense(module: String, license: License) {
215-
overrideLicense(module, license.asExpression())
214+
overrideLicense(module, license.expression)
216215
}
217216

218217
fun overrideLicense(module: String, licenseExpression: LicenseExpression) {
@@ -224,7 +223,7 @@ open class GatherLicenseTask @Inject constructor(
224223
private fun Any.toLicenseExpression() =
225224
when (this) {
226225
is String -> licenseExpressionParser.parse(this)
227-
is License -> this.asExpression()
226+
is License -> this.expression
228227
is LicenseExpression -> this
229228
else -> throw GradleException("Illegal value $this for LicenseExpression. Expecting String, License, or LicenseExpression")
230229
}
@@ -489,7 +488,7 @@ open class GatherLicenseTask @Inject constructor(
489488
licenseExpressionParser: LicenseExpressionParser
490489
) {
491490
val bundleLicenseParser = OsgiBundleLicenseParser(licenseExpressionParser) {
492-
SpdxLicense.fromUriOrNull(it)?.asExpression()
491+
SpdxLicense.fromUriOrNull(it)?.expression
493492
}
494493
for (e in detectedLicenses) {
495494
if (e.value.license != null) {

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/GuessBasedNormalizer.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.github.vlsi.gradle.license.api.SimpleLicense
2323
import com.github.vlsi.gradle.license.api.SimpleLicenseExpression
2424
import com.github.vlsi.gradle.license.api.SpdxLicense
2525
import com.github.vlsi.gradle.license.api.WithException
26-
import com.github.vlsi.gradle.license.api.asExpression
2726
import org.slf4j.Logger
2827
import java.net.URI
2928

@@ -33,7 +32,7 @@ class GuessBasedNormalizer(
3332
) : LicenseExpressionNormalizer() {
3433

3534
private val nameGuesser = TfIdfBuilder<LicenseExpression>().apply {
36-
SpdxLicense.values().forEach { addDocument(it.asExpression(), it.title) }
35+
SpdxLicense.values().forEach { addDocument(it.expression, it.title) }
3736
}.build()
3837

3938
private fun String.trimTextExtensions() = removeSuffix(".txt").removeSuffix(".md")
@@ -52,9 +51,9 @@ class GuessBasedNormalizer(
5251

5352
override fun normalize(license: SimpleLicense): LicenseExpression? {
5453
if (license.title.equals("PUBLIC DOMAIN", ignoreCase = true)) {
55-
return SpdxLicense.CC0_1_0.asExpression()
54+
return SpdxLicense.CC0_1_0.expression
5655
}
57-
SpdxLicense.fromIdOrNull(license.title)?.let { return it.asExpression() }
56+
SpdxLicense.fromIdOrNull(license.title)?.let { return it.expression }
5857

5958
val guessList = nameGuesser.predict(license.title)
6059
.entries

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/LicenseCompatibilityInterpreter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.github.vlsi.gradle.license.api.ConjunctionLicenseExpression
2424
import com.github.vlsi.gradle.license.api.DisjunctionLicenseExpression
2525
import com.github.vlsi.gradle.license.api.LicenseEquivalence
2626
import com.github.vlsi.gradle.license.api.LicenseExpression
27-
import com.github.vlsi.gradle.license.api.disjunctions
2827

2928
enum class CompatibilityResult {
3029
ALLOW, UNKNOWN, REJECT;
@@ -54,7 +53,7 @@ internal class LicenseCompatibilityInterpreter(
5453
private val resolvedCases: Map<LicenseExpression, LicenseCompatibility>
5554
) {
5655
val resolvedParts = resolvedCases.asSequence().flatMap { (license, _) ->
57-
licenseEquivalence.expand(license).disjunctions().asSequence().map { it to license }
56+
licenseEquivalence.expand(license).disjunctions.asSequence().map { it to license }
5857
}.groupingBy { it.first }.aggregate { key, acc: LicenseExpression?, element, first ->
5958
if (first) {
6059
element.second

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/MetadataStore.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import com.github.vlsi.gradle.license.api.SpdxLicense
3434
import com.github.vlsi.gradle.license.api.StandardLicense
3535
import com.github.vlsi.gradle.license.api.StandardLicenseException
3636
import com.github.vlsi.gradle.license.api.WithException
37-
import com.github.vlsi.gradle.license.api.asExpression
3837
import com.github.vlsi.gradle.license.api.orLater
3938
import groovy.util.XmlSlurper
4039
import groovy.util.slurpersupport.GPathResult
@@ -86,8 +85,8 @@ object MetadataStore {
8685

8786
fun GPathResult.readLicenseExpression(): LicenseExpression =
8887
when (name()) {
89-
"license" -> toLicense().asExpression()
90-
"or-later" -> toLicense().orLater()
88+
"license" -> toLicense().expression
89+
"or-later" -> toLicense().orLater
9190
"expression" -> expressionParser.parse(text())
9291
"and" -> ConjunctionLicenseExpression(
9392
getList("*")

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/PomLicenseLoader.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.github.vlsi.gradle.license.api.License
2222
import com.github.vlsi.gradle.license.api.LicenseExpression
2323
import com.github.vlsi.gradle.license.api.LicenseExpressionNormalizer
2424
import com.github.vlsi.gradle.license.api.SimpleLicense
25-
import com.github.vlsi.gradle.license.api.asExpression
2625
import groovy.util.XmlSlurper
2726
import groovy.util.slurpersupport.GPathResult
2827
import kotlinx.coroutines.runBlocking
@@ -76,12 +75,12 @@ class LicenseDetector(
7675
} else {
7776
val parsedRawLicense =
7877
if (licenses.size == 1) {
79-
licenses.first().asExpression()
78+
licenses.first().expression
8079
} else {
8180
// When more than one license is present, assume AND was intended
8281
// It allows less freedom, however it seems to be a safe choice.
8382
ConjunctionLicenseExpression(
84-
licenses.mapTo(mutableSetOf()) { it.asExpression() }
83+
licenses.mapTo(mutableSetOf()) { it.expression }
8584
)
8685
}
8786
normalizer.normalize(parsedRawLicense)

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/api/License.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ interface License : LicenseExpressionSet, java.io.Serializable {
2424
val uri: List<URI>
2525

2626
override val disjunctions: Set<LicenseExpression>
27-
get() = setOf(asExpression())
27+
get() = setOf(expression)
2828

2929
override val conjunctions: Set<LicenseExpression>
3030
get() = disjunctions
31+
32+
val expression: JustLicense
33+
get() = JustLicense(this)
34+
35+
val orLater: OrLaterLicense
36+
get() = OrLaterLicense(this)
37+
38+
infix fun with(exception: LicenseException): LicenseExpression =
39+
expression with exception
40+
41+
infix fun and(other: License): LicenseExpression = expression and other
42+
infix fun and(other: LicenseExpression): LicenseExpression = expression and other
43+
44+
infix fun or(other: License): LicenseExpression = expression or other
45+
infix fun or(other: LicenseExpression): LicenseExpression = expression or other
3146
}
3247

3348
interface LicenseException : java.io.Serializable {

plugins/license-gather-plugin/src/main/kotlin/com/github/vlsi/gradle/license/api/LicenseExpression.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ sealed class LicenseExpression : LicenseExpressionSet, java.io.Serializable {
3333
override val disjunctions: Set<LicenseExpression>
3434
get() = setOf(this)
3535

36+
infix fun and(other: License): LicenseExpression = this and other.expression
37+
infix fun or(other: License): LicenseExpression = this or other.expression
38+
39+
infix fun and(other: LicenseExpression): LicenseExpression {
40+
val ops = conjunctions + other.conjunctions
41+
return when (ops.size) {
42+
0 -> throw IllegalArgumentException("Empty argument to ConjunctionLicenseExpression")
43+
1 -> ops.first()
44+
else -> ConjunctionLicenseExpression(ops)
45+
}
46+
}
47+
48+
infix fun or(other: LicenseExpression): LicenseExpression {
49+
val ops = disjunctions + other.disjunctions
50+
return when (ops.size) {
51+
0 -> throw IllegalArgumentException("Empty argument to DisjunctionLicenseExpression")
52+
1 -> ops.first()
53+
else -> DisjunctionLicenseExpression(ops)
54+
}
55+
}
56+
3657
object NONE : LicenseExpression()
3758
object NOASSERTION : LicenseExpression()
3859
}
@@ -50,10 +71,16 @@ abstract class SimpleLicenseExpression(open val license: License) : LicenseExpre
5071
else -> it.title + it.uri.asString()
5172
}
5273
}
74+
75+
infix fun with(exception: LicenseException): WithException =
76+
WithException(this, exception)
5377
}
5478

5579
data class JustLicense(override val license: License) : SimpleLicenseExpression(license) {
5680
override fun toString() = super.toString()
81+
82+
val orLater: OrLaterLicense
83+
get() = OrLaterLicense(license)
5784
}
5885

5986
data class OrLaterLicense(override val license: License) : SimpleLicenseExpression(license) {

0 commit comments

Comments
 (0)