Skip to content

Relative paths fix, Xcode generation at non-root path fix #193

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

Merged
merged 1 commit into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/Utility/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ public struct Path {
if path.starts(with: pivot) {
let relativePortion = path.dropFirst(pivot.count)
return join(Array(relativePortion))
} else if path.starts(with: pivot.prefix(1)) {
//only the first matches, so we will be able to find a relative
//path by adding jumps back the directory tree
var newPath = ArraySlice(path)
var newPivot = ArraySlice(pivot)
repeat {
//remove all shared components in the prefix
newPath = newPath.dropFirst()
newPivot = newPivot.dropFirst()
} while newPath.prefix(1) == newPivot.prefix(1)

//as we found the first differing point, the final path is
//a) as many ".." as there are components in newPivot
//b) what's left in newPath
var final = Array(repeating: "..", count: newPivot.count)
final.append(contentsOf: newPath)
let relativePath = Path.join(final)
return relativePath
} else {
let prefix = abs.path ? "/" : ""
return prefix + join(path)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Xcodeproj/generate().swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public func generate(path path: String, package: Package, modules: [SwiftModule]

////// the pbxproj file describes the project and its targets
try open(rootdir, "project.pbxproj") { fwrite in
pbxproj(package: package, modules: modules, products: products, printer: fwrite)
pbxproj(projectPath: path, package: package, modules: modules, products: products, printer: fwrite)
}

////// the scheme acts like an aggregate target for all our targets
Expand Down
4 changes: 2 additions & 2 deletions Sources/Xcodeproj/pbxproj().swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import PackageType
import Utility

public func pbxproj(package package: Package, modules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {
public func pbxproj(projectPath projectPath: String, package: Package, modules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {

let srcroot = package.path
let srcroot = projectPath
let nontests = modules.filter{ !($0 is TestModule) }
let tests = modules.filter{ $0 is TestModule }

Expand Down
1 change: 1 addition & 0 deletions Tests/Utility/FileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ extension RelativePathTests {
("testAbsolute", testAbsolute),
("testRelative", testRelative),
("testMixed", testMixed),
("testRelativeCommonSubprefix", testRelativeCommonSubprefix)
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/Utility/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,10 @@ class RelativePathTests: XCTestCase {
func testMixed() {
XCTAssertEqual("3/4", Path(try! getcwd() + "/1/2/3/4").relative(to: "1/2"))
}

func testRelativeCommonSubprefix() {
XCTAssertEqual("../4", Path("/1/2/4").relative(to: "/1/2/3"))
XCTAssertEqual("../4/5", Path("/1/2/4/5").relative(to: "/1/2/3"))
XCTAssertEqual("../../../4/5", Path("/1/2/4/5").relative(to: "/1/2/3/6/7"))
}
}