-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathinstall_tailwindcss.rb
55 lines (45 loc) · 2.29 KB
/
install_tailwindcss.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
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
CENTERING_CONTAINER_INSERTION_POINT = /^\s*<%= yield %>/.freeze
TAILWIND_ASSET_PATH = Rails.root.join("app/assets/tailwind")
if APPLICATION_LAYOUT_PATH.exist?
unless File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/)
say "Add Tailwindcss include tags in application layout"
insert_into_file APPLICATION_LAYOUT_PATH.to_s, <<~ERB.indent(4), before: /^\s*<%= stylesheet_link_tag/
<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
ERB
end
say "Add Tailwindcss container element in application layout"
if File.open(APPLICATION_LAYOUT_PATH).read =~ /<body>\n\s*<%= yield %>\n\s*<\/body>/
insert_into_file APPLICATION_LAYOUT_PATH.to_s, %( <main class="container mx-auto mt-28 px-5 flex">\n ), before: CENTERING_CONTAINER_INSERTION_POINT
insert_into_file APPLICATION_LAYOUT_PATH.to_s, %(\n </main>), after: CENTERING_CONTAINER_INSERTION_POINT
end
else
say "Default application.html.erb is missing!", :red
say %( Add <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %> within the <head> tag in your custom layout.)
end
say "Build into app/assets/builds"
empty_directory "app/assets/builds"
keep_file "app/assets/builds"
if (sprockets_manifest_path = Rails.root.join("app/assets/config/manifest.js")).exist?
append_to_file sprockets_manifest_path, %(//= link_tree ../builds\n)
end
if Rails.root.join(".gitignore").exist?
append_to_file(".gitignore", %(\n/app/assets/builds/*\n!/app/assets/builds/.keep\n))
end
unless TAILWIND_ASSET_PATH.join("application.tailwind.css").exist?
say "Add default #{TAILWIND_ASSET_PATH}/application.tailwind.css"
copy_file "#{__dir__}/application.tailwind.css", TAILWIND_ASSET_PATH.join("application.tailwind.css")
end
if Rails.root.join("Procfile.dev").exist?
append_to_file "Procfile.dev", "css: bin/rails tailwindcss:watch\n"
else
say "Add default Procfile.dev"
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
say "Ensure foreman is installed"
run "gem install foreman"
end
say "Add bin/dev to start foreman"
copy_file "#{__dir__}/dev", "bin/dev", force: true
chmod "bin/dev", 0755, verbose: false
say "Compile initial Tailwind build"
run "rails tailwindcss:build"