diff --git a/lib/commands/autofixes/exported_symbols.rb b/lib/commands/autofixes/exported_symbols.rb new file mode 100644 index 0000000..1ec0691 --- /dev/null +++ b/lib/commands/autofixes/exported_symbols.rb @@ -0,0 +1,62 @@ +require 'dry/cli' +require 'xcodeproj' + +module EmergeCLI + module Commands + module Autofixes + class ExportedSymbols < EmergeCLI::Commands::GlobalOptions + desc 'Remove exported symbols from built binaries' + + option :path, type: :string, required: true, desc: 'Path to the xcarchive' + + # Constants + DEFAULT_EXPORTED_SYMBOLS = %(_main +__mh_execute_header).freeze + EXPORTED_SYMBOLS_FILE = 'EXPORTED_SYMBOLS_FILE'.freeze + EXPORTED_SYMBOLS_PATH = '$(SRCROOT)/EmergeToolsHelperFiles/ExportedSymbols'.freeze + EXPORTED_SYMBOLS_FILE_NAME = 'ExportedSymbols'.freeze + EMERGE_TOOLS_GROUP = 'EmergeToolsHelperFiles'.freeze + + def call(**options) + @options = options + before(options) + + raise 'Path must be an xcodeproj' unless @options[:path].end_with?('.xcodeproj') + raise 'Path does not exist' unless File.exist?(@options[:path]) + + Sync do + project = Xcodeproj::Project.open(@options[:path]) + + # Add the exported symbols file to the project + group = project.main_group + emergetools_group = group.find_subpath(EMERGE_TOOLS_GROUP, true) + emergetools_group.set_path(EMERGE_TOOLS_GROUP) + + unless emergetools_group.find_file_by_path(EXPORTED_SYMBOLS_FILE_NAME) + emergetools_group.new_file(EXPORTED_SYMBOLS_FILE_NAME) + end + + # Create Folder if it doesn't exist + + FileUtils.mkdir_p(File.join(File.dirname(@options[:path]), EMERGE_TOOLS_GROUP)) + + # Create the exported symbols file + path = File.join(File.dirname(@options[:path]), EMERGE_TOOLS_GROUP, EXPORTED_SYMBOLS_FILE_NAME) + File.write(path, DEFAULT_EXPORTED_SYMBOLS) + + project.targets.each do |target| + # Only do it for app targets + next unless target.product_type == 'com.apple.product-type.application' + + target.build_configurations.each do |config| + config.build_settings[EXPORTED_SYMBOLS_FILE] = EXPORTED_SYMBOLS_PATH + end + end + + project.save + end + end + end + end + end +end diff --git a/lib/emerge_cli.rb b/lib/emerge_cli.rb index d193ead..97fba8e 100644 --- a/lib/emerge_cli.rb +++ b/lib/emerge_cli.rb @@ -18,6 +18,7 @@ require_relative 'commands/build_distribution/validate_app' require_relative 'commands/build_distribution/download_and_install' require_relative 'commands/autofixes/minify_strings' +require_relative 'commands/autofixes/exported_symbols' require_relative 'reaper/ast_parser' require_relative 'reaper/code_deleter' @@ -72,6 +73,7 @@ module EmergeCLI register 'autofix' do |prefix| prefix.register 'minify-strings', Commands::Autofixes::MinifyStrings + prefix.register 'exported-symbols', Commands::Autofixes::ExportedSymbols end end diff --git a/test/commands/autofixes/exported_symbols_test.rb b/test/commands/autofixes/exported_symbols_test.rb new file mode 100644 index 0000000..07bee19 --- /dev/null +++ b/test/commands/autofixes/exported_symbols_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +module EmergeCLI + module Commands + module Autofixes + class ExportedSymbolsTest < Minitest::Test + EMERGE_TOOLS_GROUP = 'EmergeToolsHelperFiles'.freeze + EXPORTED_SYMBOLS_FILE = 'EXPORTED_SYMBOLS_FILE'.freeze + EXPORTED_SYMBOLS_PATH = '$(SRCROOT)/EmergeToolsHelperFiles/ExportedSymbols'.freeze + DEFAULT_EXPORTED_SYMBOLS = %(_main +__mh_execute_header).freeze + + def setup + @command = EmergeCLI::Commands::Autofixes::ExportedSymbols.new + + FileUtils.mkdir_p('tmp/test_autofix_exported_symbols') + FileUtils.cp_r('test/test_files/ExampleApp.xcodeproj', + 'tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj') + end + + def teardown + FileUtils.rm_rf('tmp/test_autofix_exported_symbols') + end + + def test_exported_symbols_is_set + options = { + path: 'tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj' + } + + @command.call(**options) + + project = Xcodeproj::Project.open('tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj') + group = project.main_group + + emergetools_group = group.find_subpath(EMERGE_TOOLS_GROUP, false) + assert !emergetools_group.nil? + + project.targets[0].build_configurations.each do |config| + assert_equal EXPORTED_SYMBOLS_PATH, config.build_settings[EXPORTED_SYMBOLS_FILE] + end + + file_path = 'tmp/test_autofix_exported_symbols/EmergeToolsHelperFiles/ExportedSymbols' + assert File.exist?(file_path) + assert_equal DEFAULT_EXPORTED_SYMBOLS, File.read(file_path) + end + end + end + end +end