|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2017 Caleb Kleveter |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import Console |
| 24 | +import Helpers |
| 25 | +import Foundation |
| 26 | + |
| 27 | +public final class VersonSet: Command { |
| 28 | + public let id: String = "set" |
| 29 | + |
| 30 | + public var signature: [Argument] = [ |
| 31 | + Value(name: "name", help: [ |
| 32 | + "The name of the package to change the version for" |
| 33 | + ]), |
| 34 | + Value(name: "version", help: [ |
| 35 | + "The value for the new version. The format varies depending on the version type used" |
| 36 | + ]), |
| 37 | + Option(name: "xcode", short: "x", help: [ |
| 38 | + "Regenerate the Xcode project after updating a package's version" |
| 39 | + ]), |
| 40 | + Option(name: "from", short: "f", help: [ |
| 41 | + "Sets the dependency version argument to `from: VERSION`" |
| 42 | + ]), |
| 43 | + Option(name: "up-to-next-major", short: "u", help: [ |
| 44 | + "Sets the dependency version argument to `.upToNextMinor(from: \"VERSION\")`" |
| 45 | + ]), |
| 46 | + Option(name: "exact", short: "e", help: [ |
| 47 | + "(Default) Sets the dependency version argument to `.exact(\"VERSION\")`" |
| 48 | + ]), |
| 49 | + Option(name: "range", short: "r", help: [ |
| 50 | + "Sets the dependency version argument to `VERSION`" |
| 51 | + ]), |
| 52 | + Option(name: "branch", short: "b", help: [ |
| 53 | + "Sets the dependency version argument to `.branch(\"VERSION\")`" |
| 54 | + ]), |
| 55 | + Option(name: "revision", help: [ |
| 56 | + "Sets the dependency version argument to `.revision(\"VERSION\")`" |
| 57 | + ]) |
| 58 | + ] |
| 59 | + |
| 60 | + public var help: [String] = [ |
| 61 | + "Changes the version of a single dependency" |
| 62 | + ] |
| 63 | + |
| 64 | + public let console: ConsoleProtocol |
| 65 | + |
| 66 | + public init(console: ConsoleProtocol) { |
| 67 | + self.console = console |
| 68 | + } |
| 69 | + |
| 70 | + public func run(arguments: [String]) throws { |
| 71 | + let updateBar = console.loadingBar(title: "Updating Package Version") |
| 72 | + updateBar.start() |
| 73 | + |
| 74 | + let package = try value("name", from: arguments) |
| 75 | + let version = try value("version", from: arguments) |
| 76 | + let versionLitteral = versionOption(from: arguments, with: version) |
| 77 | + |
| 78 | + let url = try Manifest.current.getPackageUrl(for: package) |
| 79 | + let manifest = try NSMutableString(string: Manifest.current.get()) |
| 80 | + let pattern = try NSRegularExpression( |
| 81 | + pattern: "(\\,?\\n *\\.package\\(url: *\"\(url)\", *)(.*?)(\\),?\\n)", |
| 82 | + options: [] |
| 83 | + ) |
| 84 | + pattern.replaceMatches(in: manifest, options: [], range: NSMakeRange(0, manifest.length), withTemplate: "$1\(versionLitteral)$3") |
| 85 | + try Manifest.current.write(String(manifest)) |
| 86 | + |
| 87 | + _ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"]) |
| 88 | + _ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"]) |
| 89 | + |
| 90 | + updateBar.finish() |
| 91 | + |
| 92 | + if let _ = arguments.options["xcode"] { |
| 93 | + let xcodeBar = console.loadingBar(title: "Generating Xcode Project") |
| 94 | + xcodeBar.start() |
| 95 | + _ = try console.backgroundExecute(program: "swift", arguments: ["package", "generate-xcodeproj"]) |
| 96 | + xcodeBar.finish() |
| 97 | + try console.execute(program: "/bin/sh", arguments: ["-c", "open *.xcodeproj"], input: nil, output: nil, error: nil) |
| 98 | + } |
| 99 | + |
| 100 | + console.output("\(package) version was updated", style: .plain, newLine: true) |
| 101 | + } |
| 102 | + |
| 103 | + private func versionOption(from arguments: [String], with version: String) -> String { |
| 104 | + if arguments.option("from") != nil { |
| 105 | + return "from: \"\(version)\"" |
| 106 | + } else if arguments.option("up-to-next-major") != nil { |
| 107 | + return ".upToNextMajor(from: \"\(version)\")" |
| 108 | + } else if arguments.option("range") != nil { |
| 109 | + return "\"\(version.dropLast(8))\"\(String(version.dropFirst(5)).dropLast(5))\"\(version.dropFirst(8))\"" |
| 110 | + } else if arguments.option("branch") != nil { |
| 111 | + return ".branch(\"\(version)\")" |
| 112 | + } else if arguments.option("revision") != nil { |
| 113 | + return ".revision(\"\(version)\")" |
| 114 | + } |
| 115 | + return ".exact(\"\(version)\")" |
| 116 | + } |
| 117 | +} |
0 commit comments