Skip to content

Commit

Permalink
Merge pull request #133 from funzin/support-mintfile
Browse files Browse the repository at this point in the history
Support Mintfile 🌱
  • Loading branch information
mono0926 authored Apr 6, 2020
2 parents a17a817 + b07176a commit 331a6bc
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ You can see options by `license-plist --help`.

- Default: `Cartfile`

#### `--mintfile-path`

- Default: `Mintfile`

#### `--pods-path`

- Default: `Pods`
Expand Down
4 changes: 3 additions & 1 deletion Sources/LicensePlist/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private func loadConfig(configPath: URL) -> Config {
}

let main = command(Option("cartfile-path", default: Consts.cartfileName),
Option("mintfile-path", default: Consts.mintfileName),
Option("pods-path", default: Consts.podsDirectoryName),
Option("package-path", default: Consts.packageName),
Option("xcodeproj-path", default: "*.xcodeproj"),
Expand All @@ -24,7 +25,7 @@ let main = command(Option("cartfile-path", default: Consts.cartfileName),
Flag("add-version-numbers"),
Flag("suppress-opening-directory"),
Flag("single-page"),
Flag("fail-if-missing-license")) { cartfile, podsPath, packagePath, xcodeprojPath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen, singlePage, failIfMissingLicense in
Flag("fail-if-missing-license")) { cartfile, mintfile, podsPath, packagePath, xcodeprojPath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen, singlePage, failIfMissingLicense in

Logger.configure()
var config = loadConfig(configPath: URL(fileURLWithPath: configPath))
Expand All @@ -35,6 +36,7 @@ let main = command(Option("cartfile-path", default: Consts.cartfileName),
config.failIfMissingLicense = failIfMissingLicense
let options = Options(outputPath: URL(fileURLWithPath: output),
cartfilePath: URL(fileURLWithPath: cartfile),
mintfilePath: URL(fileURLWithPath: mintfile),
podsPath: URL(fileURLWithPath: podsPath),
packagePath: URL(fileURLWithPath: packagePath),
xcodeprojPath: URL(fileURLWithPath: xcodeprojPath),
Expand Down
1 change: 1 addition & 0 deletions Sources/LicensePlistCore/Consts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

public struct Consts {
public static let cartfileName = "Cartfile"
public static let mintfileName = "Mintfile"
public static let podsDirectoryName = "Pods"
public static let packageName = "Package.swift"
public static let xcodeprojExtension = "xcodeproj"
Expand Down
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/Entity/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Config {
let manuals = value["manual"].array ?? []
let manualList = Manual.load(manuals, renames: renames, configBasePath: configBasePath)
let githubs = value["github"].array?.map { $0.string }.compactMap { $0 } ?? []
let gitHubList = githubs.map { GitHub.load($0, renames: renames, mark: "", quotes: "") }.flatMap { $0 }
let gitHubList = githubs.map { GitHub.load(.licensePlist(content: $0), renames: renames) }.flatMap { $0 }
gitHubList.forEach {
Log.warning("\($0.name) is specified by the depricated format. It will be removed at Version 2." +
"See: https://github.com/mono0926/LicensePlist/blob/master/Tests/LicensePlistTests/Resources/license_plist.yml .")
Expand Down
22 changes: 8 additions & 14 deletions Sources/LicensePlistCore/Entity/GitHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,19 @@ extension GitHub: CustomStringConvertible {
}

extension GitHub {
public static func load(_ content: String, renames: [String: String] = [:]) -> [GitHub] {
return load(content, renames: renames, mark: "github ")
}

public static func load(_ content: String, renames: [String: String], mark: String, quotes: String = "\"") -> [GitHub] {
let r = load(content, renames: renames, mark: mark, quotes: quotes, version: true)
public static func load(_ file: GitHubLibraryConfigFile, renames: [String: String] = [:]) -> [GitHub] {
let r = load(file, renames: renames, version: true)
if !r.isEmpty {
return r
}
return load(content, renames: renames, mark: mark, quotes: quotes, version: false)
return load(file, renames: renames, version: false)
}

public static func load(_ content: String,
renames: [String: String],
mark: String,
quotes: String = "\"",
version: Bool = false) -> [GitHub] {
let pattern = "[\\w\\.\\-]+"
let regexString = "\(mark)\(quotes)(\(pattern))/(\(pattern))\(quotes)" + (version ? " \(quotes)([\\w\\.\\-]+)\(quotes)" : "")
private static func load(_ file: GitHubLibraryConfigFile,
renames: [String: String],
version: Bool = false) -> [GitHub] {
guard let content = file.content else { return [] }
let regexString = file.type.regexString(version: version)
let regex = try! NSRegularExpression(pattern: regexString, options: [])
let nsContent = content as NSString
let matches = regex.matches(in: content, options: [], range: NSRange(location: 0, length: nsContent.length))
Expand Down
31 changes: 31 additions & 0 deletions Sources/LicensePlistCore/Entity/GitHubLibraryConfigFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public struct GitHubLibraryConfigFile: Equatable {
let type: GitHubLibraryConfigFileType
let content: String?
}

extension GitHubLibraryConfigFile {
static func carthage(content: String?) -> GitHubLibraryConfigFile { .init(type: .carthage, content: content) }
static func mint(content: String?) -> GitHubLibraryConfigFile { .init(type: .mint, content: content) }
static func licensePlist(content: String?) -> GitHubLibraryConfigFile { .init(type: .licensePlist, content: content) }
}

public enum GitHubLibraryConfigFileType: Int, CaseIterable {
case carthage
case mint
case licensePlist
}

extension GitHubLibraryConfigFileType {
func regexString(version: Bool) -> String {
let pattern = "[\\w\\.\\-]+"
switch self {
case .carthage:
let quotes = "\""
return "github \(quotes)(\(pattern))/(\(pattern))\(quotes)" + (version ? " \(quotes)(\(pattern))\(quotes)" : "")
case .mint:
return "(\(pattern))/(\(pattern))" + (version ? "@(\(pattern))" : "")
case .licensePlist:
return "(\(pattern))/(\(pattern))" + (version ? " (\(pattern))" : "")
}
}
}
4 changes: 4 additions & 0 deletions Sources/LicensePlistCore/Entity/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
public struct Options {
public let outputPath: URL
public let cartfilePath: URL
public let mintfilePath: URL
public let podsPath: URL
public let packagePath: URL
public let xcodeprojPath: URL
Expand All @@ -14,6 +15,7 @@ public struct Options {

public static let empty = Options(outputPath: URL(fileURLWithPath: ""),
cartfilePath: URL(fileURLWithPath: ""),
mintfilePath: URL(fileURLWithPath: ""),
podsPath: URL(fileURLWithPath: ""),
packagePath: URL(fileURLWithPath: ""),
xcodeprojPath: URL(fileURLWithPath: ""),
Expand All @@ -25,6 +27,7 @@ public struct Options {

public init(outputPath: URL,
cartfilePath: URL,
mintfilePath: URL,
podsPath: URL,
packagePath: URL,
xcodeprojPath: URL,
Expand All @@ -35,6 +38,7 @@ public struct Options {
config: Config) {
self.outputPath = outputPath
self.cartfilePath = cartfilePath
self.mintfilePath = mintfilePath
self.podsPath = podsPath
self.packagePath = packagePath
self.xcodeprojPath = xcodeprojPath
Expand Down
15 changes: 12 additions & 3 deletions Sources/LicensePlistCore/Entity/PlistInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ struct PlistInfo {
cocoaPodsLicenses = config.filterExcluded(licenses).sorted()
}

mutating func loadGitHubLibraries(cartfile: String?) {
Log.info("Carthage License collect start")
githubLibraries = options.config.apply(githubs: GitHub.load(cartfile ?? "", renames: options.config.renames)).sorted()
mutating func loadGitHubLibraries(file: GitHubLibraryConfigFile) {
switch file.type {
case .carthage:
Log.info("Carthage License collect start")
case .mint:
Log.info("Mint License collect start")
case .licensePlist:
// should not reach here
preconditionFailure()
}
let githubs = GitHub.load(file, renames: options.config.renames)
githubLibraries = ((githubLibraries ?? []) + options.config.apply(githubs: githubs)).sorted()
}

mutating func loadSwiftPackageLibraries(packageFile: String?) {
Expand Down
16 changes: 12 additions & 4 deletions Sources/LicensePlistCore/LicensePlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public final class LicensePlist {
GitHubAuthorization.shared.token = options.gitHubToken
var info = PlistInfo(options: options)
info.loadCocoaPodsLicense(acknowledgements: readPodsAcknowledgements(path: options.podsPath))
info.loadGitHubLibraries(cartfile: readCartfile(path: options.cartfilePath))
info.loadGitHubLibraries(file: readCartfile(path: options.cartfilePath))
info.loadGitHubLibraries(file: readMintfile(path: options.mintfilePath))
info.loadSwiftPackageLibraries(packageFile: readSwiftPackages(path: options.packagePath) ?? readXcodeProject(path: options.xcodeprojPath))
info.loadManualLibraries()
info.compareWithLatestSummary()
Expand All @@ -26,14 +27,21 @@ public final class LicensePlist {
}
}

private func readCartfile(path: URL) -> String? {
private func readCartfile(path: URL) -> GitHubLibraryConfigFile {
if path.lastPathComponent != Consts.cartfileName {
fatalError("Invalid Cartfile name: \(path.lastPathComponent)")
}
if let content = path.appendingPathExtension("resolved").lp.read() {
return content
return GitHubLibraryConfigFile(type: .carthage, content: content)
}
return path.lp.read()
return .carthage(content: path.lp.read())
}

private func readMintfile(path: URL) -> GitHubLibraryConfigFile {
if path.lastPathComponent != Consts.mintfileName {
fatalError("Invalid MintFile name: \(path.lastPathComponent)")
}
return .mint(content: path.lp.read())
}

private func readSwiftPackages(path: URL) -> String? {
Expand Down
11 changes: 11 additions & 0 deletions Tests/LicensePlistTests/Entity/GitHubLibraryConfigFileTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XCTest
@testable import LicensePlistCore

class GitHubLibraryConfigFileTests: XCTestCase {

func testInit() {
XCTAssertEqual(GitHubLibraryConfigFile.carthage(content: "content"), GitHubLibraryConfigFile(type: .carthage, content: "content"))
XCTAssertEqual(GitHubLibraryConfigFile.mint(content: "content"), GitHubLibraryConfigFile(type: .mint, content: "content"))
XCTAssertEqual(GitHubLibraryConfigFile.licensePlist(content: "content"), GitHubLibraryConfigFile(type: .licensePlist, content: "content"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import XCTest
@testable import LicensePlistCore

class GitHubLibraryConfigFileTypeTests: XCTestCase {

func testRegexString() {
// carthage
do {
let type = GitHubLibraryConfigFileType.carthage
XCTAssertEqual(type.regexString(version: false), "github \"([\\w\\.\\-]+)/([\\w\\.\\-]+)\"")
XCTAssertEqual(type.regexString(version: true), "github \"([\\w\\.\\-]+)/([\\w\\.\\-]+)\" \"([\\w\\.\\-]+)\"")
}

// mint
do {
let type = GitHubLibraryConfigFileType.mint
XCTAssertEqual(type.regexString(version: false), "([\\w\\.\\-]+)/([\\w\\.\\-]+)")
XCTAssertEqual(type.regexString(version: true), "([\\w\\.\\-]+)/([\\w\\.\\-]+)@([\\w\\.\\-]+)")
}

// licensePlist
do {
let type = GitHubLibraryConfigFileType.licensePlist
XCTAssertEqual(type.regexString(version: false), "([\\w\\.\\-]+)/([\\w\\.\\-]+)")
XCTAssertEqual(type.regexString(version: true), "([\\w\\.\\-]+)/([\\w\\.\\-]+) ([\\w\\.\\-]+)")
}
}
}
18 changes: 9 additions & 9 deletions Tests/LicensePlistTests/Entity/GitHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ import XCTest
class GitHubTests: XCTestCase {

func testParse_empty() {
let results = GitHub.load("( ´・‿・`)")
let results = GitHub.load(.carthage(content: "( ´・‿・`)"))
XCTAssertTrue(results.isEmpty)
}

func testParse_one() {
let results = GitHub.load("github \"mono0926/NativePopup\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: nil))
}

func testParse_one_rename() {
let results = GitHub.load("github \"mono0926/NativePopup\"", renames: ["NativePopup": "NativePopup2"])
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\""), renames: ["NativePopup": "NativePopup2"])
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "NativePopup", nameSpecified: "NativePopup2", owner: "mono0926", version: nil))
}

func testParse_one_dot() {
let results = GitHub.load("github \"tephencelis/SQLite.swift\"")
let results = GitHub.load(.carthage(content: "github \"tephencelis/SQLite.swift\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "SQLite.swift", nameSpecified: nil, owner: "tephencelis", version: nil))
}

func testParse_one_hyphen() {
let results = GitHub.load("github \"mono0926/ios-license-generator\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/ios-license-generator\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "ios-license-generator", nameSpecified: nil, owner: "mono0926", version: nil))
}

func testParse_multiple() {
let results = GitHub.load("github \"mono0926/NativePopup\"\ngithub \"ReactiveX/RxSwift\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\"\ngithub \"ReactiveX/RxSwift\""))
XCTAssertTrue(results.count == 2)
let result1 = results[0]
XCTAssertEqual(result1, GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: nil))
Expand All @@ -47,21 +47,21 @@ class GitHubTests: XCTestCase {
}

func testParse_one_versoin() {
let results = GitHub.load("github \"mono0926/NativePopup\" \"1.8.4\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\" \"1.8.4\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: "1.8.4"))
}

func testParse_one_versoin_v() {
let results = GitHub.load("github \"mono0926/NativePopup\" \"v1.8.4\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\" \"v1.8.4\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: "v1.8.4"))
}

func testParse_one_hash() {
let results = GitHub.load("github \"mono0926/NativePopup\" \"e64dcc63d4720f04eec8700b31ecaee188b6483a\"")
let results = GitHub.load(.carthage(content: "github \"mono0926/NativePopup\" \"e64dcc63d4720f04eec8700b31ecaee188b6483a\""))
XCTAssertTrue(results.count == 1)
let result = results.first
XCTAssertEqual(result, GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: "e64dcc6"))
Expand Down
5 changes: 3 additions & 2 deletions Tests/LicensePlistTests/Entity/PlistInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PlistInfoTests: XCTestCase {

private let options = Options(outputPath: URL(fileURLWithPath: "test_result_dir"),
cartfilePath: URL(fileURLWithPath: "test_result_dir"),
mintfilePath: URL(fileURLWithPath: "test_result_dir"),
podsPath: URL(fileURLWithPath: "test_result_dir"),
packagePath: URL(fileURLWithPath: "test_result_dir"),
xcodeprojPath: URL(fileURLWithPath: "test_result_dir"),
Expand Down Expand Up @@ -53,7 +54,7 @@ class PlistInfoTests: XCTestCase {
func testLoadGitHubLibraries() {
var target = PlistInfo(options: options)
XCTAssertNil(target.githubLibraries)
target.loadGitHubLibraries(cartfile: "github \"ikesyo/Himotoki\" \"3.0.1\"")
target.loadGitHubLibraries(file: .carthage(content: "github \"ikesyo/Himotoki\" \"3.0.1\""))
let libraries = target.githubLibraries!
XCTAssertEqual(libraries.count, 2)
let lib1 = libraries[0]
Expand All @@ -65,7 +66,7 @@ class PlistInfoTests: XCTestCase {
func testLoadGitHubLibraries_empty() {
var target = PlistInfo(options: options)
XCTAssertNil(target.githubLibraries)
target.loadGitHubLibraries(cartfile: nil)
target.loadGitHubLibraries(file: .carthage(content: nil))
let libraries = target.githubLibraries!
XCTAssertEqual(libraries.count, 1)
let lib1 = libraries[0]
Expand Down

0 comments on commit 331a6bc

Please sign in to comment.