Module: StimulusPlumbers::Generators::CssEntrypoint
- Included in:
- InstallGenerator
- Defined in:
- lib/stimulus_plumbers/generators/css_entrypoint.rb
Constant Summary collapse
- STIMULUS_PLUMBERS_CSS_FILE =
"STIMULUS_PLUMBERS_CSS_FILE"
Instance Method Summary collapse
- #apply_edit(css_file, directive, stale_pattern:, anchor_pattern: nil) ⇒ Object
-
#content_edit(content, directive, stale_pattern:, anchor_pattern: nil) ⇒ Object
stale_pattern:is required — every call site needs stale-import detection, so making it optional would leave an untested "anchor without stale" state. - #entry_css_file(candidates:, env_var:, fallback_env_var: nil) ⇒ Object
- #insert_near_anchor(content, directive, anchor_pattern) ⇒ Object
- #prepend(content, directive) ⇒ Object
- #relative_to_destination(path) ⇒ Object
- #warn_entry_css_not_found(candidates:, env_var:, label:, fallback_env_var: nil) ⇒ Object
Instance Method Details
#apply_edit(css_file, directive, stale_pattern:, anchor_pattern: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 31 def apply_edit(css_file, directive, stale_pattern:, anchor_pattern: nil) content = File.read(css_file) new_content, status = content_edit(content, directive, stale_pattern: stale_pattern, anchor_pattern: anchor_pattern) if new_content File.write(css_file, new_content) say_status status, relative_to_destination(css_file), :green else say_status :identical, relative_to_destination(css_file) end rescue Errno::EROFS, Errno::EACCES => e say "Could not update #{relative_to_destination(css_file)}: #{e.}. Skipping.", :yellow end |
#content_edit(content, directive, stale_pattern:, anchor_pattern: nil) ⇒ Object
stale_pattern: is required — every call site needs stale-import detection,
so making it optional would leave an untested "anchor without stale" state.
47 48 49 50 51 52 53 54 55 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 47 def content_edit(content, directive, stale_pattern:, anchor_pattern: nil) return nil if content.include?(directive) if (existing = content.match(stale_pattern)) return [content.sub(existing[0], directive), :update] end anchor_pattern ? insert_near_anchor(content, directive, anchor_pattern) : prepend(content, directive) end |
#entry_css_file(candidates:, env_var:, fallback_env_var: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 8 def entry_css_file(candidates:, env_var:, fallback_env_var: nil) [env_var, fallback_env_var].compact.each do |var| next unless ENV[var] path = File.(ENV.fetch(var, nil)) return path if File.exist?(path) end candidates .map { |c| File.join(destination_root, c) } .find { |f| File.exist?(f) } end |
#insert_near_anchor(content, directive, anchor_pattern) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 61 def insert_near_anchor(content, directive, anchor_pattern) if (anchor = content.match(anchor_pattern)) [content.sub(anchor[0], "#{anchor[0]}\n#{directive}"), :insert] else ["#{content.rstrip}\n#{directive}\n", :append] end end |
#prepend(content, directive) ⇒ Object
57 58 59 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 57 def prepend(content, directive) ["#{directive}\n#{content}", :prepend] end |
#relative_to_destination(path) ⇒ Object
69 70 71 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 69 def relative_to_destination(path) path.delete_prefix("#{destination_root}/") end |
#warn_entry_css_not_found(candidates:, env_var:, label:, fallback_env_var: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/stimulus_plumbers/generators/css_entrypoint.rb', line 21 def warn_entry_css_not_found(candidates:, env_var:, label:, fallback_env_var: nil) tried = candidates.map { |c| File.join(destination_root, c) } [env_var, fallback_env_var].compact.each do |var| tried.unshift(File.(ENV[var])) if ENV[var] end say "Could not find a #{label} entry file. Tried: #{tried.join(", ")}. " \ "Set #{env_var}=/path/to/entry.css and re-run.", :red end |