Skip to content

Commit c9c7dc2

Browse files
committed
A better script for bumping the version
1 parent f7d6ab5 commit c9c7dc2

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

script/version_bump.rb

+60-27
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,88 @@
33
# Increases the version. e.g., from 0.8.5 to 0.8.6.
44
# If you want to bump the minor or major version numbers, do it manually
55
# or edit lib/version.rb before running this file.
6-
#
7-
# Optional arguments:
8-
# no-commit: Don't commit the file changes
9-
# no-push: Don't push the commits to github. no-commit implies no-push too.
106

11-
VERSION_FILE_PATH = File.expand_path('../../lib/version.rb', __FILE__)
7+
usage = <<-END
8+
9+
Arguments:
10+
<version>: The new version. Must have at least 2 parts. Examples: 0.9.8, 0.10, 0.9.7.3
11+
no-commit: Don't commit the changes.
12+
push: Push the commits to github. If used by itself without the version argument,
13+
it's assumed that the commit and tags are ready to be pushed.
14+
15+
Example:
16+
17+
To update the version in one step, and then push the commits in a second step:
1218
13-
puts '', "Updating #{VERSION_FILE_PATH}..."
19+
ruby script/version_bump.rb 0.9.7.3
20+
ruby script/version_bump.rb push
21+
22+
To do everything in one step:
23+
24+
ruby script/version_bump.rb 0.9.8 push
25+
26+
END
27+
28+
VERSION_FILE_PATH = File.expand_path('../../lib/version.rb', __FILE__)
1429

15-
contents = ''
16-
tiny_version_bumped = false
17-
File.open(VERSION_FILE_PATH) do |f|
18-
line = f.read
19-
m = /TINY\s*=\s*([\d]+)/.match(line)
20-
tiny_version_bumped = true if m
21-
contents += m ? line.sub(m[0], m[0].sub(m[1], (m[1].to_i + 1).to_s)) : line
30+
if ARGV.length < 1
31+
puts usage
32+
exit 1
2233
end
2334

24-
unless tiny_version_bumped
25-
puts "ERROR: couldn't update lib/version.rb. Is it missing the TINY constant?"
35+
new_version = ARGV[0].split('.')
36+
if new_version.length < 2 and !ARGV.include?('push')
37+
puts "First argument must be a version number with at least 2 parts. Examples: 0.9.8, 0.10, 0.9.7.3"
2638
exit 1
2739
end
2840

29-
puts "Saving..."
41+
update_version_file = new_version.length >= 2
42+
43+
if update_version_file
44+
puts '', "New Version: #{new_version.join('.')}", "Updating #{VERSION_FILE_PATH}..."
45+
46+
contents = ''
47+
tiny_version_bumped = false
48+
File.open(VERSION_FILE_PATH) do |f|
49+
contents = f.read
50+
['MAJOR', 'MINOR', 'TINY', 'PRE'].each_with_index do |name, i|
51+
r = Regexp.new(name + '\s*=\s*(nil|[\d]+)')
52+
m = r.match(contents)
53+
v = new_version[i].to_i > 0 ? new_version[i] : (name == 'PRE' ? 'nil' : '0')
54+
contents.sub!(m[0], m[0].sub(m[1], v)) if m
55+
end
56+
end
57+
58+
puts "Saving..."
3059

31-
File.open(VERSION_FILE_PATH, 'w+') do |f|
32-
f.write(contents)
60+
File.open(VERSION_FILE_PATH, 'w+') do |f|
61+
f.write(contents)
62+
end
3363
end
3464

3565
require File.expand_path('../../lib/version', __FILE__)
3666

3767
version = Discourse::VERSION::STRING
3868
puts "New version is: #{version}"
3969

40-
puts ARGV
41-
42-
unless ARGV.include?('no-commit')
43-
puts "Commiting..."
70+
unless ARGV.include?('no-commit') or !update_version_file
71+
puts "Committing..."
4472

4573
`git add lib/version.rb`
4674
`git commit -m "Version bump to v#{version}"`
4775
sha = `git rev-parse HEAD`.strip
76+
`git tag -d latest-release`
77+
`git push origin :latest-release`
4878
`git tag -a v#{version} -m "version #{version}" #{sha}`
79+
`git tag -a latest-release -m "latest release" #{sha}`
80+
end
4981

50-
unless ARGV.include?('no-push')
51-
puts "Pushing..."
52-
`git push origin master`
53-
`git push origin v#{version}`
54-
end
82+
if ARGV.include?('push')
83+
puts "Pushing..."
84+
85+
`git push origin master`
86+
`git push origin v#{version}`
87+
`git push origin latest-release`
5588
end
5689

5790
puts "Done",''

0 commit comments

Comments
 (0)