Skip to content

Commit

Permalink
Remove Exported Symbols command (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itaybre authored Dec 27, 2024
1 parent fe06ff3 commit d09485a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lib/commands/autofixes/exported_symbols.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/emerge_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand Down
49 changes: 49 additions & 0 deletions test/commands/autofixes/exported_symbols_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d09485a

Please sign in to comment.