|
| 1 | +require 'dry/cli' |
| 2 | +require 'xcodeproj' |
| 3 | + |
| 4 | +module EmergeCLI |
| 5 | + module Commands |
| 6 | + module Autofixes |
| 7 | + class ExportedSymbols < EmergeCLI::Commands::GlobalOptions |
| 8 | + desc 'Remove exported symbols from built binaries' |
| 9 | + |
| 10 | + option :path, type: :string, required: true, desc: 'Path to the xcarchive' |
| 11 | + |
| 12 | + # Constants |
| 13 | + DEFAULT_EXPORTED_SYMBOLS = %(_main |
| 14 | +__mh_execute_header).freeze |
| 15 | + EXPORTED_SYMBOLS_FILE = 'EXPORTED_SYMBOLS_FILE'.freeze |
| 16 | + EXPORTED_SYMBOLS_PATH = '$(SRCROOT)/EmergeToolsHelperFiles/ExportedSymbols'.freeze |
| 17 | + EXPORTED_SYMBOLS_FILE_NAME = 'ExportedSymbols'.freeze |
| 18 | + EMERGE_TOOLS_GROUP = 'EmergeToolsHelperFiles'.freeze |
| 19 | + |
| 20 | + def call(**options) |
| 21 | + @options = options |
| 22 | + before(options) |
| 23 | + |
| 24 | + raise 'Path must be an xcodeproj' unless @options[:path].end_with?('.xcodeproj') |
| 25 | + raise 'Path does not exist' unless File.exist?(@options[:path]) |
| 26 | + |
| 27 | + Sync do |
| 28 | + project = Xcodeproj::Project.open(@options[:path]) |
| 29 | + |
| 30 | + # Add the exported symbols file to the project |
| 31 | + group = project.main_group |
| 32 | + emergetools_group = group.find_subpath(EMERGE_TOOLS_GROUP, true) |
| 33 | + emergetools_group.set_path(EMERGE_TOOLS_GROUP) |
| 34 | + |
| 35 | + unless emergetools_group.find_file_by_path(EXPORTED_SYMBOLS_FILE_NAME) |
| 36 | + emergetools_group.new_file(EXPORTED_SYMBOLS_FILE_NAME) |
| 37 | + end |
| 38 | + |
| 39 | + # Create Folder if it doesn't exist |
| 40 | + |
| 41 | + FileUtils.mkdir_p(File.join(File.dirname(@options[:path]), EMERGE_TOOLS_GROUP)) |
| 42 | + |
| 43 | + # Create the exported symbols file |
| 44 | + path = File.join(File.dirname(@options[:path]), EMERGE_TOOLS_GROUP, EXPORTED_SYMBOLS_FILE_NAME) |
| 45 | + File.write(path, DEFAULT_EXPORTED_SYMBOLS) |
| 46 | + |
| 47 | + project.targets.each do |target| |
| 48 | + # Only do it for app targets |
| 49 | + next unless target.product_type == 'com.apple.product-type.application' |
| 50 | + |
| 51 | + target.build_configurations.each do |config| |
| 52 | + config.build_settings[EXPORTED_SYMBOLS_FILE] = EXPORTED_SYMBOLS_PATH |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + project.save |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments