|
| 1 | +import AppKit |
| 2 | +import Files |
| 3 | +import Foundation |
| 4 | +import SwiftShell |
| 5 | + |
| 6 | +func validateImageResource(fileName: String?, kind: String) throws { |
| 7 | + guard let path = fileName else { |
| 8 | + return |
| 9 | + } |
| 10 | + |
| 11 | + guard FileManager.default.fileExists(atPath: path) else { |
| 12 | + throw ScriptError.fileNotFound(message: "\(kind) image: \(path)") |
| 13 | + } |
| 14 | + |
| 15 | + guard NSImage(contentsOfFile: path) != nil else { |
| 16 | + throw ScriptError.generalError(message: "Unable to load \(kind) image: \(path)") |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func getAppSetup(scriptSetup: ScriptSetup) throws -> AppSetup { |
| 21 | + #if DEBUGGING |
| 22 | + // Enter testing values from your project |
| 23 | + let sourceRootPath = "" |
| 24 | + let projectDir = "" |
| 25 | + let infoPlistFile = "" |
| 26 | + #else |
| 27 | + guard |
| 28 | + let sourceRootPath = main.env["SRCROOT"], |
| 29 | + let projectDir = main.env["PROJECT_DIR"], |
| 30 | + let infoPlistFile = main.env["INFOPLIST_FILE"] |
| 31 | + else { |
| 32 | + print("Missing environment variables") |
| 33 | + throw ScriptError.moreInfoNeeded(message: "Missing required environment variables: SRCROOT, PROJECT_DIR, INFOPLIST_FILE. Please run script from Xcode script build phase.") |
| 34 | + } |
| 35 | + #endif |
| 36 | + |
| 37 | + print(" sourceRootPath: \(sourceRootPath)") |
| 38 | + print(" projectDir: \(projectDir)") |
| 39 | + print(" infoPlistFile: \(infoPlistFile)") |
| 40 | + |
| 41 | + let sourceFolder = try Folder(path: sourceRootPath) |
| 42 | + let resolvedInfoPlistFile = try resolveInfoPlistPath( |
| 43 | + infoPlistFile: infoPlistFile, |
| 44 | + projectDir: projectDir, |
| 45 | + sourceRootPath: sourceRootPath |
| 46 | + ) |
| 47 | + |
| 48 | + guard let appIconFolder = try locateAppIconFolder( |
| 49 | + named: scriptSetup.appIcon, |
| 50 | + projectDir: projectDir, |
| 51 | + sourceRootFolder: sourceFolder |
| 52 | + ) else { |
| 53 | + throw ScriptError.folderNotFound(message: "\(scriptSetup.appIcon).appiconset - icon asset folder") |
| 54 | + } |
| 55 | + |
| 56 | + guard let originalAppIconFolder = try locateAppIconFolder( |
| 57 | + named: scriptSetup.appIconOriginal, |
| 58 | + projectDir: projectDir, |
| 59 | + sourceRootFolder: sourceFolder |
| 60 | + ) else { |
| 61 | + throw ScriptError.folderNotFound(message: "\(scriptSetup.appIconOriginal).appiconset - source icon asset for modifications") |
| 62 | + } |
| 63 | + |
| 64 | + return try AppSetup( |
| 65 | + sourceRootPath: sourceRootPath, |
| 66 | + projectDir: projectDir, |
| 67 | + infoPlistFile: resolvedInfoPlistFile, |
| 68 | + appIconFolder: appIconFolder, |
| 69 | + appIconContents: iconMetadata(iconFolder: appIconFolder), |
| 70 | + originalAppIconFolder: originalAppIconFolder, |
| 71 | + originalAppIconContents: iconMetadata(iconFolder: originalAppIconFolder) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +func iconMetadata(iconFolder: Folder) throws -> IconMetadata { |
| 76 | + let contentsFile = try iconFolder.file(named: "Contents.json") |
| 77 | + let jsonData = try contentsFile.read() |
| 78 | + do { |
| 79 | + return try JSONDecoder().decode(IconMetadata.self, from: jsonData) |
| 80 | + } catch { |
| 81 | + throw ScriptError.generalError(message: String(describing: error)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func getVersionText(appSetup: AppSetup, designStyle: DesignStyle) throws -> String { |
| 86 | + #if DEBUGGING |
| 87 | + return "1.0 - 20" |
| 88 | + #endif |
| 89 | + |
| 90 | + let versionNumberResult = run("/usr/libexec/PlistBuddy", "-c", "Print CFBundleShortVersionString", appSetup.infoPlistFile) |
| 91 | + let buildNumberResult = run("/usr/libexec/PlistBuddy", "-c", "Print CFBundleVersion", appSetup.infoPlistFile) |
| 92 | + |
| 93 | + guard versionNumberResult.succeeded else { |
| 94 | + throw ScriptError.generalError(message: "Unable to read CFBundleShortVersionString from \(appSetup.infoPlistFile): \(versionNumberResult.stderror)") |
| 95 | + } |
| 96 | + |
| 97 | + guard buildNumberResult.succeeded else { |
| 98 | + throw ScriptError.generalError(message: "Unable to read CFBundleVersion from \(appSetup.infoPlistFile): \(buildNumberResult.stderror)") |
| 99 | + } |
| 100 | + |
| 101 | + var versionNumber = versionNumberResult.stdout |
| 102 | + if versionNumber == "$(MARKETING_VERSION)" { |
| 103 | + versionNumber = main.env["MARKETING_VERSION"] ?? "" |
| 104 | + } |
| 105 | + |
| 106 | + var buildNumber = buildNumberResult.stdout |
| 107 | + if buildNumber == "$(CURRENT_PROJECT_VERSION)" { |
| 108 | + buildNumber = main.env["CURRENT_PROJECT_VERSION"] ?? "" |
| 109 | + } |
| 110 | + |
| 111 | + switch designStyle.versionStyle { |
| 112 | + case .dash: |
| 113 | + return "\(versionNumber) - \(buildNumber)" |
| 114 | + case .parenthesis: |
| 115 | + return "\(versionNumber)(\(buildNumber))" |
| 116 | + case .parenthesisTwoLines: |
| 117 | + return "\(versionNumber)\n(\(buildNumber))" |
| 118 | + case .twoLines: |
| 119 | + return "\(versionNumber)\n\(buildNumber)" |
| 120 | + case .versionOnly: |
| 121 | + return "\(versionNumber)" |
| 122 | + case .buildOnly: |
| 123 | + return "\(buildNumber)" |
| 124 | + case .empty: |
| 125 | + return "" |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +private func resolveInfoPlistPath( |
| 130 | + infoPlistFile: String, |
| 131 | + projectDir: String, |
| 132 | + sourceRootPath: String |
| 133 | +) throws -> String { |
| 134 | + if infoPlistFile.hasPrefix("/") { |
| 135 | + return infoPlistFile |
| 136 | + } |
| 137 | + |
| 138 | + let projectRelativePath = projectDir.appendingPathComponent(path: infoPlistFile) |
| 139 | + if FileManager.default.fileExists(atPath: projectRelativePath) { |
| 140 | + return projectRelativePath |
| 141 | + } |
| 142 | + |
| 143 | + let sourceRelativePath = sourceRootPath.appendingPathComponent(path: infoPlistFile) |
| 144 | + if FileManager.default.fileExists(atPath: sourceRelativePath) { |
| 145 | + return sourceRelativePath |
| 146 | + } |
| 147 | + |
| 148 | + throw ScriptError.fileNotFound(message: "Info.plist: \(infoPlistFile)") |
| 149 | +} |
| 150 | + |
| 151 | +private func locateAppIconFolder( |
| 152 | + named appIcon: String, |
| 153 | + projectDir: String, |
| 154 | + sourceRootFolder: Folder |
| 155 | +) throws -> Folder? { |
| 156 | + let iconFolderName = "\(appIcon).appiconset" |
| 157 | + |
| 158 | + if let projectFolder = try? Folder(path: projectDir), |
| 159 | + let iconFolder = projectFolder.findFirstFolder(name: iconFolderName) { |
| 160 | + return iconFolder |
| 161 | + } |
| 162 | + |
| 163 | + return sourceRootFolder.findFirstFolder(name: iconFolderName) |
| 164 | +} |
0 commit comments