-
-
Notifications
You must be signed in to change notification settings - Fork 627
/
bootstrap_generator.rb
89 lines (71 loc) · 3.29 KB
/
bootstrap_generator.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
require "rails/generators"
require File.expand_path("../generator_helper", __FILE__)
include GeneratorHelper
module ReactOnRails
module Generators
class BootstrapGenerator < Rails::Generators::Base
hide!
source_root(File.expand_path("../templates", __FILE__))
def copy_bootstrap_files
base_path = "bootstrap/"
%w(app/assets/stylesheets/_bootstrap-custom.scss
client/assets/stylesheets/_post-bootstrap.scss
client/assets/stylesheets/_pre-bootstrap.scss
client/assets/stylesheets/_react-on-rails-sass-helper.scss
client/bootstrap-sass.config.js).each { |file| copy_file(base_path + file, file) }
end
# if there still is not application.scss, just create one
def create_application_scss_if_necessary
path = File.join(destination_root, "app/assets/stylesheets/application.scss")
return if File.exist?(path)
File.open(path, "w") { |f| f.puts "// Created by React on Rails gem\n\n" }
end
def prepend_to_application_scss
data = <<-DATA.strip_heredoc
// DO NOT REQUIRE TREE! It will interfere with load order!
// Account for differences between Rails and Webpack Sass code.
$rails: true;
// Included from bootstrap-sprockets gem and loaded in app/assets/javascripts/application.rb
@import 'bootstrap-sprockets';
// Customizations - needs to be imported after bootstrap-sprocket but before bootstrap-custom!
// The _pre-bootstrap.scss file is located under
// client/assets/stylesheets, which has been added to the Rails asset
// pipeline search path. See config/application.rb.
@import 'pre-bootstrap';
// These scss files are located under client/assets/stylesheets
// (which has been added to the Rails asset pipeline search path in config/application.rb).
@import 'bootstrap-custom';
// This must come after all the boostrap styles are loaded so that these styles can override those.
@import 'post-bootstrap';
DATA
application_scss = File.join(destination_root, "app/assets/stylesheets/application.scss")
if File.exist?(application_scss)
append_to_file(application_scss, data)
else
puts_setup_file_error(application_scss, data)
end
end
def strip_application_scss_of_incompatible_sprockets_statements
application_scss = File.join(destination_root, "app/assets/stylesheets/application.scss")
gsub_file(application_scss, "*= require_tree .", "")
gsub_file(application_scss, "*= require_self", "")
end
def add_bootstrap_sprockets_to_gemfile
append_to_file("Gemfile", "gem 'bootstrap-sass'\n")
end
def add_bootstrap_sprockets_to_application_js
data = <<-DATA.strip_heredoc
// bootstrap-sprockets depends on generated/vendor-bundle for jQuery.
//= require bootstrap-sprockets
DATA
app_js_path = "app/assets/javascripts/application.js"
found_app_js = dest_file_exists?(app_js_path) || dest_file_exists?(app_js_path + ".coffee")
if found_app_js
append_to_file(found_app_js, data)
else
create_file(app_js_path, data)
end
end
end
end
end