-
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
354 additions
and
6 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package completion | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"github.com/go-task/task/v3/internal/templater" | ||
"github.com/go-task/task/v3/taskfile" | ||
) | ||
|
||
type TemplateValues struct { | ||
Entrypoint string | ||
Flags []*pflag.Flag | ||
Tasks []*taskfile.Task | ||
} | ||
|
||
//go:embed templates/* | ||
var templates embed.FS | ||
|
||
func Compile(completion string, tasks taskfile.Tasks) (string, error) { | ||
// Get the file extension for the selected shell | ||
var ext string | ||
switch completion { | ||
case "bash": | ||
ext = "bash" | ||
case "fish": | ||
ext = "fish" | ||
case "powershell": | ||
ext = "ps1" | ||
case "zsh": | ||
ext = "zsh" | ||
default: | ||
return "", fmt.Errorf("unknown completion shell: %s", completion) | ||
} | ||
|
||
// Load the template | ||
templateName := fmt.Sprintf("task.tpl.%s", ext) | ||
tpl, err := template.New(templateName). | ||
Funcs(templater.TemplateFuncs). | ||
ParseFS(templates, filepath.Join("templates", templateName)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
values := TemplateValues{ | ||
Entrypoint: os.Args[0], | ||
Flags: getFlagNames(), | ||
Tasks: tasks.Values(), | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := tpl.Execute(&buf, values); err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} | ||
|
||
func getFlagNames() []*pflag.Flag { | ||
var flags []*pflag.Flag | ||
pflag.VisitAll(func(flag *pflag.Flag) { | ||
flags = append(flags, flag) | ||
}) | ||
return flags | ||
} |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package completion | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/sebdah/goldie/v2" | ||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/go-task/task/v3/internal/orderedmap" | ||
"github.com/go-task/task/v3/taskfile" | ||
) | ||
|
||
var ( | ||
tasks = taskfile.Tasks{ | ||
OrderedMap: orderedmap.FromMapWithOrder( | ||
map[string]*taskfile.Task{ | ||
"foo": { | ||
Task: "foo", | ||
Cmds: []*taskfile.Cmd{ | ||
{Cmd: "echo foo"}, | ||
}, | ||
Desc: "Prints foo", | ||
Aliases: []string{"f"}, | ||
}, | ||
"bar": { | ||
Task: "bar", | ||
Cmds: []*taskfile.Cmd{ | ||
{Cmd: "echo bar"}, | ||
}, | ||
Desc: "Prints bar", | ||
Aliases: []string{"b"}, | ||
}, | ||
}, | ||
[]string{"foo", "bar"}, | ||
), | ||
} | ||
flags struct { | ||
foo string | ||
bar int | ||
noDesc bool | ||
} | ||
) | ||
|
||
func init() { | ||
os.Args[0] = "task" | ||
pflag.StringVarP(&flags.foo, "foo", "f", "default", "A regular flag") | ||
pflag.IntVar(&flags.bar, "bar", 99, "A flag with no short variant") | ||
pflag.BoolVar(&flags.noDesc, "no-desc", true, "") | ||
} | ||
|
||
func TestCompile(t *testing.T) { | ||
tests := []struct { | ||
shell string | ||
}{ | ||
{shell: "bash"}, | ||
{shell: "fish"}, | ||
{shell: "powershell"}, | ||
{shell: "zsh"}, | ||
} | ||
|
||
data := map[string]interface{}{ | ||
"Entrypoint": os.Args[0], | ||
} | ||
|
||
spew.Dump(data) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.shell, func(t *testing.T) { | ||
completion, err := Compile(tt.shell, tasks) | ||
require.NoError(t, err) | ||
|
||
g := goldie.New(t, goldie.WithTestNameForDir(true)) | ||
g.AssertWithTemplate(t, tt.shell, data, []byte(completion)) | ||
}) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# vim: set tabstop=2 shiftwidth=2 expandtab: | ||
|
||
_GO_TASK_COMPLETION_LIST_OPTION='--list-all' | ||
|
||
function _task() | ||
{ | ||
local cur prev words cword | ||
_init_completion -n : || return | ||
|
||
# Check for `--` within command-line and quit or strip suffix. | ||
local i | ||
for i in "${!words[@]}"; do | ||
if [ "${words[$i]}" == "--" ]; then | ||
# Do not complete words following `--` passed to CLI_ARGS. | ||
[ $cword -gt $i ] && return | ||
# Remove the words following `--` to not put --list in CLI_ARGS. | ||
words=( "${words[@]:0:$i}" ) | ||
break | ||
fi | ||
done | ||
|
||
# Handle special arguments of options. | ||
case "$prev" in | ||
-d|--dir) | ||
_filedir -d | ||
return $? | ||
;; | ||
-t|--taskfile) | ||
_filedir yaml || return $? | ||
_filedir yml | ||
return $? | ||
;; | ||
-o|--output) | ||
COMPREPLY=( $( compgen -W "interleaved group prefixed" -- $cur ) ) | ||
return 0 | ||
;; | ||
esac | ||
|
||
# Handle normal options. | ||
case "$cur" in | ||
-*) | ||
COMPREPLY=( $( compgen -W "$(_parse_help $1)" -- $cur ) ) | ||
return 0 | ||
;; | ||
esac | ||
|
||
# Prepare task name completions. | ||
local tasks=( $( "${words[@]}" --silent $_GO_TASK_COMPLETION_LIST_OPTION 2> /dev/null ) ) | ||
COMPREPLY=( $( compgen -W "${tasks[*]}" -- "$cur" ) ) | ||
|
||
# Post-process because task names might contain colons. | ||
__ltrim_colon_completions "$cur" | ||
} | ||
|
||
complete -F _task task |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
set GO_TASK_PROGNAME task | ||
|
||
function __task_get_tasks --description "Prints all available tasks with their description" | ||
# Read the list of tasks (and potential errors) | ||
$GO_TASK_PROGNAME --list-all 2>&1 | read -lz rawOutput | ||
|
||
# Return on non-zero exit code (for cases when there is no Taskfile found or etc.) | ||
if test $status -ne 0 | ||
return | ||
end | ||
|
||
# Grab names and descriptions (if any) of the tasks | ||
set -l output (echo $rawOutput | sed -e '1d; s/\* \(.*\):\s*\(.*\)\s*(aliases.*/\1\t\2/' -e 's/\* \(.*\):\s*\(.*\)/\1\t\2/'| string split0) | ||
if test $output | ||
echo $output | ||
end | ||
end | ||
|
||
complete -c $GO_TASK_PROGNAME -d 'Runs the specified task(s). Falls back to the "default" task if no task name was specified, or lists all tasks if an unknown task name was | ||
specified.' -xa "(__task_get_tasks)" | ||
|
||
complete -c $GO_TASK_PROGNAME -s c -l color -d 'colored output (default true)' | ||
complete -c $GO_TASK_PROGNAME -s d -l dir -d 'sets directory of execution' | ||
complete -c $GO_TASK_PROGNAME -l dry -d 'compiles and prints tasks in the order that they would be run, without executing them' | ||
complete -c $GO_TASK_PROGNAME -s f -l force -d 'forces execution even when the task is up-to-date' | ||
complete -c $GO_TASK_PROGNAME -s h -l help -d 'shows Task usage' | ||
complete -c $GO_TASK_PROGNAME -s i -l init -d 'creates a new Taskfile.yml in the current folder' | ||
complete -c $GO_TASK_PROGNAME -s l -l list -d 'lists tasks with description of current Taskfile' | ||
complete -c $GO_TASK_PROGNAME -s o -l output -d 'sets output style: [interleaved|group|prefixed]' -xa "interleaved group prefixed" | ||
complete -c $GO_TASK_PROGNAME -s p -l parallel -d 'executes tasks provided on command line in parallel' | ||
complete -c $GO_TASK_PROGNAME -s s -l silent -d 'disables echoing' | ||
complete -c $GO_TASK_PROGNAME -l status -d 'exits with non-zero exit code if any of the given tasks is not up-to-date' | ||
complete -c $GO_TASK_PROGNAME -l summary -d 'show summary about a task' | ||
complete -c $GO_TASK_PROGNAME -s t -l taskfile -d 'choose which Taskfile to run. Defaults to "Taskfile.yml"' | ||
complete -c $GO_TASK_PROGNAME -s v -l verbose -d 'enables verbose mode' | ||
complete -c $GO_TASK_PROGNAME -l version -d 'show Task version' | ||
complete -c $GO_TASK_PROGNAME -s w -l watch -d 'enables watch of the given task' |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$scriptBlock = { | ||
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters ) | ||
$reg = "\* ($commandName.+?):" | ||
$listOutput = $(task --list-all) | ||
$listOutput | Select-String $reg -AllMatches | ForEach-Object { $_.Matches.Groups[1].Value } | ||
} | ||
|
||
Register-ArgumentCompleter -CommandName task -ScriptBlock $scriptBlock |
Oops, something went wrong.