|
| 1 | +require 'xcodeproj' |
| 2 | + |
| 3 | +module EmergeCLI |
| 4 | + module Commands |
| 5 | + module Config |
| 6 | + class OrderFilesIOS < EmergeCLI::Commands::GlobalOptions |
| 7 | + desc 'Configure order files for iOS' |
| 8 | + |
| 9 | + # Optional options |
| 10 | + option :skip_download_script, type: :boolean, required: false, desc: 'Only enable linkmaps' |
| 11 | + option :project_path, type: :string, required: false, |
| 12 | + desc: 'Path to the xcode project (will use first found if not provided)' |
| 13 | + |
| 14 | + # Constants |
| 15 | + LINK_MAPS_CONFIG = 'LD_GENERATE_MAP_FILE'.freeze |
| 16 | + LINK_MAPS_PATH = 'LD_MAP_FILE_PATH'.freeze |
| 17 | + PATH_TO_LINKMAP = '$(TARGET_TEMP_DIR)/$(PRODUCT_NAME)-LinkMap-$(CURRENT_VARIANT)-$(CURRENT_ARCH).txt'.freeze |
| 18 | + ORDER_FILE = 'ORDER_FILE'.freeze |
| 19 | + ORDER_FILE_PATH = '$(PROJECT_DIR)/orderfiles/orderfile.txt'.freeze |
| 20 | + |
| 21 | + def initialize; end |
| 22 | + |
| 23 | + def call(**options) |
| 24 | + @options = options |
| 25 | + before(options) |
| 26 | + |
| 27 | + if @options[:project_path] |
| 28 | + project = Xcodeproj::Project.open(@options[:project_path]) |
| 29 | + else |
| 30 | + project = Xcodeproj::Project.open(Dir.glob('*.xcodeproj').first) |
| 31 | + Logger.warn 'No project path provided, using first found xcodeproj in current directory' |
| 32 | + end |
| 33 | + |
| 34 | + enable_linkmaps(project) |
| 35 | + |
| 36 | + add_order_files_download_script(project) unless @options[:skip_download_script] |
| 37 | + |
| 38 | + project.save |
| 39 | + end |
| 40 | + |
| 41 | + private |
| 42 | + |
| 43 | + def enable_linkmaps(project) |
| 44 | + Logger.info 'Enabling Linkmaps' |
| 45 | + project.targets.each do |target| |
| 46 | + # Only do it for app targets |
| 47 | + next unless target.product_type == 'com.apple.product-type.application' |
| 48 | + |
| 49 | + Logger.info " Target: #{target.name}" |
| 50 | + target.build_configurations.each do |config| |
| 51 | + config.build_settings[LINK_MAPS_CONFIG] = 'YES' |
| 52 | + config.build_settings[LINK_MAPS_PATH] = PATH_TO_LINKMAP |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def add_order_files_download_script(project) |
| 58 | + Logger.info 'Adding order files download script' |
| 59 | + project.targets.each do |target| |
| 60 | + # Only do it for app targets |
| 61 | + next unless target.product_type == 'com.apple.product-type.application' |
| 62 | + |
| 63 | + Logger.info " Target: #{target.name}" |
| 64 | + |
| 65 | + # Create the script phase if it doesn't exist |
| 66 | + phase = target.shell_script_build_phases.find { |item| item.name == 'EmergeTools Download Order Files' } |
| 67 | + if phase.nil? |
| 68 | + Logger.info " Creating script 'EmergeTools Download Order Files'" |
| 69 | + phase = target.new_shell_script_build_phase('EmergeTools Download Order Files') |
| 70 | + phase.shell_script = <<~BASH |
| 71 | + if [ "$CONFIGURATION" != "Release" ]; then |
| 72 | + echo "Skipping script for non-Release build" |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | +
|
| 76 | + if curl --fail "https://order-files-prod.emergetools.com/$PRODUCT_BUNDLE_IDENTIFIER/$MARKETING_VERSION" \ |
| 77 | + -H "X-API-Token: $EMERGE_API_TOKEN" -o ORDER_FILE.gz ; then |
| 78 | + mkdir -p "$PROJECT_DIR/orderfiles" |
| 79 | + gunzip -c ORDER_FILE.gz > $PROJECT_DIR/orderfiles/orderfile.txt |
| 80 | + else |
| 81 | + echo "cURL request failed. Creating an empty file." |
| 82 | + mkdir -p "$PROJECT_DIR/orderfiles" |
| 83 | + touch "$PROJECT_DIR/orderfiles/orderfile.txt" |
| 84 | + fi; |
| 85 | + BASH |
| 86 | + phase.output_paths = ['$(PROJECT_DIR)/orderfiles/orderfile.txt'] |
| 87 | + else |
| 88 | + Logger.info " 'EmergeTools Download Order Files' already exists" |
| 89 | + end |
| 90 | + # Make sure it is the first build phase |
| 91 | + target.build_phases.move(phase, 0) |
| 92 | + |
| 93 | + target.build_configurations.each do |config| |
| 94 | + config.build_settings[ORDER_FILE] = ORDER_FILE_PATH |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments