Skip to content

Commit 7a8b2c1

Browse files
Merge pull request #14 from Ether-CLI/develop
Set Package Version Command
2 parents 6fdcca1 + 05a23fa commit 7a8b2c1

11 files changed

+154
-39
lines changed

.swift-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.2
1+
4.1

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## [1.10.0] 2018-02-18
2+
3+
### Added
4+
- `ether version set` command to update the version of a single package.
5+
6+
### Updated
7+
- The RegEx for any command that matches the version of a package, so it handles any version type for a package.
8+
19
## [1.9.2] 2017-11-27
210

311
### Fixed

Sources/Ether/FixInstall.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public class FixInstall: Command {
4848
fixBar.start()
4949

5050
_ = try console.backgroundExecute(program: "rm", arguments: ["-rf", ".build"])
51-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "update"])
52-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "resolve"])
51+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"])
52+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"])
5353

5454
if arguments.option("no-build") == nil {
55-
_ = try console.backgroundExecute(program: "swift", arguments: ["build", "--enable-prefetching"])
55+
_ = try console.backgroundExecute(program: "swift", arguments: ["build"])
5656
}
5757

5858
fixBar.finish()

Sources/Ether/Install.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public final class Install: Command {
111111
try String(mutablePackageManifest).data(using: .utf8)?.write(to: URL(string: "file:\(fileManager.currentDirectoryPath)/Package.swift")!)
112112

113113
// Update the packages.
114-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "update"])
115-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "resolve"])
114+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"])
115+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"])
116116

117117
// Get the new package name and add it to the previously accepted targets.
118118
let dependencyName = try Manifest.current.getPackageName(for: newPackageData.url)
@@ -135,7 +135,7 @@ public final class Install: Command {
135135
if let _ = arguments.options["xcode"] {
136136
let xcodeBar = console.loadingBar(title: "Generating Xcode Project")
137137
xcodeBar.start()
138-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "generate-xcodeproj"])
138+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "generate-xcodeproj"])
139139
xcodeBar.finish()
140140
try console.execute(program: "/bin/sh", arguments: ["-c", "open *.xcodeproj"], input: nil, output: nil, error: nil)
141141
}

Sources/Ether/Remove.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class Remove: Command {
5454
let name = try value("name", from: arguments)
5555
let url = try Manifest.current.getPackageUrl(for: name)
5656

57-
let regex = try NSRegularExpression(pattern: "\\,?\\n *\\.package\\(url: *\"\(url)\", *\\.?\\w+(:|\\() *\"([\\d\\.]+)\"\\)?\\),?", options: .caseInsensitive)
57+
let regex = try NSRegularExpression(pattern: "(\\,?\\n *\\.package\\(url: *\"\(url)\", *)(.*?)(?=,?\n)", options: .caseInsensitive)
5858
let oldPins = try Manifest.current.getPins()
5959

6060
let packageString = try Manifest.current.get()
@@ -69,8 +69,8 @@ public final class Remove: Command {
6969

7070
do {
7171
try String(mutableString).data(using: .utf8)?.write(to: URL(string: "file:\(manager.currentDirectoryPath)/Package.swift")!)
72-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "update"])
73-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "resolve"])
72+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"])
73+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"])
7474
} catch let error {
7575
removingProgressBar.fail()
7676
throw error
@@ -84,7 +84,7 @@ public final class Remove: Command {
8484
if let _ = arguments.options["xcode"] {
8585
let xcodeBar = console.loadingBar(title: "Generating Xcode Project")
8686
xcodeBar.start()
87-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "generate-xcodeproj"])
87+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "generate-xcodeproj"])
8888
xcodeBar.finish()
8989
try console.execute(program: "/bin/sh", arguments: ["-c", "open *.xcodeproj"], input: nil, output: nil, error: nil)
9090
}

Sources/Ether/Update.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public final class Update: Command {
5757
let updateBar = console.loadingBar(title: "Updating Packages")
5858
updateBar.start()
5959
_ = try console.backgroundExecute(program: "rm", arguments: ["-rf", ".build"])
60-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "update"])
61-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "resolve"])
62-
_ = try console.backgroundExecute(program: "swift", arguments: ["build", "--enable-prefetching"])
60+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"])
61+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"])
62+
_ = try console.backgroundExecute(program: "swift", arguments: ["build"])
6363
updateBar.finish()
6464

6565
if let _ = arguments.options["xcode"] {
6666
let xcodeBar = console.loadingBar(title: "Generating Xcode Project")
6767
xcodeBar.start()
68-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "generate-xcodeproj"])
68+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "generate-xcodeproj"])
6969
xcodeBar.finish()
7070
try console.execute(program: "/bin/sh", arguments: ["-c", "open *.xcodeproj"], input: nil, output: nil, error: nil)
7171
}

