Class: Bootswatch::Generators::InstallGenerator

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

Constant Summary collapse

DEFAULT_THEME_NAME =
'bootswatch'

Instance Method Summary collapse

Instance Method Details

#add_assetsObject


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

def add_assets

  if use_default_theme_name?
    if File.exist?('app/assets/javascripts/application.js')
      unless File.read('app/assets/javascripts/application.js').include?(theme_name)
        insert_into_file "app/assets/javascripts/application.js",
                       "//= require #{theme_name}/loader\n",
                       :after => "jquery_ujs\n"
      end
    else
      template 'application.js.tt', 'app/assets/stylesheets/application.js', {theme_name: theme_name, theme_info: theme_info}
    end

    if File.exist?('app/assets/stylesheets/application.css')
      unless File.read('app/assets/stylesheets/application.css').include?(theme_name)
        insert_into_file "app/assets/stylesheets/application.css",
                       " *= require #{theme_name}/loader\n",
                       :after => "require_self\n"
      end

      unless File.read('app/assets/stylesheets/application.css').include?('font-awesome')
        insert_into_file "app/assets/stylesheets/application.css",
                       " *= require font-awesome/font-awesome\n",
                       :after => "*= require #{theme_name}/loader\n"
      end

    else
      template 'application.css.tt', 'app/assets/stylesheets/application.css', {theme_name: theme_name, theme_info: theme_info}
    end
  else
    template 'application.js.tt', "app/assets/javascripts/#{theme_name}.js", {theme_name: theme_name, theme_info: theme_info}
    template 'application.css.tt', "app/assets/stylesheets/#{theme_name}.css", {theme_name: theme_name, theme_info: theme_info}
  end

end

#add_javascriptsObject


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

def add_javascripts

  javascripts_dest_path = "app/assets/javascripts/#{theme_name}"
  empty_directory javascripts_dest_path

  template 'loader.coffee.tt', File.join(javascripts_dest_path,'loader.coffee'), {theme_name: theme_name, theme_info: theme_info}

   # let's auto backup if a custom bootswatch.coffee already exists
  if File.exist?(File.join(javascripts_dest_path,'bootswatch.coffee'))
    File.rename(File.join(javascripts_dest_path,'bootswatch.coffee'), File.join(javascripts_dest_path,'bootswatch.coffee_bak'))
  end

  template 'bootswatch.coffee.tt', File.join(javascripts_dest_path,'bootswatch.coffee'), {theme_name: theme_name, theme_info: theme_info}

end

#add_stylesheetsObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/generators/bootswatch/install/install_generator.rb', line 79

def add_stylesheets

  stylesheets_dest_path = "app/assets/stylesheets/#{theme_name}"
  empty_directory stylesheets_dest_path

  less_imports = File.read(find_in_source_paths('bootstrap.less')).scan(Less::Rails::ImportProcessor::IMPORT_SCANNER).flatten.compact.uniq

  # let's auto backup if a custom loader.css.less already exists
  if File.exist?(File.join(stylesheets_dest_path,'loader.css.less'))
    File.rename(File.join(stylesheets_dest_path,'loader.css.less'), File.join(stylesheets_dest_path,'loader.css.less_bak'))
  end
  template 'loader.css.less.tt', File.join(stylesheets_dest_path,'loader.css.less'), {less_imports: less_imports, theme_name: theme_name, theme_info: theme_info}

  # let's auto backup if a custom variables.less already exists
  if File.exist?(File.join(stylesheets_dest_path,'variables.less'))
    File.rename(File.join(stylesheets_dest_path,'variables.less'), File.join(stylesheets_dest_path,'variables.less_bak'))
  end
  template 'variables.less.tt', File.join(stylesheets_dest_path,'variables.less'), {theme_name: theme_name, theme_info: theme_info}

  # let's auto backup if a custom mixins.less already exists
  if File.exist?(File.join(stylesheets_dest_path,'mixins.less'))
    File.rename(File.join(stylesheets_dest_path,'mixins.less'), File.join(stylesheets_dest_path,'mixins.less_bak'))
  end
  template 'mixins.less.tt', File.join(stylesheets_dest_path,'mixins.less'), {theme_name: theme_name, theme_info: theme_info}

  # let's auto backup if a custom bootswatch.less already exists
  if File.exist?(File.join(stylesheets_dest_path,'bootswatch.less'))
    File.rename(File.join(stylesheets_dest_path,'bootswatch.less'), File.join(stylesheets_dest_path,'bootswatch.less_bak'))
  end
  template 'bootswatch.less.tt', File.join(stylesheets_dest_path,'bootswatch.less'), {theme_name: theme_name, theme_info: theme_info}

  # let's auto backup if a custom base.less already exists
  if File.exist?(File.join(stylesheets_dest_path,'base.less'))
    File.rename(File.join(stylesheets_dest_path,'base.less'), File.join(stylesheets_dest_path,'base.less_bak'))
  end
  template 'base.less.tt', File.join(stylesheets_dest_path,'base.less'), {theme_name: theme_name, theme_info: theme_info}

end

#theme_infoObject


23
24
25
# File 'lib/generators/bootswatch/install/install_generator.rb', line 23

def theme_info
  "#{use_default_theme_name? ? 'bootstrap'.capitalize : theme_name.capitalize} #{Twitter::Bootswatch::Rails::VERSION.chop.chop}"
end

#theme_nameObject


15
16
17
# File 'lib/generators/bootswatch/install/install_generator.rb', line 15

def theme_name
  file_name
end

#use_default_theme_name?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/generators/bootswatch/install/install_generator.rb', line 19

def use_default_theme_name?
  theme_name === DEFAULT_THEME_NAME
end