#!/bin/bash

BASE_URL="https://plugins.jetbrains.com"
SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
STUDIO_DIR=`ls -d $SCRIPT_PATH/../studio/*`
STUDIO_BUILD=`cat "$STUDIO_DIR"/android-studio/product-info.json \
  | jq -r '.productCode + "-" + .buildNumber'`

if [[ -z "$1" ]] || [[ "$1" == "help" ]]; then
  echo -n \
"usage: plugins <command> [<args>]

A CLI for JB's plugin marketplace that supports querying and installing plugins that support the current version of Studio.

Commands:
  help        	Display this help text
  ls [<query>]	Query plugin marketplace by plugin name for plugin ids supporting the current version of Studio
  install <id>	Download and install plugins by plugin id
"
elif [[ $1 == "ls" ]]; then
  QUERY="$2"
  curl -s "$BASE_URL/plugins/list?build=$STUDIO_BUILD"     \
    | egrep -o "<name>[^<]+</name><id>[a-zA-Z0-9\.]+</id>" \
    | sed 's/<id>/id: /g'                                  \
    | sed 's/<\/id>//g'                                    \
    | sed 's/<name>/name: /g'                              \
    | sed 's/<\/name>/>/g'                                 \
    | grep -i "$QUERY"                                     \
    | column -t -s\>
elif [[ $1 == "install" ]]; then
  ID="$2"
  wget "$BASE_URL/pluginManager?action=download&id=$ID&build=$STUDIO_BUILD" -O ~/.dustinlam_plugins_download \
    && unzip -od "$STUDIO_DIR/android-studio/plugins" ~/.dustinlam_plugins_download
elif [[ $1 == "help" ]]; then
  echo "ls [query]"
  echo "install [id]"
fi

Add a bash-based cli for plugin marketplace
Add a bash-based cli for plugin marketplace

Adds a CLI script for interacting with the plugin marketplace to make it
easier to restore dev environment on studio version changes, which would
normally nuke the plugin directory.

Help text is available via `./development/plugins help`.

Test: ./development/plugins
Change-Id: I9f1633c87313d4268119257c66f9f3c62a5c9bb4
