diff --git a/.assets/scripts/completion/bash/hazel b/.assets/scripts/completion/bash/hazel new file mode 100644 index 0000000..c4f3851 --- /dev/null +++ b/.assets/scripts/completion/bash/hazel @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +_hazel_complete() { + COMPREPLY=() + local completions + local word="${COMP_WORDS[COMP_CWORD]}" + local line=(${COMP_WORDS[@]}) + line=(${COMP_WORDS[@]:1}) + + if [ $COMP_CWORD -eq 1 ] + then + completions="$(hazel cmplt-bash "${line[@]}")" + else + completions="$(hazel cmplt-bash "${line[@]}" " ")" + fi + + COMPREPLY=( $(compgen -W "$completions" -- "$word") ) +} + +complete -F _hazel_complete hazel diff --git a/.assets/scripts/completion/completion.bash b/.assets/scripts/completion/completion.bash deleted file mode 100644 index 5969aa4..0000000 --- a/.assets/scripts/completion/completion.bash +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -_hazel_complete() { - COMPREPLY=() - local word="${COMP_WORDS[COMP_CWORD]}" - local line="${COMP_WORDS[@]}" - local completions="$(hazel cmplt "$line" "$word")" - COMPREPLY=( $(compgen -W "$completions" -- "$word") ) -} - -complete -F _hazel_complete hazel diff --git a/.assets/scripts/completion/completion.zsh b/.assets/scripts/completion/completion.zsh deleted file mode 100644 index 17f13d9..0000000 --- a/.assets/scripts/completion/completion.zsh +++ /dev/null @@ -1,11 +0,0 @@ -# completion.zsh - -_hazel_complete() { - local line word completions - read -c line - word="$1" - completions="$(hazel cmplt "${line}" "${word}")" - reply=( "${(ps:\n:)completions}" ) -} - -compctl -K _hazel_complete hazel diff --git a/.assets/scripts/completion/init.sh b/.assets/scripts/completion/init.sh index 423d3bd..f22dbc9 100644 --- a/.assets/scripts/completion/init.sh +++ b/.assets/scripts/completion/init.sh @@ -2,9 +2,9 @@ if [ -n "$BASH_VERSION" ]; then root="$(dirname "${BASH_SOURCE[0]}")" - source "$root/completion.bash" + source "$root/bash/hazel" elif [ -n "$ZSH_VERSION" ]; then root="$(dirname "$0")" - source "$root/completion.zsh" + source "$root/zsh/_hazel" fi diff --git a/.assets/scripts/completion/zsh/_hazel b/.assets/scripts/completion/zsh/_hazel new file mode 100644 index 0000000..e0071bb --- /dev/null +++ b/.assets/scripts/completion/zsh/_hazel @@ -0,0 +1,42 @@ +#compdef hazel +#autoload + +_hazel() { + typeset -A opt_args + + _arguments -C \ + '1:command:->commands' \ + '2:argument:->arguments' \ + '*:: :->opts' \ + && ret=0 + + case "$state" in + (commands) + local complete; complete=$(hazel cmplt "${line[1]}") + local commands; commands=( + ${(ps:\n:)complete} + ) + + _describe -t commands 'commands' commands && ret=0 + ;; + (arguments) + local complete; complete=$(hazel cmplt "${line[@]}") + local argument; argument=( + ${(ps:\n:)complete} + ) + + _describe -t argument 'argument' argument && ret=0 + ;; + (opts) + local complete; complete=$(hazel cmplt "${line[@]}") + local argument; argument=( + ${(ps:\n:)complete} + ) + + _describe -t argument 'argument' argument && ret=0 + esac; + + return 1; +} + +_hazel "$@" diff --git a/Makefile b/Makefile index f7342e9..5852e83 100644 --- a/Makefile +++ b/Makefile @@ -61,3 +61,8 @@ clean: rm -rf .build/ rm -rf xcov_report rm -f hazel_debug + +compress: + cd ../ && tar czf hazel-1.0.3.tar.gz Hazel + mv ../hazel-1.0.3.tar.gz . + shasum -a 256 hazel-1.0.3.tar.gz diff --git a/Package.swift b/Package.swift index 51ba743..63ccedc 100644 --- a/Package.swift +++ b/Package.swift @@ -7,8 +7,7 @@ let package = Package( name: "Hazel", dependencies: [ .package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0"), - // .package(url: "https://github.com/pkrll/SwiftArgs", from: "0.4.0") - .package(url: "https://github.com/pkrll/SwiftArgs", .branch("dev/0.5.4")) + .package(url: "https://github.com/pkrll/SwiftArgs", from: "0.5.0") ], targets: [ .target( diff --git a/README.md b/README.md index 1d21ad7..0aef1b7 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ + + ## Table of contents * [Installation](#installation) @@ -142,7 +144,7 @@ Options: ``` ```bash $ hazel init --help -Usage: hazel init [argument] +Usage: ./hazel_debug init [argument] Options: -t, --template Choose project template diff --git a/Sources/HazelCore/Hazel.swift b/Sources/HazelCore/Hazel.swift index 1197d76..3635fa3 100644 --- a/Sources/HazelCore/Hazel.swift +++ b/Sources/HazelCore/Hazel.swift @@ -19,19 +19,19 @@ public struct Hazel { return } - var projType: String? + var template: String? var skipConf: Bool = false for argument in command.arguments { switch argument.name { - case "type": projType = (argument as? StringOption)?.value - case "conf": skipConf = (argument as? BoolOption)?.value! ?? false + case "template": template = (argument as? StringOption)?.value + case "skipConf": skipConf = (argument as? BoolOption)?.value! ?? false default: break } } - guard let type = projType else { - self.console.forceQuit(withMessage: "Project type not recognized.") + guard let type = template else { + self.console.forceQuit(withMessage: "No project template given.") return } diff --git a/Sources/HazelCore/Support/Arguments.swift b/Sources/HazelCore/Support/Arguments.swift index 8f226ae..deb6bd2 100644 --- a/Sources/HazelCore/Support/Arguments.swift +++ b/Sources/HazelCore/Support/Arguments.swift @@ -11,7 +11,7 @@ public struct Arguments { public let help: BoolOption public let quiet: BoolOption public let version: BoolOption - public let type: StringOption + public let template: StringOption public let skipConf: BoolOption public let initialize: CommandOption @@ -37,16 +37,16 @@ public struct Arguments { description: "Print version information and exit" ) - self.type = StringOption( - name: "type", + self.template = StringOption( + name: "template", shortFlag: "t", - longFlag: "type", - description: "Set language for project", + longFlag: "template", + description: "Choose project template", isRequired: true ) self.skipConf = BoolOption( - name: "conf", + name: "skipConf", longFlag: "no-config", description: "Do not generate .editorconfig" ) @@ -54,7 +54,7 @@ public struct Arguments { self.initialize = CommandOption( "init", withArguments: [ - self.type, + self.template, self.skipConf, self.help ], diff --git a/Sources/HazelCore/Support/Completion.swift b/Sources/HazelCore/Support/Completion.swift index 0b50af6..074bbc7 100644 --- a/Sources/HazelCore/Support/Completion.swift +++ b/Sources/HazelCore/Support/Completion.swift @@ -8,49 +8,62 @@ import Foundation struct Completion { - func complete(_ argument: String) { - let arguments = Array(argument.components(separatedBy: " ").dropFirst()) + var args = [ + " ": [ + "init": "Initiate a new project", + "--help": "Print help message and exit", + "-h": "Print help message and exit", + "--version": "Print version information and exit", + "-v": "Print version information and exit", + "--quiet": "Silent mode", + "-q": "Silent mode" + ], + "init": [ + "--template": "Choose project template", + "-t": "Choose project template", + "--help": "Print help message and exit", + "-h": "Print help message and exit" + ] + ] + + func complete(_ arguments: [String], forBash isBash: Bool = false) { guard arguments.count > 1 else { - if arguments[0].hasPrefix("--") { - self.reply("--help", "--version", "--quiet") - } else if arguments[0].hasPrefix("-") { - self.reply("-h", "-v", "-q") - } else { - self.reply("init", "--help", "--version", "--quiet") + for (key, value) in self.args[" "]! { + print( (isBash) ? key : "\(key):\(value)") } return } - if arguments[0] == "init" { self.handleInit(Array(arguments[1...])) } - } - - private func handleInit(_ arguments: [String]) { - switch arguments[0] { - case "--type", "-t": - guard arguments.count < 3 else { return } + guard arguments[0] != "-q" else { + let arguments = Array(arguments.dropFirst()) + self.complete(arguments, forBash: isBash) + return + } - let templates = Application.Paths.templatesPath - if let folders = try? FileManager.default.contentsOfDirectory(atPath: templates) { - for folder in folders where folder != "defaults" { print(folder) } + guard arguments.count > 2 else { + if let arg = self.args[arguments[0]] { + for (key, value) in arg { + print( (isBash) ? key : "\(key):\(value)") + } } - case "--help", "-h": + return - default: - if arguments[0].hasPrefix("--") { - self.reply("--type", "--help") - } else if arguments[0].hasPrefix("-") { - self.reply("-t", "-h") - } else { - self.reply("--type", "--help") - } } - } - private func reply(_ replies: String...) { - for string in replies { - print(string) + if arguments[0] == "init" { + switch arguments[1] { + case "--template", "-t": + guard arguments.count < 4 else { return } + + let templates = Application.Paths.templatesPath + if let folders = try? FileManager.default.contentsOfDirectory(atPath: templates) { + for folder in folders where folder != "defaults" { print(folder) } + } + default: + return + } } } diff --git a/Sources/HazelCore/Support/ConsoleIO.swift b/Sources/HazelCore/Support/ConsoleIO.swift index 246250b..ece18a1 100644 --- a/Sources/HazelCore/Support/ConsoleIO.swift +++ b/Sources/HazelCore/Support/ConsoleIO.swift @@ -61,8 +61,8 @@ public struct ConsoleIO { guard CommandLine.argc > 1 else { return } let arguments = CommandLine.arguments - if arguments[1] == "cmplt" { - Completion().complete(arguments[2]) + if arguments[1].hasPrefix("cmplt") { + Completion().complete(Array(arguments.dropFirst(2)), forBash: (arguments[1] == "cmplt-bash")) exit(0) } else if arguments[1] == "--completion-path" { print("\(Application.Paths.configPath)/completion/init.sh") diff --git a/Tests/HazelTests/HazelTests.swift b/Tests/HazelTests/HazelTests.swift index 6b6c67b..9e5c0b8 100644 --- a/Tests/HazelTests/HazelTests.swift +++ b/Tests/HazelTests/HazelTests.swift @@ -36,7 +36,7 @@ final class HazelTests: XCTestCase { func testCProjectWithMakeAndConf() { let console = ConsoleIO.default - let options = console.parse(["-q", "init", "--type", "c" ]) + let options = console.parse(["-q", "init", "--template", "c" ]) let hazel = Hazel(console) if options.initialize.value { @@ -53,7 +53,7 @@ final class HazelTests: XCTestCase { func testCProjectWithMakeNoConf() { let console = ConsoleIO.default - let options = console.parse([ "init", "--type", "c", "--no-config" ]) + let options = console.parse([ "init", "--template", "c", "--no-config" ]) let hazel = Hazel(console) ConsoleIO.default.silentMode = true @@ -72,7 +72,7 @@ final class HazelTests: XCTestCase { func testSwiftProject() { let console = ConsoleIO.default - let options = console.parse([ "init", "--type", "swift" ]) + let options = console.parse([ "init", "--template", "swift" ]) let hazel = Hazel(console) ConsoleIO.default.silentMode = true