Class: Bootstrap::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_assetsObject



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

def add_assets

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

  if File.exist?(js_manifest)
    insert_into_file js_manifest, "//= require twitter/bootstrap\n", :after => "application\n"
  else
    copy_file "application.js", js_manifest
  end

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

  if File.exist?(css_manifest)
    content = File.read(css_manifest)
    unless content.include?('bootstrap')
      style_require_block = " *= require bootstrap\n"
      insert_into_file css_manifest, style_require_block, :after => "require_self\n"
    end
  else
    copy_file "application.css", "app/assets/stylesheets/application.css"
  end

  copy_bootstrap_assets

end

#add_bootstrapObject



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

def add_bootstrap
  if use_coffeescript?
    copy_file "bootstrap.coffee", "app/assets/javascripts/bootstrap.js.coffee"
  else
    copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js"
  end
  if use_less?
    copy_file "bootstrap_and_overrides.less", "app/assets/stylesheets/bootstrap_and_overrides.css.less"
  else
    copy_file "bootstrap_and_overrides.css", "app/assets/stylesheets/bootstrap_and_overrides.css"
  end
end

#add_localeObject



74
75
76
77
78
79
80
81
# File 'lib/generators/bootstrap/install/install_generator.rb', line 74

def add_locale
  if File.exist?("config/locales/en.bootstrap.yml")
    localez = File.read("config/locales/en.bootstrap.yml")
    insert_into_file "config/locales/en.bootstrap.yml", localez, :after => "en\n"
  else
    copy_file "en.bootstrap.yml", "config/locales/en.bootstrap.yml"
  end
end

#cleanup_legacyObject



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

def cleanup_legacy
  gsub_file("app/assets/stylesheets/application.css", %r|\s*\*=\s*twitter/bootstrap\s*\n|, "")
  if File.exist?('app/assets/stylesheets/bootstrap_override.css.less')
    puts <<-EOM
    Warning:
      app/assets/stylesheets/bootstrap_override.css.less exists
      It should be removed, as it has been superceded by app/assets/stylesheets/bootstrap_and_overrides.css.less
    EOM
  end
end

#copy_bootstrap_assetsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/bootstrap/install/install_generator.rb', line 39

def copy_bootstrap_assets
  gem_root = File.expand_path("../../../../..", __FILE__)
  bootstrap_css_src = "#{gem_root}/vendor/assets/stylesheets/twitter/bootstrap"
  bootstrap_js_src = "#{gem_root}/vendor/assets/javascripts/twitter/bootstrap"
  
  css_dest = "app/assets/stylesheets/twitter/bootstrap"
  js_dest = "app/assets/javascripts/twitter/bootstrap"
  
  FileUtils.mkdir_p(css_dest)
  FileUtils.mkdir_p(js_dest)
  
  Dir.glob("#{bootstrap_css_src}/*").each do |src|
    dest = "#{css_dest}/#{File.basename(src)}"
    FileUtils.cp(src, dest)
  end
  
  Dir.glob("#{bootstrap_js_src}/*").each do |src|
    dest = "#{js_dest}/#{File.basename(src)}"
    FileUtils.cp(src, dest)
  end
end