Skip to content

Commit d09485a

Browse files
authored
Remove Exported Symbols command (#46)
1 parent fe06ff3 commit d09485a

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

lib/emerge_cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require_relative 'commands/build_distribution/validate_app'
1919
require_relative 'commands/build_distribution/download_and_install'
2020
require_relative 'commands/autofixes/minify_strings'
21+
require_relative 'commands/autofixes/exported_symbols'
2122

2223
require_relative 'reaper/ast_parser'
2324
require_relative 'reaper/code_deleter'
@@ -72,6 +73,7 @@ module EmergeCLI
7273

7374
register 'autofix' do |prefix|
7475
prefix.register 'minify-strings', Commands::Autofixes::MinifyStrings
76+
prefix.register 'exported-symbols', Commands::Autofixes::ExportedSymbols
7577
end
7678
end
7779

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
module EmergeCLI
4+
module Commands
5+
module Autofixes
6+
class ExportedSymbolsTest < Minitest::Test
7+
EMERGE_TOOLS_GROUP = 'EmergeToolsHelperFiles'.freeze
8+
EXPORTED_SYMBOLS_FILE = 'EXPORTED_SYMBOLS_FILE'.freeze
9+
EXPORTED_SYMBOLS_PATH = '$(SRCROOT)/EmergeToolsHelperFiles/ExportedSymbols'.freeze
10+
DEFAULT_EXPORTED_SYMBOLS = %(_main
11+
__mh_execute_header).freeze
12+
13+
def setup
14+
@command = EmergeCLI::Commands::Autofixes::ExportedSymbols.new
15+
16+
FileUtils.mkdir_p('tmp/test_autofix_exported_symbols')
17+
FileUtils.cp_r('test/test_files/ExampleApp.xcodeproj',
18+
'tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj')
19+
end
20+
21+
def teardown
22+
FileUtils.rm_rf('tmp/test_autofix_exported_symbols')
23+
end
24+
25+
def test_exported_symbols_is_set
26+
options = {
27+
path: 'tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj'
28+
}
29+
30+
@command.call(**options)
31+
32+
project = Xcodeproj::Project.open('tmp/test_autofix_exported_symbols/ExampleApp.xcodeproj')
33+
group = project.main_group
34+
35+
emergetools_group = group.find_subpath(EMERGE_TOOLS_GROUP, false)
36+
assert !emergetools_group.nil?
37+
38+
project.targets[0].build_configurations.each do |config|
39+
assert_equal EXPORTED_SYMBOLS_PATH, config.build_settings[EXPORTED_SYMBOLS_FILE]
40+
end
41+
42+
file_path = 'tmp/test_autofix_exported_symbols/EmergeToolsHelperFiles/ExportedSymbols'
43+
assert File.exist?(file_path)
44+
assert_equal DEFAULT_EXPORTED_SYMBOLS, File.read(file_path)
45+
end
46+
end
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)