-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic.rb
213 lines (169 loc) · 6.98 KB
/
basic.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
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
run "if uname | grep -q 'Darwin'; then pgrep spring | xargs kill -9; fi"
say "starting template creation: Rails 6, Tailwind 2", :green
inject_into_file 'Gemfile', before: 'group :development, :test do' do
<<~RUBY
gem 'autoprefixer-rails'
gem 'font-awesome-sass'
gem 'inline_svg'
RUBY
end
inject_into_file 'Gemfile', after: 'group :development, :test do' do
<<-RUBY
gem 'pry-byebug'
gem 'pry-rails'
gem 'dotenv-rails'
RUBY
end
gsub_file('Gemfile', /# gem 'redis'/, "gem 'redis'")
def add_tailwind
run "yarn remove @rails/webpacker"
run "yarn add @rails/webpacker"
gsub_file('Gemfile', /gem 'webpacker', '~> 4.0'/, "gem 'webpacker', github: 'rails/webpacker'")
run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
run 'yarn add @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio'
run "mkdir -p app/javascript/stylesheets"
run "touch app/javascript/stylesheets/application.scss"
inject_into_file "app/javascript/stylesheets/application.scss" do <<~EOF
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
@import "components/base";
@import "components/buttons";
@import "components/cards";
@import "components/forms";
@import "components/icons";
@import "components/navigation";
EOF
end
run 'curl -L https://github.com/thomasvanholder/tailwind-components/archive/master.zip > components.zip'
run 'unzip components.zip -d app && rm components.zip && mv app/tailwind-components-master app/javascript/components'
run "npx tailwindcss init --full"
gsub_file "tailwind.config.js", /plugins:\s\[],/, "plugins: [require('@tailwindcss/forms'), require('@tailwindcss/typography'), require('@tailwindcss/aspect-ratio'),],"
run "mv tailwind.config.js app/javascript/stylesheets/tailwind.config.js"
append_to_file("app/javascript/packs/application.js", 'import "stylesheets/application"')
inject_into_file("./postcss.config.js",
"let tailwindcss = require('tailwindcss');\n", before: "module.exports")
inject_into_file("./postcss.config.js", "\n tailwindcss('./app/javascript/stylesheets/tailwind.config.js'),", after: "plugins: [")
end
def add_assets
run 'rm -rf vendor'
run 'rm -rf app/assets'
run 'curl -L https://github.com/thomasvanholder/assets/archive/master.zip > assets.zip'
run 'unzip assets.zip -d app && rm assets.zip && mv app/assets-master app/assets'
gsub_file('config/environments/development.rb', /config\.assets\.debug.*/, 'config.assets.debug = false')
end
# Layout
gsub_file('app/views/layouts/application.html.erb', "<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>", "<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload', defer: true %>")
style = <<~HTML
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
HTML
gsub_file('app/views/layouts/application.html.erb', "<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>", style)
def add_navbar
run "mkdir -p app/views/shared"
run 'curl -L https://raw.githubusercontent.com/thomasvanholder/jumpstart/main/templates/_navbar_basic.html.erb > app/views/shared/_navbar.html.erb'
end
def add_flashes
run 'curl -L https://raw.githubusercontent.com/thomasvanholder/jumpstart/main/templates/_flashes.html.erb > app/views/shared/_flashes.html.erb'
end
inject_into_file 'app/views/layouts/application.html.erb', after: '<body>' do
<<-HTML
\n
<%= render 'shared/navbar' %>
<%= render 'shared/flashes' %>
HTML
end
# README
########################################
markdown_file_content = <<-MARKDOWN
Rails app generated by [thomasvanholder/jumpstart](https://github.com/thomasvanholder/jumpstart), inspired by the [Le Wagon](https://www.lewagon.com).
MARKDOWN
file 'README.md', markdown_file_content, force: true
# Generators
########################################
generators = <<~RUBY
config.generators do |generate|
generate.assets false
generate.helper false
generate.test_framework :test_unit, fixture: false
end
RUBY
def set_routes
route "root to: 'pages#home'"
end
def add_git_ignore
append_file '.gitignore', <<~TXT
# Ignore .env file containing credentials.
.env*
# Ignore Mac and Linux file system files
*.swp
.DS_Store
TXT
end
def add_svg_helper
run 'rm -rf app/helpers/application_helper.rb'
run 'curl -L https://raw.githubusercontent.com/thomasvanholder/jumpstart/main/application_helper.rb > app/helpers/application_helper.rb'
end
environment generators
########################################
# AFTER BUNDLE
########################################
after_bundle do
rails_command 'db:drop db:create db:migrate'
generate(:controller, 'pages', 'home', '--skip-routes', '--no-test-framework')
set_routes
add_assets
add_git_ignore
# Environments
########################################
environment 'config.action_mailer.default_url_options = { host: "http://localhost:3000" }', env: 'development'
environment 'config.action_mailer.default_url_options = { host: "http://TODO_PUT_YOUR_DOMAIN_HERE" }', env: 'production'
# Webpacker / Yarn
########################################
append_file 'app/javascript/packs/application.js', <<~JS
// ----------------------------------------------------
// ABOVE IS RAILS DEFAULT CONFIGURATION
// WRITE YOUR OWN JS STARTING FROM HERE đ
// ----------------------------------------------------
// External imports
import "../stylesheets/application.scss";
// Internal imports, e.g:
// import { initSelect2 } from '../components/init_select2';
document.addEventListener('turbolinks:load', () => {
// Call your functions here, e.g:
// initSelect2();
});
JS
inject_into_file 'config/webpack/environment.js', before: 'module.exports' do
<<~JS
const webpack = require('webpack');
// Preventing Babel from transpiling NodeModules packages
environment.loaders.delete('nodeModules');
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
})
);
JS
end
run 'touch .env'
run 'curl -L https://raw.githubusercontent.com/lewagon/rails-templates/master/.rubocop.yml > .rubocop.yml'
# Git
########################################
git add: '.'
git commit: "-m 'Initial commit with template from https://github.com/thomasvanholder/jumpstart'"
# Fix puma config
gsub_file('config/puma.rb', 'pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }', '# pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }')
add_tailwind
add_navbar
add_flashes
add_svg_helper
run "bundle install"
say "--------------------------------"
say
say "Kickoff app successfully created! đ", :green
say
say "Switch to your app by running:", :green
say " cd #{app_name}"
say
end