diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb index 73a5ef0b8..9b1596771 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb @@ -2,13 +2,11 @@ module Fastlane module Actions class IosGenerateStringsFileFromCodeAction < Action def self.run(params) - enc = begin - Encoding.find(params[:output_encoding]) - rescue ArgumentError => error - UI.user_error!(error.message) - end - - cmd_output = nil + output_encoding = begin + Encoding.find(params[:output_encoding]) + rescue ArgumentError => error + UI.user_error!(error.message) + end Dir.mktmpdir('genstrings-output-') do |tmpdir| # Build the command arguments @@ -26,23 +24,10 @@ def self.run(params) UI.user_error!(errors.join("\n")) unless !params[:fail_on_error] || errors.empty? # Convert generated files to requested encoding if necessary, and copy to final destination - output_dir = params[:output_dir] - Dir.each_child(tmpdir) do |filename| - source = File.join(tmpdir, filename) - next if filename.start_with?('.') || !File.file?(source) - - destination = File.join(output_dir, filename) - if enc.name == 'UTF-16LE' - # genstrings generates UTF-16 LittleEndian by default, so if that's the requested output encoding, we just copy - # the file directly, to avoid the read/write dance, reduce memory footprint, and reduce risk of encoding errors - FileUtils.cp(source, destination) - else - content = File.read(source, binmode: true, encoding: 'BOM|UTF-16LE') - File.write(destination, content, binmode: true, encoding: enc.name) - end - end + post_process_generated_files(source_dir: tmpdir, dest_dir: params[:output_dir], dest_encoding: output_encoding) + + cmd_output end - cmd_output end # Adds the proper `**/*.{m,swift}` to the list of paths @@ -56,6 +41,7 @@ def self.glob_pattern(path) end end + # List files matching a list of glob patterns, except the ones matching the list of exclusion patterns def self.files_matching(paths:, exclude:) globbed_paths = paths.map { |p| glob_pattern(p) } Dir.glob(globbed_paths).reject do |file| @@ -63,6 +49,24 @@ def self.files_matching(paths:, exclude:) end end + # Convert the generate files in `source_dir` to the `dest_encoding` if necessary, then copy them to the final `dest_dir` + def self.post_process_generated_files(source_dir:, dest_dir:, dest_encoding:) + Dir.each_child(source_dir) do |filename| + source = File.join(source_dir, filename) + next if filename.start_with?('.') || !File.file?(source) + + destination = File.join(dest_dir, filename) + if dest_encoding.name == 'UTF-16LE' + # genstrings generates UTF-16 LittleEndian by default, so if that's the requested output encoding, we just copy + # the file directly, to avoid the read/write dance, reduce memory footprint, and reduce risk of encoding errors + FileUtils.cp(source, destination) + else + content = File.read(source, binmode: true, encoding: 'BOM|UTF-16LE') + File.write(destination, content, binmode: true, encoding: dest_encoding.name) + end + end + end + ##################################################### # @!group Documentation ##################################################### diff --git a/spec/ios_generate_strings_file_from_code_spec.rb b/spec/ios_generate_strings_file_from_code_spec.rb index cf115518f..7c874a672 100644 --- a/spec/ios_generate_strings_file_from_code_spec.rb +++ b/spec/ios_generate_strings_file_from_code_spec.rb @@ -72,20 +72,19 @@ def test_genstrings(params:, expected_dir_name:, expected_logs: nil, expected_fa user_errors << error.message end - output_files = Dir[File.join(tmp_dir, '*.strings')] - expected_files = expected_dir_name.nil? ? [] : Dir[File.join(test_data_dir, expected_dir_name, '*.strings')] - # Assert: UI.messages, UI.user_error! and return value from the action + unless expected_failures.nil? + expect(clean_abs_dirs[user_errors]).to eq(expected_failures) + end + unless expected_logs.nil? expect(clean_abs_dirs[cmd_output]).to eq(expected_logs) expect(clean_abs_dirs[return_value]).to eq(expected_logs) end - unless expected_failures.nil? - expect(clean_abs_dirs[user_errors]).to eq(expected_failures) - end - # Assert: same list of generated files + output_files = Dir[File.join(tmp_dir, '*.strings')] + expected_files = expected_dir_name.nil? ? [] : Dir[File.join(test_data_dir, expected_dir_name, '*.strings')] expect(output_files.map { |f| File.basename(f) }.sort).to eq(expected_files.map { |f| File.basename(f) }.sort) # Assert: each generated file has expected content