Class: KisoThemes::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
HandlerSupport
Defined in:
lib/generators/kiso_themes/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_app_name_to_application_helperObject



147
148
149
150
151
152
153
154
# File 'lib/generators/kiso_themes/install_generator.rb', line 147

def add_app_name_to_application_helper
  sentinel = "module ApplicationHelper\n"
  code = app_name_helper_code

  file = 'app/helpers/application_helper.rb'
  create_file file, "module ApplicationHelper\nend" unless File.file?(file)
  inject_into_file file, code, { :after => sentinel }
end

#add_jquery_require_to_app_jsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/kiso_themes/install_generator.rb', line 37

def add_jquery_require_to_app_js
  return if rails6? || force_webpacker?
  return unless Gem::Version.new(::Rails.version) >= Gem::Version.new("5.1.0.rc1")
  sentinel = "= require rails-ujs"

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

  # Plain JS
  if File.file?(file)
    inject_into_file file, "\n//= require jquery\n", { :before => "//#{sentinel}" }
  # CoffeeScript
  elsif File.file?("#{file}.coffee")
    inject_into_file "#{file}.coffee", "\n#require jquery\n", { :before => "##{sentinel}" }
  # No main JS file
  else
    say_status :warning, "Can't find #{file}. " +
      "Make sure to include add `require kiso_themes` in your Javascript.", :red
  end
end

#add_jquery_to_gemfileObject



29
30
31
32
33
34
35
# File 'lib/generators/kiso_themes/install_generator.rb', line 29

def add_jquery_to_gemfile
  return if rails6? || force_webpacker?
  return unless Gem::Version.new(::Rails.version) >= Gem::Version.new("5.1.0.rc1")
  say_status :info, "Adding jQuery back into the Gemfile"
  gem "jquery-rails"
  system('bundle install')
end

#copy_favicons_into_asset_pipelineObject



156
157
158
159
# File 'lib/generators/kiso_themes/install_generator.rb', line 156

def copy_favicons_into_asset_pipeline
  return unless Gem::Version.new(::Rails.version) >= Gem::Version.new("5.1.0.rc1")
  copy_file Rails.root.join('public', 'favicon.ico'), Rails.root.join('app', 'assets', 'images', 'favicon.ico')
end

#copy_layoutsObject



16
17
18
19
20
21
22
23
# File 'lib/generators/kiso_themes/install_generator.rb', line 16

def copy_layouts
  layouts_path = "app/views/layouts"
  layouts = Dir.glob(File.expand_path("../templates/layouts/*", __FILE__)).select{ |lf| lf.end_with?(handler) }.map { |lf| File.basename(lf, ".html.#{handler}")}

  layouts.each do |name|
    copy_file "layouts/#{name}.html.#{handler}", "#{layouts_path}/#{name}.html.#{handler}"
  end
end

#copy_stylesheetObject



25
26
27
# File 'lib/generators/kiso_themes/install_generator.rb', line 25

def copy_stylesheet
  copy_file "kiso_themes.css", "app/assets/stylesheets/kiso_themes.css"
end

#install_for_rails6Object



76
77
78
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/generators/kiso_themes/install_generator.rb', line 76

def install_for_rails6
  return unless rails6? || force_webpacker?

  say "Configuring for Webpacker..."

  # Nothing needs to happen yet for Styles as Sprockets is still used
  # in Rails 6 for handling styles and images etc.

  vendor_libs = %w{
    webpack-merge
    jquery
    popper.js
    [email protected]
    bootstrap-switch
    chartjs
    clipboard
    jquery-countdown
    jquery-countto
    cd-easypiechart
    fastclick
    object-fit-images
    flot
    gmaps.core
    jasny-bootstrap
    jqvmap
    [email protected]
    [email protected]
    modernizr
    jquery-parallax.js
    code-prettify
    prismjs
    jquery-slimscroll
    sparklines
  }

  # Add jquery to the package.json
  system("yarn add #{vendor_libs.join(' ')}")

  # Copy the pack index file
  directory "rails6", "app/javascript/kiso_themes"

  # Add jQuery to application pack
  prepend_to_file 'app/javascript/packs/application.js', "\n#{jquery_requires}\n"
  append_to_file 'app/javascript/packs/application.js', "\n#{new_requires}\n"

  # Add KisoThemes import to application pack
  append_to_file 'app/javascript/packs/application.js', "\nrequire(\"kiso_themes\")\n"

  # Change layout javascript_include to javascript_pack
  gsub_file "app/views/layouts/_base.html.#{handler}", "javascript_include_tag", "javascript_pack_tag"

  # Add jQuery symbols as provide plugins in Webpack
  inject_into_file 'config/webpack/environment.js', after: "const { environment } = require('@rails/webpacker')" do <<-JS

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    jquery: 'jquery',
    Popper: ['popper.js', 'default'], // for Bootstrap 4

    // Include Clipboard.js with a rename since it conflicts with
    // Chrome's own upcoming Clipboard API.
    ClipboardJS: 'clipboard'
  })
)
  JS
  end
end

#require_kiso_themes_javascriptObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/kiso_themes/install_generator.rb', line 57

def require_kiso_themes_javascript
  return if rails6? || force_webpacker?
  sentinel = "= require_tree ."
  code = "= require kiso_themes\n\n"

  file = 'app/assets/javascripts/application.js'
  # Plain JS
  if File.file?(file)
    inject_into_file file, "\n//#{code}", { :before => "//#{sentinel}" }
  # CoffeeScript
  elsif File.file?("#{file}.coffee")
    inject_into_file "#{file}.coffee", "\n##{code}", { :before => "##{sentinel}" }
  # No main JS file
  else
    say_status :warning, "Can't find #{file}. " +
      "Make sure to include add `require kiso_themes` in your Javascript.", :red
  end
end

#show_readmeObject



161
162
163
# File 'lib/generators/kiso_themes/install_generator.rb', line 161

def show_readme
  readme_template "README.tt" if behavior == :invoke
end