Skip to content

Commit

Permalink
Merge pull request #34 from mono0926/28_exclude-libraries-using-regix
Browse files Browse the repository at this point in the history
28 exclude libraries using regix
  • Loading branch information
mono0926 committed May 8, 2017
2 parents dc0a3f5 + c70384f commit 6d82451
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
36 changes: 36 additions & 0 deletions Sources/LicensePlistCore/Entity/Config.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
import Foundation
import LoggerAPI

struct Config {
let githubs: [GitHub]
let excludes: [String]

func excluded(name: String) -> Bool {
if excludes.contains(name) {
return true
}
for text in excludes {
if let pattern = type(of: self).extractRegex(text), let regex = try? NSRegularExpression(pattern: pattern, options: []) {
let nsName = name as NSString
let matches = regex.matches(in: name, options: [], range: NSRange(location: 0, length: nsName.length))
assert(matches.count <= 1)
if !matches.isEmpty {
return true
}
}
}
return false
}

static func extractRegex(_ text: String) -> String? {
let nsText = text as NSString
let regex = try! NSRegularExpression(pattern: "^/(.+)/$", options: [])
let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsText.length))
if matches.count > 1 {
Log.warning("\(text) contains multiple regex pattern(sandwitched by `/`), but those are ignored except for first one.")
}
guard let match = matches.first else {
return nil
}
let numberOfRanges = match.numberOfRanges
guard numberOfRanges == 2 else {
assert(false, "maybe invalid regular expression to: \(nsText.substring(with: match.range))")
return nil
}
return nsText.substring(with: match.rangeAt(1))
}
}

extension Config: Equatable {
Expand Down
19 changes: 19 additions & 0 deletions Sources/LicensePlistCore/Entity/HasName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import LoggerAPI

public protocol HasName {
var name: String { get }
}

extension Array where Element: HasName {
func filterExcluded(config: Config?) -> [Element] {
return filter {
let name = $0.name
guard let config = config else { return true }
let result = !config.excluded(name: name)
if !result {
Log.warning("\(type(of: Element.self))'s \(name) was excluded according to config yaml.")
}
return result
}
}
}
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/Entity/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import APIKit
import LoggerAPI

public protocol Library: Hashable, Equatable {
public protocol Library: Hashable, Equatable, HasName {
var name: String { get }
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/Entity/License.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import APIKit
import LoggerAPI

public protocol LicenseInfo {
public protocol LicenseInfo: HasName {
var name: String { get }
var body: String { get }
}
Expand Down
18 changes: 3 additions & 15 deletions Sources/LicensePlistCore/LicensePlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ public final class LicensePlist {

private func collectLicenseInfos(cartfilePath: URL, podsPath: URL, config: Config?, outputPath: URL, force: Bool) -> [LicenseInfo] {
Log.info("Pods License parse start")
let excludes = config?.excludes ?? []

let podsAcknowledgements = readPodsAcknowledgements(path: podsPath)
let cocoaPodsLicenses = podsAcknowledgements.map { CocoaPodsLicense.parse($0) }.flatMap { $0 }.filter { cocoapods in
if excludes.contains(cocoapods.name) {
Log.warning("CocoaPods \(cocoapods.name) was excluded according to config yaml.")
return false
}
return true
}
let cocoaPodsLicenses = podsAcknowledgements.map { CocoaPodsLicense.parse($0) }.flatMap { $0 }
.filterExcluded(config: config)

Log.info("Carthage License collect start")

Expand All @@ -51,13 +45,7 @@ public final class LicensePlist {
if let cartfileContent = readCartfile(path: cartfilePath) {
gitHubLibraries += GitHub.parse(cartfileContent)
}
gitHubLibraries = gitHubLibraries.filter { github in
if excludes.contains(github.name) {
Log.warning("Carthage \(github.name) was excluded according to config yaml.")
return false
}
return true
}
gitHubLibraries = gitHubLibraries.filterExcluded(config: config)

let contents = (cocoaPodsLicenses.map { String(describing: $0) } + gitHubLibraries.map { String(describing: $0) })
.joined(separator: "\n\n")
Expand Down
24 changes: 24 additions & 0 deletions Tests/LicensePlistTests/Entity/ConfigTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import XCTest
@testable import LicensePlistCore

class ConfigTests: XCTestCase {

func testExcluded() {
let target = Config(githubs: [], excludes: ["lib1"])
XCTAssertTrue(target.excluded(name: "lib1"))
XCTAssertFalse(target.excluded(name: "lib2"))
}

func testExcluded_isRegex() {
XCTAssertEqual(Config.extractRegex("/^Core.*$/"), "^Core.*$")
XCTAssertNil(Config.extractRegex("/^Core.*$/a"))
}

func testExcluded_regex() {
let target = Config(githubs: [], excludes: ["/^lib.*$/"])
XCTAssertTrue(target.excluded(name: "lib1"))
XCTAssertTrue(target.excluded(name: "lib2"))
XCTAssertFalse(target.excluded(name: "hello"))
}
}
14 changes: 14 additions & 0 deletions Tests/LicensePlistTests/Entity/HasNameTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import XCTest
@testable import LicensePlistCore

class HasNameTests: XCTestCase {

func testfilterExcluded() {
let config = Config(githubs: [], excludes: ["lib2"])
let shouldBeIncluded = GitHub.init(name: "lib1", owner: "o1")
let result = [shouldBeIncluded, GitHub.init(name: "lib2", owner: "o2")]
.filterExcluded(config: config)
XCTAssertEqual(result, [shouldBeIncluded])
}
}

0 comments on commit 6d82451

Please sign in to comment.