Skip to content

Commit 25caf20

Browse files
committed
update CLI
1 parent 5879b74 commit 25caf20

File tree

7 files changed

+281
-125
lines changed

7 files changed

+281
-125
lines changed

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let package = Package(
1616
.package(url: "https://github.com/kylef/PathKit.git", from: "0.8.0"),
1717
.package(url: "https://github.com/JohnSundell/ShellOut.git", from: "1.2.0"),
1818
.package(url: "https://github.com/onevcat/Rainbow.git", from: "2.1.0"),
19+
.package(url: "https://github.com/nsomar/Guaka.git", from: "0.1.3"),
1920
],
2021
targets: [
2122
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -25,6 +26,7 @@ let package = Package(
2526
dependencies: [
2627
"MintKit",
2728
"Rainbow",
29+
"Guaka",
2830
]),
2931
.target(
3032
name: "MintKit",

README.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,47 @@ And then import wherever needed: `import MintKit`
5757

5858
## Usage
5959

60-
Mint has 2 commands, `run` and `install`.
60+
Run `mint` to see usage instructions.
6161

62-
#### Install
63-
This will checkout, build and copy a specific build by git tag
62+
```
63+
$ mint
64+
Usage:
65+
mint [command]
66+
67+
Available Commands:
68+
install Install a package
69+
run Run a package
70+
update Update a package
71+
72+
Use "mint [command] --help" for more information about a command.
73+
```
6474

65-
#### Run
66-
Run will run a certain tool from the package. If it's not installed, it will install it first
75+
- **Install**: Installs a package. If it is already installed this won't do anything
76+
- **Run**: Runs a package. This will install if first if it doesn't exist
77+
- **Update**: Installs a package while enforcing an update and rebuild. Shouldn't be required unless you are pointing at a branch and want to update.
6778

68-
#### Options
79+
These 3 commands use the same flags. They must all be followed by a repo name.
80+
This can be a shorthand for a github repo `install realm/SwiftLint` or a fully qualified git path `install https://github.com/realm/SwiftLint.git`.
6981

70-
Each command is followed by git path, version and tool name. Anything after that in `run` will be passed to the command line tool.
82+
##### Repo
83+
In the case of `run` you can also just pass the name of the repo if it is already installed `run swiftlint`. This will do a lookup of all installed packages.
7184

72-
Git path can take the form:
85+
#### Version
86+
By default the version that is used is the latest git tag (or master if no tags exist). You can pass a specific version with the version flag (-v, --version) eg. `--version 1.2.0`
7387

74-
- `yonaskolb/xcodegen` (github repo name)
75-
- `github.com/yonaskolb/xcodegen` (github repo)
76-
- `http://github.com/yonaskolb/xcodegen.git` (any full git path)
88+
#### Name
89+
By default the tool name will be the last path in the repo (so for `realm/swiftlint` it will be `swiftlint`). If there are any packages that have a different executable name when build you can specify it with `--name`
7790

78-
The first 2 examples would be transformed into the last
91+
#### Arguments
92+
To pass in arguments to the final tool on `run` you need to use `--args="any arguments"`
7993

94+
#### Examples
8095
```
81-
$ mint run yonaskolb/xcodegen 1.2.0 xcodegen
96+
$ mint install yonaskolb/xcodegen 1.2.0 --args="--spec my_spec.yml"
97+
$ mint install yonaskolb/xcodegen
98+
$ mint run xcodegen
8299
```
83100

84-
85101
## Package.resources
86102
As the Swift Packager doesn't yet have a way of specifying resources directories, Mint looks for a custom `Package.resources` file in the repo. This is a plain text file that lists the resources directories on different lines:
87103

Sources/Mint/main.swift

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,81 @@ import MintKit
22
import Rainbow
33
import Foundation
44
import ShellOut
5+
import Guaka
56

6-
do {
7-
try Mint.execute()
8-
} catch {
9-
if let error = error as? ShellOutError {
10-
let message = "Error: \(error.message)".red
11-
print("🌱 \(message)\n\(error.output)")
12-
} else {
13-
print("🌱 Error: \(error)".red)
7+
func catchError(closure: () throws -> ()) {
8+
do {
9+
try closure()
10+
} catch {
11+
if let error = error as? ShellOutError {
12+
if !error.message.isEmpty {
13+
print(error.message.red)
14+
}
15+
if !error.output.isEmpty {
16+
print(error.output)
17+
}
18+
} else {
19+
print("🌱 Error: \(error)".red)
20+
}
21+
exit(1)
1422
}
15-
exit(1)
1623
}
24+
25+
let command = Command(usage: "mint")
26+
command.run = { _, _ in
27+
print(command.helpMessage)
28+
}
29+
30+
let repoHelp = "You must pass a repo either in the shorthand for of a github repo \"githubName/repo\", or a fully qualified .git path.\nAn optional version can be passed, otherwise the newest tag or master will be used"
31+
let versionFlag = Flag(shortName: "v", longName: "version", type: String.self, description: "The version to use. Usually this is a tag, but can also be a branch. If left out will use the latest tag or master")
32+
33+
let nameFlag = Flag(shortName: "n", longName: "name", type: String.self, description: "The command to run")
34+
35+
let argsFlag = Flag(shortName: "a", longName: "args", type: String.self, description: "the arguments to pass to the command being run")
36+
37+
let runCommand = Command(usage: "run repo (version)", shortMessage: "Run a package", longMessage: "This will run a package tool. If it isn't installed if will do so first.\n\(repoHelp)", flags: [nameFlag, argsFlag], example: "mint run realm/swiftlint 0.22.0") { flags, args in
38+
guard let repo = args.first else {
39+
print("repo required".red)
40+
return
41+
}
42+
let version = args.count >= 2 ? args[1] : ""
43+
let name = flags.getString(name: "name")
44+
let arguments = flags.getString(name: "args")
45+
catchError {
46+
try Mint.run(repo: repo, version: version, name: name, arguments: arguments)
47+
}
48+
}
49+
50+
let installCommand = Command(usage: "install repo (version)", shortMessage: "Install a package", longMessage: "This will install a package. If it's already installed no action will be taken.\n\(repoHelp)", flags: [nameFlag], example: "mint install realm/swiftlint 0.22.0") { flags, args in
51+
guard let repo = args.first else {
52+
print("repo required".red)
53+
return
54+
}
55+
let version = args.count >= 2 ? args[1] : ""
56+
let name = flags.getString(name: "name")
57+
catchError {
58+
try Mint.install(repo: repo, version: version, name: name, force: false)
59+
}
60+
}
61+
62+
let updateCommand = Command(usage: "update repo (version)", shortMessage: "Update a package", longMessage: "This will update a package even if it's already installed.\n\(repoHelp)", flags: [nameFlag], example: "mint install realm/swiftlint 0.22.0") { flags, args in
63+
guard let repo = args.first else {
64+
print("repo required".red)
65+
return
66+
}
67+
let version = args.count >= 2 ? args[1] : ""
68+
let name = flags.getString(name: "name")
69+
catchError {
70+
try Mint.install(repo: repo, version: version, name: name, force: true)
71+
}
72+
}
73+
74+
let bootstrapCommand = Command(usage: "bootstrap") { flags, args in
75+
76+
}
77+
command.add(subCommand: runCommand)
78+
command.add(subCommand: installCommand)
79+
//command.add(subCommand: bootstrapCommand)
80+
81+
command.execute()
82+

0 commit comments

Comments
 (0)