12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gradleのtaskをpecoで選択する

Last updated at Posted at 2015-05-24

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で選べるようになったのでめっちゃ便利

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?