Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonnam committed Jun 11, 2018
2 parents bcf97a9 + 6ddb322 commit 2488d01
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Sources/Flint/Convenience/processString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Foundation
func process(_ string: String, variables: [Variable], inputs: [String: String]) -> String {
var string = string
for variable in variables {
guard let value = inputs[variable.name] ?? variable.defaultValue else { continue }
let value = inputs[variable.name] ?? ""
string = string.replacingOccurrences(of: "___\(variable.name)___", with: value)
string = string.replacingOccurrences(of: "__\(variable.name)__", with: value)
string = string.replacingOccurrences(of: "--\(variable.name)--", with: value)
Expand Down
6 changes: 1 addition & 5 deletions Sources/Flint/input/inputCommandHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ let inputCommandHandler: CommandHandler = { _, _, operandValues, optionValues in
} else {
var output = "{\n"
for variable in template.manifest.variables ?? [] {
if let defaultValue = variable.defaultValue {
output.append(" \"\(variable.name)\": \"\(defaultValue)\",\n")
} else {
output.append(" \"\(variable.name)\":,\n")
}
output.append(" \"\(variable.name)\": \"\(variable.defaultValue ?? "")\",\n")
}
output.append("}\n")
do {
Expand Down
141 changes: 64 additions & 77 deletions Sources/Flint/spark/sparkCommandHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,9 @@ let sparkCommandHandler: CommandHandler = { _, _, operandValues, optionValues in
}
}

// Check if output path is valid.
if outputPath.exists {
if force {
do {
try outputPath.remove()
} catch {
printError(error.localizedDescription)
return
}
} else {
printWarning("\(outputPath.path) is not empty")
return
}
}

// Get inputs.
var inputs: [String: String] = [:]

if let inputFilePathOptionValue = inputFilePathOptionValue {
let inputPath = Path(fileURLWithPath: inputFilePathOptionValue)
do {
Expand All @@ -124,35 +110,19 @@ let sparkCommandHandler: CommandHandler = { _, _, operandValues, optionValues in
printError(error.localizedDescription)
return
}
} else {
for variable in template.manifest.variables ?? [] {
var output = variable.name.boldOutput
if let defaultValue = variable.defaultValue {
output += " (\(defaultValue))"
}
print("\(output): ", terminator: "")
if let input = readLine(), input.count > 0 {
inputs[variable.name] = input
} else {
inputs[variable.name] = variable.defaultValue
}
}
}

// Process template.

// Copy template directory.
if verbose {
printVerbose("Copy \(template.templateFilesPath.path) into \(outputPath.path)")
}
do {
if !outputPath.parent.exists {
try outputPath.parent.createDirectory()
for variable in (template.manifest.variables ?? []).filter({ !inputs.keys.contains($0.name) }) {
var output = variable.name.boldOutput
if let defaultValue = variable.defaultValue {
output += " (\(defaultValue))"
}
print("\(output): ", terminator: "")
if let input = readLine(), input.count > 0 {
inputs[variable.name] = input
} else {
inputs[variable.name] = variable.defaultValue
}
try template.templateFilesPath.copy(to: outputPath)
} catch {
printError(error.localizedDescription)
return
}

// Prehooks.
Expand Down Expand Up @@ -182,50 +152,67 @@ let sparkCommandHandler: CommandHandler = { _, _, operandValues, optionValues in
}

// Process variables.
if verbose {
printVerbose("Process variables \(outputPath.path)")
}

var directoryPaths: [Path] = []

do {
for content in try outputPath.enumerated() {
let templateFilesPath = template.templateFilesPath
enumerationLoop: for content in try templateFilesPath.enumerated() {
if content.isDirectory {
directoryPaths.append(content)
} else {
if verbose {
printVerbose("Process \(content.path)")
}
let processedName = process(content.rawValue.lastPathComponent,
variables: template.manifest.variables ?? [],
inputs: inputs)
continue
}

let relativeRawPath = String(content.path.dropFirst(templateFilesPath.path.count + 1))

var encoding = String.Encoding.utf8
if let dataString = try? String(contentsOfFile: content.path, usedEncoding: &encoding) {
let processedString = process(dataString,
variables: template.manifest.variables ?? [],
inputs: inputs)
try content.remove()
try processedString.write(to: content.parent[processedName].rawValue,
atomically: true,
encoding: encoding)
if verbose {
printVerbose("Process \(relativeRawPath)")
}

let relativePath = process(relativeRawPath,
variables: template.manifest.variables ?? [],
inputs: inputs)

let contentOutputPath = outputPath[relativePath]

// Check existing file
if contentOutputPath.exists {
if force {
try contentOutputPath.remove()
} else {
if verbose {
printVerbose("Process \(content.path)")
}
try content.move(to: content.parent[processedName])
print("File already exists at \(contentOutputPath.path)")
inputLoop: repeat {
print("override(o), skip(s), abort(a): ", terminator: "")
if let option = readLine() {
switch option {
case "override", "o":
try contentOutputPath.remove()
break inputLoop
case "skip", "s":
continue enumerationLoop
case "abort", "a":
return
default:
continue inputLoop
}
} else {
continue inputLoop
}
} while true
}
}
}

for directoryPath in directoryPaths.reversed() {
if verbose {
printVerbose("Process \(directoryPath.path)")
if !contentOutputPath.parent.exists {
try contentOutputPath.parent.createDirectory()
}

var encoding = String.Encoding.utf8
if let dataString = try? String(contentsOfFile: content.path, usedEncoding: &encoding) {
let processedString = process(dataString,
variables: template.manifest.variables ?? [],
inputs: inputs)
try processedString.write(toFile: contentOutputPath.path,
atomically: true,
encoding: encoding)
} else {
try content.copy(to: contentOutputPath)
}
let processedName = process(directoryPath.rawValue.lastPathComponent,
variables: template.manifest.variables ?? [],
inputs: inputs)
try directoryPath.move(to: directoryPath.parent[processedName])
}
} catch {
printError(error.localizedDescription)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Flint/version/version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@

import Foundation

let version = "0.1.2"
let version = "0.1.3"
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.2
0.1.3

0 comments on commit 2488d01

Please sign in to comment.