-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use SwiftCLI #29
Merged
Merged
Use SwiftCLI #29
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this go to stderr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed