-
Notifications
You must be signed in to change notification settings - Fork 1
Working on the output strings #5
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
Changes from all commits
ab0e51c
ffa4e85
86e9bdb
0a42856
3299cea
0b44e37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| // Created by Omar Abdelhafith on 27/11/2016. | ||
| // | ||
| // | ||
| import Swiftline | ||
|
|
||
|
|
||
| public struct GuakaCliConfig { | ||
| public static var dir: DirectoryType.Type = FileSystemDirectory.self | ||
|
|
@@ -20,35 +22,53 @@ public enum GuakaError: Error { | |
| case cannotReadFile(String) | ||
| case setupFileAltered | ||
| case notAGuakaProject | ||
| case commandAlreadyExist(String, String) | ||
| case missingCommandName | ||
| case tooManyArgsPassed | ||
| case wrongCommandNameFormat(String) | ||
|
|
||
| public var error: String { | ||
| switch self { | ||
| case .wrongDirectoryGiven(let path): | ||
| return "The path given cannot be used \(path)" | ||
| return [ | ||
| "Wrong path given:", | ||
| " \(path)", | ||
| "The path must be an empty directory" | ||
| ].joined(separator: "\n").f.red | ||
|
|
||
| case .triedToCreateProjectInNonEmptyDirectory(let path): | ||
| return "Cannot create project in non empty directory \(path)\n" | ||
| return "Cannot create project in non empty directory: \(path)\n".f.red | ||
|
|
||
| case .failedCreatingFolder(let path): | ||
| return "Failed creating directory \(path)" | ||
| return "Failed creating directory \(path)".f.red | ||
|
|
||
| case .cannotCreateFile (let name): | ||
| return "Cannot generate \(name) file" | ||
| return "Cannot generate \(name) file".f.red | ||
|
|
||
| case .cannotReadFile(let path): | ||
| return "Cannot read contents of file \(path)" | ||
| return "Cannot read contents of file \(path)".f.red | ||
|
|
||
| case .setupFileAltered: | ||
| return "Guaka setup.swift file has been altered.\nThe placeholder used to insert commands cannot be found \(GeneratorParts.comamndAddingPlaceholder).\nYou can try to add it yourself by updating `setup.swift` to look like\n\n\(GeneratorParts.setupFileContent())\n\nAdding command wont be possible." | ||
| return "Guaka setup.swift file has been altered.\nThe placeholder used to insert commands cannot be found \(GeneratorParts.comamndAddingPlaceholder).\nYou can try to add it yourself by updating `setup.swift` to look like\n\n\(GeneratorParts.setupFileContent())\n\nAdding command won't be possible.".f.red | ||
|
|
||
| case .notAGuakaProject: | ||
| return "This command can only be executed in a Guaka project.\nThe following directory does not contain guaka files" | ||
| return "This command can only be executed inside a Guaka project.".f.red | ||
|
|
||
| case .missingCommandName: | ||
| return [ | ||
| "`guaka add` requires a command that was not given.", | ||
| "Missing CommandName for `guaka add`.".f.red, | ||
| "", | ||
| "Call `guaka add CommandName` to create a new command.", | ||
| "" | ||
| ].joined(separator: "\n") | ||
|
|
||
| case .commandAlreadyExist(let name, let path): | ||
| return [ "The command `\(name)` already exist:".f.red, | ||
| " \(path)".f.red, | ||
| "Please use a differnt command name"].joined(separator: "\n") | ||
|
|
||
| case .wrongCommandNameFormat(let name): | ||
| return [ "The command name passed `\(name)` is incorrect.", | ||
| return [ "The command name passed `\(name)` is incorrect.".f.red, | ||
| "Please use only letters, numbers, underscodes and dashes.", | ||
| "", | ||
| "Valid examples:", | ||
|
|
@@ -57,8 +77,10 @@ public enum GuakaError: Error { | |
| " guaka new my-command", | ||
| " guaka new my_command", | ||
| " guaka new myCommand"].joined(separator: "\n") | ||
|
|
||
| case .tooManyArgsPassed: | ||
| return "Too many arguments passed to command." | ||
| return "Too many arguments passed to command.".f.red | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just ignore the extra args? Also we could say something like "Too many arguments. Trailing arguments will be ignored"
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we scope this out of this PR? I created #6 for that |
||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // | ||
| // Agree.swift | ||
| // Agree | ||
| // | ||
| // Created by Omar Abdelhafith on 03/11/2015. | ||
| // Copyright © 2015 Omar Abdelhafith. All rights reserved. | ||
| // | ||
|
|
||
| /** | ||
| Displays a yes/no prompt to the user | ||
|
|
||
| - parameter prompt: The prompt to display | ||
| - returns: the user decision | ||
| */ | ||
| public func agree(_ prompt: String) -> Bool { | ||
| PromptSettings.print(prompt, terminator: " ") | ||
| let value = readStringOrEmpty() | ||
|
|
||
| let settings = AgreeSettings(prompt: prompt) | ||
| let validatedValue = askForValidatedItem(originalValue: value, validator: settings) | ||
|
|
||
| return settings.isPositive(validatedValue) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // | ||
| // AgreeSettings.swift | ||
| // AgreeSettings | ||
| // | ||
| // Created by Omar Abdelhafith on 03/11/2015. | ||
| // Copyright © 2015 Omar Abdelhafith. All rights reserved. | ||
| // | ||
|
|
||
|
|
||
| class AgreeSettings: AskerValidator { | ||
|
|
||
| let positiveValues = ["Yes", "yes", "Y", "y"] | ||
| let negativeValues = ["No", "no", "N", "n"] | ||
|
|
||
| let prompt: String | ||
|
|
||
| init(prompt: String) { | ||
| self.prompt = prompt | ||
| } | ||
|
|
||
| func validatedItem(forString string: String) -> String { | ||
| return string | ||
| } | ||
|
|
||
| func invalidItemMessage(_ string: String?) -> String? { | ||
| if let message = string , positiveValues.contains(message) || negativeValues.contains(message) { | ||
| return nil | ||
| } | ||
|
|
||
| return "Please enter \"yes\" or \"no\"." | ||
| } | ||
|
|
||
| func newItemPromptMessage() -> String { | ||
| return "\(prompt) " | ||
| } | ||
|
|
||
| func isPositive(_ item: String) -> Bool { | ||
| return positiveValues.contains(item) | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // | ||
| // ArgConvertible.swift | ||
| // ArgConvertible | ||
| // | ||
| // Created by Omar Abdelhafith on 02/11/2015. | ||
| // Copyright © 2015 Omar Abdelhafith. All rights reserved. | ||
| // | ||
|
|
||
|
|
||
| /** | ||
| * Any type that extends ArgConvertibleType can be used in ask and choose | ||
| */ | ||
| public protocol ArgConvertibleType { | ||
|
|
||
| /// Create an instance out of a string | ||
| static func fromString(_ string: String) -> Self? | ||
|
|
||
| /// Return the display name of a type | ||
| static func typeName() -> String | ||
| } | ||
|
|
||
|
|
||
| extension Int: ArgConvertibleType { | ||
| public static func fromString(_ string: String) -> Int? { | ||
| return Int(string) | ||
| } | ||
|
|
||
| public static func typeName() -> String { | ||
| return "Integer" | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension Double: ArgConvertibleType { | ||
| public static func fromString(_ string: String) -> Double? { | ||
| return Double(string) | ||
| } | ||
|
|
||
| public static func typeName() -> String { | ||
| return "Double" | ||
| } | ||
| } | ||
|
|
||
| extension Float: ArgConvertibleType { | ||
| public static func fromString(_ string: String) -> Float? { | ||
| return Float(string) | ||
| } | ||
|
|
||
| public static func typeName() -> String { | ||
| return "Float" | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension String: ArgConvertibleType { | ||
| public static func fromString(_ string: String) -> String? { | ||
| return string | ||
| } | ||
|
|
||
| public static func typeName() -> String { | ||
| return "String" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // | ||
| // Args.swift | ||
| // Swiftline | ||
| // | ||
| // Created by Omar Abdelhafith on 25/11/2015. | ||
| // Copyright © 2015 Omar Abdelhafith. All rights reserved. | ||
| // | ||
|
|
||
|
|
||
| /// Return the command line arguments passed to the script | ||
| public class Args { | ||
|
|
||
| /// Return the list of arguments passed to the script | ||
| public static var all: [String] { | ||
| return CommandLine.arguments | ||
| } | ||
|
|
||
| static var cachedResults: ParsedArgs? | ||
|
|
||
| /// Return a parsed list of arguments containing the flags and the parameters passed to the scripts | ||
| /// The flags are recognized as short flags `-f` or long flags `--force` | ||
| /// The flag value will be the argument that follows the flag | ||
| /// `--` is used to mark the terminatin of the flags | ||
| public static var parsed: ParsedArgs { | ||
| let result = parse(argumens: all, cachedResults: cachedResults) | ||
| cachedResults = result | ||
| return result | ||
| } | ||
|
|
||
| static func parse(argumens: [String], cachedResults: ParsedArgs?) -> ParsedArgs { | ||
| if let result = cachedResults { | ||
| return result | ||
| } | ||
|
|
||
| var parsedFlags = [String: String]() | ||
| let parsedArgs = ArgsParser.parseFlags(argumens) | ||
|
|
||
| parsedArgs.0.forEach { | ||
| parsedFlags[$0.argument.name] = $0.value ?? "" | ||
| } | ||
|
|
||
| var arguments = parsedArgs.1 | ||
|
|
||
| // the first argument is always the executable's name | ||
| var commandName = "" | ||
| if let firstArgument = arguments.first { // just in case! | ||
| commandName = firstArgument | ||
| arguments.removeFirst(1) | ||
| } | ||
|
|
||
| return ParsedArgs(command: commandName, flags: parsedFlags, parameters: arguments) | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public struct ParsedArgs { | ||
| /// The name of the executable that was invoked from the command line | ||
| public let command: String | ||
|
|
||
| /// Parsed flags will be prepred in a dictionary, the key is the flag and the value is the flag value | ||
| public let flags: [String: String] | ||
|
|
||
| /// List of parameters passed to the script | ||
| public let parameters: [String] | ||
| } |
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.
Are we commiting to maintaing this branch green. I think we shoudl use another name. Like 'vendored'.
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.
The branch in Swiftline is about making it work on linux nsomar/Swiftline#30
I think we should keep the name until we merge it