Skip to content

Commit 5137f6e

Browse files
authored
Merge pull request #81324 from hamishknight/ext2
[xcodegen] Clean up file extension handling
2 parents d87b776 + 555bc1b commit 5137f6e

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

utils/swift-xcodegen/Sources/SwiftXcodeGen/Path/AnyPath.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,3 @@ extension AnyPath: ExpressibleByArgument {
7474
self.init(rawPath)
7575
}
7676
}
77-
78-
extension StringProtocol {
79-
func hasExtension(_ ext: FileExtension) -> Bool {
80-
FilePath(String(self)).extension == ext.rawValue
81-
}
82-
}

utils/swift-xcodegen/Sources/SwiftXcodeGen/Path/PathProtocol.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,11 @@ public extension PathProtocol {
6868
return RelativePath(result)
6969
}
7070

71-
func hasExtension(_ ext: FileExtension) -> Bool {
72-
storage.extension == ext.rawValue
73-
}
7471
func hasExtension(_ exts: FileExtension...) -> Bool {
7572
// Note that querying `.extension` involves re-parsing, so only do it
7673
// once here.
77-
guard let ext = storage.extension else { return false }
78-
return exts.contains(where: {
79-
ext.compare($0.rawValue, options: .caseInsensitive) == .orderedSame
80-
})
74+
guard let pathExt = storage.extension else { return false }
75+
return exts.contains(where: { $0.matches(pathExt) })
8176
}
8277

8378
func starts(with other: Self) -> Bool {
@@ -158,3 +153,16 @@ extension Collection where Element: PathProtocol {
158153
return result == first ? result.parentDir : result
159154
}
160155
}
156+
157+
extension StringProtocol {
158+
func hasExtension(_ exts: FileExtension...) -> Bool {
159+
guard let pathExt = FilePath(String(self)).extension else { return false }
160+
return exts.contains(where: { $0.matches(pathExt) })
161+
}
162+
}
163+
164+
extension FileExtension {
165+
func matches(_ extStr: String) -> Bool {
166+
rawValue.compare(extStr, options: .caseInsensitive) == .orderedSame
167+
}
168+
}

utils/swift-xcodegen/Tests/SwiftXcodeGenTest/PathTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,28 @@ class PathTests: XCTestCase {
3737
XCTAssertEqual(RelativePath("foo/bar").dropLast(2), "")
3838
XCTAssertEqual(RelativePath("foo/bar").dropLast(5), "")
3939
}
40+
41+
func testExtension() throws {
42+
func match(
43+
_ ext: FileExtension, with path: String,
44+
value: Bool = true, file: StaticString = #file, line: UInt = #line
45+
) {
46+
XCTAssert(path.hasExtension(ext) == value, file: file, line: line)
47+
XCTAssert(AnyPath(path).hasExtension(ext) == value, file: file, line: line)
48+
}
49+
match(.swift, with: "x.swift")
50+
match(.swift, with: "/x.swift")
51+
match(.swift, with: ".swift", value: false)
52+
match(.swift, with: "/.swift", value: false)
53+
54+
match(.swift, with: "x.SWIFT")
55+
match(.swift, with: "/x.SWIFT")
56+
match(.swift, with: ".SWIFT", value: false)
57+
match(.swift, with: "/.SWIFT", value: false)
58+
59+
match(.swift, with: "x.swiftx", value: false)
60+
61+
XCTAssert("x.sWift".hasExtension(.asm, .swift))
62+
XCTAssert(AnyPath("x.sWift").hasExtension(.asm, .swift))
63+
}
4064
}

0 commit comments

Comments
 (0)