Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override default behaviours in projects #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 15 additions & 3 deletions _gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,19 @@ __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" ]]; then
# override default behaviours of this script
source "$project_root_dir"/.gradle-completion/_gradle
fi
}

__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 @@ -143,10 +152,9 @@ __gradle-completion-init() {
}

__gradle_tasks() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
local cache_dir cache_name gradle_build_file gradle_files_checksum

__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 @@ -313,6 +321,7 @@ _gradle_dependency_configurations() {
}

_gradle() {
local project_root_dir
local cur=${words[CURRENT]}
local curcontext="$curcontext" state
integer ret=1
Expand Down Expand Up @@ -385,6 +394,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