Skip to content

Commit 956bf89

Browse files
committed
Modified WhiteList
1 parent 7fe370f commit 956bf89

File tree

17 files changed

+166
-111
lines changed

17 files changed

+166
-111
lines changed

.github/workflows/release-artifact-bundle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: macos-12
1212
timeout-minutes: 20
1313
env:
14-
DEVELOPER_DIR: "/Applications/Xcode_13.4.1.app/Contents/Developer"
14+
DEVELOPER_DIR: "/Applications/Xcode_14.0.1.app/Contents/Developer"
1515
ARTIFACT_BUNDLE: "license-checker.artifactbundle"
1616

1717
steps:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: macos-12
1010
timeout-minutes: 20
1111
env:
12-
DEVELOPER_DIR: "/Applications/Xcode_13.4.1.app/Contents/Developer"
12+
DEVELOPER_DIR: "/Applications/Xcode_14.0.1.app/Contents/Developer"
1313

1414
steps:
1515
- name: Checkout

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ let package = Package(
4040
dependencies: [
4141
.target(name: "LicenseCheckerModule"),
4242
.target(name: "TestResources")
43+
],
44+
resources: [
45+
.process("Resources")
4346
]
4447
),
4548
.testTarget(

README.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,27 @@ It can detect libraries that are not included in a whitelist specifying the lice
1414

1515
```shell
1616
$ license-checker --source-packages-path ~/SourcePackages --white-list-path ~/white-list.json
17-
abseil Apache
18-
BoringSSL-GRPC BoringSSL
19-
Eureka MIT
20-
Firebase Apache
21-
GoogleAppMeasurement Apache
22-
GoogleDataTransport Apache
23-
GoogleUtilities MIT
24-
gRPC Apache
25-
GTMSessionFetcher Apache
26-
Kanna MIT
27-
KeychainAccess MIT
28-
Kingfisher MIT
29-
leveldb BSD
30-
LicenseChecker unknown
31-
LicenseList MIT
32-
nanopb zlib
33-
ObjectMapper MIT
34-
Promises Apache
35-
R.swift.Library MIT
36-
Reachability MIT
37-
RxGesture MIT
38-
RxSwift MIT
39-
SwiftProtobuf Apache
17+
✔︎ abseil Apache
18+
✔︎ BoringSSL-GRPC BoringSSL
19+
✔︎ Firebase Apache
20+
✔︎ GoogleAppMeasurement Apache
21+
✔︎ GoogleDataTransport Apache
22+
✔︎ GoogleUtilities MIT
23+
✔︎ gRPC Apache
24+
✔︎ GTMSessionFetcher Apache
25+
✔︎ Kanna MIT
26+
✔︎ KeychainAccess MIT
27+
✔︎ Kingfisher MIT
28+
✔︎ leveldb BSD
29+
✔︎ LicenseList MIT
30+
✔︎ nanopb zlib
31+
✔︎ ObjectMapper MIT
32+
✔︎ Promises Apache
33+
✔︎ R.swift.Library MIT
34+
✔︎ Reachability MIT
35+
✔︎ RxGesture MIT
36+
✔︎ RxSwift MIT
37+
✔︎ SwiftProtobuf Apache
4038
✅ No problems with library licensing.
4139
$
4240
```
@@ -134,8 +132,6 @@ LicenseChecker supports the following licenses:
134132
license-checker -s ${SOURCE_PACKAGES_PATH} -w [Path to white-list.json]
135133
```
136134

137-
⚠️ `white-list.json` must be included in Target Membership.
138-
139135
### BuildToolPlugin (for Swift Package Project)
140136

141137
1. Add binary target & plugin to `Package.swift`.
@@ -165,25 +161,33 @@ LicenseChecker supports the following licenses:
165161

166162
@main
167163
struct LicenseCheckerPlugin: BuildToolPlugin {
164+
enum LCError: Error {
165+
case sourcePackagesNotFound
166+
}
167+
168+
func sourcePackages(_ pluginWorkDirectory: Path) throws -> Path {
169+
var tmpPath = pluginWorkDirectory
170+
guard pluginWorkDirectory.string.contains("SourcePackages") else {
171+
throw LCError.sourcePackagesNotFound
172+
}
173+
while tmpPath.lastComponent != "SourcePackages" {
174+
tmpPath = tmpPath.removingLastComponent()
175+
}
176+
return tmpPath
177+
}
178+
168179
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
180+
let executablePath = try context.tool(named: "license-checker").path
181+
let sourcePackagesPath = try sourcePackages(context.pluginWorkDirectory)
169182
let whiteListPath = "Path to white-list.json" // Please change accordingly.
170-
let pluginWorkDirectory = context.pluginWorkDirectory.string
171-
let regex = try NSRegularExpression(pattern: ".*SourcePackages")
172-
let range = NSMakeRange(0, pluginWorkDirectory.utf16.count)
173-
guard let result = regex.firstMatch(in: pluginWorkDirectory, range: range) else {
174-
throw NSError(domain: "com.cybozu.LicenseCheckerPlugin",
175-
code: 1,
176-
userInfo: ["message": "Failed to match"])
177-
}
178-
let sourcePackageDirectory = NSString(string: pluginWorkDirectory)
179-
.substring(with: result.range(at: 0))
183+
180184
return [
181185
.prebuildCommand(
182-
displayName: "LicenseChecker",
183-
executable: try context.tool(named: "license-checker").path,
186+
displayName: "Prepare LicenseList",
187+
executable: executablePath,
184188
arguments: [
185189
"--source-packages-path",
186-
sourcePackageDirectory,
190+
sourcePackagesPath.string,
187191
"--white-list-path",
188192
whiteListPath
189193
],
@@ -220,9 +224,9 @@ USAGE: license-checker --source-packages-path <source-packages-path> --white-lis
220224

221225
OPTIONS:
222226
-s, --source-packages-path <source-packages-path>
223-
Path of SourcePackages directory
227+
Path to SourcePackages directory
224228
-w, --white-list-path <white-list-path>
225-
Path of white-list.json
229+
Path to white-list.json
226230
--version Show the version.
227231
-h, --help Show help information.
228232
```
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
public struct Acknowledgement: Hashable {
22
public let libraryName: String
3-
public let license: String
3+
public let licenseType: LicenseType
4+
public let isForbidden: Bool
45
}

Sources/LicenseCheckerModule/LCMain.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public final class LCMain {
1414
guard let whiteList = WhiteList.load(url: whiteListURL) else {
1515
throw LCError.notLoadedWiteList
1616
}
17-
let acknowledgements = packageParser.parse(with: checkoutsPath)
17+
let acknowledgements = packageParser.parse(with: checkoutsPath, whiteList: whiteList)
1818
printAcknowledgments(acknowledgements)
19-
guard acknowledgements.allSatisfy({ whiteList.contains($0) }) else {
19+
guard acknowledgements.allSatisfy({ $0.isForbidden == false }) else {
2020
throw LCError.forbiddenLibraryFound
2121
}
2222
Swift.print("✅ No problems with library licensing.")
@@ -27,9 +27,10 @@ public final class LCMain {
2727
$0.libraryName.count < $1.libraryName.count
2828
}?.libraryName.count ?? 0
2929
acknowledgements.forEach { acknowledgement in
30+
let mark = acknowledgement.isForbidden ? "×" : "✔︎"
3031
let library = acknowledgement.libraryName
3132
.padding(toLength: length, withPad: " ", startingAt: 0)
32-
Swift.print(library, acknowledgement.license)
33+
Swift.print(mark, library, acknowledgement.licenseType.rawValue)
3334
}
3435
}
3536
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public enum LicenseType: String {
2+
case apache = "Apache"
3+
case mit = "MIT"
4+
case bsd = "BSD"
5+
case zlib = "zlib"
6+
case boringSSL = "BoringSSL"
7+
case unknown = "unknown"
8+
9+
public var lowercased: String {
10+
self.rawValue.lowercased()
11+
}
12+
}

Sources/LicenseCheckerModule/PackageParser.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ public struct PackageParser {
1515
self.workspaceState = workspaceState
1616
}
1717

18-
public func parse(with checkoutsPath: String) -> [Acknowledgement] {
18+
public func parse(with checkoutsPath: String, whiteList: WhiteList) -> [Acknowledgement] {
1919
return workspaceState.object.dependencies.map { dependency in
2020
let components = dependency.packageRef.location.components(separatedBy: "/")
2121
let repoName = components.last!.replacingOccurrences(of: ".git", with: "")
2222
let dirURL = URL(fileURLWithPath: checkoutsPath).appendingPathComponent(repoName)
23-
let license = extractLicense(dirURL: dirURL)
24-
return Acknowledgement(libraryName: dependency.packageRef.name,
25-
license: license ?? "unknown")
23+
let libraryName = dependency.packageRef.name
24+
let licenseType = extractLicense(dirURL: dirURL)
25+
let isForbidden = !whiteList.contains(libraryName, licenseType: licenseType)
26+
return Acknowledgement(libraryName: libraryName,
27+
licenseType: licenseType,
28+
isForbidden: isForbidden)
2629
}.sorted { $0.libraryName.lowercased() < $1.libraryName.lowercased() }
2730
}
2831

29-
public func extractLicense(dirURL: URL) -> String? {
32+
public func extractLicense(dirURL: URL) -> LicenseType {
3033
let fm = FileManager.default
3134
let contents = (try? fm.contentsOfDirectory(atPath: dirURL.path)) ?? []
3235
let _licenseURL = contents.map { content in
@@ -40,25 +43,25 @@ public struct PackageParser {
4043
return false
4144
}.first
4245
guard let licenseURL = _licenseURL, var text = try? String(contentsOf: licenseURL) else {
43-
return nil
46+
return .unknown
4447
}
4548
text = text.replace(of: #"( +|\n)"#, with: " ")
46-
var license: String? = nil
49+
var licenseType = LicenseType.unknown
4750
if text.contains("Apache License") || text.contains(APACHE_TEXT) {
48-
license = "Apache"
51+
licenseType = .apache
4952
}
5053
if text.contains("MIT License") || text.contains(MIT_TEXT) {
51-
license = "MIT"
54+
licenseType = .mit
5255
}
5356
if text.contains(BSD_TEXT) {
54-
license = "BSD"
57+
licenseType = .bsd
5558
}
5659
if text.contains(ZLIB_TEXT) {
57-
license = "zlib"
60+
licenseType = .zlib
5861
}
5962
if text.contains(BORINGSSL_TEXT) {
60-
license = "BoringSSL"
63+
licenseType = .boringSSL
6164
}
62-
return license
65+
return licenseType
6366
}
6467
}

Sources/LicenseCheckerModule/WhiteList.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import Foundation
22

33
public struct WhiteList: Decodable {
4-
let licenses: [String]
5-
let libraries: [String]
4+
let licenses: [String]?
5+
let libraries: [String]?
6+
7+
public init(licenses: [String]?, libraries: [String]?) {
8+
self.licenses = licenses
9+
self.libraries = libraries
10+
}
611

712
private init?(url: URL) {
813
guard let data = try? Data(contentsOf: url),
@@ -11,21 +16,17 @@ public struct WhiteList: Decodable {
1116
self = whiteList
1217
}
1318

14-
public func contains(_ acknowledgement: Acknowledgement) -> Bool {
15-
if acknowledgement.license == "unknown" {
16-
let satisfy = self.libraries.contains(acknowledgement.libraryName)
17-
if !satisfy {
18-
Swift.print(acknowledgement.libraryName, acknowledgement.license)
19-
}
20-
return satisfy
19+
public func contains(_ libraryName: String, licenseType: LicenseType) -> Bool {
20+
if let libraries, libraries.contains(libraryName) {
21+
return true
22+
}
23+
if licenseType == .unknown {
24+
return false
2125
}
22-
let satisfy = self.licenses
23-
.map({ $0.lowercased() })
24-
.contains(acknowledgement.license.lowercased())
25-
if !satisfy {
26-
Swift.print(acknowledgement.libraryName, acknowledgement.license)
26+
if let licenses {
27+
return licenses.map({ $0.lowercased() }).contains(licenseType.lowercased)
2728
}
28-
return satisfy
29+
return false
2930
}
3031

3132
static func load(url: URL) -> WhiteList? {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"licenses": [
3-
]
2+
"xxxxx": [,
43
}

0 commit comments

Comments
 (0)