From c70384f7a6665874e8ef23bdccdf14faaf960521 Mon Sep 17 00:00:00 2001 From: Masayuki Ono Date: Mon, 8 May 2017 19:43:41 +0900 Subject: [PATCH] =?UTF-8?q?Support=20regix=20for=20config=E2=80=99s=20excl?= =?UTF-8?q?udes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/LicensePlistCore/Entity/Config.swift | 34 ++++++++++++++++++- Sources/LicensePlistCore/Entity/HasName.swift | 2 +- .../Entity/ConfigTests.swift | 16 ++++++--- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Sources/LicensePlistCore/Entity/Config.swift b/Sources/LicensePlistCore/Entity/Config.swift index 3d71d449..5e0988ce 100644 --- a/Sources/LicensePlistCore/Entity/Config.swift +++ b/Sources/LicensePlistCore/Entity/Config.swift @@ -1,11 +1,43 @@ import Foundation +import LoggerAPI struct Config { let githubs: [GitHub] let excludes: [String] func excluded(name: String) -> Bool { - return excludes.contains(name) + 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)) } } diff --git a/Sources/LicensePlistCore/Entity/HasName.swift b/Sources/LicensePlistCore/Entity/HasName.swift index 8d5c97c7..dea4f095 100644 --- a/Sources/LicensePlistCore/Entity/HasName.swift +++ b/Sources/LicensePlistCore/Entity/HasName.swift @@ -11,7 +11,7 @@ extension Array where Element: HasName { guard let config = config else { return true } let result = !config.excluded(name: name) if !result { - Log.warning("\(type(of: self))'s \(name) was excluded according to config yaml.") + Log.warning("\(type(of: Element.self))'s \(name) was excluded according to config yaml.") } return result } diff --git a/Tests/LicensePlistTests/Entity/ConfigTests.swift b/Tests/LicensePlistTests/Entity/ConfigTests.swift index 30ab3be7..dc97b12a 100644 --- a/Tests/LicensePlistTests/Entity/ConfigTests.swift +++ b/Tests/LicensePlistTests/Entity/ConfigTests.swift @@ -4,13 +4,21 @@ import XCTest class ConfigTests: XCTestCase { - func testExcluded_true() { + func testExcluded() { let target = Config(githubs: [], excludes: ["lib1"]) XCTAssertTrue(target.excluded(name: "lib1")) + XCTAssertFalse(target.excluded(name: "lib2")) } - func testExcluded_false() { - let target = Config(githubs: [], excludes: ["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")) } }