-
Notifications
You must be signed in to change notification settings - Fork 740
/
Rakefile
63 lines (54 loc) · 2.01 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
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be
# available to Rake.
require File.expand_path('config/application', __dir__)
Rails.application.load_tasks
require 'fileutils'
require 'yaml'
desc 'Quick command to execute the dockerized terminal for running commands'
task app_cli: :environment do
# Run the command ignoring the results
sh('docker-compose', '-f', 'docker-compose.yml',
'-f', 'docker-compose.test.yml',
'run', '--rm', 'app', 'bash') || true
end
desc 'Quick command to launch the compose stack'
task app_run: :environment do
# Run the command ignoring the results
sh('docker-compose', '-f', 'docker-compose.yml', 'up') || true
end
desc 'Automate the Config Setup for New Environments'
task setup_workspace: :environment do
SECRETS = {
SECRET_KEY_BASE: generate_secret,
DEVISE_SECRET_KEY: generate_secret
}.freeze
%w[development test].each do |environment|
example = Rails.root.join('config', 'env', "#{environment}.example.env")
target = Rails.root.join('config', 'env', "#{environment}.env")
FileUtils.cp(example, target)
# insert the secrets into the file
content = File.read(target)
%w[SECRET_KEY_BASE DEVISE_SECRET_KEY].each do |key|
content.sub!(%(#{key}=""), %(#{key}="#{SECRETS[key.to_sym]}"))
end
File.write(target, content)
end
puts 'Workspace setup completed successfully 🎉'
rescue StandardError => e
puts "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
Rails.logger.error "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
end
# Run the secret task, grab the output, strip the new lines
def generate_secret
s = StringIO.new
oldstdout = $stdout
$stdout = s
Rake::Task['secret'].invoke
s.string.strip
ensure
# Must renable the task or else it won't execute again
Rake::Task['secret'].reenable
$stdout = oldstdout
end