.bash_completion.d/gradle
author Oleksandr Gavenko <gavenkoa@gmail.com>
Mon, 13 Jan 2020 14:00:42 +0200
changeset 936 7c8a97e5273d
parent 933 8df13eb76a24
child 938 9e7576e732d6
permissions -rw-r--r--
Added completion for --console Gradle option.

# -*- mode: sh; sh-shell-file: bash -*-

_gradle_cmd()
{
  for ((i = 1; i < $COMP_CWORD; i += 1)); do
    case "${COMP_WORDS[i]}" in
      -*) continue ;;
      *" "*) continue ;;
    esac
    echo "${COMP_WORDS[i]}"
    return
  done
}

_gradle()
{
  local OIFS=$IFS
  local _opts="-? -h --help -A --dep-tasks -C --cache -D --system-prop -I --init-script -P --project-prop -S --full-stacktrace -a --no-rebuild --all \
     -b --build-file -c --settings-file -d --debug -e --embedded -g --gradle-user-home --gui -i --info -m --dry-run --no-opt -p --project-dir \
     -q --quiet -r --properties -s --stacktrace -t --tasks -u --no-search-upward -v --version -x --exclude-task \
     --continue --foreground --no-color --project-cache-dir --include-build --recompile-scripts --refresh --refresh-dependencies \
     --fail-fast \
     --console \
     --daemon --no-daemon --offline --build-cache --parallel --parallel-threads --profile --status \
     --rerun-tasks --stop"
  local _application_cmds='run startScripts installApp distZip distTar'
  declare -a _extra
  local cur prev
  cur=${COMP_WORDS[COMP_CWORD]}
  [[ $COMP_CWORD -gt 1 ]] && prev=${COMP_WORDS[COMP_CWORD-1]}

  local _tasks_full='help - Displays a help message.
wrapper - Create ./gradlew.
assemble - Assembles the outputs of this project.
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
compileJava - Compile src/main/java.
compileTestJava - Compile src/test/java.
processResources - Process src/main/resources.
processTestResources - Process src/test/resources.
uploadArchives - Deploy build to repository.
clean - Deletes the build directory.
cleanTest - Delete tests.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
buildEnvironment - Displays all buildscript dependencies.
components - Displays the components.
dependencies - Displays all dependencies.
dependencyInsight - Displays the insight into a specific dependency.
model - Displays the configuration model.
projects - Displays the sub-projects.
properties - Displays the properties.
tasks - Displays the tasks runnable from project.
cleanIdea - Cleans IDEA project files (IML, IPR)
idea - Generates IDEA project files (IML, IPR, IWS)
check - Runs all checks.
test - Runs the unit tests.
init - Generate basic Gradle project files.
install - Installs the archives artifacts into the local Maven repository.
javadoc - Generates Javadoc API documentation for the main source code.
bootRun - Run the project with support for auto-detecting main class and reloading static resources
flywayBaseline - Baselines an existing database, excluding all migrations up to and including baselineVersion.
flywayClean - Drops all objects in the configured schemas.
flywayInfo - Prints the details and status information about all the migrations.
flywayMigrate - Migrates the schema to the latest version.
flywayRepair - Repairs the Flyway metadata table.
flywayValidate - Validate applied migrations against resolved ones'
  IFS=$'\n'
  local _tasks=( $_tasks_full )
  IFS=$' \n'
  _tasks=( ${_tasks[@]%% -*} )

  if [[ $COMP_CWORD = 1 ]] && [[ "$cur" != -* ]]; then
      IFS=$'\n'
      COMPREPLY=( $(compgen -W '${_tasks_full}' -- "$cur") )
      if [[ ${#COMPREPLY[@]} -le 1 ]]; then
          COMPREPLY=( ${COMPREPLY[0]%% *-*} )
      fi
      IFS=$OIFS; return
  fi

  # Perform default option completion for command.
  if [[ -z "$curr" ]]; then
      case "$prev" in
        help)
          COMPREPLY=( --task )
          IFS=$OIFS; return ;;
        init)
          COMPREPLY=( --type )
          IFS=$OIFS; return ;;
        wrapper)
          COMPREPLY=( --gradle-version )
          IFS=$OIFS; return ;;
        dependencies|dependencyInsight)
          COMPREPLY=( --configuration )
          IFS=$OIFS; return ;;
        --console)
          COMPREPLY=( $(compgen -W "auto plain rich verbose" -- "$cur") )
          IFS=$OIFS; return ;;
      esac
  fi

  # https://docs.gradle.org/current/userguide/java_library_plugin.html
  # configurations.each { println it.name + " - " + it.description }
  local _configs="annotationProcessor     - Annotation processors and their dependencies for source set 'main'.
