-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from jakeheis/swiftcli
Use SwiftCLI
- Loading branch information
Showing
12 changed files
with
176 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
import BeakCore | ||
import Foundation | ||
import PathKit | ||
import Utility | ||
|
||
do { | ||
let options = BeakOptions() | ||
let beak = Beak(options: options) | ||
try beak.execute(arguments: Array(ProcessInfo.processInfo.arguments.dropFirst())) | ||
} catch { | ||
if error._domain == NSCocoaErrorDomain { | ||
print("⚠️ \(error.localizedDescription)") | ||
} else { | ||
print("⚠️ \(error)") | ||
} | ||
exit(1) | ||
} | ||
let options = BeakOptions() | ||
let beak = Beak(options: options) | ||
let result = beak.execute() | ||
exit(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
import SwiftCLI | ||
|
||
public enum BeakError: Error, CustomStringConvertible { | ||
public enum BeakError: ProcessError, CustomStringConvertible { | ||
case fileNotFound(String) | ||
case compileError(String) | ||
case invalidFunction(String) | ||
case missingRequiredParam(Function.Param) | ||
case parsingError(SwiftStructure) | ||
|
||
case conversionError(Function.Param, String) | ||
|
||
public var description: String { | ||
switch self { | ||
case let .fileNotFound(file): return "File not found: \(file)" | ||
case let .compileError(error): return "File could not be compiled: \(error)" | ||
case let .invalidFunction(function): return "Function \(function) was not found" | ||
case let .missingRequiredParam(param): return "Missing required param \(param.name)" | ||
case let .parsingError(structure): return "Could not parse Beak file structure:\n\(toJSON(structure))" | ||
case let .conversionError(param, value): return "'\(value)' is not convertible to \(param.type.string) for argument \(param.name)" | ||
} | ||
} | ||
|
||
public var message: String? { | ||
return "⚠️ \(description)" | ||
} | ||
|
||
public var exitStatus: Int32 { | ||
return 1 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import Foundation | ||
import PathKit | ||
import Utility | ||
import SwiftCLI | ||
|
||
class BeakCommand { | ||
struct GlobalOptions { | ||
static let path = Key<String>("-p", "--path", description: "The path to a swift file. Defaults to beak.swift") | ||
private init() {} | ||
} | ||
|
||
let parser: ArgumentParser | ||
let options: BeakOptions | ||
let pathArgument: OptionArgument<String> | ||
protocol BeakCommand: Command { | ||
func execute(path: Path, beakFile: BeakFile) throws | ||
} | ||
|
||
init(options: BeakOptions, parentParser: ArgumentParser, name: String, description: String) { | ||
self.options = options | ||
parser = parentParser.add(subparser: name, overview: description) | ||
pathArgument = parser.add(option: "--path", shortName: "-p", kind: String.self, usage: "The path to a swift file. Defaults to beak.swift", completion: .filename) | ||
extension BeakCommand { | ||
|
||
var path: Key<String> { | ||
return GlobalOptions.path | ||
} | ||
|
||
func execute(parsedArguments: ArgumentParser.Result) throws { | ||
let path = Path(parsedArguments.get(pathArgument) ?? "beak.swift").normalize() | ||
func execute() throws { | ||
let path = Path(self.path.value ?? "beak.swift").normalize() | ||
let beakFile = try BeakFile(path: path) | ||
try execute(path: path, beakFile: beakFile, parsedArguments: parsedArguments) | ||
} | ||
|
||
func execute(path: Path, beakFile: BeakFile, parsedArguments: ArgumentParser.Result) throws { | ||
try execute(path: path, beakFile: beakFile) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
import Foundation | ||
import PathKit | ||
import Utility | ||
import SwiftCLI | ||
|
||
class FunctionCommand: BeakCommand { | ||
|
||
let name = "function" | ||
let shortDescription = "Info about a specific function" | ||
|
||
let functionName = Parameter() | ||
|
||
var functionArgument: PositionalArgument<String>! | ||
|
||
init(options: BeakOptions, parentParser: ArgumentParser) { | ||
super.init( | ||
options: options, | ||
parentParser: parentParser, | ||
name: "function", | ||
description: "Info about a specific function" | ||
) | ||
functionArgument = parser.add(positional: "function", kind: String.self, optional: false, usage: "The function to get info about", completion: ShellCompletion.none) | ||
} | ||
|
||
override func execute(path: Path, beakFile: BeakFile, parsedArguments: ArgumentParser.Result) throws { | ||
let functionName = parsedArguments.get(functionArgument)! | ||
guard let function = beakFile.functions.first(where: { $0.name == functionName }) else { | ||
throw BeakError.invalidFunction(functionName) | ||
func execute(path: Path, beakFile: BeakFile) throws { | ||
guard let function = beakFile.functions.first(where: { $0.name == functionName.value }) else { | ||
throw BeakError.invalidFunction(functionName.value) | ||
} | ||
let params = function.params.map { param in | ||
"\(param.name): \(param.optionalType)\(param.defaultValue != nil ? " = \(param.defaultValue!)" : "")\(param.description != nil ? " - \(param.description!)" : "")" | ||
stdout <<< "\(function.name):\(function.docsDescription != nil ? " \(function.docsDescription!)" : "")" | ||
function.params.forEach { param in | ||
stdout <<< " \(param.name): \(param.optionalType)\(param.defaultValue != nil ? " = \(param.defaultValue!)" : "")\(param.description != nil ? " - \(param.description!)" : "")" | ||
} | ||
print("\(function.name):\(function.docsDescription != nil ? " \(function.docsDescription!)" : "")\n \(params.joined(separator: "\n "))") | ||
} | ||
} |
Oops, something went wrong.