@@ -18,70 +18,50 @@ package com.github.jk1.license.check
18
18
import groovy.json.JsonOutput
19
19
import org.gradle.api.GradleException
20
20
21
- class LicenseChecker {
21
+ /**
22
+ * This class compares the found licences with the allowed licenses and creates a report for any missing license
23
+ */
24
+ interface LicenseChecker {
25
+ List<Tuple2<Dependency , List<ModuleLicense > > > checkAllDependencyLicensesAreAllowed (
26
+ List<AllowedLicense > allowedLicenses ,
27
+ List<Dependency > allDependencies )
22
28
23
- void checkAllDependencyLicensesAreAllowed (
24
- Object allowedLicensesFile , File projectLicensesDataFile , File notPassedDependenciesOutputFile ) {
25
- List<Dependency > allDependencies = LicenseCheckerFileReader . importDependencies(projectLicensesDataFile)
26
- List<AllowedLicense > allowedLicenses = LicenseCheckerFileReader . importAllowedLicenses(allowedLicensesFile)
27
- List<Dependency > notPassedDependencies = searchForNotAllowedDependencies(allDependencies, allowedLicenses)
28
- generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile)
29
+ default void checkAllDependencyLicensesAreAllowed (
30
+ Object allowedLicensesFile , File projectLicensesDataFile , File notPassedDependenciesOutputFile ) {
31
+ def notPassedDependencies = checkAllDependencyLicensesAreAllowed(
32
+ parseAllowedLicenseFile(allowedLicensesFile), getProjectDependencies(projectLicensesDataFile))
29
33
34
+ generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile)
30
35
if (! notPassedDependencies. isEmpty()) {
31
- throw new GradleException (" Some library licenses are not allowed.\n " +
32
- " Read [$notPassedDependenciesOutputFile . path ] for more information." )
33
- }
34
- }
35
-
36
- private List<Dependency > searchForNotAllowedDependencies (
37
- List<Dependency > dependencies , List<AllowedLicense > allowedLicenses ) {
38
- return dependencies. findAll { ! isDependencyHasAllowedLicense(it, allowedLicenses) }
39
- }
40
-
41
- private void generateNotPassedDependenciesFile (
42
- List<Dependency > notPassedDependencies , File notPassedDependenciesOutputFile ) {
43
- notPassedDependenciesOutputFile. text =
44
- JsonOutput . prettyPrint(JsonOutput . toJson(
45
- [" dependenciesWithoutAllowedLicenses" : notPassedDependencies. collect { toAllowedLicenseList(it) }. flatten()]))
46
- }
47
-
48
- private boolean isDependencyHasAllowedLicense (Dependency dependency , List<AllowedLicense > allowedLicenses ) {
49
- for (allowedLicense in allowedLicenses) {
50
- if (isDependencyMatchesAllowedLicense(dependency, allowedLicense)) return true
36
+ throw new GradleException (" Some library licenses are not allowed:\n " +
37
+ " $notPassedDependenciesOutputFile . text \n\n " +
38
+ " Read [$notPassedDependenciesOutputFile . path ] for more information." )
51
39
}
52
- return false
53
- }
54
-
55
- private boolean isDependencyMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
56
- return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
57
- isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) &&
58
- isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense)
59
40
}
60
41
61
- private boolean isDependencyNameMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
62
- return dependency. moduleName ==~ allowedLicense. moduleName || allowedLicense. moduleName == null ||
63
- dependency. moduleName == allowedLicense. moduleName
42
+ default List<AllowedLicense > parseAllowedLicenseFile(File allowedLicenseFile) {
43
+ return LicenseCheckerFileReader . importAllowedLicenses(allowedLicenseFile)
64
44
}
65
45
66
- private boolean isDependencyVersionMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
67
- return dependency. moduleVersion ==~ allowedLicense. moduleVersion || allowedLicense. moduleVersion == null ||
68
- dependency. moduleVersion == allowedLicense. moduleVersion
46
+ default List<Dependency > getProjectDependencies(File depenenciesFile) {
47
+ return LicenseCheckerFileReader . importDependencies(depenenciesFile)
69
48
}
70
49
71
- private boolean isDependencyLicenseMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
72
- if (allowedLicense. moduleLicense == null || allowedLicense. moduleLicense == " .*" ) return true
73
50
74
- for (moduleLicenses in dependency. moduleLicenses)
75
- if (moduleLicenses. moduleLicense ==~ allowedLicense. moduleLicense ||
76
- moduleLicenses. moduleLicense == allowedLicense. moduleLicense) return true
77
- return false
51
+ default void generateNotPassedDependenciesFile(List<Tuple2<Dependency , List<ModuleLicense > > > notPassedDependencies, File notPassedDependenciesOutputFile) {
52
+ notPassedDependenciesOutputFile. text = JsonOutput . prettyPrint(
53
+ JsonOutput . toJson([
54
+ " dependenciesWithoutAllowedLicenses" : notPassedDependencies. collect {
55
+ toAllowedLicenseList(it. getV1(), it. getV2())
56
+ }. flatten()
57
+ ]))
78
58
}
79
59
80
- private List<AllowedLicense > toAllowedLicenseList (Dependency dependency ) {
81
- if (dependency . moduleLicenses. isEmpty()) {
82
- return [ new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null ) ]
60
+ default List<AllowedLicense > toAllowedLicenseList(Dependency dependency, List< ModuleLicense > moduleLicenses ) {
61
+ if (moduleLicenses. isEmpty()) {
62
+ return [new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null )]
83
63
} else {
84
- return dependency . moduleLicenses. collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
64
+ return moduleLicenses. findAll { it } . collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
85
65
}
86
66
}
87
67
}
0 commit comments