forked from owenthereal/gh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
186 lines (157 loc) · 4.21 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
require "fileutils"
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("commands/version.go") => /^const Version = "(\d+.\d+.\d+)"$/,
fullpath("README.md") => /Current version is \[(\d+.\d+.\d+)\]/,
fullpath(".goxc.json") => /"PackageVersion": "(\d+.\d+.\d+)"/,
fullpath("homebrew/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
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