forked from umdio/umdio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
245 lines (200 loc) · 6.55 KB
/
Rakefile
File metadata and controls
245 lines (200 loc) · 6.55 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'net/http'
require 'json'
require_relative 'app/helpers/courses_helpers'
include Sinatra::UMDIO::Helpers
################################################################################
################################## FUNCTIONS ###################################
################################################################################
# Checks if openapi.yaml is a valid OpenAPI spec. Throws if the spec is invalid,
# otherwise returns true.
def validate_openapi
File.open 'openapi.yaml' do |openapi|
validator_url = URI("https://validator.swagger.io/validator/debug")
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/yaml' }
res = Net::HTTP.post(validator_url, openapi.read, headers)
parsed = JSON.parse res.body
if parsed['messages']
messages = parsed['schemaValidationMessages'].map{ |m| m['message'] }.join(', ')
raise StandardError.new "Invalid openapi spec: #{messages}"
end
end
return true
end
# @param [Array<String>]
# @return [Array<String>]
def get_semesters(args)
semesters = args.map do |e|
if e.length == 6
e
else
[e + '01', e + '05', e + '08', e + '12']
end
end
semesters.flatten
end
# Scrapes the current semester from Testudo
def scrape_courses(sems)
sh "ruby app/scrapers/courses_scraper.rb #{sems}"
sh "ruby app/scrapers/sections_scraper.rb #{sems}"
end
# Imports old semesters from flat files
def import_courses(sems)
sems = get_semesters(sems)
sems.each { |s| sh "ruby app/scrapers/courses_importer.rb #{s}" }
end
def scrape_bus
sh 'ruby app/scrapers/bus_routes_scraper.rb'
sh 'ruby app/scrapers/bus_schedules_scraper.rb'
end
def scrape_majors
sh 'ruby app/scrapers/majors_scraper.rb'
end
def scrape_map(args = '')
sh "ruby app/scrapers/map_scraper.rb #{args}"
end
################################################################################
#################################### TASKS #####################################
################################################################################
################################### Imports ####################################
desc 'Import previously scraped data from the umdio-data repo'
task import: ['import:courses']
namespace :import do
desc 'Import a specific semester'
task :semester, [:sem] do |_task, args|
import_courses([args[:sem]])
end
desc 'Import all past semesters'
task :courses do
years = %w[201708 201712 201801 201805 201808 201812 201901 201901 201905 201908 201912]
import_courses(years)
end
desc 'Import map data'
task :map do
scrape_map './data/umdio-data/umd-building-gis.json'
end
end
# TODO: Add export - see https://github.com/umdio/umdio-data/blob/master/courses/download-sem.rb
################################### Scraping ###################################
desc 'Scrape live data to fill databases'
task scrape: ['scrape:courses', 'scrape:bus', 'scrape:map', 'scrape:majors']
# TODO: Move this to an import task, once other datatypes are importable
desc 'Scrapes enough to run the tests'
task :test_scrape do
import_courses(['201808'])
scrape_bus
scrape_majors
scrape_map './data/umdio-data/umd-building-gis.json'
end
namespace :scrape do
desc 'Run bus route scrapers'
task :bus do
scrape_bus
end
desc 'Run course scrapers'
task :courses do
scrape_courses(current_and_next_semesters.join(' '))
end
desc 'Run course seat updater'
task :seats do
semesters = current_and_next_semesters
sh "ruby app/scrapers/sections_scraper.rb #{semesters.join(' ')}"
end
desc 'Run map scraper'
task :map do
scrape_map
end
desc 'Majors scraper'
task :majors do
scrape_majors
end
desc 'Scrapes only the current semester courses/sections'
task :current do
scrape_courses(current_semester)
end
desc 'Scrape a specific semester'
task :semester, [:sem] do |_task, args|
scrape_courses(args[:sem])
end
end
##################################### Dev ######################################
# run with 'rake rubocop' or 'rake rubocop:auto_correct' to apply safe fixes
desc 'Run RuboCop'
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rake'
task.requires << 'rubocop-rspec'
task.requires << 'rubocop-sequel'
end
task lint: :rubocop
namespace :dev do
# docker-compose command with root dev args
dc = 'docker-compose -f docker-compose-dev.yml'
desc 'Connect to the database with a SQL shell'
task :db do
system "#{dc} exec postgres psql umdio postgres"
end
desc 'Launches the dev environment with docker-compose'
task :up do
system "#{dc} up --build -d"
end
desc 'Stop and remove the dev environment (containers, networks, volumes, etc)'
task :down do
system "#{dc} down"
end
desc 'Start existing services previously stopped with dev:stop'
task :start do
system "#{dc} start"
end
desc 'Stop running services without removing them'
task :stop do
system "#{dc} stop"
end
desc 'Force a complete rebuild of all containers without using cached layers'
task :rebuild do
system "#{dc} build --no-cache --progress tty"
end
end
#################################### Server ####################################
desc 'Start the web server for dev'
task :up do
system 'shotgun -p 3000 -o 0.0.0.0'
end
task server: :up
desc 'Start the web server for prod'
task :prod do
system 'puma -p 3000'
end
desc 'Sinatra console'
task :console do
system 'bundle exec irb -r ./config.ru'
end
task c: :console
################################### Testing ####################################
desc 'Run tests in /tests that look like *_spec.rb'
RSpec::Core::RakeTask.new :test do |task|
task.pattern = Dir['tests/**/*_spec.rb']
task.rspec_opts = '--format documentation' # default to verbose testing, comment for silence
end
task spec: :test
namespace :test do
desc 'Run tests in /tests/v1 that look like *_spec.rb'
RSpec::Core::RakeTask.new :v1 do |task|
task.pattern = Dir['tests/v1/*_spec.rb']
task.rspec_opts = '--format documentation' # default to verbose testing, comment for silence
end
desc 'Run all tests in /tests in parallel'
task :parallel do
system 'parallel_rspec --type rspec -- -f documentation -- tests/**/*_spec.rb'
end
end
desc 'Type check and lint codebase'
task :validate do
system 'bundle exec solargraph scan', exception: true
system 'bundle exec solargraph typecheck' # TODO(don): add 'exception: true', right now this breaks
puts 'validating OpenAPI Spec'
validate_openapi
puts 'Spec is valid'
Rake::Task['rubocop'].execute
end
task default: ['up']