Skip to content

Instantly share code, notes, and snippets.

@j4ckofalltrades
Last active May 20, 2023 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j4ckofalltrades/d7aac303466746e67287441e4fb9e0fe to your computer and use it in GitHub Desktop.
Save j4ckofalltrades/d7aac303466746e67287441e4fb9e0fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
ide_script_name="ide_script.groovy"
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-f] -i ide -a action
Shell script wrapper that executes actions on JetBrains IDEs via the scripting console.
Available options:
-h, --help Print this help and exit
-i, --ide JetBrains IDE command-line launcher e.g. idea, webstorm, pycharm
-a, --action IDE action to execute
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
rm -f "$script_dir/$ide_script_name"
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
ide=''
action=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-f | --flag) flag=1 ;;
-i | --ide)
ide="${2-}"
shift
;;
-a | --action)
action="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${ide-}" ]] && die "Missing required parameter: ide"
[[ -z "${action-}" ]] && die "Missing required parameter: action"
return 0
}
# Invoke supported IDE actions using the IDE scripting console. See links for more details.
#
# Supported IDE actions: https://github.com/JetBrains/intellij-community/blob/idea/231.8109.175/platform/ide-core/src/com/intellij/openapi/actionSystem/IdeActions.java
# IDE scripting console: https://www.jetbrains.com/help/idea/ide-scripting-console.html
generate_ide_script() {
cat <<EOF
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.ui.Messages
var actionManager = ActionManager.getInstance()
IDE.application.invokeLater {
try {
var action = actionManager.getAction("$action")
var result = actionManager.tryToExecute(action, null, null, null, false)
if (result.rejected) {
Messages.showErrorDialog(result.error, "IDE action error")
}
} catch (ex) {
Messages.showErrorDialog(ex.message, "IDE action error")
}
}
EOF
}
parse_params "$@"
setup_colors
# Generate IDE script and execute the given action
ide_script="$script_dir/$ide_script_name"
generate_ide_script > "$ide_script"
$ide ideScript "$ide_script"
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment