Class: ReactManifest::SprocketsManifestPatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/react_manifest/sprockets_manifest_patcher.rb

Overview

Patches the Sprockets asset manifest (app/assets/config/manifest.js) to add a ‘link_tree` directive for the ux_manifests directory.

This is required so Sprockets knows to compile and serve the generated ux_*.js bundle files. Without it, javascript_include_tag raises AssetNotPrecompiledError.

Usage:

ReactManifest::SprocketsManifestPatcher.new(config).patch!

Defined Under Namespace

Classes: Result

Constant Summary collapse

MANIFEST_GLOB =
"app/assets/config/manifest.js".freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ SprocketsManifestPatcher

Returns a new instance of SprocketsManifestPatcher.



15
16
17
# File 'lib/react_manifest/sprockets_manifest_patcher.rb', line 15

def initialize(config = ReactManifest.configuration)
  @config = config
end

Instance Method Details

#already_patched?Boolean

Returns true if the manifest file already contains the link_tree directive.

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/react_manifest/sprockets_manifest_patcher.rb', line 57

def already_patched?
  path = manifest_path
  return false unless path

  File.read(path, encoding: "utf-8").include?(link_tree_directive.strip)
rescue Errno::ENOENT, Errno::EACCES
  false
end

Returns the directive string that should be present.



51
52
53
54
# File 'lib/react_manifest/sprockets_manifest_patcher.rb', line 51

def link_tree_directive
  subdir = normalize_subdir
  "//= link_tree ../javascripts/#{subdir} .js\n"
end

#patch!Object

Patch the Sprockets manifest file. Returns a Result.



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
# File 'lib/react_manifest/sprockets_manifest_patcher.rb', line 20

def patch!
  path = manifest_path
  unless path
    return Result.new(
      file: nil,
      status: :not_found,
      detail: "#{MANIFEST_GLOB} not found — create it and add //= link_tree ../javascripts/ux_manifests .js"
    )
  end

  content = File.read(path, encoding: "utf-8")
  directive = link_tree_directive

  if content.include?(directive.strip)
    return Result.new(file: path, status: :already_patched, detail: "link_tree directive already present")
  end

  new_content = append_directive(content, directive)

  if @config.dry_run?
    $stdout.puts "[ReactManifest] DRY-RUN: would patch #{short(path)}"
    $stdout.puts "  + #{directive.strip}"
    return Result.new(file: path, status: :dry_run, detail: nil)
  end

  File.write(path, new_content, encoding: "utf-8")
  $stdout.puts "[ReactManifest] Patched Sprockets manifest: #{short(path)}"
  Result.new(file: path, status: :patched, detail: nil)
end