Skip to content

Instantly share code, notes, and snippets.

@Lutzifer
Created August 2, 2023 19:55
Show Gist options
  • Save Lutzifer/b76b3347cef10666a783a870dda2a346 to your computer and use it in GitHub Desktop.
Save Lutzifer/b76b3347cef10666a783a870dda2a346 to your computer and use it in GitHub Desktop.
Automatically select Xcode based on files
name: "Select Xcode"
description: "Selects the version of xcode specified in this action"
runs:
using: "composite"
steps:
- name: set DEVELOPER_DIR to select Xcode
shell: bash
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
run: echo ::set-env name=DEVELOPER_DIR::$(/usr/bin/ruby Resources/selectXcode.rb PRINT .xcode-build)
(...)
- id: selectXcode
uses: ./.github/actions/selectXcode
(...)
# Write one or many buildnumbers here
15A5209g
# Call this in Any Script that needs the correct DEVELOPER_DIR set
eval $(/usr/bin/ruby Resources/selectXcode.rb SOURCE .xcode-build)
echo $DEVELOPER_DIR
(...)
before_all do |lane|
Dir.chdir("..") do
UI.message %x(ruby Resources/selectXcode.rb LIST .xcode-build)
ENV["DEVELOPER_DIR"] = %x(ruby Resources/selectXcode.rb PRINT .xcode-build)
UI.message "DEVELOPER_DIR set to #{ENV["DEVELOPER_DIR"]}"
end
ensure_xcode_version()
end
(...)
#!/usr/bin/env ruby
require 'open3'
def show_help
puts <<-EOF
Usage: ./script.rb [SET|PRINT|LIST|SOURCE|--help] [path/to/.xcode-build]
This script sets the DEVELOPER_DIR environment variable to the path of a specific Xcode version.
The Xcode build versions are specified in a file provided as an argument.
Options:
SET Set the DEVELOPER_DIR environment variable
PRINT Only print the Xcode path without setting the environment variable
LIST Lists the available Xcodes with their build versions
SOURCE Prints an export statement to set the DEVELOPER_DIR environment variable in the parent shell
--help Show this help message
EOF
exit(0)
end
def get_xcode_build_version(path)
`defaults read "#{path}/Contents/version.plist" ProductBuildVersion 2>/dev/null`.strip
end
def has_xcode_build_string(path, xcode_version)
xcode_build = get_xcode_build_version(path)
xcode_build == xcode_version
end
def find_xcodes
stdout, _stderr, _status = Open3.capture3('mdfind', 'kMDItemCFBundleIdentifier = "com.apple.dt.Xcode"')
stdout.split("\n")
end
def filter_xcodes(xcodes, xcode_version)
xcodes.select { |xcode| has_xcode_build_string(xcode, xcode_version) }
end
if ARGV.length < 1 || ARGV.include?('--help')
show_help
end
operation = ARGV[0]
if operation == 'LIST'
xcodes = find_xcodes
xcodes.each do |xcode|
puts "#{xcode}: #{get_xcode_build_version(xcode)}"
end
exit(0)
end
if ARGV.length != 2
show_help
end
xcode_build_version_path = ARGV[1]
unless File.file?(xcode_build_version_path)
puts "The .xcode-build file does not exist at the given path."
exit(1)
end
xcodes = find_xcodes
File.open(xcode_build_version_path).each_line do |line|
next if line.start_with?('#')
specified_xcode_build = line.strip
matching_xcodes = filter_xcodes(xcodes, specified_xcode_build)
next if matching_xcodes.empty?
developer_dir = "#{matching_xcodes.first}/Contents/Developer"
case operation
when 'SET'
puts "Looking for an Xcode with build #{specified_xcode_build} (specified in #{xcode_build_version_path})"
ENV['DEVELOPER_DIR'] = developer_dir
puts "DEVELOPER_DIR set to #{developer_dir}"
when 'PRINT'
print developer_dir
when 'SOURCE'
puts "export DEVELOPER_DIR=#{developer_dir}"
else
puts "Invalid option: #{operation}"
puts "Use --help for usage information."
exit(1)
end
exit(0)
end
puts "No Xcode with the specified build versions was found."
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment