-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.rb
151 lines (122 loc) · 3.98 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
RAILS_REQUIREMENT = "~> 5.0.0"
def go_go_template!
assert_rails_version
assert_postgresql
add_template_repository_to_source_path
template "Gemfile.tt", :force => true
remove_file "README.rdoc"
template "example.env.tt"
template "ruby-version.tt", ".ruby-version", :force => true
copy_file "rbenv-gemsets", ".rbenv-gemsets", :force => true
copy_file "gitignore", ".gitignore", :force => true
copy_file "rubocop.yml", ".rubocop.yml", :force => true
apply "app/template.rb"
apply "bin/template.rb"
apply "config/template.rb"
apply "lib/template.rb"
git :init unless preexisting_git_repo?
empty_directory ".git/safe"
run_with_clean_bundler_env "bin/setup"
apply "spec/template.rb"
add_basic_roles
add_devise_confirmable
run_with_clean_bundler_env "rails generate simple_form:install --bootstrap"
generate_spring_binstubs
binstubs = %w(
brakeman bundler-audit
)
run_with_clean_bundler_env "bundle binstubs #{binstubs.join(' ')}"
unless preexisting_git_repo?
git :add => "-A ."
git :commit => "-n -m 'Intializing a new project'"
setup_react if add_react?
git :checkout => "-b development"
end
end
def setup_react
run "spring stop"
run "rails generate react_on_rails:install"
run "bundle && npm install"
run "gem install foreman"
git :add => "-A ."
git :commit => "-n -m 'Adding react on rails'"
end
def assert_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 assert_postgresql
return if IO.read("Gemfile") =~ /^\s*gem ['"]pg['"]/
fail Rails::Generators::Error,
"This template requires PostgreSQL, "\
"but the pg gem isn’t present in your Gemfile."
end
require "fileutils"
require "shellwords"
# 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?://}
source_paths.unshift(tempdir = Dir.mktmpdir("booster-shot-"))
at_exit { FileUtils.remove_entry(tempdir) }
git :clone => [
"--quiet",
"https://github.com/dustinfisher/booster-shot.git",
tempdir
].map(&:shellescape).join(" ")
else
source_paths.unshift(File.dirname(__FILE__))
end
end
def run_with_clean_bundler_env(cmd)
return run(cmd) unless defined?(Bundler)
Bundler.with_clean_env { run(cmd) }
end
def preexisting_git_repo?
@preexisting_git_repo ||= (File.exist?(".git") || :nope)
@preexisting_git_repo == true
end
def gemfile_requirement(name)
@original_gemfile ||= IO.read("Gemfile")
req = @original_gemfile[/gem\s+['"]#{name}['"]\s*(,[><~= \t\d\.\w'"]*).*$/, 1]
req && req.gsub("'", %(")).strip.sub(/^,\s*"/, ', "')
end
def add_basic_roles
insert_into_file "app/models/user.rb",
:after => /class User.*\n/ do
"
enum role: [:user, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end\n
"
end
end
def add_devise_confirmable
gsub_file "app/models/user.rb", /:validatable/ do
":validatable,
:confirmable"
end
gsub_file "config/initializers/devise.rb", /config\.reconfirmable = true/ do
"config.reconfirmable = false"
end
end
def ask_with_default(question, color, default)
return default unless $stdin.tty?
question = (question.split("?") << " [#{default}]?").join
answer = ask(question, color)
answer.to_s.strip.empty? ? default : answer
end
def add_react?
return @add_react if defined?(@add_react)
@add_react = \
ask_with_default("Use React on Rails?", :blue, "no") =~ /^y(es)?/i
end
go_go_template!