Skip to content

Instantly share code, notes, and snippets.

@henter
Forked from chrisdarroch/idea
Created November 29, 2018 06:05
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 henter/975fcb6819aa55227d1516b995bb57aa to your computer and use it in GitHub Desktop.
Save henter/975fcb6819aa55227d1516b995bb57aa to your computer and use it in GitHub Desktop.
Open a project in IntelliJ IDEA from your command line!
#!/usr/bin/env bash
IDEA=''
PROJECT_DIR=''
function main() {
openIdea "$@"
}
function openIdea() {
if [ -f "$1" ]; then
openByFile $1
else
openWithDirectory "$1"
fi
}
function openByFile() {
if [[ "$1" =~ '.php' ]]; then
IDEA=$(getPhpStormApp)
fi
if [[ "$1" =~ '.js' ]] || [[ "$1" =~ '.css' ]]; then
IDEA=$(getWebStormApp)
fi
echo "open $1 with $IDEA"
open -a "$IDEA" "$1"
}
function openWithDirectory() {
PROJECT_DIR=$(getProjectDirectory)
detectProjectIdeaApplication
if [ -d "${PROJECT_DIR}/.idea" ]; then
echo "opening via the .idea dir"
open -a "$IDEA" ${PROJECT_DIR}
elif [ -f "${PROJECT_DIR}/*.ipr" ]; then
echo "opening via the project file"
open -a "$IDEA" $(ls -1d *.ipr | head -n1)
elif [ -f "${PROJECT_DIR}/pom.xml" ]; then
open -a "$IDEA" "${PROJECT_DIR}/pom.xml"
else
open -a "$IDEA" "${PROJECT_DIR}"
fi
}
function getProjectDirectory() {
if [ -d "$1" ]; then
ls -1d "$1" | head -n1
fi
if [ -z "$1" ]; then
pwd
fi
}
function detectProjectIdeaApplication() {
if [ -f "${PROJECT_DIR}/composer.json" ]; then
IDEA=$(getPhpStormApp)
return 0
fi
if [ -f "${PROJECT_DIR}/package.json" ]; then
IDEA=$(getWebStormApp)
return 0
fi
IDEA=$(getIntelliJ)
}
function getIntelliJ() {
ls -1d /Applications/IntelliJ\ * | tail -n1
}
function getPhpStormApp() {
ls -1d /Applications/PhpStorm* | tail -n1
}
function getWebStormApp() {
ls -1d /Applications/WebStorm* | tail -n1
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment