├── LICENSE ├── README.md └── bin └── xcopen /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Kohki Miki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xcode-opener 2 | 3 | Open Xcode project by context. 4 | 5 | ```console 6 | $ xcopen 7 | ``` 8 | 9 | ## Available Commands 10 | 11 | ```console 12 | $ xcopen 13 | $ xcopen -v 10.3 14 | $ xcopen list 15 | $ xcopen help 16 | $ xcopen version 17 | ``` 18 | 19 | ## Installation 20 | 21 | ### zplug 22 | 23 | ```bash 24 | zplug "giginet/xcode-opener", lazy:true, use:'bin/xcopen', as:command 25 | ``` 26 | -------------------------------------------------------------------------------- /bin/xcopen: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | version() { 4 | echo "xcode-opener 0.3.0 by giginet " 5 | echo "https://github.com/giginet/xcode-opener" 6 | } 7 | 8 | usage() { 9 | version 10 | cat << EOF >&2 11 | Xcode Opener 12 | Open Xcode project by context 13 | 14 | Usage: 15 | $(basename $0) [command] [] 16 | Commands: 17 | open [-v version] Open Xcode projects by context with specific Xcode version 18 | list List all available Xcodes 19 | help Display usage 20 | version Display current script version 21 | EOF 22 | } 23 | 24 | _find() { 25 | echo $(find . -maxdepth 1 -name "$1" -print -quit) | tail -n 1 26 | } 27 | 28 | _parseXcodeVersion() { 29 | PLISTBUDDY_PATH=/usr/libexec/PlistBuddy 30 | XCODE_VERSION=$($PLISTBUDDY_PATH -c "print CFBundleShortVersionString" "$1/Contents/version.plist") 31 | echo $XCODE_VERSION 32 | } 33 | 34 | _allXcodePaths() { 35 | echo $(mdfind "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'") 36 | } 37 | 38 | _findXcode() { 39 | all_xcode_versions=$(_allXcodePaths) 40 | for xcode_path in $all_xcode_versions 41 | do 42 | version=$(_parseXcodeVersion $xcode_path) 43 | if [ $version == $1 ]; then 44 | echo $xcode_path 45 | break 46 | fi 47 | done 48 | } 49 | 50 | open_xcode() { 51 | if [ -n "$1" ] && [ $1 == "-v" ]; then 52 | version=$2 53 | fi 54 | if [ -n "$version" ]; then 55 | xcode_path=$(_findXcode $version) 56 | if [ -z $xcode_path ]; then 57 | printf "\e[31mCould not found Xcode ${version}\e[m\n" 58 | fi 59 | else 60 | xcode_path=$(xcode-select -p | sed -e "s/\/Contents\/Developer//") 61 | fi 62 | xcode_path=${xcode_path:-"/Applications/Xcode.app"} 63 | if [ -z "$xcode_path" ] || [ ! -e "$xcode_path" ]; then 64 | printf "\e[31mCould not found any Xcode. Please install from Mac App Store.\e[m\n" 65 | exit 1 66 | fi 67 | 68 | version=$(_parseXcodeVersion "$xcode_path") 69 | 70 | echo "Using Xcode ${version} on ${xcode_path}" 71 | 72 | if [ -e "Package.swift" ]; then 73 | open -a "$xcode_path" ./ 74 | exit 0 75 | elif [ -n "$(_find "*.xcworkspace")" ]; then 76 | open -a "$xcode_path" *.xcworkspace 77 | exit 0 78 | elif [ -n "$(_find "*.xcodeproj")" ]; then 79 | open -a "$xcode_path" *.xcodeproj 80 | exit 0 81 | else 82 | printf "\e[31mCould not found any Xcode projects. The current directory must contain Package.swift, *.xcworkspace or *.xcodeproj\e[m\n" 83 | exit 1 84 | fi 85 | } 86 | 87 | list_xcode() { 88 | paths=$(_allXcodePaths) 89 | versions= 90 | for path in $paths 91 | do 92 | version=$(_parseXcodeVersion $path) 93 | versions="${versions} ${version}" 94 | done 95 | echo $versions | tr ' ' '\n' | uniq | sort 96 | } 97 | 98 | subcommand=$1 99 | subcommand=${subcommand:-"open"} 100 | 101 | case $subcommand in 102 | "-v" ) 103 | open_xcode -v $2 104 | exit 0 105 | ;; 106 | "open" ) 107 | open_xcode $2 $3 108 | exit 0 109 | ;; 110 | "list" ) 111 | list_xcode 112 | exit 0 113 | ;; 114 | "help" ) 115 | usage 116 | exit 0 117 | ;; 118 | "version" ) 119 | version 120 | exit 0 121 | ;; 122 | * ) 123 | echo "Unknown command $subcommand" 124 | esac 125 | --------------------------------------------------------------------------------