Skip to content

Instantly share code, notes, and snippets.

@leviathan
Last active November 1, 2024 03:31
Show Gist options
  • Save leviathan/0c806022cd83d0a51a15c92b6b53db49 to your computer and use it in GitHub Desktop.
Save leviathan/0c806022cd83d0a51a15c92b6b53db49 to your computer and use it in GitHub Desktop.
xcrun cheat sheet

Environment Info

xcode-select --print-path
xcodebuild -version
swift --version
swift build --version
pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep version   // Check Xcode Command-Line-Tools Version

MacOSX Xcode

Xcode tips

Measuring Xcode build time

Xcode build report

We can get the build time easily via Xcode. It keeps track of all your builds by default and you can examine the times and logs from the Report Navigator.

build-report

Duration appears in the activity viewer after a build, alongside the "Succeeded" message. If you are running the app, the status will be replaced by the running status before you can see the duration.

$> defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES

The builds duration appears after a build, alongside with the “Succeeded” message.

activity-viewer

Xcode build timing summary

Run Xcode Build Timing Summary to get build time insights and find bottlenecks - run it via: Product => Perform Action => Build With Timing Summary.

timing-summary

Swift Package Manager

  1. ~/Library/Caches/org.swift.swiftpm/repositories => Delete package folder and lock file
  2. in Xcode, run File -> Swift Packages -> Reset Package Caches

xcode-select

version

foo@bar:~$ xcodebuild -version
Xcode 10.1
Build version 10B61

Check the currently selected directory.

foo@bar:~$ xcode-select -p    
/Applications/Xcode_10.1.app/Contents/Developer

Select Command line tools:

$ xcode-select -s/--switch /Library/Developer/CommandLineTools/

Re-Install Command line tools => This will download and install the lastest CLT version from Apple and install it.

$> sudo rm -rf /Library/Developer/CommandLineTools
$> xcode-select --install

=> If older CLT version is needed, then uninstall current version & download PKG from Apple Downloads

xcrun

Get help

$ xcrun simctl help
usage: simctl [--set <path>] [--profiles <path>] <subcommand> ...
       simctl help [subcommand]
Command line utility to control the Simulator

Run the list subcommand to get a list of the available runtimes, device types, devices, and device pairs:

$ xcrun simctl list
-- iOS 12.1 --
    iPhone 5s (CC96B643-067E-41D5-B497-1AFD7B3D0A13) (Shutdown)
    iPhone 6 (7A27C0B9-411A-4BCD-8A67-68F00DF39C1A) (Shutdown)
    iPhone 6 Plus (A5918644-8D46-4C67-B21E-68EA25997F91) (Shutdown)
    iPhone 6s (1AB5A4EB-2434-42E4-9D2C-42E479CE8BDC) (Shutdown)
…

One such subcommand is boot, which starts up the specified device, making it available for interaction:

$ xcrun simctl boot $UUID
$ xcrun simctl shutdown $UUID
$ xcrun simctl erase $UUID

// Removes old simulators, which xcode no longer uses

$ xcrun simctl delete unavailable

Opening URLs in Simulator

// xcrun allows you to invoke developer tools from the terminal. // In this case simctl, which is a command line utility to control the iOS Simulator. // simctl has the subcommand: openurl to open URLs in a given simulator // most of the cases you would like to open on the current booted device, so you add // the flag booted or type the simulator id.

$ xcrun simctl openurl booted "http://google.com"

// open app url
$ xcrun simctl openurl booted "ios-cobi://bike.cobi/app?state=w4BZrykXl7..."

// Print the UUID of the device in your simulator

$ xcrun simctl list | egrep '(Booted)'
iPhone X (9FED67A2-3D0A-4C9C-88AC-28A9CCA44C60) (Booted)

Get the list in structured format:

$ xcrun simctl list --json

Reference: https://nshipster.com/simctl/

Custom toolchain from the command line

When using xcodebuild or ie. Fastlane to build your project, it will use the default swift toolchain (the one from the currently selected Xcode).

You can check currently selected toolchain with this command:

$> swift --version
  Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
  Target: x86_64-apple-darwin19.3.0

To use the alternative toolchain you can run xcrun and xcodebuild with a -toolchain flag like this:

xcodebuild -toolchain NAME
xcrun --toolchain NAME

where NAME is either an identifier or name of the toolchain, ie. xcodebuild --toolchain swift.

You could also export it to your current terminal session with a command:

$> export TOOLCHAINS=swift
$> export TOOLCHAINS=org.swift.51220191130a // /Library/Developer/Toolchains/swift-5.1.2-RELEASE.xctoolchain/Info.plist => CFBundleIdentifier

Carthage

Run carthage with a specific swift toolchain:

carthage update --toolchain org.swift.402017072a
@IshanG97
Copy link

this is very useful. Do you know how to open the url on a real device? I was trying xcrun devicectl device process launch --device <device-udid> com.apple.mobilesafari --payload-url http://www.google.com but no luck

@leviathan
Copy link
Author

@IshanG97 no, I don't know.

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