/
InstallCommand.swift
74 lines (61 loc) · 2.71 KB
/
InstallCommand.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
import Foundation
import TuistCore
import Utility
/// Command that installs new versions of Tuist in the system.
final class InstallCommand: Command {
// MARK: - Command
/// Command name.
static var command: String = "install"
/// Command description.
static var overview: String = "Installs a version of tuist"
// MARK: - Attributes
/// Controller to manage system versions.
private let versionsController: VersionsControlling
/// Printer instance to output messages to the user.
private let printer: Printing
/// Installer instance to run the installation.
private let installer: Installing
/// Version argument to specify the version that will be installed.
let versionArgument: PositionalArgument<String>
/// Force argument (-f). When passed, it re-installs the version compiling it from the source.
let forceArgument: OptionArgument<Bool>
// MARK: - Init
convenience init(parser: ArgumentParser) {
self.init(parser: parser,
versionsController: VersionsController(),
installer: Installer(),
printer: Printer())
}
init(parser: ArgumentParser,
versionsController: VersionsControlling,
installer: Installing,
printer: Printing) {
let subParser = parser.add(subparser: InstallCommand.command,
overview: InstallCommand.overview)
self.versionsController = versionsController
self.printer = printer
self.installer = installer
versionArgument = subParser.add(positional: "version",
kind: String.self,
optional: false,
usage: "The version of tuist to be installed")
forceArgument = subParser.add(option: "--force",
shortName: "-f",
kind: Bool.self,
usage: "Re-installs the version compiling it from the source", completion: nil)
}
/// Runs the install command.
///
/// - Parameter result: Result obtained from parsing the CLI arguments.
/// - Throws: An error if the installation process fails.
func run(with result: ArgumentParser.Result) throws {
let force = result.get(forceArgument) ?? false
let version = result.get(versionArgument)!
let versions = versionsController.versions().map({ $0.description })
if versions.contains(version) {
printer.print(warning: "Version \(version) already installed, skipping")
return
}
try installer.install(version: version, force: force)
}
}