Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created September 22, 2024 16:56
Show Gist options
  • Save minsOne/efc50aeab8e40f73ef732777e7b7f8b2 to your computer and use it in GitHub Desktop.
Save minsOne/efc50aeab8e40f73ef732777e7b7f8b2 to your computer and use it in GitHub Desktop.
path
import ProjectDescription
import Foundation
func hello() -> Project {
let projFile = Path.relativeToRoot("Project.swift")
print(#function, "\(projFile.pathString)", #filePath)
print("FileManager.default.currentDirectoryPath: ", FileManager.default.currentDirectoryPath)
let basePath = URL(fileURLWithPath: "/Users/minsone/Developer/20240923/iOSApp/Project.swift")
let targetPath = URL(fileURLWithPath: "/Users/minsone/Developer/Macro/Macros.macro")
if let relativePath = targetPath.relativePath(from: basePath) {
print("Relative path: \(relativePath)")
} else {
print("Cannot compute relative path.")
}
let project = Project(
name: "iOSModule",
targets: [
.target(
name: "iOSModule",
destinations: .iOS,
product: .app,
bundleId: "io.tuist.iOSModule",
infoPlist: .extendingDefault(
with: [
"UILaunchScreen": [
"UIColorName": "",
"UIImageName": "",
],
]
),
sources: ["Sources/**"],
resources: ["Resources/**"],
dependencies: []
),
.target(
name: "iOSAppTests",
destinations: .iOS,
product: .unitTests,
bundleId: "io.tuist.iOSAppTests",
infoPlist: .default,
sources: ["Tests/**"],
resources: [],
dependencies: [.target(name: "iOSModule")]
),
]
)
return project
}
let project = hello()
extension URL {
func relativePath(from base: URL) -> String? {
let baseComponents = base.standardized.pathComponents
let targetComponents = self.standardized.pathComponents
var index = 0
while index < baseComponents.count &&
index < targetComponents.count &&
baseComponents[index] == targetComponents[index] {
index += 1
}
// baseComponents에서 공통된 부분을 제외하고 남은 부분 만큼 ".."를 추가
let backtracks = Array(repeating: "..", count: baseComponents.count - index)
// targetComponents에서 공통된 부분 이후 남은 부분
let forwardComponents = targetComponents[index...]
// 배열을 "/"로 결합하여 String으로 변환
return (backtracks + forwardComponents).joined(separator: "/")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment