-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
167 lines (131 loc) · 3.71 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
# frozen_string_literal: true
require 'rake/clean'
require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop) do |t|
t.options = ['--display-cop-names']
end
desc 'Lint code'
task lint: :rubocop
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs.push 'test'
t.test_files = FileList['test/client/**/*.rb', 'test/server/**/*.rb']
end
task :test do # rubocop:disable Rake/Desc
warn ''
warn 'Running integration tests'
warn ''
sh '.local/bin/test'
end
require 'rubygems/tasks'
Gem::Tasks.new console: false do |tasks|
tasks.push.host = ENV.fetch('RUBYGEMS_HOST', Gem::DEFAULT_HOST)
end
CLEAN.include('*.gem', 'pkg')
if Dir.exist? 'man'
desc 'Generate manual pages'
task :man do
sh 'cd man/tr && ronn *.ronn' if Dir.exist? 'man/tr'
sh 'cd man/en && ronn *.ronn' if Dir.exist? 'man/en'
end
CLEAN.include('man/**/*[0-9].html')
CLOBBER.include('man/**/*.[0-9]')
else
task :man # rubocop:disable Rake/DuplicateTask
end
desc 'Bump version (use "major", "minor", "patch", "pre" or "auto")'
task :bump do
Bumper.bump ARGV[1]
exit
end
task default: %i[lint test]
class Bumper
Version = Struct.new :major, :minor, :patch, :pre do
def to_s
[major, minor, patch].join('.') + (pre ? ".#{pre}" : '')
end
def newer?(other)
return true if !other || other.empty?
Gem::Version.new(to_s) > Gem::Version.new(other.to_s)
end
def self.parse(string)
new(*string.strip.gsub(/^v?/, '').split('.', 4))
end
private_class_method :new
end
def self.bump(arg, version_file = 'VERSION')
new(version_file).bump(arg)
end
attr_reader :latest, :current
def initialize(version_file = 'VERSION')
git_ensure_clean
@version_file = version_file
@current = Version.parse File.read(version_file)
@latest = Version.parse git_latest_tag
end
def bump(arg)
self.current = new_version(arg)
commit(update_version_file, *update_ruby_sources)
end
private
attr_reader :version_file
attr_writer :current
def new_version(arg = nil)
return bump_by_auto if !arg || arg == 'auto'
if %i[major minor patch pre].include? arg.to_sym
bump_by_part(arg)
else
bump_by_manual(arg)
end
end
def bump_by_part(part)
target = (new_version = current.dup).send part
new_version.send "#{part}=", target&.succ
new_version
end
def bump_by_manual(new_string)
Version.parse new_string
end
def bump_by_auto
Version.parse current.to_s.succ
end
def write_version(file, version)
content = File.read(file)
content.gsub!(/^(\s*)VERSION(\s*)= .*?$/, "\\1VERSION = '#{version}'")
raise "Could not insert VERSION in #{file}" unless Regexp.last_match
File.write(file, content)
end
def update_ruby_sources
updated = []
FileList['**/version.rb'].each do |file|
warn "Updating #{file} for the new version: #{current}"
write_version(file, current.to_s)
updated << file
end
updated
end
def update_version_file
warn "Updating #{version_file} for the new version: #{current}"
File.write(version_file, "#{current}\n")
version_file
end
def commit(*files)
if current.newer?(latest)
git_commit("Bump version to #{current}", *files)
else
warn "version #{current} is not newer than #{latest}; skipping commit"
end
end
def git_latest_tag
tag = `git describe --abbrev=0 --tags 2>/dev/null`
warn 'No tag found; make a release first.' unless tag
tag
end
def git_commit(message, *files)
system('git', 'commit', '-m', message, *files) || abort('Git commit failed')
end
def git_ensure_clean
return if `git status --untracked-files=no --porcelain`.strip.empty?
abort 'There are uncommitted changes in the repository.'
end
end