Skip to content

Instantly share code, notes, and snippets.

@xingheng
Last active July 13, 2021 11:01
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 xingheng/f7bc8d7d89a68a8f6ae42851b5aa0d0e to your computer and use it in GitHub Desktop.
Save xingheng/f7bc8d7d89a68a8f6ae42851b5aa0d0e to your computer and use it in GitHub Desktop.
A handy script to open the xcode workspace/project file easily.
#!/usr/bin/env bash
# set -x
#
# Source: https://gist.github.com/xingheng/f7bc8d7d89a68a8f6ae42851b5aa0d0e
# Author: Will Han
#
target_dir=${1:-$(pwd)}
test ! -d ${target_dir} && { echo "Invalid target directory for searching!"; exit 1; }
readonly skip_signal=1
readonly fail_signal=2
function open_xcode_project() {
local ret=0
local type_name=$1
shift
local target_files=("$@")
local target_file_count=${#target_files[@]}
# echo "$type_name files = "$target_files, $target_file_count
if [[ $target_file_count = 1 ]]; then
echo "Opening xcode $type_name file: "$target_files
exit $(open "${target_files[0]}")
elif [[ $target_file_count > 1 ]]; then
echo "Found "$target_file_count" xcode $type_name files, which one will you open?"
for (( i=0; i<$target_file_count; i++ ))
do
echo "$[i+1]: ${target_files[$i]}"
done
while IFS= read -r input
do
local num_reg="^[0-9]+$"
local quit_reg="q|Q"
if [[ $input =~ $num_reg ]]; then
if [[ $input -eq 0 ]]; then
echo "Skiping all the $type_name files"
ret=$skip_signal
break
elif [[ $input -le $target_file_count ]]; then
echo "Opening xcode $type_name file: "${target_files[$input-1]}
exit $(open "${target_files[$input-1]}")
else
echo "Invalid sequence number! Use [1..$target_file_count] instead."
fi
elif [[ $input =~ $quit_reg ]]; then
exit 0
else
echo "Invalid input, please use the corresponding file number before the colon to choose it or use 0 to skip all of them."
fi
done
else
ret=$skip_signal
fi
return $ret
}
function main() {
# Refer to https://stackoverflow.com/a/23357277/1677041
workspace_files=()
while IFS= read -r -d $'\0'; do
workspace_files+=("$REPLY")
done < <(find $target_dir -maxdepth 1 -type d -name "*.xcworkspace" -print0)
open_xcode_project "workspace" "${workspace_files[@]}"
if [[ $? -ne $skip_signal ]]; then
return $fail_signal
fi
project_files=()
while IFS= read -r -d $'\0'; do
project_files+=("$REPLY")
done < <(find $target_dir -maxdepth 1 -type d -name "*.xcodeproj" -print0)
open_xcode_project "project" "${project_files[@]}"
if [[ $? -ne $skip_signal ]]; then
return $fail_signal
fi
echo "No xcode project entry found in ${target_dir}, searching in its sub-directories..."
recursive_workspace_files=()
while IFS= read -r -d $'\0'; do
recursive_workspace_files+=("$REPLY")
done < <(find $target_dir -type d -name "*.xcworkspace" -print0)
open_xcode_project "workspace" "${recursive_workspace_files[@]}"
if [[ $? -ne $skip_signal ]]; then
return $fail_signal
fi
recursive_project_files=()
while IFS= read -r -d $'\0'; do
recursive_project_files+=("$REPLY")
done < <(find $target_dir -type d -name "*.xcodeproj" -print0)
open_xcode_project "project" "${recursive_project_files[@]}"
local ret=$?
if [[ $ret -ne 0 ]]; then
echo "Nothing found."
fi
return $ret
}
main
@xingheng
Copy link
Author

xingheng commented Jan 8, 2019

Just download (and rename) it to your local environment paths and make it executable.

curl -sLo- https://gist.githubusercontent.com/xingheng/f7bc8d7d89a68a8f6ae42851b5aa0d0e/raw/1d9ffb000eb0681788d696f78a592dfb89419edd/xcopen.sh > /usr/local/bin/xcopen
chmod +x /usr/local/bin/xcopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment