Skip to content

Commit

Permalink
Merge pull request #351 from wordpress-mobile/libs-merge/source-id
Browse files Browse the repository at this point in the history
Add option to identify the source library when merging strings.xml files
  • Loading branch information
AliSoftware authored Apr 11, 2022
2 parents dfca603 + b85a7d7 commit 5cf371e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ _None_

### New Features

_None_
* Add the option for `an_localize_libs` to provide a `source_id` for each library being merged.
If provided, that identifier will be added as an `a8c-src-lib` XML attribute to the `<string>` nodes being updated with strings from said library.
This can be useful to help identify where each string come from in the resulting, merged `strings.xml`. [#351]

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def self.details
end

def self.available_options
libs_hash_description = <<~KEYS
- `:library`: The library display name
- `:strings_path`: The path to the `strings.xml` file of the library
- `:exclusions`: An optional `Array` of string keys to exclude from merging
- `:source_id`: An optional `String` which will be added as the `a8c-src-lib` XML attribute
to strings coming from this library, to help identify their source in the merged file.
KEYS
[
FastlaneCore::ConfigItem.new(key: :app_strings_path,
description: 'The path of the main strings file',
Expand All @@ -41,10 +48,7 @@ def self.available_options
# See `Fastlane::Helper::Android::LocalizeHelper.merge_lib`'s YARD doc for more details on the keys expected for each Hash.
FastlaneCore::ConfigItem.new(key: :libs_strings_path,
env_name: 'LOCALIZE_LIBS_STRINGS_PATH',
description: 'The list of libs to merge. ' \
+ 'Each item in the provided array must be a Hash with the keys `:library` (The library display name),' \
+ '`:strings_path` (The path to the `strings.xml` file of the library) and ' \
+ '`:exclusions` (Array of string keys to exclude from merging)',
description: "The list of libs to merge. Each item in the provided array must be a Hash with the following keys:\n#{libs_hash_description}",
optional: false,
type: Array),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ module Fastlane
module Helper
module Android
module LocalizeHelper
LIB_SOURCE_XML_ATTR = 'a8c-src-lib'.freeze

# Checks if string_line has the content_override flag set
def self.skip_string_by_tag(string_line)
skip = string_line.attr('content_override') == 'true' unless string_line.attr('content_override').nil?
if skip
puts " - Skipping #{string_line.attr('name')} string"
UI.message " - Skipping #{string_line.attr('name')} string"
return true
end

Expand All @@ -22,11 +24,11 @@ def self.skip_string_by_tag(string_line)

# Checks if string_name is in the excluesion list
def self.skip_string_by_exclusion_list(library, string_name)
return false unless library.key?(:exclusions)
return false if library[:exclusions].nil?

skip = library[:exclusions].include?(string_name)
if skip
puts " - Skipping #{string_name} string"
UI.message " - Skipping #{string_name} string"
return true
end
end
Expand All @@ -35,6 +37,7 @@ def self.skip_string_by_exclusion_list(library, string_name)
def self.merge_string(main_strings, library, string_line)
string_name = string_line.attr('name')
string_content = string_line.content
lib_src_id = library[:source_id]

# Skip strings in the exclusions list
return :skipped if skip_string_by_exclusion_list(library, string_name)
Expand All @@ -58,12 +61,14 @@ def self.merge_string(main_strings, library, string_line)
else
# It has the tools:ignore flag, so update the content without touching the other attributes
this_string.content = string_content
this_string[LIB_SOURCE_XML_ATTR] = lib_src_id unless lib_src_id.nil?
return result
end
end
end

# String not found, or removed because needing update and not in the exclusion list: add to the main file
string_line[LIB_SOURCE_XML_ATTR] = lib_src_id unless lib_src_id.nil?
main_strings.xpath('//string').last().add_next_sibling("\n#{' ' * 4}#{string_line.to_xml().strip}")
return result
end
Expand Down Expand Up @@ -113,12 +118,12 @@ def self.merge_lib(main, library)
res = merge_string(main_strings, library, string_line)
case res
when :updated
puts "#{string_line.attr('name')} updated."
UI.verbose "#{string_line.attr('name')} updated."
updated_count = updated_count + 1
when :found
untouched_count = untouched_count + 1
when :added
puts "#{string_line.attr('name')} added."
UI.verbose "#{string_line.attr('name')} added."
added_count = added_count + 1
when :skipped
skipped_count = skipped_count + 1
Expand Down

0 comments on commit 5cf371e

Please sign in to comment.