Skip to content

Instantly share code, notes, and snippets.

@toddmcbrearty
Last active September 26, 2022 16:18
Show Gist options
  • Save toddmcbrearty/82e86ee9d250aafe21e868938732f55c to your computer and use it in GitHub Desktop.
Save toddmcbrearty/82e86ee9d250aafe21e868938732f55c to your computer and use it in GitHub Desktop.
Open phpstorm projects
#!/bin/bash
green_text () {
nc='\033[0m' # No Color
green='\033[0;32m' # Green
echo -e "${green}$1${nc}"
}
red_text () {
nc='\033[0m' # No Color
green='\033[0;31m' # Red
echo -e "${green}$1${nc}"
}
phpstorm_script=$(which phpstorm)
if [ -z "$phpstorm_script" ]
then
red_text "This script depends on PhpStorm CLI script."
red_text "It does not appear to be installed."
red_text "Please install before continuing."
echo "https://www.jetbrains.com/help/phpstorm/working-with-the-ide-features-from-command-line.html#5d6265da"
exit 1
fi
project_name="$1"
jetbrains_dir="$HOME/.jetbrains_scripts"
varfile="$jetbrains_dir/vars"
# if the .jetbrain_scripts directory does not exist create it
if [ ! -d "$jetbrains_dir" ]
then
green_text "Creating directory $jetbrains_dir"
mkdir -p "$jetbrains_dir"
fi
# if the var file doesn't exist we'll need to create it
# ask the user what their code directory path is and store that in vars
if [ ! -f "$varfile" ]
then
red_text "Unable to locate \"$varfile\". We'll need to create it."
message="${green}Enter your code directory path:${nc} "
read -p "$message" code_path
touch "$jetbrains_dir/vars"
echo "JETBRAINS_PROJECT_CODE_DIR=$code_path" >> "$varfile"
fi
# source the varfile so our newly created vars are loaded
source $varfile
# launch the selected project
launch_project () {
filepath=$(echo "$1" | cut -d "." -f1)
filepath=$(echo "${filepath%/}")
green_text "phpstorm $filepath"
phpstorm $filepath
exit 0
}
# handle each to get the projects names and determine if it should be lauched
handle_projects () {
dapath=$(echo "$1" | rev | cut -d" " -f1 | rev)
files=$(find "$dapath" -name \*.iml)
for file in "$files"
do
filename=($(echo $(basename "$file" | cut -d"." -f1)))
if [ "$filename" = "$2" ]
then
launch_project "$file"
fi
filenames+=("$filename")
done
}
# search the code directory for all phpstorm projects
IFS=$'\n'
results=($(find "$JETBRAINS_PROJECT_CODE_DIR" -type d -name ".idea" -ls -maxdepth 2 -print0))
filenames=()
files=()
# loop through all the projects and create their filesnames to be used below
# unless a project name was supplied (arg 1) we'll see if it exists and open it
for i in ${results[@]}
do
handle_projects "$i" "$project_name"
done
# list all the projects and open the one the user selects
filenames_sorted=($(echo ${filenames[*]}| tr " " "\n" | sort))
green_text "Please choose a project: "
PS3=">"
select item in "${filenames_sorted[@]}"
do
newfile=$(echo "$item" | cut -d "." -f1)
for i in ${results[@]}
do
handle_projects "$i" "$newfile"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment