-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.rb
132 lines (105 loc) · 4.2 KB
/
template.rb
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
require 'fileutils'
require 'shellwords'
YARG_REPO = 'https://github.com/RoundtableDevelopment/yarg.git'.freeze
RAILS_REQUIREMENT = '~> 5.2.0'.freeze
def build_app!
debug_print('Checking your environment setup...')
# Template set up checks
assert_minimum_rails_version
add_template_repository_to_source_path
debug_print('Copying root config files...')
# Copy root config files
template 'ruby-version.tt', '.ruby-version', force: true
template 'Gemfile.tt', 'Gemfile', force: true
copy_file 'gitignore', '.gitignore', force: true
template 'example.env.tt'
copy_file 'Procfile'
copy_file 'Procfile.dev'
template 'README.md.tt', force: true
debug_print('Templating app...')
# Copy base application files
apply 'app/template.rb'
apply "bin/template.rb"
apply 'config/template.rb'
apply 'lib/template.rb'
apply 'spec/template.rb'
after_bundle do
git :init
git add: '.'
git commit: %Q{ -m 'Initial generator setup.' }
stop_spring
debug_print('Please select your application options...')
select_variants
debug_print('Implementing your selections...')
# Copy variants as necessary
apply 'variants/devise/template.rb' if @devise
apply 'variants/pundit/template.rb' if @devise && @pundit
apply 'variants/skylight/template.rb' if @skylight
apply 'variants/sentry/template.rb' if @sentry
apply 'variants/simple_form/template.rb' if @simple_form
apply 'variants/react/template.rb' if @react
apply 'variants/active_storage/template.rb' if @active_storage
# This should run last since it converts all generated ERB
# to HAML
apply 'variants/haml/template.rb' if @haml
debug_print('Running bin/setup to finish setting up your environment...')
run 'bin/setup'
debug_print('Updating your binstubs')
binstubs = %w(annotate bundler sidekiq)
run "bundle binstubs #{binstubs.join(' ')} --force"
git add: '.'
git commit: %Q{ -m 'Completed app templating' }
debug_print('Success!')
end
end
# Copied from: https://github.com/mattbrictson/rails-template
# Add this template directory to source_paths so that Thor actions like
# copy_file and template resolve against our source files. If this file was
# invoked remotely via HTTP, that means the files are not present locally.
# In that case, use `git clone` to download them to a local temporary dir.
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
require "tmpdir"
source_paths.unshift(tempdir = Dir.mktmpdir("yarg-"))
at_exit { FileUtils.remove_entry(tempdir) }
git clone: [
"--quiet",
YARG_REPO,
tempdir
].map(&:shellescape).join(" ")
if (branch = __FILE__[%r{yarg/(.+)/template.rb}, 1])
Dir.chdir(tempdir) { git checkout: branch }
end
else
source_paths.unshift(File.dirname(__FILE__))
end
end
def assert_minimum_rails_version
requirement = Gem::Requirement.new(RAILS_REQUIREMENT)
rails_version = Gem::Version.new(Rails::VERSION::STRING)
return if requirement.satisfied_by?(rails_version)
prompt = "This template requires Rails #{RAILS_REQUIREMENT}. "\
"You are using #{rails_version}. Continue anyway?"
exit 1 if no?(prompt)
end
def select_variants
@devise = yes?('Do you want to use Devise for user authentication?', :blue)
@pundit = yes?('Do you want to use Pundit for user authorization?', :blue) if @devise
@skylight = yes?('Do you want to use Skylight for performance monitoring?', :blue)
@skylight_token = ask('Enter your skylight token: ', :green) if @skylight
@haml = yes?('Do you want to use HAML instead of ERB view templates?', :blue)
@sentry = yes?('Do you want to use Sentry for error tracking?', :blue)
@sentry_dsn = ask('Enter your Sentry DSN url:') if @sentry
@simple_form = yes?('Do you want to use Simple Form for rails forms?', :blue)
@react = yes?('Do you want to use React?', :blue)
@active_storage = yes?('Do you want to use Active Storage for storing files?', :blue)
end
def stop_spring
run 'spring stop'
end
def debug_print(message = '')
puts "==================="
puts message
puts "==================="
end
build_app!