|
| 1 | +// |
| 2 | +// DefaultsCommand.swift |
| 3 | +// ModuloKit |
| 4 | +// |
| 5 | +// Created by Daniel Miedema on 9/25/18. |
| 6 | +// Copyright © 2018 TheHolyGrail. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +#if NOFRAMEWORKS |
| 11 | +#else |
| 12 | + import ELCLI |
| 13 | +#endif |
| 14 | + |
| 15 | +open class DefaultsCommand: NSObject, Command { |
| 16 | + // Internal Properties |
| 17 | + fileprivate var toggleVerbose: Bool = false |
| 18 | + fileprivate var verboseValue: String? = nil |
| 19 | + fileprivate var moduleFolderPath: String? = nil |
| 20 | + fileprivate var setValue: Bool = false |
| 21 | + |
| 22 | + // Protocol Conformance |
| 23 | + public var name: String { return "defaults" } |
| 24 | + |
| 25 | + public var shortHelpDescription: String { |
| 26 | + return "Set default arguments/configuration properties for this repository" |
| 27 | + } |
| 28 | + |
| 29 | + public var longHelpDescription: String { |
| 30 | + return """ |
| 31 | + Set default argument values for all commands to be run. |
| 32 | + This will make changes to the `.modulo` file reflecting the |
| 33 | + new defaults that have been set |
| 34 | + """ |
| 35 | + } |
| 36 | + |
| 37 | + public var failOnUnrecognizedOptions: Bool { return true } |
| 38 | + |
| 39 | + public var verbose: Bool = State.instance.options.verboseOutput |
| 40 | + public var quiet: Bool = false |
| 41 | + |
| 42 | + public func execute(_ otherParams: Array<String>?) -> Int { |
| 43 | + guard var spec = ModuleSpec.workingSpec() else { |
| 44 | + exit(ErrorCode.notInitialized) |
| 45 | + return ErrorCode.notInitialized.rawValue |
| 46 | + } |
| 47 | + |
| 48 | + if setValue { |
| 49 | + if toggleVerbose { |
| 50 | + let newValue: Bool |
| 51 | + switch verboseValue { |
| 52 | + case "true": |
| 53 | + newValue = true |
| 54 | + case "false": |
| 55 | + newValue = false |
| 56 | + default: |
| 57 | + writeln(.stderr, "\(verboseValue ?? "") is not `true` or `false`. Interpretting as `false`.") |
| 58 | + newValue = false |
| 59 | + } |
| 60 | + |
| 61 | + spec.options.verboseOutput = newValue |
| 62 | + State.instance.options.verboseOutput = newValue |
| 63 | + } |
| 64 | + if let moduleFolderPath = moduleFolderPath, |
| 65 | + !moduleFolderPath.isEmpty { |
| 66 | + spec.options.depdencyInstallationPath = moduleFolderPath |
| 67 | + State.instance.options.depdencyInstallationPath = moduleFolderPath |
| 68 | + } |
| 69 | + spec.save() |
| 70 | + } else { |
| 71 | + if toggleVerbose { |
| 72 | + writeln(.stdout, "VerboseOutput - \(spec.options.verboseOutput)") |
| 73 | + } |
| 74 | + if moduleFolderPath != nil { |
| 75 | + writeln(.stdout, "depdencyInstallationPath - \(spec.options.depdencyInstallationPath)") |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + return ErrorCode.success.rawValue |
| 81 | + } |
| 82 | + |
| 83 | + open func configureOptions() { |
| 84 | + addOption(["--set"], usage: "set a new value for the given") { (option, value) in |
| 85 | + self.setValue = true |
| 86 | + } |
| 87 | + |
| 88 | + addOptionValue(["--verboseOutput"], |
| 89 | + usage: "specify `verbose` for all commands that are run", |
| 90 | + valueSignature: "<[true|false}>") { (option, value) in |
| 91 | + self.toggleVerbose = true |
| 92 | + self.verboseValue = value |
| 93 | + } |
| 94 | + |
| 95 | + addOptionValue(["--moduleFolder"], |
| 96 | + usage: "specify the desired dependency path", |
| 97 | + valueSignature: "<path>") { (option, value) in |
| 98 | + self.moduleFolderPath = value ?? "" |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments