Skip to content

Commit

Permalink
Merge pull request #13 from dmiedema/ModuloFileSettings
Browse files Browse the repository at this point in the history
[WIP] Modulo file settings
  • Loading branch information
woolie authored Sep 26, 2018
2 parents 4e2176e + 7b90d2c commit 106f7a4
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Build generated
build/
.build/
DerivedData/

## Various settings
Expand Down
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# By default export all variables
export

.PHONY: install release debug build setup clean

PROJECT ?= 'modulo.xcodeproj'
SCHEME ?= 'modulo'
SYMROOT ?= 'build'
CONFIGURATION ?= 'Debug'

# Build for debugging
debug: build

# Install `modulo` to `/usr/local/bin`
install: release
cp $(SYMROOT)/Release/modulo /usr/local/bin/

# Build for release
release: CONFIGURATION = 'Release'
release: build


# Build modulo
# This will build the `PROJECT` with the given `SCHEME`
# to the `SYMROOT` with a given `CONFIGURATION`
# Defaults for these values are
# `PROJECT` - `modulo.xcodeproj`
# `SCHEME` - `modulo`
# `SYMROOM` - `build`
# `CONFIGURATION` - `Debug`
#
# These can be overwritten via ENV variables.
build: setup
xcodebuild -project $(PROJECT) -scheme $(SCHEME) -configuration $(CONFIGURATION) SYMROOT=$(SYMROOT)

# Setup the environment
setup:
mkdir -p $(SYMROOT)

clean:
rm -rfv $(SYMROOT)
2 changes: 1 addition & 1 deletion ModuloKit/Commands/AddCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class AddCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

