Skip to content

Commit

Permalink
Add tab-completion for bash
Browse files Browse the repository at this point in the history
- add completion for commands, options and targets in bash
- update documentation for command-line-completion to include bash
- update installation instructions for completions
  • Loading branch information
kblohm committed Oct 28, 2018
1 parent bd74f3b commit 94bc9cb
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 90 deletions.
22 changes: 0 additions & 22 deletions completion/bash/README.md

This file was deleted.

29 changes: 0 additions & 29 deletions completion/bash/fake

This file was deleted.

212 changes: 212 additions & 0 deletions completion/bash/fake-completion.bash
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions completion/bash/install.sh

This file was deleted.

73 changes: 45 additions & 28 deletions help/markdown/fake-completion.md
Original file line number Diff line number Diff line change
@@ -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:

<pre><code class="lang-bash">
brew install bash-completion
</code></pre>
Paste the following into `~/.bash_profile` (create the file if it doesn't already exist)
<pre><code class="lang-bash">
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
</code></pre>

### Install completion for bash
Download the contents of https:/fsharp/FAKE/tree/master/completion/bash and run

<pre><code class="lang-bash">
cd completion/bash
./install.sh
</code></pre>
* 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)
<pre><code class="lang-bash">
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
</code></pre>
3. Download the completion file https:/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in `/usr/local/etc/bash_completion.d/`
<pre><code class="lang-bash">
sudo curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o /usr/local/etc/bash_completion.d/fake-completion
</code></pre>


### Linux
1. Bash completion should be installed on a current Linux OS. Otherwise you have to install it.
2. Download the completion file https:/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in `/etc/bash_completion.d/`
<pre><code class="lang-bash">
sudo curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o /etc/bash_completion.d/fake-completion
</code></pre>

### Windows with git-bash
1. Download the completion file https:/fsharp/FAKE/tree/master/completion/bash/fake-completion.bash and place it in your home directory (`~/`)
<pre><code class="lang-bash">
curl -L https://raw.githubusercontent.com/fsharp/FAKE/master/completion/bash/fake-completion.bash -o ~/fake-completion.bash
</code></pre>
2. Add the following into `~/.bashrc` (create the file if it doesn't already exist)
<pre><code class="lang-bash">
source ~/fake-completion.bash
</code></pre>

## 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 <https:/fsharp/FAKE/tree/master/completion/powershell/posh-fake.psm1>. 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:
<pre><code class="lang-bash">
# 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
</code></pre>

To import the module, run
<pre><code class="lang-bash">
Expand Down

0 comments on commit 94bc9cb

Please sign in to comment.