From 471b0e141241bb942ec6cdf5ff6d46e7c0fab08d Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 10:28:11 +0200 Subject: [PATCH 01/10] Update Package.swift --- Package.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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( From 184ab5c35decc0dcc4ecd87468c4f9456ab5af91 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 10:28:31 +0200 Subject: [PATCH 02/10] Improved zsh completion script --- .../{completion.bash => bash/hazel} | 0 .assets/scripts/completion/completion.zsh | 11 ----- .assets/scripts/completion/init.sh | 4 +- .assets/scripts/completion/zsh/_hazel | 42 +++++++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) rename .assets/scripts/completion/{completion.bash => bash/hazel} (100%) delete mode 100644 .assets/scripts/completion/completion.zsh create mode 100644 .assets/scripts/completion/zsh/_hazel diff --git a/.assets/scripts/completion/completion.bash b/.assets/scripts/completion/bash/hazel similarity index 100% rename from .assets/scripts/completion/completion.bash rename to .assets/scripts/completion/bash/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 "$@" From c8fb8a9ca0799373742aeae41afbc69ee1b41d6e Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 10:28:41 +0200 Subject: [PATCH 03/10] Improved completion function --- Sources/HazelCore/Support/Completion.swift | 74 +++++++++++++--------- Sources/HazelCore/Support/ConsoleIO.swift | 2 +- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/Sources/HazelCore/Support/Completion.swift b/Sources/HazelCore/Support/Completion.swift index 0b50af6..89da944 100644 --- a/Sources/HazelCore/Support/Completion.swift +++ b/Sources/HazelCore/Support/Completion.swift @@ -8,49 +8,61 @@ 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": [ + "--type": "Choose template", + "-t": "Choose template", + "--help": "Print help message and exit", + "-h": "Print help message and exit" + ] + ] + func complete(_ arguments: [String]) { 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("\(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) + 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("\(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 "--type", "-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..1709cfe 100644 --- a/Sources/HazelCore/Support/ConsoleIO.swift +++ b/Sources/HazelCore/Support/ConsoleIO.swift @@ -62,7 +62,7 @@ public struct ConsoleIO { let arguments = CommandLine.arguments if arguments[1] == "cmplt" { - Completion().complete(arguments[2]) + Completion().complete(Array(arguments.dropFirst(2))) exit(0) } else if arguments[1] == "--completion-path" { print("\(Application.Paths.configPath)/completion/init.sh") From 66154a8bf02232fe9a42a964f7b38a4cb1fa354c Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 11:53:12 +0200 Subject: [PATCH 04/10] Improved bash completion script --- .assets/scripts/completion/bash/hazel | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.assets/scripts/completion/bash/hazel b/.assets/scripts/completion/bash/hazel index 5969aa4..5d41bf0 100644 --- a/.assets/scripts/completion/bash/hazel +++ b/.assets/scripts/completion/bash/hazel @@ -3,8 +3,15 @@ _hazel_complete() { COMPREPLY=() local word="${COMP_WORDS[COMP_CWORD]}" - local line="${COMP_WORDS[@]}" - local completions="$(hazel cmplt "$line" "$word")" + local line=(${COMP_WORDS[@]}) + line=(${COMP_WORDS[@]}) + line[0]="" + if [ ${#line[@]} -gt 1 ] + then + line=(${line[@]}:1) + fi + + local completions="$(hazel cmplt-bash "${line[@]}")" COMPREPLY=( $(compgen -W "$completions" -- "$word") ) } From dc88ad818ac3dfd507505e037441ad41f009fbe1 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 11:53:22 +0200 Subject: [PATCH 05/10] Fixing bash completion --- Sources/HazelCore/Support/Completion.swift | 16 +++++++++++++--- Sources/HazelCore/Support/ConsoleIO.swift | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/HazelCore/Support/Completion.swift b/Sources/HazelCore/Support/Completion.swift index 89da944..81a79a4 100644 --- a/Sources/HazelCore/Support/Completion.swift +++ b/Sources/HazelCore/Support/Completion.swift @@ -26,10 +26,16 @@ struct Completion { ] ] - func complete(_ arguments: [String]) { + func complete(_ arguments: [String], forBash isBash: Bool = false) { + guard arguments.count > 1 else { for (key, value) in self.args[" "]! { - print("\(key):\(value)") + if isBash { + print(key) + } else { + print("\(key):\(value)") + } + } return @@ -44,7 +50,11 @@ struct Completion { guard arguments.count > 2 else { if let arg = self.args[arguments[0]] { for (key, value) in arg { - print("\(key):\(value)") + if isBash { + print(key) + } else { + print("\(key):\(value)") + } } } diff --git a/Sources/HazelCore/Support/ConsoleIO.swift b/Sources/HazelCore/Support/ConsoleIO.swift index 1709cfe..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(Array(arguments.dropFirst(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") From 95629611a0dae3ec0062dddc1b2ea26f9d74087b Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 17:09:36 +0200 Subject: [PATCH 06/10] Improved bash completion --- .assets/scripts/completion/bash/hazel | 12 +++++++----- Sources/HazelCore/Support/Completion.swift | 15 +++------------ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/.assets/scripts/completion/bash/hazel b/.assets/scripts/completion/bash/hazel index 5d41bf0..c4f3851 100644 --- a/.assets/scripts/completion/bash/hazel +++ b/.assets/scripts/completion/bash/hazel @@ -2,16 +2,18 @@ _hazel_complete() { COMPREPLY=() + local completions local word="${COMP_WORDS[COMP_CWORD]}" local line=(${COMP_WORDS[@]}) - line=(${COMP_WORDS[@]}) - line[0]="" - if [ ${#line[@]} -gt 1 ] + line=(${COMP_WORDS[@]:1}) + + if [ $COMP_CWORD -eq 1 ] then - line=(${line[@]}:1) + completions="$(hazel cmplt-bash "${line[@]}")" + else + completions="$(hazel cmplt-bash "${line[@]}" " ")" fi - local completions="$(hazel cmplt-bash "${line[@]}")" COMPREPLY=( $(compgen -W "$completions" -- "$word") ) } diff --git a/Sources/HazelCore/Support/Completion.swift b/Sources/HazelCore/Support/Completion.swift index 81a79a4..be944be 100644 --- a/Sources/HazelCore/Support/Completion.swift +++ b/Sources/HazelCore/Support/Completion.swift @@ -30,12 +30,7 @@ struct Completion { guard arguments.count > 1 else { for (key, value) in self.args[" "]! { - if isBash { - print(key) - } else { - print("\(key):\(value)") - } - + print( (isBash) ? key : "\(key):\(value)") } return @@ -43,18 +38,14 @@ struct Completion { guard arguments[0] != "-q" else { let arguments = Array(arguments.dropFirst()) - self.complete(arguments) + self.complete(arguments, forBash: isBash) return } guard arguments.count > 2 else { if let arg = self.args[arguments[0]] { for (key, value) in arg { - if isBash { - print(key) - } else { - print("\(key):\(value)") - } + print( (isBash) ? key : "\(key):\(value)") } } From 5e6c3346baadda4b3bcdcb0426ca37937bb187a2 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 17:26:26 +0200 Subject: [PATCH 07/10] Changed all mentions of 'type' to 'template' --- Sources/HazelCore/Hazel.swift | 10 +++++----- Sources/HazelCore/Support/Arguments.swift | 14 +++++++------- Sources/HazelCore/Support/Completion.swift | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) 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 be944be..074bbc7 100644 --- a/Sources/HazelCore/Support/Completion.swift +++ b/Sources/HazelCore/Support/Completion.swift @@ -19,8 +19,8 @@ struct Completion { "-q": "Silent mode" ], "init": [ - "--type": "Choose template", - "-t": "Choose template", + "--template": "Choose project template", + "-t": "Choose project template", "--help": "Print help message and exit", "-h": "Print help message and exit" ] @@ -54,7 +54,7 @@ struct Completion { if arguments[0] == "init" { switch arguments[1] { - case "--type", "-t": + case "--template", "-t": guard arguments.count < 4 else { return } let templates = Application.Paths.templatesPath From bb75781c6202fcdee34abe745700d20d35a9b4d8 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 17:27:21 +0200 Subject: [PATCH 08/10] Updated tests --- Tests/HazelTests/HazelTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From 3ecc5a26c6911c38d5eccc63f2a5c985c37ad83a Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 17:28:15 +0200 Subject: [PATCH 09/10] Update README --- README.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cf22354..2a42775 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ - [x] Support for macOS - [ ] Support for Linux + + ## Table of contents * [Installation](#installation) @@ -53,11 +55,11 @@ This will install ``Hazel`` in ``/usr/local/bin``, along with its templates file ## Usage -Run ``Hazel`` inside the root folder for your project with the ``init`` command, along with the ``--type``, or ``-t``, flag to generate the directory structure and the files: +Run ``Hazel`` inside the root folder for your project with the ``init`` command, along with the ``--template``, or ``-t``, flag to generate the directory structure and the files: ```bash $ mkdir SomeApp && cd SomeApp -$ hazel init --type c +$ hazel init --template c Created obj Created bin @@ -71,8 +73,6 @@ Created src/SomeApp.c Created .editorconfig ``` - - ### Customization ``Hazel`` generates new projects based on predefined templates, placed in ``~/.hazel/templates``. You can add your own templates by simply creating a desired directory structure, along with the files you want to be automatically generated, in a subdirectory to ``~/.hazel/templates``. @@ -101,11 +101,11 @@ $ tree    └── __PROJECTNAME__.swift ``` -To generate new projects based on the above template, we can now initiate a new project of type ``swift``: +To generate new projects based on the above template, we can now initiate a new project with template ``swift``: ```bash $ mkdir AwesomeApp && cd AwesomeApp -$ hazel init --type swift +$ hazel init --template swift Created README.md Created src @@ -138,20 +138,22 @@ Commands: Options: -h, --help Print help message and exit -v, --version Print version information and exit + -q, --quiet Silent mode ``` ```bash $ hazel init --help -Usage: hazel init [argument] +Usage: ./hazel_debug init [argument] Options: - -t, --type Choose project type - -v, --version Print version information and exit + -t, --template Choose project template + --no-config Do not generate .editorconfig + -h, --help Print help message and exit ``` ## Tab completion -``Hazel`` comes equipped with tab completion for ``bash`` and ``zsh``. Add the following to your startup script (``.bashrc``, ``.zshrc``, etc...) to enable tab completion: +``Hazel`` comes equipped with tab completion for ``bash`` and ``zsh``. If you've installed it manually (not with Homebrew), then you need to add the following to your startup script (``.bashrc``, ``.zshrc``, etc...) to enable tab completion: ```bash which hazel > /dev/null && . "$( hazel --completion-path )" From 4b2dcd287bce265b7474413215d649548119d34e Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sat, 16 Jun 2018 17:39:21 +0200 Subject: [PATCH 10/10] Update Makefile --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) 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