open func configureOptions() {
Expand Down
101 changes: 101 additions & 0 deletions ModuloKit/Commands/DefaultsCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// DefaultsCommand.swift
// ModuloKit
//
// Created by Daniel Miedema on 9/25/18.
// Copyright © 2018 TheHolyGrail. All rights reserved.
//

import Foundation
#if NOFRAMEWORKS
#else
import ELCLI
#endif

open class DefaultsCommand: NSObject, Command {
// Internal Properties
fileprivate var toggleVerbose: Bool = false
fileprivate var verboseValue: String? = nil
fileprivate var moduleFolderPath: String? = nil
fileprivate var setValue: Bool = false

// Protocol Conformance
public var name: String { return "defaults" }

public var shortHelpDescription: String {
return "Set default arguments/configuration properties for this repository"
}

public var longHelpDescription: String {
return """
Set default argument values for all commands to be run.
This will make changes to the `.modulo` file reflecting the
new defaults that have been set
"""
}

public var failOnUnrecognizedOptions: Bool { return true }

public var verbose: Bool = State.instance.options.verboseOutput
public var quiet: Bool = false

public func execute(_ otherParams: Array<String>?) -> Int {
guard var spec = ModuleSpec.workingSpec() else {
exit(ErrorCode.notInitialized)
return ErrorCode.notInitialized.rawValue
}

if setValue {
if toggleVerbose {
let newValue: Bool
switch verboseValue {
case "true":
newValue = true
case "false":
newValue = false
default:
writeln(.stderr, "\(verboseValue ?? "") is not `true` or `false`. Interpretting as `false`.")
newValue = false
}

spec.options.verboseOutput = newValue
State.instance.options.verboseOutput = newValue
}
if let moduleFolderPath = moduleFolderPath,
!moduleFolderPath.isEmpty {
spec.options.depdencyInstallationPath = moduleFolderPath
State.instance.options.depdencyInstallationPath = moduleFolderPath
}
spec.save()
} else {
if toggleVerbose {
writeln(.stdout, "VerboseOutput - \(spec.options.verboseOutput)")
}
if moduleFolderPath != nil {
writeln(.stdout, "depdencyInstallationPath - \(spec.options.depdencyInstallationPath)")
}

}

return ErrorCode.success.rawValue
}

open func configureOptions() {
addOption(["--set"], usage: "set a new value for the given") { (option, value) in
self.setValue = true
}

addOptionValue(["--verboseOutput"],
usage: "specify `verbose` for all commands that are run",
valueSignature: "<[true|false}>") { (option, value) in
self.toggleVerbose = true
self.verboseValue = value
}

addOptionValue(["--moduleFolder"],
usage: "specify the desired dependency path",
valueSignature: "<path>") { (option, value) in
self.moduleFolderPath = value ?? ""
}
}
}
4 changes: 2 additions & 2 deletions ModuloKit/Commands/InitCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ open class InitCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

open func configureOptions() {
Expand Down Expand Up @@ -53,7 +53,7 @@ open class InitCommand: NSObject, Command {
}

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

if !success {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/MapCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class MapCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

fileprivate var simple = false
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/RemoveCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class RemoveCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

open func configureOptions() {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/SetCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class SetCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

// subcommands
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/StatusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class StatusCommand: NSObject, Command {
open var failOnUnrecognizedOptions: Bool { return true }
open var ignoreMain: Bool = false

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

open func configureOptions() {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/Commands/UpdateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class UpdateCommand: NSObject, Command {
}
open var failOnUnrecognizedOptions: Bool { return true }

open var verbose: Bool = false
open var verbose: Bool = State.instance.options.verboseOutput
open var quiet: Bool = false

open func configureOptions() {
Expand Down
10 changes: 8 additions & 2 deletions ModuloKit/Modulo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ open class Modulo: NSObject {
}

public static func run(_ args: [String]) -> ErrorCode {
let cli = CLI(name: "modulo", version: "0.6.4", description: "A simple dependency manager")
let cli = CLI(name: "modulo", version: "0.7.0", description: "A simple dependency manager")

// before we do anything make sure our options are applied to our
// current state. If we don't have a working spec the defaults will do fine
if let options = ModuleSpec.workingSpec()?.options {
State.instance.options = options
}

if args.count > 0 {
cli.allArgumentsToExecutable = args
}

cli.addCommands([InitCommand(), AddCommand(), UpdateCommand(), StatusCommand(), MapCommand(), SetCommand()])
cli.addCommands([InitCommand(), AddCommand(), UpdateCommand(), StatusCommand(), MapCommand(), SetCommand(), DefaultsCommand()])

if let error = ErrorCode(rawValue: cli.run()) {
if error == .success {
Expand Down
2 changes: 1 addition & 1 deletion ModuloKit/SCM/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class Git: SCM {
return .error(code: 1, message: "Module path '\(path)' already exists.")
}

let cloneCommand = "git clone \(url) \(path)"
let cloneCommand = "git clone \(url) '\(path)'"
let status = runCommand(cloneCommand)

if status != 0 {
Expand Down
7 changes: 6 additions & 1 deletion ModuloKit/Specs/ModuleSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct ModuleSpec {
public let module: Bool
public let sourcePath: String?
public var dependencies: [DependencySpec]
public var options: OptionsSpec

public var path: String
}
Expand All @@ -31,6 +32,7 @@ extension ModuleSpec: ELDecodable {
module: json ==> "module",
sourcePath: json ==> "sourcePath",
dependencies: json ==> "dependencies",
options: json ==> "options" ?? OptionsSpec(),
path: ""
)
}
Expand All @@ -46,7 +48,8 @@ extension ModuleSpec: ELEncodable {
"name" <== name,
"module" <== module,
"sourcePath" <== sourcePath,
"dependencies" <== dependencies
"dependencies" <== dependencies,
"options" <== options
])
}
}
Expand All @@ -65,6 +68,8 @@ extension ModuleSpec {
if var spec = result {
spec.path = filePath
result = spec
// Set our state.instance options off of our own
State.instance.options = spec.options
}

return result
Expand Down
38 changes: 38 additions & 0 deletions ModuloKit/Specs/OptionsSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// OptionsSpec.swift
// ModuloKit
//
// Created by Daniel Miedema on 9/25/18.
// Copyright © 2018 TheHolyGrail. All rights reserved.
//

import Foundation
#if NOFRAMEWORKS
#else
import ELCodable
#endif

public struct OptionsSpec {
/// Should we have `verbose` on all commands
var verboseOutput: Bool = false
/// Path to store our 'modules'/dependencies in
var depdencyInstallationPath: String = "modules"
}

extension OptionsSpec: ELDecodable {
public static func decode(_ json: JSON?) throws -> OptionsSpec {
return try OptionsSpec(
verboseOutput: json ==> "verboseOutput",
depdencyInstallationPath: json ==> "depdencyInstallationPath"
)
}
}

extension OptionsSpec: ELEncodable {
public func encode() throws -> JSON {
return try encodeToJSON([
"verboseOutput" <== verboseOutput,
"depdencyInstallationPath" <== depdencyInstallationPath
])
}
}
5 changes: 4 additions & 1 deletion ModuloKit/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import Foundation
public struct State {
public static var instance = State()

public var modulePathName = "modules"
public var modulePathName: String {
return options.depdencyInstallationPath
}

var implictDependencies = [DependencySpec]()
var explicitDependencies = [DependencySpec]()
var removedDependencies = [DependencySpec]()
var options = OptionsSpec()

public func dependenciesWereCloned() -> Bool {
return (implictDependencies.count > 0 || explicitDependencies.count > 0)
Expand Down
17 changes: 17 additions & 0 deletions ModuloKitTests/TestDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TestDefaults.swift
// ModuloKitTests
//
// Created by Daniel Miedema on 9/25/18.
// Copyright © 2018 TheHolyGrail. All rights reserved.
//

import XCTest
import ELCLI
import ELFoundation
@testable import ModuloKit

class TestDefaults: XCTestCase {
let modulo = Modulo()

}
Loading

0 comments on commit 106f7a4

Please sign in to comment.