This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Rakefile
178 lines (142 loc) · 3.89 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
require 'fileutils'
require 'launchy'
require 'json'
require 'yaml'
require 'rspec/core/rake_task'
desc 'Run RSpec examples'
RSpec::Core::RakeTask.new(:spec) do |config|
config.ruby_opts = '-I./spec/v2'
end
def add_rspec_task(tag)
desc "Run RSpec #{tag} examples"
RSpec::Core::RakeTask.new(:"spec:#{tag}") do |config|
config.ruby_opts = '-I./spec/v2'
config.pattern = "spec/**/#{tag}_spec.rb"
end
end
desc 'Run RSpec examples with code coverage'
RSpec::Core::RakeTask.new(:coverage) do |config|
config.ruby_opts = '-r ./spec/coverage.rb -I./spec/v2'
end
desc 'Run RSpec examples and upload coverage stats'
RSpec::Core::RakeTask.new(:'coverage:upload') do |config|
config.ruby_opts = '-r ./spec/coverage-upload.rb -I./spec/v2'
end
# Add an RSpec task for each individual spec file.
tags = Dir['**/*_spec.rb'].map { |file| file =~ /([^\/]*)_spec\.rb$/i; $1 }
tags.each(&method(:add_rspec_task))
class GemInfo
def initialize
@gemspec_filename = Dir['*.gemspec'][0]
end
def spec
@spec ||= eval(File.read(@gemspec_filename))
end
def name
@name ||= spec.name
end
def version
@version ||= spec.version.to_s
end
def gem_filename
"#{name}-#{version}.gem"
end
def gemspec_filename
@gemspec_filename
end
end
$gem = GemInfo.new
desc "Start irb #{$gem.name} session"
task :irb do
sh "irb -rubygems -I./lib -r ./lib/#{$gem.name.gsub('-', '/')}.rb"
end
desc "Install #{$gem.name} gem"
task :'gem:install' => :'gem:build' do
gemfile = "gem/#{$gem.gem_filename}"
if !gemfile.nil?
sh "gem install --no-ri --no-rdoc #{gemfile}"
else
puts 'Could not find gem.'
end
end
desc "Uninstall #{$gem.name} gem"
task :'gem:uninstall' do
sh "gem uninstall #{$gem.name} -x"
end
desc "Build #{$gem.name} gem"
task :'gem:build' do
FileUtils.mkdir_p('gem')
sh "gem build #{$gem.gemspec_filename}"
FileUtils.mv $gem.gem_filename, 'gem'
end
desc "Release #{$gem.name} v#{$gem.version} and tag in git"
task :'gem:release' => [:not_root, :'gem:build'] do
if (`git` rescue nil).nil?
abort 'Could not run git command.'
end
if (`gem` rescue nil).nil?
abort 'Could not run gem command.'
end
unless `git branch --no-color`.strip =~ /^\*\s+master$/
abort 'You must release from the master branch.'
end
unless `git status` =~ /^nothing to commit/m
abort 'You cannot release with outstanding changes (see git status).'
end
version = $gem.version
tag = "v#{version}"
if `git tag`.strip =~ /^#{tag}$/
abort "Tag #{tag} already exists, you must bump version in version.rb."
end
puts "Releasing version #{version}."
sh "git commit --allow-empty -a -m \"Release #{version}.\""
sh "git tag #{tag}"
sh 'git push origin master'
sh "git push origin #{tag}"
sh "gem push gem/#{$gem.gem_filename}"
puts 'Fin.'
end
desc 'Run YARD server to view docs'
task :yard do
if Dir.exists?('.yardoc')
FileUtils.rm_rf('.yardoc')
end
yard = Thread.start {
system('yard server --reload')
}
Launchy.open('http://localhost:8808/docs/index')
yard.join
end
desc 'Log a Twitch request as YAML'
task :request do
$stderr.print 'Path: '
path = $stdin.gets.strip.sub(/^\/+/, '')
url = "https://api.twitch.tv/kraken/#{path}"
$stderr.puts "Requesting #{url}."
content = `curl -k -H "Accept: application/vnd.twitchtv.v2+json" "#{url}"`
$stdout.puts "#{url}:"
hash = JSON.parse(content)
yaml = YAML.dump(hash)
yaml.lines.drop(1).each do |line|
$stdout.puts " #{line}"
end
end
desc 'View code coverage results'
task :'coverage:view' do
file = File.absolute_path('coverage/index.html')
if !File.exists?(file)
$stderr.puts "Coverage results not present. Run 'rake coverage' first."
exit
end
url = "file:///#{file}"
Launchy.open(url)
end
task :not_root do
if !(`whoami` rescue nil).nil?
if `whoami`.strip == 'root'
abort 'Do not run as root.'
end
end
end
desc 'Run tests'
task :default => :spec