@@ -11,19 +11,19 @@ module Android
11
11
module LocalizeHelper
12
12
LIB_SOURCE_XML_ATTR = 'a8c-src-lib' . freeze
13
13
14
- # Checks if string_line has the content_override flag set
15
- def self . skip_string_by_tag ( string_line )
16
- skip = string_line . attr ( 'content_override' ) == 'true' unless string_line . attr ( 'content_override' ) . nil?
14
+ # Checks if `string_node` has the content_override flag set
15
+ def self . skip_string_by_tag? ( string_node )
16
+ skip = string_node . attr ( 'content_override' ) == 'true' unless string_node . attr ( 'content_override' ) . nil?
17
17
if skip
18
- UI . message " - Skipping #{ string_line . attr ( 'name' ) } string"
18
+ UI . message " - Skipping #{ string_node . attr ( 'name' ) } string"
19
19
return true
20
20
end
21
21
22
22
return false
23
23
end
24
24
25
- # Checks if string_name is in the excluesion list
26
- def self . skip_string_by_exclusion_list ( library , string_name )
25
+ # Checks if ` string_name` is in the exclusion list
26
+ def self . skip_string_by_exclusion_list? ( library , string_name )
27
27
return false if library [ :exclusions ] . nil?
28
28
29
29
skip = library [ :exclusions ] . include? ( string_name )
@@ -43,66 +43,65 @@ def self.add_xml_attributes!(string_node, library)
43
43
string_node [ LIB_SOURCE_XML_ATTR ] = library [ :source_id ] unless library [ :source_id ] . nil?
44
44
end
45
45
46
- # Merge string_line into main_string
47
- def self . merge_string ( main_strings , library , string_line )
48
- string_name = string_line . attr ( 'name' )
49
- string_content = string_line . content
46
+ # Merge a single `lib_string_node` XML node into the `main_strings_xml``
47
+ def self . merge_string_node ( main_strings_xml , library , lib_string_node )
48
+ string_name = lib_string_node . attr ( 'name' )
49
+ string_content = lib_string_node . content
50
50
51
51
# Skip strings in the exclusions list
52
- return :skipped if skip_string_by_exclusion_list ( library , string_name )
52
+ return :skipped if skip_string_by_exclusion_list? ( library , string_name )
53
53
54
54
# Search for the string in the main file
55
55
result = :added
56
- main_strings . xpath ( '//string' ) . each do |this_string |
57
- if this_string . attr ( 'name' ) == string_name
56
+ main_strings_xml . xpath ( '//string' ) . each do |main_string_node |
57
+ if main_string_node . attr ( 'name' ) == string_name
58
58
# Skip if the string has the content_override tag
59
- return :skipped if skip_string_by_tag ( this_string )
59
+ return :skipped if skip_string_by_tag? ( main_string_node )
60
60
61
61
# If nodes are equivalent, skip
62
- return :found if string_line =~ this_string
62
+ return :found if lib_string_node =~ main_string_node
63
63
64
64
# The string needs an update
65
- result = :updated
66
- if this_string . attr ( 'tools:ignore' ) . nil?
67
- # It can be updated, so remove the current one and move ahead
68
- this_string . remove
69
- break
65
+ if main_string_node . attr ( 'tools:ignore' ) . nil?
66
+ # No `tools:ignore` attribute; completely replace existing main string node with lib's one
67
+ add_xml_attributes! ( lib_string_node , library )
68
+ main_string_node . replace lib_string_node
70
69
else
71
- # It has the tools:ignore flag, so update the content without touching the other attributes
72
- this_string . content = string_content
73
- add_xml_attributes! ( this_string , library )
74
- return result
70
+ # Has the `tools:ignore` flag; update the content without touching the other existing attributes
71
+ add_xml_attributes! ( main_string_node , library )
72
+ main_string_node . content = string_content
75
73
end
74
+ return :updated
76
75
end
77
76
end
78
77
79
78
# String not found, or removed because needing update and not in the exclusion list: add to the main file
80
- add_xml_attributes! ( string_line , library )
81
- main_strings . xpath ( '//string' ) . last ( ) . add_next_sibling ( "\n #{ ' ' * 4 } #{ string_line . to_xml ( ) . strip } " )
79
+ add_xml_attributes! ( lib_string_node , library )
80
+ main_strings_xml . xpath ( '//string' ) . last ( ) . add_next_sibling ( "\n #{ ' ' * 4 } #{ lib_string_node . to_xml ( ) . strip } " )
82
81
return result
83
82
end
84
83
85
- # Verify a string
86
- def self . verify_string ( main_strings , library , string_line )
87
- string_name = string_line . attr ( 'name' )
88
- string_content = string_line . content
84
+ # Verify a string node from a library has properly been merged into the main one
85
+ def self . verify_string ( main_strings_xml , library , lib_string_node )
86
+ string_name = lib_string_node . attr ( 'name' )
87
+ string_content = lib_string_node . content
89
88
90
89
# Skip strings in the exclusions list
91
- return if skip_string_by_exclusion_list ( library , string_name )
90
+ return if skip_string_by_exclusion_list? ( library , string_name )
92
91
93
92
# Search for the string in the main file
94
- main_strings . xpath ( '//string' ) . each do |this_string |
95
- if this_string . attr ( 'name' ) == string_name
93
+ main_strings_xml . xpath ( '//string' ) . each do |main_string_node |
94
+ if main_string_node . attr ( 'name' ) == string_name
96
95
# Skip if the string has the content_override tag
97
- return if skip_string_by_tag ( this_string )
96
+ return if skip_string_by_tag? ( main_string_node )
98
97
99
- # Update if needed
100
- UI . user_error! ( "String #{ string_name } [#{ string_content } ] has been updated in the main file but not in the library #{ library [ :library ] } ." ) if this_string . content != string_content
98
+ # Check if up-to-date
99
+ UI . user_error! ( "String #{ string_name } [#{ string_content } ] has been updated in the main file but not in the library #{ library [ :library ] } ." ) if main_string_node . content != string_content
101
100
return
102
101
end
103
102
end
104
103
105
- # String not found and not in the exclusion list:
104
+ # String not found and not in the exclusion list
106
105
UI . user_error! ( "String #{ string_name } [#{ string_content } ] was found in library #{ library [ :library ] } but not in the main file." )
107
106
end
108
107
@@ -122,23 +121,23 @@ def self.verify_string(main_strings, library, string_line)
122
121
#
123
122
def self . merge_lib ( main , library )
124
123
UI . message ( "Merging #{ library [ :library ] } strings into #{ main } " )
125
- main_strings = File . open ( main ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
126
- lib_strings = File . open ( library [ :strings_path ] ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
124
+ main_strings_xml = File . open ( main ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
125
+ lib_strings_xml = File . open ( library [ :strings_path ] ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
127
126
128
127
updated_count = 0
129
128
untouched_count = 0
130
129
added_count = 0
131
130
skipped_count = 0
132
- lib_strings . xpath ( '//string' ) . each do |string_line |
133
- res = merge_string ( main_strings , library , string_line )
131
+ lib_strings_xml . xpath ( '//string' ) . each do |string_node |
132
+ res = merge_string_node ( main_strings_xml , library , string_node )
134
133
case res
135
134
when :updated
136
- UI . verbose "#{ string_line . attr ( 'name' ) } updated."
135
+ UI . verbose "#{ string_node . attr ( 'name' ) } updated."
137
136
updated_count = updated_count + 1
138
137
when :found
139
138
untouched_count = untouched_count + 1
140
139
when :added
141
- UI . verbose "#{ string_line . attr ( 'name' ) } added."
140
+ UI . verbose "#{ string_node . attr ( 'name' ) } added."
142
141
added_count = added_count + 1
143
142
when :skipped
144
143
skipped_count = skipped_count + 1
@@ -148,7 +147,7 @@ def self.merge_lib(main, library)
148
147
end
149
148
150
149
File . open ( main , 'w:UTF-8' ) do |f |
151
- f . write ( main_strings . to_xml ( indent : 4 ) )
150
+ f . write ( main_strings_xml . to_xml ( indent : 4 ) )
152
151
end
153
152
154
153
UI . message ( "Done (#{ added_count } added, #{ updated_count } updated, #{ untouched_count } untouched, #{ skipped_count } skipped)." )
@@ -164,8 +163,8 @@ def self.verify_diff(diff_string, main_strings, lib_strings, library)
164
163
165
164
diff_string = diff_string . slice ( 0 ..( end_index - 1 ) )
166
165
167
- lib_strings . xpath ( '//string' ) . each do |string_line |
168
- res = verify_string ( main_strings , library , string_line ) if string_line . attr ( 'name' ) == diff_string
166
+ lib_strings . xpath ( '//string' ) . each do |string_node |
167
+ res = verify_string ( main_strings , library , string_node ) if string_node . attr ( 'name' ) == diff_string
169
168
end
170
169
end
171
170
end
0 commit comments