From 94bc9cbf8ab30e78cb6434ac197edba8aadf31d2 Mon Sep 17 00:00:00 2001 From: kolja <38011792+kblohm@users.noreply.github.com> Date: Sun, 28 Oct 2018 20:11:36 +0100 Subject: [PATCH] Add tab-completion for bash - add completion for commands, options and targets in bash - update documentation for command-line-completion to include bash - update installation instructions for completions --- completion/bash/README.md | 22 --- completion/bash/fake | 29 ---- completion/bash/fake-completion.bash | 212 +++++++++++++++++++++++++++ completion/bash/install.sh | 11 -- help/markdown/fake-completion.md | 73 +++++---- 5 files changed, 257 insertions(+), 90 deletions(-) delete mode 100644 completion/bash/README.md delete mode 100644 completion/bash/fake create mode 100644 completion/bash/fake-completion.bash delete mode 100644 completion/bash/install.sh diff --git a/completion/bash/README.md b/completion/bash/README.md deleted file mode 100644 index c318fcd0662..00000000000 --- a/completion/bash/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# BASH tab completion for possible FAKE tasks - -## Prerequisites - -If you have tab completion for other commands (such as git parameters) then you can skip to [Install](#install-completion-for-fake) - -##### OSX -```bash -brew install bash-completion -``` -Paste the following into ~/.bash_profile (create the file if it doesn't already exist) -```bash -if [ -f $(brew --prefix)/etc/bash_completion ]; then - . $(brew --prefix)/etc/bash_completion -fi -``` - -## Install completion for FAKE -```bash -cd completion/bash -./install.sh -``` diff --git a/completion/bash/fake b/completion/bash/fake deleted file mode 100644 index 52a779153bd..00000000000 --- a/completion/bash/fake +++ /dev/null @@ -1,29 +0,0 @@ -_fake_completion() -{ - COMPREPLY=() - local cur="${COMP_WORDS[COMP_CWORD]}" - - if [ -f build.fsx ] ; then - # egrep doesn't have the -P switch to match group on OSX - # so grep with perl instead as it's available on a default install - TASKS=$(perl -nle 'print $1 if m{Target \"(.+)\"}' build.fsx) - # Turn case-insensitive matching temporarily on, if necessary. - local nocasematchWasOff=0 - shopt nocasematch >/dev/null || nocasematchWasOff=1 - (( nocasematchWasOff )) && shopt -s nocasematch - - # Loop over words in list and search for case-insensitive prefix match. - local w matches=() - for w in $TASKS; do - if [[ "$w" == "${cur}"* ]]; then matches+=("$w"); fi - done - - # Restore state of 'nocasematch' option, if necessary. - (( nocasematchWasOff )) && shopt -u nocasematch - - COMPREPLY=("${matches[@]}") - return 0 - fi -} -complete -F _fake_completion fake -complete -F _fake_completion ./build.sh diff --git a/completion/bash/fake-completion.bash b/completion/bash/fake-completion.bash new file mode 100644 index 00000000000..aeb54b552d1 --- /dev/null +++ b/completion/bash/fake-completion.bash @@ -0,0 +1,212 @@ +_fake_index_of_cmdline () +{ + local word item index=1 + while [ $index -lt $cword ]; do + word="${words[index]}" + for item in $1; do + if [ "$item" = "$word" ]; then + echo "$index" + return + fi + done + ((index++)) + done +} + +_fake_complete_fake() { + local subcommands=( + build + run + ) + + local arguments=( + --help + -h + --version + --verbose + -v + -vv + --silent + -s + ) + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "${arguments[*]}" -- "$cur" ) ) + ;; + *) + COMPREPLY=( $( compgen -W "${subcommands[*]}" -- "$cur" ) ) + ;; + esac +} + + +_fake_get_targets() { + local start="The following targets are available:" + local end="Performance:" + fake 2>/dev/null "$@" | sed -n "/$start/,/$end/{//b;p}" +} + +_fake_complete_build() { + local subcommands=( + target + ) + + local arguments=( + --help + -h + --debug + -d + --nocache + -n + --partial-restore + -p + --fsiargs + --target + -t + --list + --script + -f + --single-target + -s + --parallel + -p + --environment-variable + -e + ) + + case "$prev" in + --script|-f) + # default bash filename completion + return + ;; + target|--target|-t) + + local index="$(_fake_index_of_cmdline "--script -f")" + if [ -n "$index" ]; then + local fileName="${words[(($index+1))]}" + case $fileName in + *.fsx) + local targets=$(_fake_get_targets build --script "$fileName" --list) + COMPREPLY=( $( compgen -W "${targets}" -- "$cur" ) ) + ;; + esac + return + fi + local targets=$(_fake_get_targets build --list) + COMPREPLY=( $( compgen -W "${targets}" -- "$cur" ) ) + return + ;; + esac + # target was specified but not as $prev, so we do not complete anything + if [ -n "$(_fake_index_of_cmdline "target")" ]; then + return + fi + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "${arguments[*]}" -- "$cur" ) ) + ;; + *) + COMPREPLY=( $( compgen -W "${subcommands[*]}" -- "$cur" ) ) + ;; + esac +} + + + +_fake_complete_run() { + local subcommands=( + target + ) + + local arguments=( + --help + -h + --debug + -d + --nocache + -n + --partial-restore + -p + --fsiargs + --target + -t + --list + --single-target + -s + --parallel + -p + --environment-variable + -e + ) + + case "$prev" in + run) + # default bash filename completion + return + ;; + + target|--target|-t) + local index="$(_fake_index_of_cmdline "run")" + if [ -n "$index" ]; then + local fileName="${words[(($index+1))]}" + case $fileName in + *.fsx) + local targets=$(_fake_get_targets run "$fileName" --list) + COMPREPLY=( $( compgen -W "${targets}" -- "$cur" ) ) + ;; + esac + fi + return + ;; + + esac + # target was specified but not as $prev, so we do not complete anything + if [ -n "$(_fake_index_of_cmdline "target")" ]; then + return + fi + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "${arguments[*]}" -- "$cur" ) ) + ;; + *) + COMPREPLY=( $( compgen -W "${subcommands[*]}" -- "$cur" ) ) + ;; + esac +} + + + + + + +_complete_fake() { + + + # COMPREPLY=() + local cur prev words cword + _get_comp_words_by_ref -n : cur prev words cword + + local command='fake' + local index=1 + + while [ $index -lt $cword ]; do + case "${words[$index]}" in + -*) + ;; + *) + command="${words[$index]}" + break + ;; + esac + (( index++ )) + done + + local completions_func=_fake_complete_${command} + declare -F $completions_func >/dev/null && $completions_func + + return 0 +} + + +complete -o bashdefault -o default -F _complete_fake fake fake.exe diff --git a/completion/bash/install.sh b/completion/bash/install.sh deleted file mode 100644 index 22b4fba3dde..00000000000 --- a/completion/bash/install.sh +++ /dev/null @@ -1,11 +0,0 @@ -#! /bin/bash -if [ -f $(brew --prefix)/etc/bash_completion ]; then - COMPLETIONS_FOLDER=$(brew --prefix)/etc/bash_completion.d -else - COMPLETIONS_FOLDER=$(pkg-config --variable=completionsdir bash-completion) -fi -echo "Copying FAKE completions to $COMPLETIONS_FOLDER" -THISDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -sudo cp $THISDIR/fake $COMPLETIONS_FOLDER -source $COMPLETIONS_FOLDER/fake -echo "Done!" diff --git a/help/markdown/fake-completion.md b/help/markdown/fake-completion.md index 38bbed9b79b..a2e487cc750 100644 --- a/help/markdown/fake-completion.md +++ b/help/markdown/fake-completion.md @@ -1,55 +1,72 @@ # Command-line completion -Fake provides command-line completion for bash and PowerShell. +FAKE provides command-line completion for bash and PowerShell. ## Available completions -For PowerShell, the following completions are supported: +The following completions are supported: * available FAKE commands * options supported by those commands * available targets in a fake-script -For bash, only the available targets are currently supported. - ## Installation for bash ### Prerequisites -* Tab completion for bash: - -If you have tab completion for other commands (such as git parameters) then you can skip to [Install](#Install-completion-for-bash). -On OSX, you might have to do the following: - -

