-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathinstall.rb
62 lines (53 loc) · 2.83 KB
/
install.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
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
IMPORTMAP_BINSTUB = Rails.root.join("bin/importmap")
IMPORTMAP_CONFIG_PATH = Rails.root.join("config/importmap.rb")
STIMULUS_PATH = Rails.root.join("app/javascript/controllers/index.js")
if APPLICATION_LAYOUT_PATH.exist?
say "Add Polaris styles in application layout"
insert_into_file APPLICATION_LAYOUT_PATH.to_s, "\n <%= stylesheet_link_tag \"polaris_view_components\" %>", before: /\s*<\/head>/
if File.read(APPLICATION_LAYOUT_PATH).include?("<body>")
say "Add Polaris classes and inline styles for <html> in application layout"
gsub_file APPLICATION_LAYOUT_PATH.to_s, "<html", "<html class=\"<%= polaris_html_classes %>\" style=\"<%= polaris_html_styles %>\""
say "Add Polaris inline styles for <body> in application layout"
gsub_file APPLICATION_LAYOUT_PATH.to_s, "<body>", "<body style=\"<%= polaris_body_styles %>\">"
else
say "<body> tag is not found in application layout.", :red
say " Replace <html> with <html class=\"<%= polaris_html_classes %>\" style=\"<%= polaris_html_styles %>\"> in your custom layour."
say " Replace <body> with <body style=\"<%= polaris_body_styles %>\"> in your custom layour."
end
else
say "Default application.html.erb is missing!", :red
say " 1. Add <%= stylesheet_link_tag \"polaris_view_components\" %> within the <head> tag in your custom layout."
say " 2. Replace <html> with <html class=\"<%= polaris_html_classes %>\" style=\"<%= polaris_html_styles %>\"> in your custom layour."
say " 3. Replace <body> with <body style=\"<%= polaris_body_styles %>\"> in your custom layour."
end
if IMPORTMAP_BINSTUB.exist?
importmaps = File.read(IMPORTMAP_CONFIG_PATH)
unless importmaps.include?("@rails/request.js")
say "Pin @rails/request.js dependency"
run "bin/importmap pin @rails/request.js --download"
end
say "Pin polaris_view_components"
append_to_file IMPORTMAP_CONFIG_PATH do
%(pin "polaris-view-components", to: "polaris_view_components.js"\n)
end
else
package_json = File.read(Rails.root.join("package.json"))
unless package_json.include?("@rails/request.js")
say "Add @rails/request.js dependency"
run "yarn add @rails/request.js"
end
say "Add polaris-view-components package"
run "yarn add polaris-view-components"
end
if STIMULUS_PATH.exist?
say "Add Polaris Stimulus controllers"
append_to_file STIMULUS_PATH do
"\nimport { registerPolarisControllers } from \"polaris-view-components\"\nregisterPolarisControllers(Stimulus)\n"
end
else
say "Default Stimulus location is missing: app/javascript/controllers/index.js", :red
say " Add to your Stimulus index.js:"
say " import { registerPolarisControllers } from \"polaris-view-components\""
say " registerPolarisControllers(Stimulus)"
end