Skip to content

Commit 58abd7c

Browse files
committed
Add ios_merge_strings_files action & test
Based on previously introduced ios_l10n_helper
1 parent 9760300 commit 58abd7c

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ RSpec/FilePath:
144144
- 'spec/check_localization_progress_spec.rb'
145145
- 'spec/ios_generate_strings_file_from_code_spec.rb'
146146
- 'spec/ios_l10n_helper_spec.rb'
147+
- 'spec/ios_merge_strings_files_spec.rb'
147148

148149
# Offense count: 8
149150
# Cop supports --auto-correct.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module Fastlane
2+
module Actions
3+
class IosMergeStringsFilesAction < Action
4+
def self.run(params)
5+
UI.message "Merging strings files: #{params[:paths].inspect}"
6+
7+
duplicates = Fastlane::Helper::Ios::L10nHelper.merge_strings(paths: params[:paths], output_path: params[:destination])
8+
duplicates.each do |dup_key|
9+
UI.important "Duplicate key found while merging the `.strings` files: `#{dup_key}`"
10+
end
11+
duplicates
12+
end
13+
14+
#####################################################
15+
# @!group Documentation
16+
#####################################################
17+
18+
def self.description
19+
'Merge multiple `.strings` files into one'
20+
end
21+
22+
def self.details
23+
<<~DETAILS
24+
Merge multiple `.strings` files into one.
25+
26+
Especially useful to prepare a single `.strings` file merging strings from both `Localizable.strings` from
27+
the app code — typically previously extracted from `ios_generate_strings_file_from_code` —
28+
and string files like `InfoPlist.strings` — which values may not be generated from the codebase but
29+
manually maintained by developers.
30+
31+
The action only supports merging files which are in the OpenStep (`"key" = "value";`) text format (which is
32+
the most common format for `.strings` files, and the one generated by `genstrings`), but can handle the case
33+
of different files using different encodings (UTF8 vs UTF16) and is able to detect and report duplicates.
34+
It does not handle strings files in XML or binary-plist formats `.strings` files (which are valid but more rare)
35+
DETAILS
36+
end
37+
38+
def self.available_options
39+
[
40+
FastlaneCore::ConfigItem.new(
41+
key: :paths,
42+
env_name: 'FL_IOS_MERGE_STRINGS_FILES_PATHS',
43+
description: 'The paths of all the `.strings` files to merge together',
44+
type: Array,
45+
optional: false
46+
),
47+
FastlaneCore::ConfigItem.new(
48+
key: :destination,
49+
env_name: 'FL_IOS_MERGE_STRINGS_FILES_DESTINATION',
50+
description: 'The path of the merged `.strings` file to generate. If nil, the merge will happen in-place in the first file in the `paths:` list',
51+
type: String,
52+
optional: true,
53+
default_value: nil
54+
),
55+
]
56+
end
57+
58+
def self.return_type
59+
:array_of_strings
60+
end
61+
62+
def self.return_value
63+
'The list of duplicate keys found while merging the various `.strings` files'
64+
end
65+
66+
def self.authors
67+
['automattic']
68+
end
69+
70+
def self.is_supported?(platform)
71+
platform == :ios
72+
end
73+
end
74+
end
75+
end

spec/ios_merge_strings_files_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
describe Fastlane do
2+
describe Fastlane::FastFile do
3+
let(:test_data_dir) { File.join(File.dirname(__FILE__), 'test-data', 'translations', 'ios_l10n_helper') }
4+
5+
def fixture(name)
6+
File.join(test_data_dir, name)
7+
end
8+
9+
describe '#ios_merge_strings_files' do
10+
it 'calls the action with the proper parameters and warn and return duplicate keys' do
11+
# Arrange
12+
messages = []
13+
allow(FastlaneCore::UI).to receive(:important) do |message|
14+
messages.append(message)
15+
end
16+
inputs = ['Localizable-utf16.strings', 'non-latin-utf16.strings']
17+
18+
Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmpdir|
19+
inputs.each { |f| FileUtils.cp(fixture(f), tmpdir) }
20+
21+
# Act
22+
result = Dir.chdir(tmpdir) do
23+
described_class.new.parse("lane :test do
24+
ios_merge_strings_files(
25+
paths: ['#{inputs[0]}', '#{inputs[1]}'],
26+
destination: 'output.strings'
27+
)
28+
end").runner.execute(:test)
29+
end
30+
31+
# Assert
32+
expect(File).to exist(File.join(tmpdir, 'output.strings'))
33+
expect(result).to eq(%w[key1 key2])
34+
expect(messages).to eq([
35+
'Duplicate key found while merging the `.strings` files: `key1`',
36+
'Duplicate key found while merging the `.strings` files: `key2`',
37+
])
38+
end
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)