api                     - API dependencies for source set 'main'.
apiElements             - API elements for main.
archives                - Configuration for archive artifacts.
bootArchives            - Configuration for Spring Boot archive artifacts.
compile                 - Dependencies for source set 'main' (deprecated, use 'implementation' instead).
compileClasspath        - Compile classpath for source set 'main'.
compileOnly             - Compile only dependencies for source set 'main'.
default                 - Configuration for default artifacts.
implementation          - Implementation only dependencies for source set 'main'.
runtime                 - Runtime dependencies for source set 'main' (deprecated, use 'runtimeOnly' instead).
runtimeClasspath        - Runtime classpath of source set 'main'.
runtimeElements         - Elements of runtime for main.
runtimeOnly             - Runtime only dependencies for source set 'main'.
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
testCompile             - Dependencies for source set 'test' (deprecated, use 'testImplementation' instead).
testCompileClasspath    - Compile classpath for source set 'test'.
testCompileOnly         - Compile only dependencies for source set 'test'.
testImplementation      - Implementation only dependencies for source set 'test'.
testRuntime             - Runtime dependencies for source set 'test' (deprecated, use 'testRuntimeOnly' instead).
testRuntimeClasspath    - Runtime classpath of source set 'test'.
testRuntimeOnly         - Runtime only dependencies for source set 'test'."

  # Add or perform command specific completion.
  local _cmd=$(_gradle_cmd)
  case "$_cmd" in
    help)
      case "$prev" in
        --task)
          COMPREPLY=( $(compgen -W '${_tasks[@]} $_application_cmds'  -- $cur) )
          IFS=$OIFS; return ;;
      esac
      _extra+=( --task )
      ;;
    init)
      case "$prev" in
        --type)
          COMPREPLY=( $(compgen -W "basic groovy-library java-library pom scala-library java-application groovy-application" -- "$cur") )
          IFS=$OIFS; return ;;
        --test-framework)
          COMPREPLY=( $(compgen -W "spock testng" -- "$cur") )
          IFS=$OIFS; return ;;
      esac
      _extra+=( --type --test-framework )
      ;;
    wrapper)
      _extra+=( --gradle-version )
      ;;
    dependencies)
      case "$prev" in
        --configuration)
          IFS=$'\n'
          COMPREPLY=( $(compgen -W '${_configs}' -- "$cur") )
          if [[ ${#COMPREPLY[@]} -le 1 ]]; then
              COMPREPLY=( ${COMPREPLY[0]%% *-*} )
          fi
          IFS=$OIFS; return ;;
      esac
      _extra+=( --configuration )
      ;;
    dependencyInsight)
      case "$prev" in
        --configuration)
          IFS=$'\n'
          COMPREPLY=( $(compgen -W '${_configs}' -- "$cur") )
          if [[ ${#COMPREPLY[@]} -le 1 ]]; then
              COMPREPLY=( ${COMPREPLY[0]%% *-*} )
          fi
          IFS=$OIFS; return ;;
      esac
      _extra+=( --configuration --dependency )
      ;;
  esac

  # Perform generic completion.
  case "$prev" in
    -I|--init-script|-b|--build-file|-c|--settings-file|-e|--embedded)
      COMPREPLY=( $(compgen -f -- "$cur") )
      IFS=$OIFS; return ;;
    -g|--gradle-user-home|-p|--project-dir|--project-cache-dir|--include-build)
      COMPREPLY=( $(compgen -d -- "$cur") )
      IFS=$OIFS; return ;;
  esac
  local _repl=( ${_extra[@]} ${_tasks[@]} $_application_cmds )
  COMPREPLY=( $(compgen -W '${_repl[@]} $_opts' -- $cur) )
  compopt +o nospace
  IFS=$OIFS
} &&
    complete -F _gradle gradle ./gradlew ../gradlew