forked from jeremyevans/roda-sequel-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
171 lines (143 loc) · 4.37 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
# Migrate
migrate = lambda do |env, version|
ENV['RACK_ENV'] = env
require_relative 'db'
require 'logger'
Sequel.extension :migration
DB.loggers << Logger.new($stdout) if DB.loggers.empty?
Sequel::Migrator.apply(DB, 'migrate', version)
end
desc "Migrate test database to latest version"
task :test_up do
migrate.call('test', nil)
end
desc "Migrate test database all the way down"
task :test_down do
migrate.call('test', 0)
end
desc "Migrate test database all the way down and then back up"
task :test_bounce do
migrate.call('test', 0)
Sequel::Migrator.apply(DB, 'migrate')
end
desc "Migrate development database to latest version"
task :dev_up do
migrate.call('development', nil)
end
desc "Migrate development database to all the way down"
task :dev_down do
migrate.call('development', 0)
end
desc "Migrate development database all the way down and then back up"
task :dev_bounce do
migrate.call('development', 0)
Sequel::Migrator.apply(DB, 'migrate')
end
desc "Migrate production database to latest version"
task :prod_up do
migrate.call('production', nil)
end
# Shell
irb = proc do |env|
ENV['RACK_ENV'] = env
trap('INT', "IGNORE")
dir, base = File.split(FileUtils::RUBY)
cmd = if base.sub!(/\Aruby/, 'irb')
File.join(dir, base)
else
"#{FileUtils::RUBY} -S irb"
end
sh "#{cmd} -r ./models"
end
desc "Open irb shell in test mode"
task :test_irb do
irb.call('test')
end
desc "Open irb shell in development mode"
task :dev_irb do
irb.call('development')
end
desc "Open irb shell in production mode"
task :prod_irb do
irb.call('production')
end
# Specs
spec = proc do |type|
desc "Run #{type} specs"
task :"#{type}_spec" do
sh "#{FileUtils::RUBY} -w spec/#{type}.rb"
end
desc "Run #{type} specs with coverage"
task :"#{type}_spec_cov" do
ENV['COVERAGE'] = type
sh "#{FileUtils::RUBY} spec/#{type}.rb"
ENV.delete('COVERAGE')
end
end
spec.call('model')
spec.call('web')
desc "Run all specs"
task default: [:model_spec, :web_spec]
desc "Run all specs with coverage"
task :spec_cov do
ENV['RODA_RENDER_COMPILED_METHOD_SUPPORT'] = 'no'
FileUtils.rm_r('coverage') if File.directory?('coverage')
Dir.mkdir('coverage')
Rake::Task['_spec_cov'].invoke
end
task _spec_cov: [:model_spec_cov, :web_spec_cov]
# Other
desc "Annotate Sequel models"
task "annotate" do
ENV['RACK_ENV'] = 'development'
require_relative 'models'
DB.loggers.clear
require 'sequel/annotate'
Sequel::Annotate.annotate(Dir['models/**/*.rb'])
end
last_line = __LINE__
# Utils
desc "give the application an appropriate name"
task :setup, [:name] do |t, args|
unless name = args[:name]
$stderr.puts "ERROR: Must provide a name argument: example: rake \"setup[AppName]\""
exit(1)
end
require 'securerandom'
require 'fileutils'
lower_name = name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
upper_name = lower_name.upcase
random_bytes = lambda{[SecureRandom.random_bytes(64).gsub("\x00"){((rand*255).to_i+1).chr}].pack('m').inspect}
File.write('.env.rb', <<END)
case ENV['RACK_ENV'] ||= 'development'
when 'test'
ENV['#{upper_name}_SESSION_SECRET'] ||= #{random_bytes.call}.unpack('m')[0]
ENV['#{upper_name}_DATABASE_URL'] ||= "postgres:///#{lower_name}_test?user=#{lower_name}"
when 'production'
ENV['#{upper_name}_SESSION_SECRET'] ||= #{random_bytes.call}.unpack('m')[0]
ENV['#{upper_name}_DATABASE_URL'] ||= "postgres:///#{lower_name}_production?user=#{lower_name}"
else
ENV['#{upper_name}_SESSION_SECRET'] ||= #{random_bytes.call}.unpack('m')[0]
ENV['#{upper_name}_DATABASE_URL'] ||= "postgres:///#{lower_name}_development?user=#{lower_name}"
end
END
%w'views/layout.erb routes/prefix1.rb config.ru app.rb db.rb spec/web/spec_helper.rb spec/web/prefix1_spec.rb'.each do |f|
File.write(f, File.read(f).gsub('App', name).gsub('APP', upper_name))
end
File.write(__FILE__, File.read(__FILE__).split("\n")[0...(last_line-2)].join("\n") << "\n")
File.write('.gitignore', "/.env.rb\n/coverage\n")
File.delete('public/.gitkeep')
File.delete('.ci.gemfile')
FileUtils.remove_dir('stack-spec')
FileUtils.remove_dir('.github')
end
Rake::Task["default"].clear
desc "Run specs to make sure stack works properly"
task :default do
sh "#{FileUtils::RUBY} -w stack-spec/stack_spec.rb"
end
desc "Run specs to make sure stack works properly"
task :stack_spec_debug do
ENV['DEBUG'] = '1'
sh "#{FileUtils::RUBY} -w stack-spec/stack_spec.rb"
end