Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg
Created July 22, 2017 03:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZevEisenberg/af35212e277ead3a97d85a64cec78242 to your computer and use it in GitHub Desktop.
Save ZevEisenberg/af35212e277ead3a97d85a64cec78242 to your computer and use it in GitHub Desktop.
Utility functions for doing iOS and Mac development
# cd to the folder containing an Xcode project dragged from an Xcode window's proxy icon. If no file is provided, cd to the folder containing the current Xcode project
function xc
{
xcodeIsRunning=false
if [[ `osascript -e 'tell app "System Events" to count processes whose name is "Xcode"'` == 1 ]]; then
xcodeIsRunning=true
fi
if [[ $xcodeIsRunning == false ]]; then
echo "Xcode is not open. I don’t know what you want from me."
return
fi
if [[ -z $1 ]]; then
filePath="`osascript -e 'tell application "Xcode" to return POSIX path of (get file of front document)'`"
# TODO: suppress the error here when there are no open documents
if [[ -z $filePath ]]; then
echo "Xcode has no open documents."
return
fi
cd "$filePath"
returnSilentlyIfNotGitRepo
groot
else
filePath="$@"
cd "$filePath"/..
fi
}
function xc832
{
sudo xcode-select -s /Applications/Xcode_8.3.2.app && echo "Switched to `xcode-select -p`."
}
function xc833
{
sudo xcode-select -s /Applications/Xcode_8.3.3.app && echo "Switched to `xcode-select -p`."
}
function xcbeta
{
sudo xcode-select -s /Applications/Xcode-beta.app && echo "Switched to `xcode-select -p`."
}
# used by ox function
function fuzzify
{
string=$1
fuzzyString=""
separator=$2
if [[ -z $2 ]]; then
separator=".*"
fi
while test -n "$string"; do
c=${string:0:1}
fuzzyString="$fuzzyString$c$separator"
string=${string:1}
done
echo $fuzzyString
}
# Opens the Xcode project or workspace in the current folder.
# If you pass a fuzzy path, it will open that instead.
# Example: ox apl/util/myap will open Application/Utility/MyApp.xcodeproj (if it exists). Useful for multi-project repos.
function ox
{
xcodePath=`xcode-select -print-path`/../..
if [[ ! -e $xcodePath ]]; then
echo "Whoa there pardner, you need to install Xcode first."
return
fi
if [[ -z $1 ]]; then
open -a $xcodePath . 2>&1 > /dev/null
else
if [[ -e $1 ]]; then
open -a $xcodePath $1 2>&1 > /dev/null
else
fuzzyTerm=`fuzzify $1 '*'`
searchPattern1="*$fuzzyTerm.*xcodeproj"
searchPattern2="*$fuzzyTerm.*xcworkspace"
fileName=`find . -iname "$searchPattern1" -o -iname "$searchPattern2"`
fileName=$(echo $fileName | head -n 1)
open -a $xcodePath $fileName 2>&1 > /dev/null
fi
fi
}
# Does what it says on the tin.
function eviscerateDerivedData
{
local derivedDataPath="$HOME/Library/Developer/Xcode/DerivedData"
rm -rf "$derivedDataPath"
echo "Deleted $derivedDataPath"
}
function purgeZeroLengthFilesInDerivedData
{
for FILE in `find ~/Library/Developer/Xcode/DerivedData -name '*.o' -size 0`
do
if [[ -f "$FILE" ]]; then
echo "deleting $FILE"
rm "$FILE"
fi
done
}
# Copies the current Safari and macOS version and build number to the clipboard. Useful for bug reporting.
function copySafariVersion
{
local safariVersion=$(defaults read /Applications/Safari.app/Contents/Info CFBundleShortVersionString)
local safariBuild=$(defaults read /Applications/Safari.app/Contents/Info CFBundleVersion)
local macOSVersion=$(sw_vers -productVersion)
local macOSBuild=$(sw_vers -buildVersion)
local fullString="Safari ${safariVersion} (${safariBuild}) on macOS ${macOSVersion} (${macOSBuild})"
echo "Copied \"$fullString\""
echo -n $fullString | pbcopy
}
# Copies the current Chrome and macOS version and build number to the clipboard. Useful for bug reporting.
function copyChromeVersion
{
local chromeVersion=$(defaults read /Applications/Google\ Chrome.app/Contents/Info CFBundleShortVersionString)
local chromeBuild=$(defaults read /Applications/Google\ Chrome.app/Contents/Info CFBundleVersion)
local macOSVersion=$(sw_vers -productVersion)
local macOSBuild=$(sw_vers -buildVersion)
local fullString="Chrome ${chromeVersion} (${chromeBuild}) on macOS ${macOSVersion} (${macOSBuild})"
echo "Copied \"$fullString\""
echo -n $fullString | pbcopy
}
# Copies the current Chrome and macOS version and build number to the clipboard. Useful for bug reporting.
function copyXcodeVersion
{
local xcodePath=`xcode-select -p | rev | cut -d'/' -f3- | rev`
local xcodeVersion=$(defaults read "${xcodePath}"/Contents/Info CFBundleShortVersionString)
local xcodeBuild=$(defaults read "${xcodePath}"/Contents/Developer/Library/Frameworks/XcodeKit.framework/Versions/A/Resources/version ProductBuildVersion)
local macOSVersion=$(sw_vers -productVersion)
local macOSBuild=$(sw_vers -buildVersion)
local fullString="Xcode ${xcodeVersion} (${xcodeBuild}) on macOS ${macOSVersion} (${macOSBuild})"
echo "Copied \"$fullString\""
echo -n $fullString | pbcopy
}
# iOS Development
# Erases all simulators to their original state. Equivalent to choosing "Erase All Content and Settings" for each simulator.
function eraseAllSimulators
{
osascript -e 'tell application "iOS Simulator" to quit'
osascript -e 'tell application "Simulator" to quit'
xcrun simctl erase all
}
# Erase all simulators and then open Xcode from the current directory
function eox
{
eraseAllSimulators && ox $@
}
# Use Fastlane's Snapshot utility erase all simulators, then reinstate the default set for the currently active Xcode.
# If you like any extra simluators that are not normally active, look at the final commented line for a template you can use to active them.
function resetAllSimulators
{
if [[ $(bundle check > /dev/null) == 0 ]]; then
$BUNDLER_PREFIX="bundle exec"
fi
osascript -e 'tell application "iOS Simulator" to quit'
osascript -e 'tell application "Simulator" to quit'
# SNAPSHOT_FORCE_DELETE via https://github.com/fastlane/fastlane/issues/6927#issuecomment-259494121
SNAPSHOT_FORCE_DELETE=1 ${BUNDLER_PREFIX} fastlane snapshot reset_simulators
# xcrun simctl create "iPhone 4s" com.apple.CoreSimulator.SimDeviceType.iPhone-4s com.apple.CoreSimulator.SimRuntime.iOS-9-3
}
# Reset all simulators and then open Xcode from the current directory.
function rox
{
resetAllSimulators && ox $@
}
# Convenience command to make sure I don't check in xibs/storyboards with misplaced constraints.
# Should really be a commit hook or danger.tools script, but since I don't actually use IB any more, it's not worth improving it.
function misplaced
{
# Requires the_silver_searcher to be installed.
ag misplaced
}
# Record video of the iOS simulator.
# Ctrl-C ends recording and dumps the resulting video to the desktop with a similar file name format as a normal screenshot.
function iOSVideo
{
local dateString=`date +"%Y-%m-%d at %I.%M.%S %p"`
local fileName="iOS Simulator Video $dateString.mp4"
local filePath="$HOME/Desktop/$fileName"
xcrun simctl io booted recordVideo $filePath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment