Class: Bootstrap::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/bootstrap/install/install_generator.rb

Defined Under Namespace

Classes: MissingAssetError

Constant Summary collapse

VENDORED_ASSETS =
[
  ['vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css',
   'app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css'],
  ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js',
   'app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js']
].freeze

Instance Method Summary collapse

Instance Method Details

#add_assetsObject



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
# File 'lib/generators/bootstrap/install/install_generator.rb', line 33

def add_assets
  return if cdn?
  # Propshaft has no require directives to add; bootstrap:layout links the
  # vendored files explicitly instead.
  return if propshaft?

  js_manifest = 'app/assets/javascripts/application.js'

  if exists_in_app?(js_manifest)
    add_require(js_manifest,
                "//= require twitter/bootstrap/bootstrap.bundle.min\n",
                "application\n",
                'twitter/bootstrap')
  else
    copy_file "application.js", js_manifest
  end

  css_manifest = 'app/assets/stylesheets/application.css'

  if exists_in_app?(css_manifest)
    add_require(css_manifest,
                " *= require bootstrap_and_overrides\n",
                "require_self\n",
                'bootstrap')
  else
    copy_file "application.css", css_manifest
  end
end

#add_bootstrapObject



78
79
80
81
82
83
84
# File 'lib/generators/bootstrap/install/install_generator.rb', line 78

def add_bootstrap
  return if cdn?
  return if propshaft?

  copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js"
  copy_file "bootstrap_and_overrides.css", "app/assets/stylesheets/bootstrap_and_overrides.css"
end

#add_cdn_initializerObject



86
87
88
89
90
91
92
93
94
# File 'lib/generators/bootstrap/install/install_generator.rb', line 86

def add_cdn_initializer
  return unless cdn?

  create_file "config/initializers/bootstrap.rb", <<~RUBY
    # Bootstrap's CSS and JS are loaded from jsDelivr rather than vendored into
    # this app. `rails g bootstrap:layout` reads this to emit CDN tags by default.
    Twitter::Bootstrap::Rails.asset_mode = :cdn
  RUBY
end

#add_localeObject



96
97
98
99
100
# File 'lib/generators/bootstrap/install/install_generator.rb', line 96

def add_locale
  return if exists_in_app?("config/locales/en.bootstrap.yml")

  copy_file "en.bootstrap.yml", "config/locales/en.bootstrap.yml"
end

#cleanup_legacyObject



102
103
104
105
106
# File 'lib/generators/bootstrap/install/install_generator.rb', line 102

def cleanup_legacy
  return unless exists_in_app?('app/assets/stylesheets/application.css')

  gsub_file("app/assets/stylesheets/application.css", %r|\s*\*=\s*twitter/bootstrap\s*\n|, "", :verbose => false)
end

#copy_bootstrap_assetsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/bootstrap/install/install_generator.rb', line 62

def copy_bootstrap_assets
  return if cdn?

  VENDORED_ASSETS.each do |relative_source, destination|
    source = File.join(gem_root, relative_source)

    unless File.file?(source)
      raise MissingAssetError,
        "Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} asset missing from the gem: " \
        "#{relative_source}. The gem install looks incomplete; reinstall twitter-bootstrap-rails."
    end

    create_file destination, File.binread(source)
  end
end

#reportObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/bootstrap/install/install_generator.rb', line 108

def report
  version = Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION

  if cdn?
    say "Bootstrap #{version} will be loaded from jsDelivr. " \
        "Run `rails g bootstrap:layout` to generate a layout with the CDN tags."
  elsif propshaft?
    say "Bootstrap #{version} vendored for Propshaft. " \
        "Run `rails g bootstrap:layout` to generate a layout that links it."
  else
    say "Bootstrap #{version} vendored into the asset pipeline."
  end
end

#validate_asset_modeObject

Raises:

  • (::Rails::Generators::Error)


26
27
28
29
30
31
# File 'lib/generators/bootstrap/install/install_generator.rb', line 26

def validate_asset_mode
  return if %w[static cdn].include?(asset_mode)

  raise ::Rails::Generators::Error,
    "Unknown asset mode #{asset_mode.inspect}. Expected 'static' or 'cdn'."
end