-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtemplate.rb
More file actions
200 lines (153 loc) · 4.47 KB
/
template.rb
File metadata and controls
200 lines (153 loc) · 4.47 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
fail("Rails 8.0.0 or greater is required") if Rails.version <= "8"
def apply_template!
add_template_repository_to_source_path
template('template_files/gitignore', '.gitignore', force: true)
after_bundle do
pre_template_commit
ensure_node_version_set
setup_readme
setup_dependabot
configure_generators
setup_frontend
setup_test_suite
add_docker
add_docker_compose
setup_linting
setup_solargraph # Needs to come after linting
setup_adrs # Put last for correct ordering in README
setup_semantic_logger
fix_ci
fix_setup
bundle_with_checksums
apply_rubocop_fixes
post_template_commit
end
end
# Taken from https://github.com/mattbrictson/rails-template/blob/215a87d00ff2b2a656be3ebd277e7f71607f5d49/template.rb#L99
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
require "tmpdir"
source_paths.unshift(tempdir = Dir.mktmpdir("rails-template-"))
at_exit { FileUtils.remove_entry(tempdir) }
git clone: [
"--quiet",
"https://github.com/DFE-Digital/rails-template.git",
tempdir
].map(&:shellescape).join(" ")
if (branch = __FILE__[%r{rails-template/(.+)/template.rb}, 1])
say("==BRANCH: #{branch}", :blue)
Dir.chdir(tempdir) { git checkout: branch.gsub("refs/heads/", "") }
end
else
source_paths.unshift(File.dirname(__FILE__))
end
end
def pre_template_commit
say "\n=== Creating pre-template commit ==="
git(add: ".")
git(commit: <<~COMMIT)
-m "Pre template commit
Rails new commit, before we start applying the template"
COMMIT
end
def ensure_node_version_set
say "\n=== Setting Node version ==="
return if file_contains?("package.json", "engines")
node_engine_config = <<-JSON
"engines": {
"node": "^#{node_version}"
},
JSON
insert_into_file("package.json", node_engine_config, after: "{")
end
def setup_readme
say "\n=== Setting up README ==="
apply 'templates/readme.rb'
end
def setup_dependabot
say "\n=== Setting up Dependabot ==="
template('template_files/dependabot.yml', '.github/dependabot.yml', force: true)
end
def configure_generators
say "\n=== Configuring generators ==="
initializer "generators.rb", <<-RUBY
Rails.application.config.generators do |g|
g.test_framework :rspec, fixture: false
g.helper false
g.stylesheets false
g.scaffold_stylesheet false
g.template_engine :erb
# Don't generate system test files.
g.view_specs false
g.helper_specs false
# Uncomment to configure generators to use ULID primary keys
# g.orm :active_record, primary_key_type: :string
end
RUBY
end
def setup_frontend
say "\n=== Setting up GOV.UK Frontend ==="
apply "templates/frontend.rb"
end
def setup_test_suite
say "\n=== Setting up test suite ==="
apply 'templates/test_suite.rb'
end
def add_docker
say "\n=== Docker ==="
template('template_files/Dockerfile', 'Dockerfile', force: true)
template('template_files/dockerignore', '.dockerignore', force: true)
remove_file("bin/docker-entrypoint")
end
def add_docker_compose
say "\n=== Docker Compose ==="
apply 'templates/docker_compose.rb'
end
def setup_linting
say("\n=== Rubocop and prettier ===")
apply 'templates/linting.rb'
end
def setup_solargraph
say("\n=== solargraph https://solargraph.org/ ===")
apply 'templates/solargraph.rb'
end
def setup_adrs
say("\n=== Architecture Decision Records (ADRs) ===")
apply 'templates/adr.rb'
end
def setup_semantic_logger
say("\n=== semantic logger https://logger.rocketjob.io/ ===")
apply 'templates/semantic_logger.rb'
end
def fix_ci
say "\n=== Fixing CI configuration ==="
gsub_file("config/ci.rb", "yarn audit", "yarn npm audit")
end
def fix_setup
say "\n=== Fixing setup script ==="
gsub_file("bin/setup", "yarn install --check-files", "yarn install")
end
def bundle_with_checksums
say "\n=== Adding checksums to Gemfile.lock ==="
run("bundle lock --add-checksums")
end
def apply_rubocop_fixes
say "\n=== Running Rubocop fixes ==="
run("bin/rubocop --autocorrect-all --format quiet")
end
def post_template_commit
say "\n=== Creating post-template commit ==="
git(add: ".")
git(commit: <<~COMMIT)
-m "Post template commit
Built using the Department for Education's Rails template"
COMMIT
end
def file_exists?(file)
File.exist?(file)
end
def file_contains?(file, contains)
return false unless file_exists?(file)
File.foreach(file).any? { |line| line.include?(contains) }
end
apply_template!