Class: Spotlight::Assets::PropshaftGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorCommonUtilities
Defined in:
lib/generators/spotlight/assets/propshaft_generator.rb

Overview

Spotlight Propshaft Generator

Instance Method Summary collapse

Methods included from GeneratorCommonUtilities

#blacklight_yarn_version, #bootstrap_version, #bootstrap_yarn_version, #package_yarn_version, #spotlight_yarn_version

Instance Method Details

#add_blacklight_frontend ⇒ Object



34
35
36
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 34

def add_blacklight_frontend
  run "yarn add blacklight-frontend@#{blacklight_yarn_version}"
end

#add_bootstrap ⇒ Object



38
39
40
41
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 38

def add_bootstrap
  run "yarn add bootstrap@\"^#{bootstrap_yarn_version}\""
  run 'yarn add @popperjs/core'
end

#add_frontend ⇒ Object

Pick a version of the frontend asset package and install it.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 44

def add_frontend
  if ENV['CI']
    run "yarn add file:#{Spotlight::Engine.root}"
  elsif options[:test]
    run 'yarn link spotlight-frontend'

  # If a branch was specified (e.g. you are running a template.rb build
  # against a test branch), use the latest version available on npm
  elsif ENV['BRANCH']
    run 'yarn add spotlight-frontend@latest'

  # Otherwise, pick the version from npm that matches the Spotlight
  # gem version
  else
    run "yarn add spotlight-frontend@#{spotlight_yarn_version}"
  end
end

#add_javascript ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 62

def add_javascript
  copy_file 'javascript/jquery-shim.js', 'app/javascript/jquery-shim.js'
  gsub_file 'app/javascript/application.js', 'import "controllers"', '// import "controllers"'

  # This may have been added from Blacklight, but it is a Spotlight dependency so ensure it is present.
  insert_into_file 'app/javascript/application.js', "import githubAutoCompleteElement from \"@github/auto-complete-element\";\n"

  append_to_file 'app/javascript/application.js' do
    <<~CONTENT
      import Spotlight from "spotlight-frontend"

      Blacklight.onLoad(function() {
        Spotlight.activate();
      });
    CONTENT
  end
end

#add_stylesheets ⇒ Object



80
81
82
83
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 80

def add_stylesheets
  copy_file 'assets/spotlight.scss', 'app/assets/stylesheets/spotlight.scss'
  append_to_file 'app/assets/stylesheets/application.bootstrap.scss', "\n@import \"spotlight\";\n"
end

#configure_esbuild ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 85

def configure_esbuild
  # The main-fields option resolves a bundling issue with bootstrap/popper on esbuild.
  custom_options = '--main-fields=main,module --alias:jquery=./app/javascript/jquery-shim.js'
  custom_options = "#{custom_options} --preserve-symlinks" if options[:test]
  gsub_file 'package.json',
            'esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets',
            "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets #{custom_options}"
end

#install_dependencies ⇒ Object



26
27
28
29
30
31
32
# File 'lib/generators/spotlight/assets/propshaft_generator.rb', line 26

def install_dependencies
  run 'yarn add @github/auto-complete-element'
  run 'yarn add @hotwired/turbo-rails'
  run 'yarn add leaflet'
  run 'yarn add sir-trevor'
  run 'yarn add sortablejs'
end