Sources/Ether/VersionAll.swift

+6-17
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,12 @@ public final class VersionAll: Command {
4343
let fetchingDataBar = console.loadingBar(title: "Getting Package Data")
4444
fetchingDataBar.start()
4545

46-
let manager = FileManager.default
47-
48-
guard let packageData = manager.contents(atPath: "\(manager.currentDirectoryPath)/Package.resolved") else {
49-
throw fail(bar: fetchingDataBar, with: "Make sure you are in the root of an SPM project.")
50-
}
51-
52-
guard let packageJson = try packageData.json()?["object"] as? [String: AnyObject] else {
53-
throw fail(bar: fetchingDataBar, with: "Unable to parse data from Package.resolved.")
54-
}
55-
56-
if let pins = packageJson["pins"] as? [[String: AnyObject]] {
57-
fetchingDataBar.finish()
58-
pins.forEach { package in
59-
console.output("\(package["package"] ?? "N/A" as AnyObject): ", style: .success, newLine: false)
60-
if let state = package["state"] as? [String: AnyObject] {
61-
console.output("v\(state["version"] ?? "N/A" as AnyObject)", style: .plain, newLine: true)
62-
}
46+
let pins = try Manifest.current.getPins()
47+
fetchingDataBar.finish()
48+
pins.forEach { package in
49+
console.output("\(package["package"] ?? "N/A" as AnyObject): ", style: .success, newLine: false)
50+
if let state = package["state"] as? [String: AnyObject] {
51+
console.output("v\(state["version"] ?? "N/A" as AnyObject)", style: .plain, newLine: true)
6352
}
6453
}
6554
}

Sources/Ether/VersionLatest.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class VersionLatest: Command {
5555
let fileManager = FileManager.default
5656
let manifest = try Manifest.current.get()
5757
let nsManifest = NSMutableString(string: manifest)
58-
let versionPattern = try NSRegularExpression(pattern: "(.package\\(url:\\s*\".*?\\.com\\/(.*?)\\.git\",\\s*)(\\.?\\w+(\\(|:)\\s*\"[\\w\\.]+\"\\)?)(\\))", options: [])
58+
let versionPattern = try NSRegularExpression(pattern: "(.package\\(url:\\s*\".*?\\.com\\/(.*?)\\.git\",\\s*)(.*?)(\\),?\\n)", options: [])
5959
let matches = versionPattern.matches(in: manifest, options: [], range: NSMakeRange(0, manifest.utf8.count))
6060
let packageNames = matches.map { match -> String in
6161
let name = versionPattern.replacementString(for: match, in: manifest, offset: 0, template: "$2")
@@ -72,15 +72,15 @@ public final class VersionLatest: Command {
7272
}
7373

7474
try String(nsManifest).data(using: .utf8)?.write(to: URL(string: "file:\(fileManager.currentDirectoryPath)/Package.swift")!)
75-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "update"])
76-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "resolve"])
75+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "update"])
76+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "resolve"])
7777

7878
updateBar.finish()
7979

8080
if let _ = arguments.options["xcode"] {
8181
let xcodeBar = console.loadingBar(title: "Generating Xcode Project")
8282
xcodeBar.start()
83-
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "--enable-prefetching", "generate-xcodeproj"])
83+
_ = try console.backgroundExecute(program: "swift", arguments: ["package", "generate-xcodeproj"])
8484
xcodeBar.finish()
8585
try console.execute(program: "/bin/sh", arguments: ["-c", "open *.xcodeproj"], input: nil, output: nil, error: nil)
8686
}

Sources/Ether/VersionSet.swift

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

Sources/Executable/main.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Helpers
2727
import libc
2828

2929
// The current version of Ether. This string should be updated with each release.
30-
let version = "1.9.2"
30+
let version = "1.10.0"
3131
var arguments = CommandLine.arguments
3232
let terminal = Terminal(arguments: arguments)
3333
var iterator = arguments.makeIterator()
@@ -57,7 +57,8 @@ do {
5757
FixInstall(console: terminal),
5858
Group(id: "version", commands: [
5959
VersionLatest(console: terminal),
60-
VersionAll(console: terminal)
60+
VersionAll(console: terminal),
61+
VersonSet(console: terminal)
6162
], help: ["For interacting with dependency versions"]),
6263
CleanManifest(console: terminal)
6364
]

Sources/Helpers/Manifest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class Manifest {
9999
return name
100100
}
101101

102-
/// Gets the name of the package that has a specefied URL by reading the `Package.resolved` file data.
102+
/// Gets the URL of the package that has a specefied name by reading the `Package.resolved` file data.
103103
///
104104
/// - Parameter name: The ame of the package that the URL is to get fetched from.
105105
/// - Returns: The URL of the package that was found.

0 commit comments

Comments
 (0)