-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ololx/develop
Add the relative path usage instead absolute path in symlink target URL
- Loading branch information
Showing
13 changed files
with
342 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Path.swift | ||
// quick-symlink | ||
// | ||
// Created by Alexander A. Kropotin on 22/08/2021. | ||
// Copyright © 2021 Alexander A. Kropotin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol Path { | ||
|
||
func relativize(to other: Path!) -> Path!; | ||
|
||
func getPathFragments() -> [String]!; | ||
|
||
func toUrl() -> URL?; | ||
|
||
func toUriString() -> String?; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// ResourcePath.swift | ||
// quick-symlink | ||
// | ||
// Created by Alexander A. Kropotin on 22/08/2021. | ||
// Copyright © 2021 Alexander A. Kropotin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class ResourcePath: Path { | ||
|
||
public static func of(url: URL!) -> Path! { | ||
return ResourcePath.of(fragments: url.pathComponents) | ||
} | ||
|
||
public static func of(fragments: [String]!) -> Path! { | ||
return ResourcePath.init(of: fragments); | ||
} | ||
|
||
private var uriFragments: [String]!; | ||
|
||
public init(of fragments: [String]) { | ||
self.uriFragments = fragments; | ||
} | ||
|
||
public func getPathFragments() -> [String]! { | ||
return self.uriFragments; | ||
} | ||
|
||
public func relativize(to other: Path!) -> Path! { | ||
var pathFragments = self.uriFragments; | ||
var targetPathFragments = other.getPathFragments(); | ||
|
||
var destinationPath = URL.init(string: "./")!; | ||
for targetPathFragment in targetPathFragments! { | ||
if (!(pathFragments?.contains(targetPathFragment))!) { | ||
break; | ||
} | ||
|
||
pathFragments?.remove(at: 0); | ||
targetPathFragments?.remove(at: 0); | ||
} | ||
|
||
for _ in targetPathFragments! { | ||
destinationPath.appendPathComponent("../"); | ||
} | ||
|
||
for pathFragment in pathFragments! { | ||
destinationPath.appendPathComponent(pathFragment); | ||
} | ||
|
||
//pathFragments!.append(contentsOf: targetPathFragments!); | ||
|
||
return ResourcePath.of(url: destinationPath); | ||
} | ||
|
||
public func toUrl() -> URL? { | ||
if self.uriFragments.count == 0 { | ||
return nil; | ||
} | ||
|
||
var uri = URL.init(string: self.uriFragments.first!)!; | ||
for fragment in self.uriFragments.dropFirst().dropLast() { | ||
uri.appendPathComponent(fragment); | ||
uri.appendPathComponent("/"); | ||
} | ||
uri.appendPathComponent(self.uriFragments.last!); | ||
|
||
return uri; | ||
} | ||
|
||
public func toUriString() -> String? { | ||
if self.uriFragments.count == 0 { | ||
return nil; | ||
} | ||
|
||
return self.toUrl()!.absoluteString; | ||
} | ||
|
||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>BNDL</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ResourcePathTest.swift | ||
// quick-symlink-tests | ||
// | ||
// Created by Alexander A. Kropotin on 22/08/2021. | ||
// Copyright © 2021 Alexander A. Kropotin. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
class ResourcePathTest: XCTestCase { | ||
|
||
func test_toURL_methodExecution_returnedURLIsEqualToInitialURL() { | ||
let uri: URL = URL.init(string: "/a/b/c/d")!; | ||
let path: Path = ResourcePath.of(url: uri); | ||
|
||
XCTAssert(path.toUrl() == uri, "The Path URL is not equal to URL"); | ||
} | ||
|
||
func test_relativize_whenCurrentDirectoryIsNestedToOtherDirectory_thenReturnPathStartingFromOtherDirectory() { | ||
let currentUri: URL = URL.init(string: "/a/b/c/d")!; | ||
let otherUri: URL = URL.init(string: "/a/b")!; | ||
|
||
let relativePath: Path = ResourcePath.of(url: currentUri) | ||
.relativize(to: ResourcePath.of(url: otherUri)); | ||
|
||
XCTAssert(relativePath.toUriString() == "./c/d", "The relative path is wrong"); | ||
} | ||
|
||
func test_relativize_whenOtherDirectoryIsNestedToCurrentDirectory_thenReturnPathWithOnlyJumpsAboveOtherDirectory() { | ||
let currentUri: URL = URL.init(string: "/a/b")!; | ||
let otherUri: URL = URL.init(string: "/a/b/c/d")!; | ||
|
||
let relativePath: Path = ResourcePath.of(url: currentUri) | ||
.relativize(to: ResourcePath.of(url: otherUri)); | ||
|
||
XCTAssert(relativePath.toUriString() == "./../..", "The relative path is wrong"); | ||
} | ||
|
||
func test_relativize_whenCurrentDirectoryAndOtherDirectoryAreNestedToGeneralDirectoryy_thenReturnPathWithJumpsAboveOtherDirectoryAndPartOfCurrentDirectory() { | ||
let currentUri: URL = URL.init(string: "/a/b/c1/d1")!; | ||
let otherUri: URL = URL.init(string: "/a/b/c2/d2")!; | ||
|
||
let relativePath: Path = ResourcePath.of(url: currentUri) | ||
.relativize(to: ResourcePath.of(url: otherUri)); | ||
|
||
XCTAssert(relativePath.toUriString() == "./../../c1/d1", "The relative path is wrong"); | ||
} | ||
} |
Oops, something went wrong.