Skip to content

Commit

Permalink
Extract post-processing in dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed Sep 5, 2024
1 parent a98ce71 commit 95b5857
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -56,13 +41,32 @@ 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|
exclude&.any? { |ex| File.fnmatch?(ex, file) }
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
#####################################################
Expand Down
13 changes: 6 additions & 7 deletions spec/ios_generate_strings_file_from_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 95b5857

Please sign in to comment.