Module: Esp::Mw::ScriptExtractor

Defined in:
lib/esp/mw/script_extractor.rb

Overview

Inverse of preflight’s text_source resolution. Given a JSON-format mod source whose Script records carry inline ‘text`, hoist each script body out to `scripts/<id>.mwscript` and replace the inline fields (`text`, `bytecode`, `variables`, `header`) with a single `text_source` pointer. Preflight regenerates everything on build, so the resulting source round-trips losslessly through `esp build`.

Skips records that already have ‘text_source` set — re-running is a no-op. .rb sources are out of scope; their author owns the layout.

Defined Under Namespace

Classes: Result

Constant Summary collapse

SAFE_ID =
/\A[A-Za-z0-9_-]+\z/

Class Method Summary collapse

Class Method Details

.extract!(mod, root: Esp::ROOT) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/esp/mw/script_extractor.rb', line 20

def extract!(mod, root: Esp::ROOT)
  source = Esp::Mw::Loader.resolve(mod, root: root)
  unless source.end_with?('.json')
    raise ArgumentError, Esp.t('errors.script_extractor.json_only', file: File.basename(source))
  end

  records = JSON.parse(File.read(source))
  source_dir = File.dirname(source)
  result = Result.new(extracted: [], skipped: [])

  records.each do |record|
    next unless record['type'] == 'Script'

    process_record!(record, source_dir, result)
  end

  File.write(source, "#{JSON.pretty_generate(records)}\n")
  result
end