-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rb
148 lines (103 loc) · 3.37 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
require_relative 'tools/copyrighter/copyright'
# check condition and raise exception
def print_header(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_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
############################################################
# 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 fix_copyrights(dir_project, dir_headers)
print_header 'Fixing copyright...'
modified_files = []
file_header = resolve_path "#{dir_headers}/copyright.txt"
copyright_header = File.read file_header
Dir["#{dir_project}/**/*.cs"].each do |file|
next if File.basename(file) == 'Plist.cs' # FIXME: make a better filter for ignored files
modified_files.push file if fix_copyright(file, copyright_header)
end
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! '"', '\\"'
return notes
end