diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index 66523bb7..6ec7fcb3 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -10,33 +10,45 @@ import Foundation var verbose = false -func printUsage() { - let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent - print("Generate appcast from a directory of Sparkle update archives\n", - "Usage:\n", - " \(command) \n", - " e.g. \(command) ./my-app-release-zipfiles/\n", - "\nOR for old apps that have a DSA keys (deprecated):\n", - " \(command) \n", - " e.g. \(command) dsa_priv.pem archives/\n", - "\n", - "Appcast files and deltas will be written to the archives directory.\n", - "Note that pkg-based updates are not supported.\n" - ) +// Enum that contains keys for command-line arguments +struct CommandLineArguments { + var privateDSAKey : SecKey? + var privateEdString : String? + var downloadURLPrefix : URL? + var releaseNotesURLPrefix: URL? + var outputPathURL: URL? + var archivesSourceDir: URL? } -func printHelp() { +func printUsage() { let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent - print( - "Usage: \(command) [OPTIONS] [ARCHIVES_FOLDER]\n", - "Options:\n", - "\t-f: provide the path to the private DSA key\n", - "\t-n: provide the name of the private DSA key. This option has to be used together with `-k`\n", - "\t-k: provide the name of the keychain. This option has to be used together with `-n`\n", - "\t-s: provide the path to the private EdDSA key\n", - "\t--download-url-prefix: provide a prefix used to construct URLs for update downloads\n", - "\t--release-notes-url-prefix: provide a prefix used to construct URLs for release notes\n" - ) + let usage = """ + +Generate appcast from a directory of Sparkle update archives + +Usage: \(command) [OPTIONS] [ARCHIVES_FOLDER] + -h: prints this message + -f: provide the path to the private DSA key + -n: provide the name of the private DSA key. This option must be used with `-k` + -k: provide the name of the keychain. This option must be used with `-n` + -s: provide the path to the private EdDSA key + -o: provide a filename for the generated appcast (allowed when only one will be created) + + --download-url-prefix: provide a prefix used to construct URLs for update downloads + --release-notes-url-prefix: provide a prefix used to construct URLs for release notes + +Examples: + \(command) ./my-app-release-zipfiles/ + \(command) -o appcast-name.xml ./my-app-release-zipfiles/ + + \(command) dsa_priv.pem ./my-app-release-zipfiles/ [DEPRECATED] + +Appcast files and deltas will be written to the archives directory. +Note that pkg-based updates are not supported. + +""" + + print(usage) } func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> PrivateKeys { @@ -77,26 +89,24 @@ func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> Pr } /** - * Parses all possible command line options and returns the values in a tuple. + * Parses all possible command line options and returns a struct that contains them */ -func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, privateEdString: String?, downloadUrlPrefix: URL?, releaseNotesURLPrefix: URL?, archivesSourceDir: URL) { - // if the option `-h` is in the argument list print the help dialog - if argumentList.contains("-h") { - printHelp() +func parseCommandLineOptions() -> CommandLineArguments { + var arguments = CommandLine.arguments + + // If there are fewer than two arguments (the name of the application + + // the required archive directory), or if `-h` is in the argument list, + // then show the usage message + if arguments.count < 2 || arguments.contains("-h") { + printUsage() exit(1) } - - // make a mutable copy of the argument list - var arguments = argumentList - // remove the first element since this is the path to executable which we don't need + + // Remove the first element since this is the path to executable which we don't need arguments.removeFirst() - // define the variables for the possible argument values - var privateDSAKey: SecKey? - var privateEdString: String? - var downloadUrlPrefix: URL? - var releaseNotesURLPrefix: URL? - var archivesSourceDir: URL + // Create the struct that will hold the parsed args + var commandLineArguments = CommandLineArguments() // check if the private dsa key option is present if let privateDSAKeyOptionIndex = arguments.firstIndex(of: "-f") { @@ -109,7 +119,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, // get the private DSA key let privateKeyUrl = URL(fileURLWithPath: arguments[privateDSAKeyOptionIndex + 1]) do { - privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl) + commandLineArguments.privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl) } catch { print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error) exit(1) @@ -132,7 +142,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, let keyName: String = arguments[keyNameOptionIndex + 1] let keychainUrl: URL = URL(fileURLWithPath: arguments[keychainNameOptionIndex + 1]) do { - privateDSAKey = try loadPrivateDSAKey(named: keyName, fromKeychainAt: keychainUrl) + commandLineArguments.privateDSAKey = try loadPrivateDSAKey(named: keyName, fromKeychainAt: keychainUrl) } catch { print("Unable to load DSA private key '\(keyName)' from keychain at", keychainUrl.path, "\n", error) exit(1) @@ -154,7 +164,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, } // get the private EdDSA key string - privateEdString = arguments[privateEdDSAKeyOptionIndex + 1] + commandLineArguments.privateEdString = arguments[privateEdDSAKeyOptionIndex + 1] // remove the already parsed argument arguments.remove(at: privateEdDSAKeyOptionIndex + 1) @@ -170,7 +180,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, } // get the download url prefix - downloadUrlPrefix = URL(string: arguments[downloadUrlPrefixOptionIndex + 1]) + commandLineArguments.downloadURLPrefix = URL(string: arguments[downloadUrlPrefixOptionIndex + 1]) // remove the parsed argument arguments.remove(at: downloadUrlPrefixOptionIndex + 1) @@ -180,28 +190,44 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, // Check if a URL prefix was specified for the release notes if let releaseNotesURLPrefixOptionIndex = arguments.firstIndex(of: "--release-notes-url-prefix") { if releaseNotesURLPrefixOptionIndex + 1 >= arguments.count { - print("Foo few arguments were given") + print("Too few arguments were given") exit(1) } // Get the URL prefix for the release notes - releaseNotesURLPrefix = URL(string: arguments[releaseNotesURLPrefixOptionIndex + 1]) + commandLineArguments.releaseNotesURLPrefix = URL(string: arguments[releaseNotesURLPrefixOptionIndex + 1]) // Remove the parsed argument arguments.remove(at: releaseNotesURLPrefixOptionIndex + 1) arguments.remove(at: releaseNotesURLPrefixOptionIndex) } + + // Check if an output filename was specified + if let outputFilenameOptionIndex = arguments.firstIndex(of: "-o") { + // check that when accessing the value of the option we don't get out of bounds + if outputFilenameOptionIndex + 1 >= arguments.count { + print("Too few arguments were given") + exit(1) + } + + // Get the URL prefix for the release notes + commandLineArguments.outputPathURL = URL(fileURLWithPath: arguments[outputFilenameOptionIndex + 1]) + + // Remove the parsed argument + arguments.remove(at: outputFilenameOptionIndex + 1) + arguments.remove(at: outputFilenameOptionIndex) + } // now that all command line options have been removed from the arguments array // there should only be the path to the private DSA key (if provided) path to the archives dir left if arguments.count == 2 { // if there are two arguments left they are the private DSA key and the path to the archives directory (in this order) // first get the private DSA key - let privateKeyUrl = URL(fileURLWithPath: arguments[0]) + let privateKeyURL = URL(fileURLWithPath: arguments[0]) do { - privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl) + commandLineArguments.privateDSAKey = try loadPrivateDSAKey(at: privateKeyURL) } catch { - print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error) + print("Unable to load DSA private key from", privateKeyURL.path, "\n", error) exit(1) } @@ -210,49 +236,64 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, } // now only the archives source dir is left - archivesSourceDir = URL(fileURLWithPath: arguments[0], isDirectory: true) + if let archivesSourceDir = arguments.first { + commandLineArguments.archivesSourceDir = URL(fileURLWithPath: archivesSourceDir, isDirectory: true) + } else { + print("Archive folder must be specified") + exit(1); + } - return (privateDSAKey, privateEdString, downloadUrlPrefix, releaseNotesURLPrefix, archivesSourceDir) + return commandLineArguments } func main() { - let args = CommandLine.arguments - if args.count < 2 { - printUsage() - exit(1) - } - - var privateDSAKey: SecKey? - var privateEdString: String? - var downloadUrlPrefix: URL? - var releaseNotesURLPrefix: URL? - var archivesSourceDir: URL - - (privateDSAKey, privateEdString, downloadUrlPrefix, releaseNotesURLPrefix, archivesSourceDir) = parseCommandLineOptions(argumentList: args) - - let keys = loadPrivateKeys(privateDSAKey, privateEdString) - + // Parse the command line arguments + let args = parseCommandLineOptions() + + // If parsing the command line options was successful, then + // the archivesSourceDir must exist + let archivesSourceDir = args.archivesSourceDir! + + // Extract the keys + let keys = loadPrivateKeys(args.privateDSAKey, args.privateEdString) + do { let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, keys: keys, verbose: verbose) // If a URL prefix was provided, set on the archive items - if downloadUrlPrefix != nil || releaseNotesURLPrefix != nil { + if args.downloadURLPrefix != nil || args.releaseNotesURLPrefix != nil { for (_, archiveItems) in allUpdates { for archiveItem in archiveItems { - if downloadUrlPrefix != nil { - archiveItem.downloadUrlPrefix = downloadUrlPrefix + if let downloadURLPrefix = args.downloadURLPrefix { + archiveItem.downloadUrlPrefix = downloadURLPrefix } - if releaseNotesURLPrefix != nil { + if let releaseNotesURLPrefix = args.releaseNotesURLPrefix { archiveItem.releaseNotesURLPrefix = releaseNotesURLPrefix } } } } + // If a (single) output filename was specified on the command-line, but more than one + // appcast file was found in the archives, then it's an error. + if let outputPathURL = args.outputPathURL, + allUpdates.count > 1 { + print("Cannot write to \(outputPathURL.path): multiple appcasts found") + exit(1); + } + for (appcastFile, updates) in allUpdates { - let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir) + // If an output filename was specified, use it. + // Otherwise, use the name of the appcast file found in the archive. + let appcastDestPath = args.outputPathURL ?? URL(fileURLWithPath: appcastFile, + relativeTo: archivesSourceDir) + + // Write the appcast try writeAppcast(appcastDestPath: appcastDestPath, updates: updates) - print("Written", appcastDestPath.path, "based on", updates.count, "updates") + + // Inform the user, pluralizing "update" if necessary + let updateString = (updates.count == 1) ? "update" : "updates" + print("Wrote \(updates.count) \(updateString) to: \(appcastDestPath.path)") } } catch { print("Error generating appcast from directory", archivesSourceDir.path, "\n", error)