Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230import Foundation
struct DevError: Error, CustomStringConvertible { let description: String init(_ description: String) { self.description = description }}
enum DevHex { static func string<Bytes: Sequence>(_ bytes: Bytes) -> String where Bytes.Element == UInt8 { let digits = Array("0123456789abcdef".utf8) var encoded: [UInt8] = [] encoded.reserveCapacity(64) for byte in bytes { encoded.append(digits[Int(byte >> 4)]) encoded.append(digits[Int(byte & 15)]) } return String(decoding: encoded, as: UTF8.self) }}
struct DevProcessResult: Sendable { let status: Int32 let stdout: Data let stderr: Data var output: String { String(decoding: stdout, as: UTF8.self) } var errorOutput: String { String(decoding: stderr, as: UTF8.self) }}
enum DevProcess { static func run( _ executable: String, _ arguments: [String], input: Data? = nil, directory: URL? = nil, environment: [String: String]? = nil, inspectFiles: ((URL) throws -> Void)? = nil ) throws -> DevProcessResult { let temporary = FileManager.default.temporaryDirectory.appendingPathComponent("parcel-dev-\(UUID())") try FileManager.default.createDirectory( at: temporary, withIntermediateDirectories: false, attributes: [.posixPermissions: 0o700]) defer { do { try FileManager.default.removeItem(at: temporary) } catch { FileHandle.standardError.write(Data("Could not remove process files: \(error)\n".utf8)) } } let output = temporary.appendingPathComponent("stdout") let errors = temporary.appendingPathComponent("stderr") try Data().write(to: output) try Data().write(to: errors) for path in [output, errors] { try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path.path) } let outputHandle = try FileHandle(forWritingTo: output) let errorHandle = try FileHandle(forWritingTo: errors) let inputHandle: FileHandle if let input { let path = temporary.appendingPathComponent("stdin") try input.write(to: path) try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path.path) inputHandle = try FileHandle(forReadingFrom: path) } else { inputHandle = FileHandle.nullDevice } let process = Process() if executable.contains("/") { process.executableURL = URL(fileURLWithPath: executable) process.arguments = arguments } else { process.executableURL = URL(fileURLWithPath: "/usr/bin/env") process.arguments = [executable] + arguments } process.currentDirectoryURL = directory process.environment = environment ?? ProcessInfo.processInfo.environment process.standardInput = inputHandle process.standardOutput = outputHandle process.standardError = errorHandle do { try inspectFiles?(temporary) try process.run() process.waitUntilExit() } catch { try outputHandle.close() try errorHandle.close() if input != nil { try inputHandle.close() } throw error } try outputHandle.close() try errorHandle.close() if input != nil { try inputHandle.close() } return try DevProcessResult( status: process.terminationStatus, stdout: Data(contentsOf: output), stderr: Data(contentsOf: errors)) }
static func checked( _ executable: String, _ arguments: [String], input: Data? = nil, directory: URL? = nil, environment: [String: String]? = nil ) throws -> Data { let result = try run(executable, arguments, input: input, directory: directory, environment: environment) guard result.status == 0 else { throw DevError( "\(executable) failed (\(result.status)): \(result.errorOutput.trimmingCharacters(in: .whitespacesAndNewlines))" ) } return result.stdout }}
indirect enum DevJSON: Codable, Sendable, Equatable { case object([String: DevJSON]) case array([DevJSON]) case string(String) case number(Double) case bool(Bool) case null
init(from decoder: any Decoder) throws { let container = try decoder.singleValueContainer() if container.decodeNil() { self = .null } else if let value = try Self.candidate(Bool.self, container: container) { self = .bool(value) } else if let value = try Self.candidate(String.self, container: container) { self = .string(value) } else if let value = try Self.candidate(Double.self, container: container) { self = .number(value) } else if let value = try Self.candidate([DevJSON].self, container: container) { self = .array(value) } else { self = .object(try container.decode([String: DevJSON].self)) } }
private static func candidate<Value: Decodable>( _ type: Value.Type, container: any SingleValueDecodingContainer ) throws -> Value? { do { return try container.decode(type) } catch DecodingError.typeMismatch { return nil } }
func encode(to encoder: any Encoder) throws { var container = encoder.singleValueContainer() switch self { case .object(let value): try container.encode(value) case .array(let value): try container.encode(value) case .string(let value): try container.encode(value) case .number(let value): try container.encode(value) case .bool(let value): try container.encode(value) case .null: try container.encodeNil() } }
subscript(_ key: String) -> DevJSON? { object?[key] } var object: [String: DevJSON]? { if case .object(let value) = self { value } else { nil } } var array: [DevJSON]? { if case .array(let value) = self { value } else { nil } } var string: String? { if case .string(let value) = self { value } else { nil } } var number: Double? { if case .number(let value) = self { value } else { nil } } var bool: Bool? { if case .bool(let value) = self { value } else { nil } }
static func read(_ path: String) throws -> DevJSON { try JSONDecoder().decode(Self.self, from: Data(contentsOf: URL(fileURLWithPath: path))) } static func decode(_ data: Data) throws -> DevJSON { try JSONDecoder().decode(Self.self, from: data) } func encoded() throws -> Data { let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] return try encoder.encode(self) } func requireObject() throws -> [String: DevJSON] { guard let object else { throw DevError("Expected a JSON object") } return object } func requireArray() throws -> [DevJSON] { guard let array else { throw DevError("Expected a JSON array") } return array } func requireString() throws -> String { guard let string else { throw DevError("Expected a JSON string") } return string }}
struct DevProduct: Sendable { let root: URL let values: [String: String] subscript(_ key: String) -> String? { values[key] } static func read(root: URL) throws -> DevProduct { let text = try String(contentsOf: root.appendingPathComponent("Scripts/product.env"), encoding: .utf8) var values: [String: String] = [:] for line in text.components(separatedBy: .newlines) { let line = line.trimmingCharacters(in: .whitespaces) if line.isEmpty || line.hasPrefix("#") { continue } guard line.hasPrefix("export "), let separator = line.firstIndex(of: "=") else { throw DevError("Product settings must be literal exports") } let key = String(line.dropFirst(7).prefix(upTo: separator)) guard try NameRegex.matches("PRODUCT_[A-Z0-9_]+", key) else { throw DevError("Product settings must be literal exports") } var value = String(line[line.index(after: separator)...]) if value.hasPrefix("'") && value.hasSuffix("'") && value.count >= 2 { value = String(value.dropFirst().dropLast()) guard !value.contains("'") else { throw DevError("Product settings must be literal exports") } } else if value.hasPrefix("\"") && value.hasSuffix("\"") && value.count >= 2 { value = String(value.dropFirst().dropLast()) guard !value.contains("\"") else { throw DevError("Product settings must be literal exports") } } else { guard try NameRegex.matches("[A-Za-z0-9_./:@%+\\-]*", value) else { throw DevError("Product settings must be literal exports") } } guard !value.contains("$"), !value.contains("`"), !value.contains("\\") else { throw DevError("Product settings must be literal exports") } values[key] = ProcessInfo.processInfo.environment[key] ?? value } return DevProduct(root: root, values: values) } func require(_ key: String) throws -> String { guard let value = values[key], !value.isEmpty else { throw DevError("Missing product setting: \(key)") } return value }}
enum DevVersion { static func validate(_ value: String, root: URL, kind: String = "version") throws { var environment = ProcessInfo.processInfo.environment environment.removeValue(forKey: "BASH_ENV") environment.removeValue(forKey: "ENV") _ = try DevProcess.checked( "/bin/bash", [root.appendingPathComponent("Scripts/version.sh").path, kind, value], environment: environment ) }}