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_frontendObject



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

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

#add_bootstrapObject



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

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

#add_frontendObject

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



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

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_javascriptObject



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

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_stylesheetsObject



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

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_esbuildObject



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

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_dependenciesObject



26
27
28
29
30
31
32
33
# 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 clipboard'
  run 'yarn add leaflet'
  run 'yarn add sir-trevor'
  run 'yarn add sortablejs'
end