1#!/bin/bash
2
3BASE_URL="https://plugins.jetbrains.com"
4SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
5STUDIO_DIR=`ls -d $SCRIPT_PATH/../studio/*`
6STUDIO_BUILD=`cat "$STUDIO_DIR"/android-studio/product-info.json \
7  | jq -r '.productCode + "-" + .buildNumber'`
8
9if [[ -z "$1" ]] || [[ "$1" == "help" ]]; then
10  echo -n \
11"usage: plugins <command> [<args>]
12
13A CLI for JB's plugin marketplace that supports querying and installing plugins that support the current version of Studio.
14
15Commands:
16  help        	Display this help text
17  ls [<query>]	Query plugin marketplace by plugin name for plugin ids supporting the current version of Studio
18  install <id>	Download and install plugins by plugin id
19"
20elif [[ $1 == "ls" ]]; then
21  QUERY="$2"
22  curl -s "$BASE_URL/plugins/list?build=$STUDIO_BUILD"     \
23    | egrep -o "<name>[^<]+</name><id>[a-zA-Z0-9\.]+</id>" \
24    | sed 's/<id>/id: /g'                                  \
25    | sed 's/<\/id>//g'                                    \
26    | sed 's/<name>/name: /g'                              \
27    | sed 's/<\/name>/>/g'                                 \
28    | grep -i "$QUERY"                                     \
29    | column -t -s\>
30elif [[ $1 == "install" ]]; then
31  ID="$2"
32  wget "$BASE_URL/pluginManager?action=download&id=$ID&build=$STUDIO_BUILD" -O ~/.dustinlam_plugins_download \
33    && unzip -od "$STUDIO_DIR/android-studio/plugins" ~/.dustinlam_plugins_download
34elif [[ $1 == "help" ]]; then
35  echo "ls [query]"
36  echo "install [id]"
37fi
38