|
1 | 1 | import Foundation |
2 | 2 |
|
3 | 3 | struct PackageBuilder { |
4 | | - let executor: ShellExecutor |
5 | | - let logger: Logger |
6 | | - |
7 | | - /// Analyze the payload and create a component plist |
8 | | - func analyze( |
9 | | - payloadDir: URL, |
10 | | - identifier: String, |
11 | | - version: String, |
12 | | - installLocation: String, |
13 | | - outputPlist: URL |
14 | | - ) async throws { |
15 | | - let arguments = [ |
16 | | - "/usr/bin/pkgbuild", |
17 | | - "--analyze", |
18 | | - "--root", payloadDir.path, |
19 | | - "--identifier", identifier, |
20 | | - "--version", version, |
21 | | - "--install-location", installLocation, |
22 | | - outputPlist.path |
23 | | - ] |
24 | | - |
25 | | - let result = try await executor.runOrThrow(arguments) |
26 | | - logger.log(result.stdout, level: 1) |
| 4 | + let executor: ShellExecutor |
| 5 | + let logger: Logger |
| 6 | + |
| 7 | + /// Analyze the payload and create a component plist |
| 8 | + func analyze( |
| 9 | + payloadDir: URL, |
| 10 | + identifier: String, |
| 11 | + version: String, |
| 12 | + installLocation: String, |
| 13 | + outputPlist: URL |
| 14 | + ) async throws { |
| 15 | + let arguments = [ |
| 16 | + "/usr/bin/pkgbuild", |
| 17 | + "--analyze", |
| 18 | + "--root", payloadDir.path, |
| 19 | + "--identifier", identifier, |
| 20 | + "--version", version, |
| 21 | + "--install-location", installLocation, |
| 22 | + outputPlist.path |
| 23 | + ] |
| 24 | + |
| 25 | + let result = try await executor.runOrThrow(arguments) |
| 26 | + logger.log(result.stdout, level: 1) |
| 27 | + } |
| 28 | + |
| 29 | + /// Build the package |
| 30 | + func build( |
| 31 | + payloadDir: URL, |
| 32 | + outputPath: String, |
| 33 | + identifier: String, |
| 34 | + version: String, |
| 35 | + installLocation: String, |
| 36 | + scripts: URL?, |
| 37 | + ownership: Ownership?, |
| 38 | + relocatable: Bool, |
| 39 | + sign: String?, |
| 40 | + keychain: String?, |
| 41 | + cert: String?, |
| 42 | + tempDir: URL |
| 43 | + ) async throws { |
| 44 | + // First, create the component plist |
| 45 | + let componentPlist = tempDir.appendingPathComponent("\(identifier).plist") |
| 46 | + try await analyze( |
| 47 | + payloadDir: payloadDir, |
| 48 | + identifier: identifier, |
| 49 | + version: version, |
| 50 | + installLocation: installLocation, |
| 51 | + outputPlist: componentPlist |
| 52 | + ) |
| 53 | + |
| 54 | + // Modify relocatable setting if needed |
| 55 | + if !relocatable { |
| 56 | + try PlistHandler.setRelocatable(false, in: componentPlist) |
27 | 57 | } |
28 | | - |
29 | | - /// Build the package |
30 | | - func build( |
31 | | - payloadDir: URL, |
32 | | - outputPath: String, |
33 | | - identifier: String, |
34 | | - version: String, |
35 | | - installLocation: String, |
36 | | - scripts: URL?, |
37 | | - ownership: Ownership?, |
38 | | - relocatable: Bool, |
39 | | - sign: String?, |
40 | | - keychain: String?, |
41 | | - cert: String?, |
42 | | - tempDir: URL |
43 | | - ) async throws { |
44 | | - // First, create the component plist |
45 | | - let componentPlist = tempDir.appendingPathComponent("\(identifier).plist") |
46 | | - try await analyze( |
47 | | - payloadDir: payloadDir, |
48 | | - identifier: identifier, |
49 | | - version: version, |
50 | | - installLocation: installLocation, |
51 | | - outputPlist: componentPlist |
52 | | - ) |
53 | | - |
54 | | - // Modify relocatable setting if needed |
55 | | - if !relocatable { |
56 | | - try PlistHandler.setRelocatable(false, in: componentPlist) |
57 | | - } |
58 | | - |
59 | | - // Build the pkgbuild command |
60 | | - var arguments = [ |
61 | | - "/usr/bin/pkgbuild", |
62 | | - "--root", payloadDir.path, |
63 | | - "--component-plist", componentPlist.path, |
64 | | - "--identifier", identifier, |
65 | | - "--version", version, |
66 | | - "--install-location", installLocation |
67 | | - ] |
68 | | - |
69 | | - if let scriptsDir = scripts { |
70 | | - arguments += ["--scripts", scriptsDir.path] |
71 | | - logger.log("Scripts path: \(scriptsDir.path)", level: 1) |
72 | | - } |
73 | | - |
74 | | - if let ownership = ownership { |
75 | | - arguments += ["--ownership", ownership.rawValue] |
76 | | - } |
77 | | - |
78 | | - if let sign = sign { |
79 | | - arguments += ["--sign", sign] |
80 | | - } |
81 | | - |
82 | | - if let keychain = keychain { |
83 | | - arguments += ["--keychain", keychain] |
84 | | - } |
85 | | - |
86 | | - if let cert = cert { |
87 | | - arguments += ["--cert", cert] |
88 | | - } |
89 | | - |
90 | | - arguments.append(outputPath) |
91 | | - |
92 | | - logger.log("Building package: \(outputPath)", level: 1) |
93 | | - let result = try await executor.run(arguments) |
94 | | - |
95 | | - logger.log(result.stdout, level: 1) |
96 | | - |
97 | | - if result.exitCode != 0 { |
98 | | - throw QuickPkgError.pkgbuildFailed("(\(result.exitCode)) \(result.stderr)") |
99 | | - } |
| 58 | + |
| 59 | + // Build the pkgbuild command |
| 60 | + var arguments = [ |
| 61 | + "/usr/bin/pkgbuild", |
| 62 | + "--root", payloadDir.path, |
| 63 | + "--component-plist", componentPlist.path, |
| 64 | + "--identifier", identifier, |
| 65 | + "--version", version, |
| 66 | + "--install-location", installLocation |
| 67 | + ] |
| 68 | + |
| 69 | + if let scriptsDir = scripts { |
| 70 | + arguments += ["--scripts", scriptsDir.path] |
| 71 | + logger.log("Scripts path: \(scriptsDir.path)", level: 1) |
| 72 | + } |
| 73 | + |
| 74 | + if let ownership = ownership { |
| 75 | + arguments += ["--ownership", ownership.rawValue] |
| 76 | + } |
| 77 | + |
| 78 | + if let sign = sign { |
| 79 | + arguments += ["--sign", sign] |
| 80 | + } |
| 81 | + |
| 82 | + if let keychain = keychain { |
| 83 | + arguments += ["--keychain", keychain] |
| 84 | + } |
| 85 | + |
| 86 | + if let cert = cert { |
| 87 | + arguments += ["--cert", cert] |
| 88 | + } |
| 89 | + |
| 90 | + arguments.append(outputPath) |
| 91 | + |
| 92 | + logger.log("Building package: \(outputPath)", level: 1) |
| 93 | + let result = try await executor.run(arguments) |
| 94 | + |
| 95 | + logger.log(result.stdout, level: 1) |
| 96 | + |
| 97 | + if result.exitCode != 0 { |
| 98 | + throw QuickPkgError.pkgbuildFailed("(\(result.exitCode)) \(result.stderr)") |
100 | 99 | } |
| 100 | + } |
101 | 101 | } |
0 commit comments