-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
79 lines (63 loc) · 1.7 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
# frozen_string_literal: true
require 'dotenv/load'
load 'migration/database.rake'
ENV['APP_ENV'] = ENV['RACK_ENV']
task default: %w[run]
task :run do
case ENV['RACK_ENV']
when 'development'
Rake::Task['rundev'].invoke
when 'test'
Rake::Task['test'].invoke
when 'production'
Rake::Task['runsys'].invoke
end
end
task :runsys do
bundle exec 'puma'
end
task :rundev do
bundle exec 'foreman start'
end
task :test do
ENV['RACK_ENV'] = ENV['APP_ENV'] = 'test'
sh 'rspec .'
end
task :routes do
$LOAD_PATH.unshift File.join(File.expand_path(File.join(__dir__, '.')), 'api')
@routes = {}
def map(relative_path)
@current_map = relative_path
@routes[@current_map] = {}
yield
end
def middleware_controllers(klass)
klass.instance_variable_get(:@middleware).collect { |arr| arr[0] }
.filter { |mid| mid.instance_variable_defined?(:@routes) }
end
def register_route(klass)
sub_routes = klass.routes.map do |method, paths|
[method, paths.map { |path| "#{@current_map.chomp('/')}#{path[0]}" }]
end.to_h
@routes[@current_map].merge!(klass.to_s => sub_routes)
end
def run(main_klass)
klasses = [main_klass] + middleware_controllers(main_klass)
klasses.each { |klass| register_route klass }
end
require_relative 'api/application_controller'
map('/') { run Api::Controller::ApplicationController }
print_pretty @routes
end
def print_pretty(routes_hash)
routes_hash.each do |main, sub|
print "#{main} =>"
sub.each do |controller, methods|
puts " #{controller}"
methods.each do |method, occurences|
puts "\t\t#{method}"
occurences.each { |route| puts "\t\t\t\t#{route}" }
end
end
end
end