Skip to content

Commit 106f7a4

Browse files
authored
Merge pull request #13 from dmiedema/ModuloFileSettings
[WIP] Modulo file settings
2 parents 4e2176e + 7b90d2c commit 106f7a4

18 files changed

+250
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Build generated
44
build/
5+
.build/
56
DerivedData/
67

78
## Various settings

Makefile

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# By default export all variables
2+
export
3+
4+
.PHONY: install release debug build setup clean
5+
6+
PROJECT ?= 'modulo.xcodeproj'
7+
SCHEME ?= 'modulo'
8+
SYMROOT ?= 'build'
9+
CONFIGURATION ?= 'Debug'
10+
11+
# Build for debugging
12+
debug: build
13+
14+
# Install `modulo` to `/usr/local/bin`
15+
install: release
16+
cp $(SYMROOT)/Release/modulo /usr/local/bin/
17+
18+
# Build for release
19+
release: CONFIGURATION = 'Release'
20+
release: build
21+
22+
23+
# Build modulo
24+
# This will build the `PROJECT` with the given `SCHEME`
25+
# to the `SYMROOT` with a given `CONFIGURATION`
26+
# Defaults for these values are
27+
# `PROJECT` - `modulo.xcodeproj`
28+
# `SCHEME` - `modulo`
29+
# `SYMROOM` - `build`
30+
# `CONFIGURATION` - `Debug`
31+
#
32+
# These can be overwritten via ENV variables.
33+
build: setup
34+
xcodebuild -project $(PROJECT) -scheme $(SCHEME) -configuration $(CONFIGURATION) SYMROOT=$(SYMROOT)
35+
36+
# Setup the environment
37+
setup:
38+
mkdir -p $(SYMROOT)
39+
40+
clean:
41+
rm -rfv $(SYMROOT)

ModuloKit/Commands/AddCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ open class AddCommand: NSObject, Command {
3131
}
3232
open var failOnUnrecognizedOptions: Bool { return true }
3333

34-
open var verbose: Bool = false
34+
open var verbose: Bool = State.instance.options.verboseOutput
3535
open var quiet: Bool = false
3636

