-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.rb
executable file
·228 lines (160 loc) · 5.3 KB
/
common.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require_relative 'tools/copyrighter/copyright'
require 'colorize'
module Builder
# prints header
def print_header(message)
puts message.blue
end
def print_progress(message)
puts message
end
############################################################
# check condition and raise exception
def fail_script(message)
raise "Build failed! #{message}"
end
############################################################
# check condition and raise exception
def fail_script_if(condition, message)
fail_script message if condition
end
############################################################
# check condition and raise exception
def fail_script_unless(condition, message)
fail_script message unless condition
end
############################################################
# check if file exists and raise exception
def fail_script_unless_file_exists(path)
fail_script_unless path != nil && (File.directory?(path) || File.exists?(path)), "File doesn't exist: '#{path}'"
end
############################################################
def not_nil(value)
fail_script_unless value != nil, 'Value is nil'
return value
end
############################################################
def extract_regex(text, pattern)
text =~ pattern
return $1
end
############################################################
# checks if path exists and returns it
def resolve_path(path)
fail_script_unless_file_exists path
return path
end
############################################################
def build_ios_app(proj_dir, proj_name, configuration, target, export_path = nil)
fail_script_unless_file_exists proj_dir
Dir.chdir(proj_dir) do
dir_build = 'build'
sdk_name = 'iphoneos'
# cleanup
FileUtils.rm_rf(dir_build)
# build
cmd = %(xcodebuild -project "#{proj_name}.xcodeproj" -configuration "#{configuration}" -target "#{target}" -sdk #{sdk_name})
exec_shell(cmd, "Can't build ios app")
# result file
export_path = "build/#{configuration}-#{sdk_name}" if export_path.nil?
Dir.chdir export_path do
app_files = Dir['*.app']
fail_script_unless app_files.length == 1, "Unexpected apps count: #{app_files.join(',')}"
app_file = app_files.first
return File.expand_path app_file
end
end
end
############################################################
# execute shell command and raise exception if fails
def exec_shell(command, error_message, options = {})
puts "Running command: #{command}" unless options[:silent] == true
result = `#{command}`
if options[:dont_fail_on_error] == true
puts error_message unless $?.success?
else
fail_script_unless($?.success?, "#{error_message}\nShell failed: #{command}\n#{result}")
end
return result
end
def delete_file(path)
if Dir.exist? path
FileUtils.rm_rf path
elsif File.exist? path
FileUtils.rm_f path
end
end
def list_files(dir, options = {})
files = []
ignored_files = options[:ignored_files] || []
types = options[:types]
list_directories = options[:list_directories]
Dir["#{dir}/*"].each {|file|
next if ignored_files.include?(File.basename file)
if File.directory? file
files.push file if list_directories
files.push *(list_files file, options)
else
next if types && !types.include?(File.extname file)
files.push file
end
}
return files
end
def fix_copyrights(dir_project, dir_headers, options = {})
print_header 'Fixing copyright...'
file_header = resolve_path "#{dir_headers}/copyright.txt"
copyright_header = File.read file_header
files = list_files dir_project, options
modified_files = []
files.each {|file|
modified_files.push file if fix_copyright(file, copyright_header)
}
return modified_files
end
def fix_copyright(file, header)
old_source = File.read file
source_no_header = Copyright.remove_header_comment old_source
copyright = Copyright.new header
copyright.set_param 'date.year', Time.now.year.to_s
copyright.set_param 'file.name.ext', File.basename(file)
copyright.set_param 'file.name', File.basename(file, '.*')
new_source = copyright.process
new_source << "\n\n"
new_source << source_no_header
if new_source != old_source
File.open(file, 'w') { |f|
f.write new_source
}
return true
end
return false
end
def get_release_notes(dir_repo, version)
header = "## v.#{version}"
file_release_notes = resolve_path "#{dir_repo}/CHANGELOG.md"
lines = File.readlines file_release_notes
start_index = -1
end_index = -1
(0 .. lines.length - 1).each do |index|
line = lines[index]
if line.include? header
start_index = index + 1
break
end
end
(start_index + 1 .. lines.length - 1).each do |index|
line = lines[index]
if line =~ /## v\.\d+\.\d+\.\d+/
end_index = index - 1
break
end
end
fail_script_unless start_index != -1 && end_index != -1, "Can't extract release notes"
notes = lines[start_index..end_index].join
notes.strip!
notes.gsub! '"', '\\"'
notes.gsub! '``', '\\`'
return notes
end
end