-brew install bash-completion
-
-Paste the following into `~/.bash_profile` (create the file if it doesn't already exist) -

-if [ -f $(brew --prefix)/etc/bash_completion ]; then
-    . $(brew --prefix)/etc/bash_completion
-fi
-
- -### Install completion for bash -Download the contents of https://github.com/fsharp/FAKE/tree/master/completion/bash and run - -

-cd completion/bash
-./install.sh
-
+* bash completion is installed +* FAKE is installed as a global tool + +### OSX + + 1. Install bash completion if it is not installed `brew install bash-completion` + 2. Add the following into `~/.bash_profile` (create the file if it doesn't already exist) +

+    if [ -f $(brew --prefix)/etc/bash_completion ]; then
+        . $(brew --prefix)/etc/bash_completion
+    fi
+    
+ 3. Download the completion file https://github.com/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in `/usr/local/etc/bash_completion.d/` +

+        sudo curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o /usr/local/etc/bash_completion.d/fake-completion
+    
+ + +### Linux + 1. Bash completion should be installed on a current Linux OS. Otherwise you have to install it. + 2. Download the completion file https://github.com/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in `/etc/bash_completion.d/` +

+        sudo curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o /etc/bash_completion.d/fake-completion
+    
+ +### Windows with git-bash + 1. Download the completion file https://github.com/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in your home directory (`~/`) +

+        curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o ~/fake-completion.bash
+    
+ 2. Add the following into `~/.bashrc` (create the file if it doesn't already exist) +

+        source ~/fake-completion.bash
+    
## Installation for PowerShell ### Prerequisites * PowerShell >= 5.0 -* FAKE installed as a global tool +* FAKE is installed as a global tool ### Install completion for PowerShell Download the `posh-fake` module from . Copy the module into a folder called `posh-fake`, inside one of your PowerShell module folders. You can find those in `$env:PSModulePath`. -For example C:\Users\UserName\Documents\WindowsPowerShell\Modules\posh-fake. +For example `C:\Users\UserName\Documents\WindowsPowerShell\Modules\posh-fake`. +You can download the file with: +

+    # create directory
+    New-Item -ItemType Directory -Path \$HOME\Documents\WindowsPowerShell\Modules\posh-fake
+    # download
+    Invoke-WebRequest -Uri https://raw.githubusercontent.com/fsharp/FAKE/master/completion/powershell/posh-fake.psm1 -OutFile \$HOME\Documents\WindowsPowerShell\Modules\posh-fake\posh-fake.psm1
+    
To import the module, run