This repository has been archived by the owner on Jan 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Command.swift
135 lines (119 loc) · 3.43 KB
/
Command.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Marathon
* Copyright (c) John Sundell 2017
* Licensed under the MIT license. See LICENSE file.
*/
import Foundation
import Files
// MARK: - Error
public enum CommandError: PrintableError {
case invalid(String)
}
public extension CommandError {
var message: String {
switch self {
case .invalid(let command):
return "'\(command)' is not a valid command"
}
}
var hints: [String] {
return ["Type 'marathon help' for available commands"]
}
}
// MARK: - Command
internal enum Command: String {
case create
case edit
case remove
case run
case install
case add
case list
case update
case help
}
extension Command {
private typealias Error = CommandError
static var all: [Command] {
return [.create, .edit, .remove, .run, .install, .add, .list, .update, .help]
}
var description: String {
switch self {
case .create:
return "Create new script at a given path and open it"
case .edit:
return "Edit a script at a given path"
case .remove:
return "Remove a package or the cache data for a script at a given path"
case .run:
return "Run a script at a given path"
case .install:
return "Install a script at a given path or URL as a binary"
case .add:
return "Add a package from a given URL to be able to use it from your scripts"
case .list:
return "List all packages and cached script data"
case .update:
return "Update all added packages to their latest versions"
case .help:
return "Print these instructions"
}
}
var usageText: String {
switch self {
case .create:
return "<script-path> [<script-content>] [--no-xcode] [--no-open]"
case .edit:
return "<script-path> [--no-xcode] [--no-open]"
case .remove:
return "<name-of-package-or-path-to-script> [--all-script-data] [--all-packages]"
case .run:
return "<script-path> [<script-arguments...>]"
case .install:
return "<script-path-or-url> [<install-path>] [--force]"
case .add:
return "<url-or-path-to-package>"
case .list:
return ""
case .update:
return ""
case .help:
return ""
}
}
var makeTaskClosure: (Folder, [String], ScriptManager, PackageManager, Printer) -> Executable {
switch self {
case .create:
return CreateTask.init
case .add:
return AddTask.init
case .edit:
return EditTask.init
case .remove:
return RemoveTask.init
case .run:
return RunTask.init
case .install:
return InstallTask.init
case .list:
return ListTask.init
case .update:
return UpdateTask.init
case .help:
return HelpTask.init
}
}
var allowsProgressOutput: Bool {
return self != .run
}
init(arguments: [String], index: Int = 1) throws {
guard let commandString = arguments.element(at: index) else {
self = .help
return
}
guard let command = Command(rawValue: commandString) else {
throw Error.invalid(commandString)
}
self = command
}
}