Skip to content

Instantly share code, notes, and snippets.

@jnjosh
Last active December 30, 2015 13:39
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 jnjosh/7837207 to your computer and use it in GitHub Desktop.
Save jnjosh/7837207 to your computer and use it in GitHub Desktop.
Script that gets the path to your built product from an Xcode workspace no matter your build location (Derived Data or Legacy Build Directory)
#
# Usage:
#
# app_path = product_path :workspace => "MyProject.xcworkspace",
# :scheme => "MyProjectScheme",
# :configuration => "Debug",
# :sdk => "iphonesimulator"
#
# puts app_path //=> /Users/yourusername/Library/Developer/Xcode/DerivedData/MyProject-bltlcokzhdicjggqmcrwibpdgdcp/Build/Products/Debug-iphonesimulator/MyProject.app
#
def build_settings options = { :workspace => nil, :scheme => nil, :configuration => nil, :sdk => nil }
build_output = `xcodebuild -workspace #{options[ :workspace ]} -scheme #{options[ :scheme ]} -configuration #{options[ :configuration ]} -sdk #{options[ :sdk ]} -showBuildSettings`
Hash[build_output.split(/\r?\n/).collect { |setting|
setting.split(' = ', 2).collect &:strip
}.reject { |setting|
setting.length != 2 || setting[0] == ""
}]
end
def product_path options = { :workspace => nil, :scheme => nil, :configuration => nil, :sdk => nil }
settings = build_settings options
target_build_directory = settings[ "TARGET_BUILD_DIR" ]
executable_file_directory = settings[ "EXECUTABLE_FOLDER_PATH" ]
File.join(target_build_directory, executable_file_directory)
end
@prachigauriar
Copy link

Seems like you could further simplify this code by using split instead of a regex when building your hash.

build_settings.collect do |setting|
  pair = setting.split(' = ', 2).collect &:strip
end

This was typed on an iPad, so it probably contains errors.

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