3737
open func configureOptions() {
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

ModuloKit/Commands/InitCommand.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ open class InitCommand: NSObject, Command {
2525
}
2626
open var failOnUnrecognizedOptions: Bool { return true }
2727

28-
open var verbose: Bool = false
28+
open var verbose: Bool = State.instance.options.verboseOutput
2929
open var quiet: Bool = false
3030

3131
open func configureOptions() {
@@ -53,7 +53,7 @@ open class InitCommand: NSObject, Command {
5353
}
5454

5555
let specPath = workingPath.appendPathComponent(specFilename)
56-
let spec = ModuleSpec(name: FileManager.directoryName(), module: isModule, sourcePath: nil, dependencies: [], path: specPath)
56+
let spec = ModuleSpec(name: FileManager.directoryName(), module: isModule, sourcePath: nil, dependencies: [], options: OptionsSpec(), path: specPath)
5757
let success = spec.save()
5858

5959
if !success {

ModuloKit/Commands/MapCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open class MapCommand: NSObject, Command {
2121
}
2222
open var failOnUnrecognizedOptions: Bool { return true }
2323

24-
open var verbose: Bool = false
24+
open var verbose: Bool = State.instance.options.verboseOutput
2525
open var quiet: Bool = false
2626

2727
fileprivate var simple = false

ModuloKit/Commands/RemoveCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open class RemoveCommand: NSObject, Command {
2121
}
2222
open var failOnUnrecognizedOptions: Bool { return true }
2323

24-
open var verbose: Bool = false
24+
open var verbose: Bool = State.instance.options.verboseOutput
2525
open var quiet: Bool = false
2626

2727
open func configureOptions() {

ModuloKit/Commands/SetCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open class SetCommand: NSObject, Command {
2121
}
2222
open var failOnUnrecognizedOptions: Bool { return true }
2323

24-
open var verbose: Bool = false
24+
open var verbose: Bool = State.instance.options.verboseOutput
2525
open var quiet: Bool = false
2626

2727
// subcommands

ModuloKit/Commands/StatusCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class StatusCommand: NSObject, Command {
2323
open var failOnUnrecognizedOptions: Bool { return true }
2424
open var ignoreMain: Bool = false
2525

26-
open var verbose: Bool = false
26+
open var verbose: Bool = State.instance.options.verboseOutput
2727
open var quiet: Bool = false
2828

2929
open func configureOptions() {

ModuloKit/Commands/UpdateCommand.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class UpdateCommand: NSObject, Command {
2929
}
3030
open var failOnUnrecognizedOptions: Bool { return true }
3131

32-
open var verbose: Bool = false
32+
open var verbose: Bool = State.instance.options.verboseOutput
3333
open var quiet: Bool = false
3434

3535
open func configureOptions() {

ModuloKit/Modulo.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ open class Modulo: NSObject {
2323
}
2424

2525
public static func run(_ args: [String]) -> ErrorCode {
26-
let cli = CLI(name: "modulo", version: "0.6.4", description: "A simple dependency manager")
26+
let cli = CLI(name: "modulo", version: "0.7.0", description: "A simple dependency manager")
27+
28+
// before we do anything make sure our options are applied to our
29+
// current state. If we don't have a working spec the defaults will do fine
30+
if let options = ModuleSpec.workingSpec()?.options {
31+
State.instance.options = options
32+
}
2733

2834
if args.count > 0 {
2935
cli.allArgumentsToExecutable = args
3036
}
3137

32-
cli.addCommands([InitCommand(), AddCommand(), UpdateCommand(), StatusCommand(), MapCommand(), SetCommand()])
38+
cli.addCommands([InitCommand(), AddCommand(), UpdateCommand(), StatusCommand(), MapCommand(), SetCommand(), DefaultsCommand()])
3339

3440
if let error = ErrorCode(rawValue: cli.run()) {
3541
if error == .success {

ModuloKit/SCM/Git.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class Git: SCM {
8282
return .error(code: 1, message: "Module path '\(path)' already exists.")
8383
}
8484

85-
let cloneCommand = "git clone \(url) \(path)"
85+
let cloneCommand = "git clone \(url) '\(path)'"
8686
let status = runCommand(cloneCommand)
8787

8888
if status != 0 {

ModuloKit/Specs/ModuleSpec.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct ModuleSpec {
2020
public let module: Bool
2121
public let sourcePath: String?
2222
public var dependencies: [DependencySpec]
23+
public var options: OptionsSpec
2324

2425
public var path: String
2526
}
@@ -31,6 +32,7 @@ extension ModuleSpec: ELDecodable {
3132
module: json ==> "module",
3233
sourcePath: json ==> "sourcePath",
3334
dependencies: json ==> "dependencies",
35+
options: json ==> "options" ?? OptionsSpec(),
3436
path: ""
3537
)
3638
}
@@ -46,7 +48,8 @@ extension ModuleSpec: ELEncodable {
4648
"name" <== name,
4749
"module" <== module,
4850
"sourcePath" <== sourcePath,
49-
"dependencies" <== dependencies
51+
"dependencies" <== dependencies,
52+
"options" <== options
5053
])
5154
}
5255
}
@@ -65,6 +68,8 @@ extension ModuleSpec {
6568
if var spec = result {
6669
spec.path = filePath
6770
result = spec
71+
// Set our state.instance options off of our own
72+
State.instance.options = spec.options
6873
}
6974

7075
return result

ModuloKit/Specs/OptionsSpec.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// OptionsSpec.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 ELCodable
13+
#endif
14+
15+
public struct OptionsSpec {
16+
/// Should we have `verbose` on all commands
17+
var verboseOutput: Bool = false
18+
/// Path to store our 'modules'/dependencies in
19+
var depdencyInstallationPath: String = "modules"
20+
}
21+
22+
extension OptionsSpec: ELDecodable {
23+
public static func decode(_ json: JSON?) throws -> OptionsSpec {
24+
return try OptionsSpec(
25+
verboseOutput: json ==> "verboseOutput",
26+
depdencyInstallationPath: json ==> "depdencyInstallationPath"
27+
)
28+
}
29+
}
30+
31+
extension OptionsSpec: ELEncodable {
32+
public func encode() throws -> JSON {
33+
return try encodeToJSON([
34+
"verboseOutput" <== verboseOutput,
35+
"depdencyInstallationPath" <== depdencyInstallationPath
36+
])
37+
}
38+
}

ModuloKit/State.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ import Foundation
1616
public struct State {
1717
public static var instance = State()
1818

19-
public var modulePathName = "modules"
19+
public var modulePathName: String {
20+
return options.depdencyInstallationPath
21+
}
2022

2123
var implictDependencies = [DependencySpec]()
2224
var explicitDependencies = [DependencySpec]()
2325
var removedDependencies = [DependencySpec]()
26+
var options = OptionsSpec()
2427

2528
public func dependenciesWereCloned() -> Bool {
2629
return (implictDependencies.count > 0 || explicitDependencies.count > 0)

ModuloKitTests/TestDefaults.swift

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// TestDefaults.swift
3+
// ModuloKitTests
4+
//
5+
// Created by Daniel Miedema on 9/25/18.
6+
// Copyright © 2018 TheHolyGrail. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import ELCLI
11+
import ELFoundation
12+
@testable import ModuloKit
13+
14+
class TestDefaults: XCTestCase {
15+
let modulo = Modulo()
16+
17+
}

0 commit comments

Comments
 (0)