-
Notifications
You must be signed in to change notification settings - Fork 818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compute relative paths to support sources outside a spec's directory #524
Changes from all commits
9f6f521
3b15144
a03e5a1
0639629
d12eab6
b5dcc9c
34f7d3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,7 +119,7 @@ class SourceGenerator { | |
if let fileReference = fileReferencesByPath[fileReferenceKey] { | ||
return fileReference | ||
} else { | ||
let fileReferencePath = path.byRemovingBase(path: inPath) | ||
let fileReferencePath = (try? path.relativePath(from: inPath)) ?? path | ||
var fileReferenceName: String? = name ?? fileReferencePath.lastComponent | ||
if fileReferencePath.string == fileReferenceName { | ||
fileReferenceName = nil | ||
|
@@ -254,7 +254,7 @@ class SourceGenerator { | |
|
||
let groupName = name ?? path.lastComponent | ||
let groupPath = isTopLevelGroup ? | ||
path.byRemovingBase(path: project.basePath).string : | ||
((try? path.relativePath(from: project.basePath)) ?? path).string : | ||
elliottwilliams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
path.lastComponent | ||
let group = PBXGroup( | ||
children: children, | ||
|
@@ -474,7 +474,7 @@ class SourceGenerator { | |
var sourcePath = path | ||
switch type { | ||
case .folder: | ||
let folderPath = Path(targetSource.path) | ||
let folderPath = project.basePath + Path(targetSource.path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to change this line to fix a test that broke when I swapped out It seems like folder target sources were being created without the base directory as part of the path. In the tests, I saw that a folder "Sources/A" would passed to |
||
let fileReference = getFileReference( | ||
path: folderPath, | ||
inPath: project.basePath, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Spectre | ||
import PathKit | ||
import XCTest | ||
|
||
class PathExtensionsTests: XCTestCase { | ||
|
||
func testPathRelativeToPath() { | ||
func relativePath(to path: String, from base: String) throws -> String { | ||
return try Path(path).relativePath(from: Path(base)).string | ||
} | ||
|
||
// These are based on ruby's tests for Pathname#relative_path_from: | ||
// https://github.com/ruby/ruby/blob/7c2bbd1c7d40a30583844d649045824161772e36/test/pathname/test_pathname.rb#L297 | ||
describe { | ||
$0.it("resolves single-level paths") { | ||
try expect(relativePath(to: "a", from: "b")) == "../a" | ||
try expect(relativePath(to: "a", from: "b/")) == "../a" | ||
try expect(relativePath(to: "a/", from: "b")) == "../a" | ||
try expect(relativePath(to: "a/", from: "b/")) == "../a" | ||
try expect(relativePath(to: "/a", from: "/b")) == "../a" | ||
try expect(relativePath(to: "/a", from: "/b/")) == "../a" | ||
try expect(relativePath(to: "/a/", from: "/b")) == "../a" | ||
try expect(relativePath(to: "/a/", from: "/b/")) == "../a" | ||
} | ||
|
||
$0.it("resolves paths with a common parent") { | ||
try expect(relativePath(to: "a/b", from: "a/c")) == "../b" | ||
try expect(relativePath(to: "../a", from: "../b")) == "../a" | ||
} | ||
|
||
$0.it("resolves dot paths") { | ||
try expect(relativePath(to: "a", from: ".")) == "a" | ||
try expect(relativePath(to: ".", from: "a")) == ".." | ||
try expect(relativePath(to: ".", from: ".")) == "." | ||
try expect(relativePath(to: "..", from: "..")) == "." | ||
try expect(relativePath(to: "..", from: ".")) == ".." | ||
} | ||
|
||
$0.it("resolves multi-level paths") { | ||
try expect(relativePath(to: "/a/b/c/d", from: "/a/b")) == "c/d" | ||
try expect(relativePath(to: "/a/b", from: "/a/b/c/d")) == "../.." | ||
try expect(relativePath(to: "/e", from: "/a/b/c/d")) == "../../../../e" | ||
try expect(relativePath(to: "a/b/c", from: "a/d")) == "../b/c" | ||
try expect(relativePath(to: "/../a", from: "/b")) == "../a" | ||
try expect(relativePath(to: "../a", from: "b")) == "../../a" | ||
try expect(relativePath(to: "/a/../../b", from: "/b")) == "." | ||
try expect(relativePath(to: "a/..", from: "a")) == ".." | ||
try expect(relativePath(to: "a/../b", from: "b")) == "." | ||
} | ||
|
||
$0.it("backtracks on a non-normalized base path") { | ||
try expect(relativePath(to: "a", from: "b/..")) == "a" | ||
try expect(relativePath(to: "b/c", from: "b/..")) == "b/c" | ||
} | ||
|
||
$0.it("throws when given unresolvable paths") { | ||
try expect(relativePath(to: "/", from: ".")).toThrow() | ||
try expect(relativePath(to: ".", from: "/")).toThrow() | ||
try expect(relativePath(to: "a", from: "..")).toThrow() | ||
try expect(relativePath(to: ".", from: "..")).toThrow() | ||
try expect(relativePath(to: "a", from: "b/../..")).toThrow() | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does normalize just do the same thing? What does adding the components together again do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalize calls
NSString.standardizingPath
, which almost does the same thing but doesn't remove parent directory references in relative paths:PathKit's + operator backs up a level if you try to add
..
to a path, which means it can handle this normalization edge case for us. In other words: