Skip to content

Commit

Permalink
Allow to override functions to change default behaviour of gradle com…
Browse files Browse the repository at this point in the history
…pletion per project
  • Loading branch information
wosiu committed Mar 4, 2020
1 parent fa72b79 commit 47e5238
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ export GRADLE_COMPLETION_UNQUALIFIED_TASKS="true"

You may need to invalidate the cache using the cache config above or by executing `touch build.gradle`.


#### Overriding default completion behoviour per project
If you need to customize this tool for one of your projects, just create `.gradle-completion` directory in your project root.
Then place there empty `gradle-completion.bash` (for bash) and/or `_gradle` (for zsh) and fill it with redefined original functions only.
Example content of `<path_to_your_gradle_project>/.gradle-completion/gradle-completion.bash`:

```bash
# Overrides default function that generates completion cache
__gradle-generate-tasks-cache() {
__gradle-set-files-checksum

"${project_root_dir}"/gradlew initCompletionCache --completionCachePath="$cache_dir/$gradle_files_checksum" --no-scan
echo "$gradle_files_checksum" >| "$cache_dir/$cache_name.md5"
}

```

But do not hesitate to make PR to this repo to support more generic features straight away.


## Troubleshooting
If zsh completion isn't working, first try checking your `$fpath` with `echo $fpath`.

Expand Down
15 changes: 13 additions & 2 deletions _gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ __gradle-generate-tasks-cache() {

__gradle-completion-init() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
__gradle-init-cache-dir

__gradle-set-project-root-dir
__gradle-source-project-overrides
__gradle-init-cache-dir
__gradle-set-build-file
if [[ -f $gradle_build_file ]]; then
__gradle-set-cache-name
Expand All @@ -146,7 +148,6 @@ __gradle_tasks() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir

__gradle-init-cache-dir
__gradle-set-project-root-dir
__gradle-set-build-file
if [[ -f $gradle_build_file ]]; then
__gradle-set-cache-name
Expand Down Expand Up @@ -312,6 +313,13 @@ _gradle_dependency_configurations() {
_describe -t 'dependency configurations' "dependency configuration" configurations
}

__gradle-source-project-overrides() {
if [[ -f "$project_root_dir/.gradle-completion/gradle-completion.bash" ]]; then
# override default behaviours of this script
source "$project_root_dir"/.gradle-completion/_gradle
fi
}

_gradle() {
local cur=${words[CURRENT]}
local curcontext="$curcontext" state
Expand Down Expand Up @@ -385,6 +393,9 @@ _gradle() {
{-x,--exclude-task}'[Specify a task to be excluded from execution.]' \
'(-)*:: :->task-or-option' && ret=0

__gradle-set-project-root-dir
__gradle-source-project-overrides

if [[ $words[CURRENT] != -* && $state != "argument-expected" ]]; then
__gradle_tasks && ret=0
else
Expand Down
14 changes: 12 additions & 2 deletions gradle-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ __gradle-tasks() {
_get_comp_words_by_ref -n : cur

__gradle-init-cache-dir
__gradle-set-project-root-dir
__gradle-set-build-file
if [[ -f "$gradle_build_file" ]]; then
__gradle-set-cache-name
Expand Down Expand Up @@ -310,14 +309,22 @@ __gradle-generate-tasks-cache() {
echo "$gradle_files_checksum" >| "$cache_dir/$cache_name.md5"
}

__gradle-source-project-overrides() {
if [[ -f "$project_root_dir/.gradle-completion/gradle-completion.bash" ]]; then
# override default behaviours of this script
source "$project_root_dir"/.gradle-completion/gradle-completion.bash
fi
}

__gradle-completion-init() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir

local OLDIFS="$IFS"
local IFS=$'\n'

__gradle-init-cache-dir
__gradle-set-project-root-dir
__gradle-source-project-overrides
__gradle-init-cache-dir
__gradle-set-build-file
if [[ -f "$gradle_build_file" ]]; then
__gradle-set-cache-name
Expand All @@ -342,6 +349,9 @@ _gradle() {
local OLDIFS="$IFS"
local IFS=$'\n'

__gradle-set-project-root-dir
__gradle-source-project-overrides

if [[ ${cur} == --* ]]; then
__gradle-long-options
elif [[ ${cur} == -D* ]]; then
Expand Down

0 comments on commit 47e5238

Please sign in to comment.