Modern Apps git

← xtool.git

Validate path to Xcode.app/xip (#100)

Commit
5beec343704a7788c227825f9c06e66adc711e00
Author
Kabir Oberai <[email protected]>
When
2025-05-27 12:41 UTC (1 year ago)
Parents
3fb3971
Fixes #16

Diff

Sources/XToolSupport/SDKBuilder.swift
diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swiftindex aa3cf5d..75230cf 100644--- a/Sources/XToolSupport/SDKBuilder.swift+++ b/Sources/XToolSupport/SDKBuilder.swift@@ -16,12 +16,35 @@ struct SDKBuilder {         case app(String)          init(path: String) throws {-            if path.hasSuffix(".xip") {-                self = .xip(path)-            } else if path.hasSuffix(".app") || path.hasSuffix(".app/") {+            var isDir: ObjCBool = false+            guard FileManager.default.fileExists(atPath: path, isDirectory: &isDir) else {+                throw Console.Error("Could not read file or directory at path '\(path)'")+            }++            let url = URL(fileURLWithPath: path)++            if isDir.boolValue {                 self = .app(path)+                let devDir = url.appendingPathComponent("Contents/Developer")+                guard devDir.dirExists else {+                    throw Console.Error("""+                    The provided directory at '\(path)' does not appear to be a version of Xcode: \+                    could not read '\(devDir.path)'.+                    """)+                }             } else {-                throw Console.Error("Expected input path to end in .xip or .app")+                self = .xip(path)+                let handle = try FileHandle(forReadingFrom: url)+                defer { try? handle.close() }++                let expectedMagic = "xar!".utf8+                let actualMagic = try handle.read(upToCount: expectedMagic.count) ?? Data()++                guard actualMagic.elementsEqual(expectedMagic) else {+                    throw Console.Error("""+                    The file at '\(path)' does not appear to be a valid XIP file.+                    """)+                }             }         }     }