Skip to content

Instantly share code, notes, and snippets.

@marcprux
Created December 30, 2022 15:54
Show Gist options
  • Save marcprux/db91be01e80ffab3059e7de9bc086137 to your computer and use it in GitHub Desktop.
Save marcprux/db91be01e80ffab3059e7de9bc086137 to your computer and use it in GitHub Desktop.
func loadSourceRoot() throws -> URL {
let env = ProcessInfo.processInfo.environment // "DYLD_LIBRARY_PATH": "/Users/marc/Library/Developer/Xcode/DerivedData/App-eyahphpvsfdoezahxmpgdlrnhwxg/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection"
guard let dyld = env["DYLD_LIBRARY_PATH"]?.split(separator: ":").first else { // e.g. /Users/marc/Library/Developer/Xcode/DerivedData/App-eyahphpvsfdoezahxmpgdlrnhwxg/Build/Products/Debug-iphonesimulator
throw CocoaError(.fileNoSuchFile)
}
// we have a bunch of potential places to guess here, but this is one of them…
let workspaceInfo = URL(fileURLWithPath: "../../../info.plist", isDirectory: false, relativeTo: URL(fileURLWithPath: String(dyld), isDirectory: true))
let plist = try PropertyListSerialization.propertyList(from: Data(contentsOf: workspaceInfo), options: [], format: nil)
guard let workspacePath = (plist as? NSDictionary)?["WorkspacePath"] as? String else {
// I don't know what this will contain if it isn't launched from a workspace
throw CocoaError(.fileNoSuchFile)
}
// workspacePath will be something like: /opt/src/appfair/World-Fair/App.xcworkspace
return URL(fileURLWithPath: workspacePath).deletingLastPathComponent() // /opt/src/appfair/World-Fair
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment