Class: TurboMaterial::InstallGenerator

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

Constant Summary collapse

START_MARKER =
"// #{Engine.name} raw CSS. This section is auto-generated by the turbo_material installer.".freeze
END_MARKER =
"// End of auto-generated #{Engine.name} raw CSS. Version:".freeze
<<-HTML.rstrip.freeze


<link href="//cdn.jsdelivr.net/npm/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<script src="//cdn.jsdelivr.net/npm/material-components-web@latest/dist/material-components-web.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> 
HTML

Instance Method Summary collapse

Instance Method Details

#add_material_components_web_to_app_layoutObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/turbo_material/install_generator.rb', line 67

def add_material_components_web_to_app_layout
  layout_path = Rails.root.join('app/views/layouts/application.html.erb')
  if layout_path.exist? && layout_path.read.include?('<%= csp_meta_tag %>')
    if layout_path.read.include?('<link href="//cdn.jsdelivr.net/npm/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">')
      puts "`app/views/layouts/application.html.erb` head already contains material components web links"
    else
      insert_into_file layout_path, after: '<%= csp_meta_tag %>' do
        HEAD_LINKS
      end
    end
  else
    raise "`app/views/layouts/application.html.erb` does not exist or does not contain `<%= csp_meta_tag %>`"
  end
end

#add_turbo_material_js_controllersObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generators/turbo_material/install_generator.rb', line 52

def add_turbo_material_js_controllers
  controllers_path = Rails.root.join('app/javascript/controllers/index.js')
  if controllers_path.exist? && controllers_path.read.include?('eagerLoadControllersFrom("controllers", application)')
    if controllers_path.read.include?('eagerLoadControllersFrom("turbo_material", application)')
      puts "`app/javascript/controllers/index.js` already contains `eagerLoadControllersFrom(\"turbo_material\", application)`"
    else
      insert_into_file controllers_path, after: 'eagerLoadControllersFrom("controllers", application)' do
        "\neagerLoadControllersFrom(\"turbo_material\", application)\n"
      end
    end
  else
    puts "`app/javascript/controllers/index.js` does not exist or does not contain `eagerLoadControllersFrom(\"controllers\", application)`"
  end
end

#update_tailwind_configObject



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
# File 'lib/generators/turbo_material/install_generator.rb', line 17

def update_tailwind_config
  tailwind_css_path = TurboMaterial::Engine.root.join('app/assets/dist/turbo_material/tailwind.css')
  css_content = File.read(tailwind_css_path)
  css_content.gsub!(/\/\*.*?\*\//m, '')
  css_content.gsub!(/\{[^}]*}/m, '{}')
  css_content.gsub!('>:not([hidden])~:not([hidden])', '')
  css_content.gsub!(/\\\[/, '[')
  css_content.gsub!( /\\\]/, ']')
  css_content.gsub!( /\\\//, '/')
  css_content.gsub!( /\\:/, ':')

  class_regex = /\.\\?(!?[-_a-zA-Z0-9\[\]\/:]+)(?=[^}]*\{)/
  classes = css_content.scan(class_regex).flatten.uniq.sort

  tailwind_config_path = Rails.root.join('config/tailwind.config.js')

  if tailwind_config_path.exist?
    content_config = <<~CONFIG.strip_heredoc
      #{START_MARKER}
      { raw: '<div class="#{classes.join(' ')}"></div>', extension: 'html' },
      #{END_MARKER} #{TurboMaterial::VERSION}
    CONFIG

    if File.read(tailwind_config_path.to_s).include?(START_MARKER)
      gsub_file tailwind_config_path, /#{Regexp.escape(START_MARKER)}.*?#{Regexp.escape(END_MARKER)}.*?$/m do |match|
        content_config.strip
      end
    else
      insert_into_file tailwind_config_path, after: "content: [" do
        "\n" + content_config.strip
      end
    end
  end
end