Skip to content

Commit

Permalink
Use glob to support wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
shiraji committed Oct 24, 2016
1 parent 45a3293 commit 9a32a54
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 5 additions & 3 deletions R.swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
3406F9501DB64E8C00351D0E /* IgnoreFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */; };
3406F9511DB65FAB00351D0E /* IgnoreFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */; };
5D45C2191CA12913000B8DC9 /* StringsStructGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C941C7C291900B2F376 /* StringsStructGenerator.swift */; };
5D997C951C7C291900B2F376 /* StringsStructGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C941C7C291900B2F376 /* StringsStructGenerator.swift */; };
D5214C1F1DA6AA9300212FF1 /* Struct+InternalProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5214C1E1DA6AA9300212FF1 /* Struct+InternalProperties.swift */; };
D5214C201DA6AAC300212FF1 /* Struct+InternalProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5214C1E1DA6AA9300212FF1 /* Struct+InternalProperties.swift */; };
3406F9501DB64E8C00351D0E /* IgnoreFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */; };
3406F9511DB65FAB00351D0E /* IgnoreFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */; };
D5646DE41BE2016E0034F4D7 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE11BE2016E0034F4D7 /* Extensions.swift */; };
D5646DE51BE2016E0034F4D7 /* PBXObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE21BE2016E0034F4D7 /* PBXObject.swift */; };
D5646DE61BE2016E0034F4D7 /* Serialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE31BE2016E0034F4D7 /* Serialization.swift */; };
Expand Down Expand Up @@ -117,9 +117,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IgnoreFile.swift; sourceTree = "<group>"; };
5D997C941C7C291900B2F376 /* StringsStructGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringsStructGenerator.swift; sourceTree = "<group>"; };
D5214C1E1DA6AA9300212FF1 /* Struct+InternalProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Struct+InternalProperties.swift"; sourceTree = "<group>"; };
3406F94F1DB64E8C00351D0E /* IgnoreFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IgnoreFile.swift; sourceTree = "<group>"; };
D5646DE11BE2016E0034F4D7 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
D5646DE21BE2016E0034F4D7 /* PBXObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBXObject.swift; sourceTree = "<group>"; };
D5646DE31BE2016E0034F4D7 /* Serialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Serialization.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -174,6 +174,7 @@
E2762AC61CCCEC2A0009BCAA /* Locale.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locale.swift; sourceTree = "<group>"; };
E2762ACE1CCD0B970009BCAA /* LocalizableStrings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizableStrings.swift; sourceTree = "<group>"; };
E2A918741CD4B61200394F6F /* Unifiable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unifiable.swift; sourceTree = "<group>"; };
FAF521711DBD9716009B221A /* rswift-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "rswift-Bridging-Header.h"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -270,6 +271,7 @@
D5EA0DEB1A3DF45600FFEBC4 = {
isa = PBXGroup;
children = (
FAF521711DBD9716009B221A /* rswift-Bridging-Header.h */,
D5EA0DF61A3DF45600FFEBC4 /* R.swift */,
D57946691B9347A70044D2FC /* Xcode.swift */,
D5C5A8ED1BB7193B00163E71 /* OptionKit */,
Expand Down
28 changes: 24 additions & 4 deletions R.swift/Util/IgnoreFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import Foundation

class IgnoreFile {
private let patterns: [String]
private let ignoreFileURL: URL?

init() {
patterns = []
ignoreFileURL = nil
}

init(ignoreFileURL: URL) throws {
let ignoreFileParentPath = ignoreFileURL.baseURL?.absoluteString ?? ""
self.ignoreFileURL = ignoreFileURL
patterns = try String(contentsOf: ignoreFileURL)
.components(separatedBy: CharacterSet.newlines)
.filter(IgnoreFile.isPattern)
.map { ignoreFileParentPath + $0 }
}

static private func isPattern(potentialPattern: String) -> Bool {
Expand All @@ -35,7 +36,26 @@ class IgnoreFile {

func match(url: NSURL) -> Bool {
return patterns
.map { NSURL.init(string: $0) }
.any { url == $0 }
.flatMap { listFilePaths(pattern: $0) }
.map { NSURL.init(string: $0, relativeTo: ignoreFileURL?.baseURL)?.absoluteURL }
.flatMap { $0 }
.any { url == $0 as NSURL }
}

private func listFilePaths(pattern: String) -> [String] {
if (pattern.isEmpty) { return [] }

var globObj = glob_t()
var paths = [String]()
glob(pattern, 0, nil, &globObj)
for i in 0 ..< Int(globObj.gl_matchc) {
let mutablePointer = globObj.gl_pathv[Int(i)]
if let charPointer = UnsafePointer<CChar>(mutablePointer) {
let filePath = String.init(cString: charPointer)
paths.append(filePath)
}
}
globfree(&globObj)
return paths
}
}
5 changes: 5 additions & 0 deletions rswift-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import <glob.h>

0 comments on commit 9a32a54

Please sign in to comment.