|
| 1 | +require 'dry/cli' |
| 2 | +require 'xcodeproj' |
| 3 | + |
| 4 | +module EmergeCLI |
| 5 | + module Commands |
| 6 | + module Autofixes |
| 7 | + class MinifyStrings < EmergeCLI::Commands::GlobalOptions |
| 8 | + desc 'Minify strings in the app' |
| 9 | + |
| 10 | + option :path, type: :string, required: true, desc: 'Path to the xcarchive' |
| 11 | + |
| 12 | + # Constants |
| 13 | + SCRIPT_NAME = 'EmergeTools Minify Strings'.freeze |
| 14 | + ENABLE_USER_SCRIPT_SANDBOXING = 'ENABLE_USER_SCRIPT_SANDBOXING'.freeze |
| 15 | + STRINGS_FILE_OUTPUT_ENCODING = 'STRINGS_FILE_OUTPUT_ENCODING'.freeze |
| 16 | + STRINGS_FILE_OUTPUT_ENCODING_VALUE = 'UTF-8'.freeze |
| 17 | + SCRIPT_CONTENT = %{import os |
| 18 | +import json |
| 19 | +from multiprocessing.pool import ThreadPool |
| 20 | +
|
| 21 | +def minify(file_path): |
| 22 | + os.system(f"plutil -convert json '{file_path}'") |
| 23 | + new_content = '' |
| 24 | + try: |
| 25 | + with open(file_path, 'r') as input_file: |
| 26 | + data = json.load(input_file) |
| 27 | +
|
| 28 | + for key, value in data.items(): |
| 29 | + fixed_key = json.dumps(key, ensure_ascii=False).encode('utf8').decode() |
| 30 | + fixed_value = json.dumps(value, ensure_ascii=False).encode('utf8').decode() |
| 31 | + new_line = f'{fixed_key} = {fixed_value};\\n' |
| 32 | + new_content += new_line |
| 33 | +
|
| 34 | + with open(file_path, 'w') as output_file: |
| 35 | + output_file.write(new_content) |
| 36 | + except: |
| 37 | + return |
| 38 | +
|
| 39 | +file_extension = '.strings' |
| 40 | +stringFiles = [] |
| 41 | +
|
| 42 | +for root, _, files in os.walk(os.environ['BUILT_PRODUCTS_DIR'], followlinks=True): |
| 43 | + for filename in files: |
| 44 | + if filename.endswith(file_extension): |
| 45 | + input_path = os.path.join(root, filename) |
| 46 | + stringFiles.append(input_path) |
| 47 | +
|
| 48 | +# create a thread pool |
| 49 | +with ThreadPool() as pool: |
| 50 | + pool.map(minify, stringFiles) |
| 51 | +}.freeze |
| 52 | + |
| 53 | + def call(**options) |
| 54 | + @options = options |
| 55 | + before(options) |
| 56 | + |
| 57 | + raise 'Path must be an xcodeproj' unless @options[:path].end_with?('.xcodeproj') |
| 58 | + raise 'Path does not exist' unless File.exist?(@options[:path]) |
| 59 | + |
| 60 | + Sync do |
| 61 | + project = Xcodeproj::Project.open(@options[:path]) |
| 62 | + |
| 63 | + project.targets.each do |target| |
| 64 | + target.build_configurations.each do |config| |
| 65 | + enable_user_script_sandboxing(config) |
| 66 | + set_output_encoding(config) |
| 67 | + end |
| 68 | + |
| 69 | + add_run_script(target) |
| 70 | + end |
| 71 | + |
| 72 | + project.save |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + private |
| 77 | + |
| 78 | + def enable_user_script_sandboxing(config) |
| 79 | + Logger.info "Enabling user script sandboxing for #{config.name}" |
| 80 | + config.build_settings[ENABLE_USER_SCRIPT_SANDBOXING] = 'NO' |
| 81 | + end |
| 82 | + |
| 83 | + def set_output_encoding(config) |
| 84 | + Logger.info "Setting output encoding for #{config.name}" |
| 85 | + config.build_settings[STRINGS_FILE_OUTPUT_ENCODING] = STRINGS_FILE_OUTPUT_ENCODING_VALUE |
| 86 | + end |
| 87 | + |
| 88 | + def add_run_script(target) |
| 89 | + phase = target.shell_script_build_phases.find { |item| item.name == SCRIPT_NAME } |
| 90 | + return unless phase.nil? |
| 91 | + Logger.info "Creating script '#{SCRIPT_NAME}'" |
| 92 | + phase = target.new_shell_script_build_phase(SCRIPT_NAME) |
| 93 | + phase.shell_script = SCRIPT_CONTENT |
| 94 | + phase.shell_path = `which python3`.strip |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments