Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pkrll/Hazel
Browse files Browse the repository at this point in the history
  • Loading branch information
pkrll committed Jun 16, 2018
2 parents b2a61b6 + 0d97da4 commit 978a590
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 75 deletions.
20 changes: 20 additions & 0 deletions .assets/scripts/completion/bash/hazel
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions .assets/scripts/completion/completion.bash

This file was deleted.

11 changes: 0 additions & 11 deletions .assets/scripts/completion/completion.zsh

This file was deleted.

4 changes: 2 additions & 2 deletions .assets/scripts/completion/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions .assets/scripts/completion/zsh/_hazel
Original file line number Diff line number Diff line change
@@ -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 "$@"
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<img src=".assets/hazel.gif" width="40%" align="right">

<img src=".assets/hazel.gif" width="40%" align="right">

## Table of contents

* [Installation](#installation)
Expand Down Expand Up @@ -142,7 +144,7 @@ Options:
```
```bash
$ hazel init --help
Usage: hazel init [argument]
Usage: ./hazel_debug init [argument]

Options:
-t, --template Choose project template
Expand Down
10 changes: 5 additions & 5 deletions Sources/HazelCore/Hazel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/HazelCore/Support/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -37,24 +37,24 @@ 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"
)

self.initialize = CommandOption(
"init",
withArguments: [
self.type,
self.template,
self.skipConf,
self.help
],
Expand Down
75 changes: 44 additions & 31 deletions Sources/HazelCore/Support/Completion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/HazelCore/Support/ConsoleIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions Tests/HazelTests/HazelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 978a590

Please sign in to comment.