@@ -18,70 +18,80 @@ package com.github.jk1.license.check
18
18
import groovy.json.JsonOutput
19
19
import org.gradle.api.GradleException
20
20
21
+ /**
22
+ * This class compares the found licences with the allowed licenses and creates a report for any missing license
23
+ */
21
24
class LicenseChecker {
22
-
23
- void checkAllDependencyLicensesAreAllowed (
24
- Object allowedLicensesFile , File projectLicensesDataFile , File notPassedDependenciesOutputFile ) {
25
+ static void checkAllDependencyLicensesAreAllowed (
26
+ Object allowedLicensesFile ,
27
+ File projectLicensesDataFile ,
28
+ File notPassedDependenciesOutputFile ,
29
+ CheckType checkType ) {
25
30
List<Dependency > allDependencies = LicenseCheckerFileReader . importDependencies(projectLicensesDataFile)
26
31
List<AllowedLicense > allowedLicenses = LicenseCheckerFileReader . importAllowedLicenses(allowedLicensesFile)
27
- List<Dependency > notPassedDependencies = searchForNotAllowedDependencies(allDependencies, allowedLicenses)
32
+ List<Tuple2<Dependency , List<ModuleLicense > > > notPassedDependencies = getNotAllowedLicenses(allDependencies, allowedLicenses)
33
+ if (checkType == CheckType . ANY ) {
34
+ // when we have ANY_MATCH check type, we will not show an error if any license is allowed
35
+ // this means: we are only interested in those dependencies where all licenses are not allowed
36
+ notPassedDependencies = notPassedDependencies. findAll { it. get(0 ). moduleLicenses == null || it. get(1 ). size() == it. get(0 ). moduleLicenses. size() }
37
+ }
28
38
generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile)
29
39
30
40
if (! notPassedDependencies. isEmpty()) {
31
- throw new GradleException (" Some library licenses are not allowed.\n " +
32
- " Read [$notPassedDependenciesOutputFile . path ] for more information." )
41
+ throw new GradleException (" Some library licenses are not allowed:\n " +
42
+ " $notPassedDependenciesOutputFile . text \n\n " +
43
+ " Read [$notPassedDependenciesOutputFile . path ] for more information." )
33
44
}
34
45
}
35
46
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
47
+ private static List<Tuple2<Dependency , List<ModuleLicense > > > getNotAllowedLicenses (List<Dependency > dependencies , List<AllowedLicense > allowedLicenses ) {
48
+ List<Tuple2<Dependency , List<ModuleLicense > > > result = new ArrayList<> ()
49
+ for (Dependency dependency : dependencies) {
50
+ List<AllowedLicense > perDependencyAllowedLicenses = allowedLicenses. findAll { isDependencyNameMatchesAllowedLicense(dependency, it) && isDependencyVersionMatchesAllowedLicense(dependency, it) }
51
+ // allowedLicense matches anything, so we don't need to further check
52
+ if (perDependencyAllowedLicenses. any { it. moduleLicense == null || it. moduleLicense == " .*" }) {
53
+ continue
54
+ }
55
+ def notAllowedLicenses = dependency. moduleLicenses. findAll { ! isDependencyLicenseMatchesAllowedLicense(it, perDependencyAllowedLicenses) }
56
+ if (! notAllowedLicenses. isEmpty()) {
57
+ result. add(Tuple2 . of(dependency, notAllowedLicenses))
58
+ }
51
59
}
52
- return false
60
+ return result
53
61
}
54
62
55
- private boolean isDependencyMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
56
- return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
57
- isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) &&
58
- isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense)
63
+ private static void generateNotPassedDependenciesFile (
64
+ List<Tuple2<Dependency , List<ModuleLicense > > > notPassedDependencies , File notPassedDependenciesOutputFile ) {
65
+ notPassedDependenciesOutputFile. text =
66
+ JsonOutput . prettyPrint(JsonOutput . toJson(
67
+ [" dependenciesWithoutAllowedLicenses" : notPassedDependencies. collect { toAllowedLicenseList(it. get(0 ), it. get(1 )) }. flatten()]))
59
68
}
60
69
61
- private boolean isDependencyNameMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
70
+ private static boolean isDependencyNameMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
62
71
return dependency. moduleName ==~ allowedLicense. moduleName || allowedLicense. moduleName == null ||
63
- dependency. moduleName == allowedLicense. moduleName
72
+ dependency. moduleName == allowedLicense. moduleName
64
73
}
65
74
66
- private boolean isDependencyVersionMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
75
+ private static boolean isDependencyVersionMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
67
76
return dependency. moduleVersion ==~ allowedLicense. moduleVersion || allowedLicense. moduleVersion == null ||
68
- dependency. moduleVersion == allowedLicense. moduleVersion
77
+ dependency. moduleVersion == allowedLicense. moduleVersion
69
78
}
70
79
71
- private boolean isDependencyLicenseMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
72
- if (allowedLicense. moduleLicense == null || allowedLicense. moduleLicense == " .*" ) return true
80
+ private static boolean isDependencyLicenseMatchesAllowedLicense (ModuleLicense moduleLicense , List<AllowedLicense > allowedLicenses ) {
81
+ for (AllowedLicense allowedLicense : allowedLicenses) {
82
+ if (allowedLicense. moduleLicense == null || allowedLicense. moduleLicense == " .*" ) return true
73
83
74
- for (moduleLicenses in dependency . moduleLicenses)
75
- if (moduleLicenses . moduleLicense ==~ allowedLicense. moduleLicense ||
76
- moduleLicenses . moduleLicense == allowedLicense . moduleLicense) return true
84
+ if (moduleLicense . moduleLicense ==~ allowedLicense . moduleLicense ||
85
+ moduleLicense . moduleLicense == allowedLicense. moduleLicense) return true
86
+ }
77
87
return false
78
88
}
79
89
80
- private List<AllowedLicense > toAllowedLicenseList (Dependency dependency ) {
81
- if (dependency . moduleLicenses. isEmpty()) {
82
- return [ new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null ) ]
90
+ private static List<AllowedLicense > toAllowedLicenseList (Dependency dependency , List< ModuleLicense > moduleLicenses ) {
91
+ if (moduleLicenses. isEmpty()) {
92
+ return [new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null )]
83
93
} else {
84
- return dependency . moduleLicenses. collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
94
+ return moduleLicenses. findAll { it } . collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
85
95
}
86
96
}
87
97
}
0 commit comments