-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathan_metadata_update_helper.rb
152 lines (123 loc) · 4.06 KB
/
an_metadata_update_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module Fastlane
module Helper
# Basic line handler
class MetadataBlock
attr_reader :block_key
def initialize(block_key)
@block_key = block_key
end
def handle_line(fw, line)
fw.puts(line) # Standard line handling: just copy
end
def is_handler_for(key)
true
end
end
class UnknownMetadataBlock < MetadataBlock
attr_reader :content_file_path
def initialize
super(nil)
end
end
class StandardMetadataBlock < MetadataBlock
attr_reader :content_file_path
def initialize(block_key, content_file_path)
super(block_key)
@content_file_path = content_file_path
end
def is_handler_for(key)
key == @block_key.to_s
end
def handle_line(fw, line)
# put the new content on block start
# and skip all the other content
generate_block(fw) if line.start_with?('msgctxt')
end
def generate_block(fw)
# init
fw.puts("msgctxt \"#{@block_key}\"")
line_count = File.foreach(@content_file_path).inject(0) { |c, _line| c + 1 }
if line_count <= 1
# Single line output
fw.puts("msgid \"#{File.open(@content_file_path, 'r').read}\"")
else
# Multiple line output
fw.puts('msgid ""')
# insert content
File.open(@content_file_path, 'r').each do |line|
fw.puts("\"#{line.strip}\\n\"")
end
end
# close
fw.puts('msgstr ""')
fw.puts('')
end
end
class ReleaseNoteMetadataBlock < StandardMetadataBlock
attr_reader :new_key, :keep_key, :rel_note_key, :release_version
def initialize(block_key, content_file_path, release_version)
super(block_key, content_file_path)
@rel_note_key = 'release_note'
@release_version = release_version
generate_keys(release_version)
end
def generate_keys(release_version)
values = release_version.split('.')
version_major = Integer(values[0])
version_minor = Integer(values[1])
@new_key = "#{@rel_note_key}_#{version_major.to_s.rjust(2, '0')}#{version_minor}"
version_major = version_major - 1 if version_minor == 0
version_minor = version_minor == 0 ? 9 : version_minor - 1
@keep_key = "#{@rel_note_key}_#{version_major.to_s.rjust(2, '0')}#{version_minor}"
end
def is_handler_for(key)
values = key.split('_')
key.start_with?(@rel_note_key) && values.length == 3 && is_int?(values[2].sub(/^0*/, ''))
end
def handle_line(fw, line)
# put content on block start or if copying the latest one
# and skip all the other content
if line.start_with?('msgctxt')
key = extract_key(line)
@is_copying = (key == @keep_key)
generate_block(fw) if @is_copying
end
fw.puts(line) if @is_copying
end
def generate_block(fw)
# init
fw.puts("msgctxt \"#{@new_key}\"")
fw.puts('msgid ""')
fw.puts("\"#{@release_version}:\\n\"")
# insert content
File.open(@content_file_path, 'r').each do |line|
fw.puts("\"#{line.strip}\\n\"")
end
# close
fw.puts('msgstr ""')
fw.puts('')
end
def extract_key(line)
line.split[1].tr('\"', '')
end
def is_int?(value)
true if Integer(value) rescue false
end
end
class ReleaseNoteShortMetadataBlock < ReleaseNoteMetadataBlock
def initialize(block_key, content_file_path, release_version)
super(block_key, content_file_path, release_version)
@rel_note_key = 'release_note_short'
@release_version = release_version
generate_keys(release_version)
end
def is_handler_for(key)
values = key.split('_')
key.start_with?(@rel_note_key) && values.length == 4 && is_int?(values[3].sub(/^0*/, ''))
end
def generate_block(fw)
super(fw) unless File.zero?(@content_file_path)
end
end
end
end