-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathRakefile
255 lines (213 loc) · 5.8 KB
/
Rakefile
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
require "fileutils"
#
# Release
#
class VersionedFile
def initialize(file, regex)
@file = file
@regex = regex
end
def current_version!
@current_version ||= matched_data![1]
end
def bump_version!(type)
position = case type
when :major
0
when :minor
1
when :patch
2
end
@current_version = current_version!.split(".").tap do |v|
v[position] = v[position].to_i + 1
# Reset consequent numbers
((position + 1)..2).each { |p| v[p] = 0 }
end.join(".")
end
def save!
text = File.read(@file)
new_line = matched_data![0].gsub(matched_data![1], @current_version)
text.gsub!(matched_data![0], new_line)
File.open(@file, "w") { |f| f.puts text }
end
private
def matched_data!
@matched_data ||= begin
m = @regex.match File.read(@file)
raise "No version #{@regex} matched in #{@file}" unless m
m
end
end
end
def fullpath(file)
File.expand_path(file, File.dirname(__FILE__))
end
VERSION_FILES = {
fullpath("README.md") => /v(\d+.\d+.\d+)/,
fullpath("commands/version.go") => /^const Version = "(\d+.\d+.\d+)"$/,
fullpath(".goxc.json") => /"PackageVersion": "(\d+.\d+.\d+)"/,
fullpath("homebrew-gh/gh.rb") => /VERSION = "(\d+.\d+.\d+)"/
}
class Git
class << self
def dirty?
!`git status -s`.empty?
end
def checkout
`git checkout .`
end
def commit_all(msg)
`git commit -am "#{msg}"`
end
def create_tag(tag, msg)
`git tag -a #{tag} -m "#{msg}"`
end
end
end
namespace :release do
desc "Current released version"
task :current do
vf = VersionedFile.new(*VERSION_FILES.first)
puts vf.current_version!
end
[:major, :minor, :patch].each do |type|
desc "Release #{type} version"
task type do
if Git.dirty?
puts "Please commit all changes first"
exit 1
end
new_versions = VERSION_FILES.map do |file, regex|
begin
vf = VersionedFile.new(file, regex)
current_version = vf.current_version!
vf.bump_version!(type)
vf.save!
puts "Successfully bump #{file} from #{current_version} to #{vf.current_version!}"
vf.current_version!
rescue => e
Git.checkout
raise e
end
end
require "set"
new_versions = new_versions.to_set
if new_versions.size != 1
raise "More than one version found among #{VERSION_FILES}"
end
new_version = "v#{new_versions.first}"
msg = "Bump version to #{new_version}"
Git.commit_all(msg)
Git.create_tag(new_version, msg)
end
end
end
#
# Build (deprecated, prefer gh_task.go)
#
module OS
class << self
def type
if darwin?
"darwin"
elsif linux?
"linux"
elsif windows?
"windows"
else
raise "Unknown OS type #{RUBY_PLATFORM}"
end
end
def dropbox_dir
if darwin? || linux?
File.join ENV["HOME"], "Dropbox"
elsif windows?
File.join ENV["DROPBOX_DIR"]
else
raise "Unknown OS type #{RUBY_PLATFORM}"
end
end
def windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def darwin?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def linux?
(/linux/ =~ RUBY_PLATFORM) != nil
end
end
end
namespace :build do
desc "Build for current operating system"
task :current => [:update_goxc, :remove_build_target, :build_gh, :move_to_dropbox]
task :update_goxc do
puts "Updating goxc..."
result = system "go get -u github.com/laher/goxc"
raise "Fail to update goxc" unless result
end
task :remove_build_target do
FileUtils.rm_rf fullpath("target")
end
task :build_gh do
puts "Building for #{OS.type}..."
puts `goxc -wd=. -os=#{OS.type} -c=#{OS.type}`
end
task :move_to_dropbox do
vf = VersionedFile.new(*VERSION_FILES.first)
build_dir = fullpath("target/#{vf.current_version!}-snapshot")
dropbox_dir = File.join(OS.dropbox_dir, "Public", "gh")
FileUtils.cp_r build_dir, dropbox_dir, :verbose => true
end
end
#
# Tests
#
task :default => [:features]
desc "Run cucumber feature tests from Hub"
task :features do
features = ENV.fetch("FEATURE", "features")
profile = ENV.fetch("PROFILE", "default")
sh "cucumber -p #{profile} -t ~@wip #{features}"
end
#
# Manual
#
desc "Show man page"
task :man => "man:build" do
exec "man man/gh.1"
end
desc "Build man pages"
task "man:build" => ["man/gh.1", "man/gh.1.html"]
extract_examples = lambda { |readme_file|
# split readme in sections
examples = File.read(readme_file).split(/^-{4,}$/)[6].strip
examples.sub!(/^.+?(###)/m, '\1') # strip intro paragraph
examples.sub!(/\n+.+\Z/, '') # remove last line
examples
}
# inject examples from README file to .ronn source
source_with_examples = lambda { |source, readme|
examples = extract_examples.call(readme)
compiled = File.read(source)
compiled.sub!('{{README}}', examples)
compiled
}
# generate man page with ronn
compile_ronn = lambda { |destination, type, contents|
File.popen("ronn --pipe --#{type} --organization=GITHUB --manual='gh Manual'", 'w+') { |io|
io.write contents
io.close_write
File.open(destination, 'w') { |f| f << io.read }
}
abort "ronn --#{type} conversion failed" unless $?.success?
}
file "man/gh.1" => ["man/gh.1.ronn", "README.md"] do |task|
contents = source_with_examples.call(*task.prerequisites)
compile_ronn.call(task.name, 'roff', contents)
compile_ronn.call("#{task.name}.html", 'html', contents)
end
file "man/gh.1.html" => ["man/gh.1.ronn", "README.md"] do |task|
Rake::Task["man/gh.1"].invoke
end