Class: BlacklightRangeLimit::AssetsGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#option_asset_delivery_modeObject (readonly)

Returns the value of attribute option_asset_delivery_mode.



24
25
26
# File 'lib/generators/blacklight_range_limit/assets_generator.rb', line 24

def option_asset_delivery_mode
  @option_asset_delivery_mode
end

#option_js_fileObject (readonly)

Returns the value of attribute option_js_file.



24
25
26
# File 'lib/generators/blacklight_range_limit/assets_generator.rb', line 24

def option_js_file
  @option_js_file
end

#option_yarn_local_packageObject (readonly)

Returns the value of attribute option_yarn_local_package.



24
25
26
# File 'lib/generators/blacklight_range_limit/assets_generator.rb', line 24

def option_yarn_local_package
  @option_yarn_local_package
end

Instance Method Details

#add_to_package_jsonObject



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

def add_to_package_json
  # for apps using jsbundling_rails, vite-ruby, etc.
  if option_asset_delivery_mode == "yarn-package"
    say_status "info", "Adding blacklight-range-limit to package.json", :blue

    if option_yarn_local_package
      run "yarn add blacklight-range-limit@file:#{BlacklightRangeLimit::Engine.root}", abort_on_failure: true
    else
      # are we actually going to release one-to-one? Maybe just matching major
      # version would be enough?
      run "yarn add blacklight-range-limit@^#{BlacklightRangeLimit::VERSION.split(".").first}.0.0", abort_on_failure: true
    end
  else
    say_status "info", "No package.json, not adding blacklight-range-limit npm package", :blue
  end
end

#dependencies_to_importmap_rbObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/generators/blacklight_range_limit/assets_generator.rb', line 69

def dependencies_to_importmap_rb
 if option_asset_delivery_mode == "importmap-rails"
    # No need to pin "blacklight-range-limit", importmaps can find it when imported
    # already, because our engine put it in importmap.paths
    append_to_file("config/importmap.rb") do
      # We'll want to update these version numbers periodically in source here, no other way to do it?
      # And generated apps will have to manually update them too?
      <<~EOS
        # chart.js is dependency of blacklight-range-limit, currently is not working
        # as vendored importmaps, but instead must be pinned to CDN. You may want to update
        # versions perioidically.
        pin "chart.js", to: "https://ga.jspm.io/npm:[email protected]/dist/chart.js"
        # single dependency of chart.js:
        pin "@kurkle/color", to: "https://ga.jspm.io/npm:@kurkle/[email protected]/dist/color.esm.js"
      EOS
    end
  else
    say_status(:info, "no config/importmap.rb, so did not pin JS dependencies for blacklight-range-limit there", :yellow)
  end
end

#import_and_start_in_application_jsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/generators/blacklight_range_limit/assets_generator.rb', line 91

def import_and_start_in_application_js
  if root.join(option_js_file).exist?
    js_file_path = root.join(option_js_file).to_s

    append_to_file js_file_path do
      <<~EOS

        import BlacklightRangeLimit from "blacklight-range-limit";
        BlacklightRangeLimit.init({onLoadHandler: Blacklight.onLoad });
      EOS
    end
  else
    say_status(:warn, "No file detected at #{option_js_file} so JS setup not added", :yellow)
  end
end

#set_default_optionsObject



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/blacklight_range_limit/assets_generator.rb', line 26

def set_default_options
  @option_js_file = options[:js_file]

  @option_asset_delivery_mode = options[:asset_delivery_mode]
  if option_asset_delivery_mode.nil?
    # prefererntially default to importmap
    if defined?(Importmap) && root.join("config/importmap.rb").exist?
      @option_asset_delivery_mode = "importmap-rails"
    elsif root.join("package.json").exist?
      @option_asset_delivery_mode = "yarn-package"
    else
      raise ArgumentError.new("Could not identify asset_delivery_mode, try supplying --asset-delivery-mode=[importmap-rails|yarn-package]")
    end
  end

  unless option_asset_delivery_mode.in?(["importmap-rails", "yarn-package"])
    raise ArgumentError.new("Illegal --asset-delivery-mode '#{option_asset_delivery_mode}', must be importmap-rails or yarn-package")
  end

  @option_yarn_local_package = options[:yarn_local_package]
  if option_yarn_local_package.nil?
    # default guess by CI in ENV or app name that we use for test apps
    @option_yarn_local_package = ENV['CI'].present? || Rails.application.class.name == "Internal::Application"
  end
end