Help us understand the problem. What is going on with this article?

Gradleのtaskをpecoで選択する

More than 5 years have passed since last update.

Gradleを使ってAndroid開発しているとき、Gradleのtaskを選択することが多く、以前書いた内容でTAB補完は出来るようになった
Android - GradleをZshで補完する - Qiita

今回は、taskをTAB補完ではなくpecoで選択出来るようにした
gradle_comp.gif

手順

  1. カレントディレクトリに合ったgradleコマンドを探す
  2. gradle tasks --allを実行して文字列として取得
  3. 得られた文字列をTAB補完の場合はSpace区切りにする
  4. pecoで絞り込む場合は\n区切りに変換する
    • zshのフラグを用いてソートする
/usr/local/bin/_gradle
#!/usr/bin/env zsh

# 使用するgradleコマンド
function gradle_command() {
  local gradle_cmd='gradle'
  if [[ -x ./gradlew ]]; then
    gradle_cmd='./gradlew'
  fi
  if [[ -x ../gradlew ]]; then
    gradle_cmd='../gradlew'
  fi
  echo $gradle_cmd
}

# gradleのtask一覧
function gradle_tasks() {
  local gradle_cmd=$(gradle_command)
  local completions=''
  local cache_dir="$HOME/.gradle_tabcompletion"
  mkdir -p $cache_dir

  # cacheするファイル名
  local gradle_files_checksum='hoge';
  if [[ -f build.gradle ]]; then 
    if [[ -x `which md5 2> /dev/null` ]]; then # mac
      local all_gradle_files="$(find . -name build.gradle 2> /dev/null)"
      gradle_files_checksum="$(md5 -q -s "${all_gradle_files}")"
    else # linux
      gradle_files_checksum="$(find . -name build.gradle | xargs md5sum | md5sum)"
    fi
  else
    gradle_files_checksum='no_gradle_files'
  fi

  if [[ -f $cache_dir/$gradle_files_checksum ]]; then # cached! yay!
    completions=$(\cat $cache_dir/$gradle_files_checksum)
  else
    # ジョブ制御を無効化
    set +m
    # バックグラウンドでindicatorを回す
    loading 1000 & >/dev/null 2>&1
    set -m
    # indicatorのprocess id
    local LOADING_PID=$!
    completions=$($gradle_cmd --no-color --quiet tasks --all | grep --color=none ' - ' | awk '{print $1}' | tr '\n' ' ')
    # indicatorを殺す
    kill -INT $LOADING_PID
    if [[ ! -z $completions ]]; then
      echo $completions > $cache_dir/$gradle_files_checksum
    fi
  fi
  echo $completions
}

# pecoでtask選択
function peco-select-gradle-tasks() {
  local gradle_cmd=$(gradle_command)
  local completions=$(gradle_tasks)
  local selected_task=$(echo ${(o)${(z)completions}} | tr ' ' '\n' | peco)
  if [ -n "$selected_task" ]; then
    BUFFER="$gradle_cmd $selected_task"
    zle accept-line
  fi
  zle clear-screen
}

zle -N peco-select-gradle-tasks
bindkey "^g^t" peco-select-gradle-tasks

# gradleのtab補完
function _gradle() {
  local cur="$1"
  local completions=$(gradle_tasks)
  local -a tasks
  tasks=("${(z)completions}")
  compadd $tasks
  return 0;
}

compdef _gradle gradle
compdef _gradle gradlew
compdef _gradle ./gradlew

これをsource /path/to/scriptすれば使えるようになる
^g^tでgradleをpecoで選べるようになったのでめっちゃ便利

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした