Class: Opal::Rails::OutputsManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/rails/outputs_manifest.rb

Constant Summary collapse

FILE_NAME =
'.opal-build-manifest.json'

Instance Method Summary collapse

Constructor Details

#initialize(build_path:) ⇒ OutputsManifest

Returns a new instance of OutputsManifest.



13
14
15
# File 'lib/opal/rails/outputs_manifest.rb', line 13

def initialize(build_path:)
  @build_path = Pathname(build_path).expand_path
end

Instance Method Details

#clobber!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opal/rails/outputs_manifest.rb', line 47

def clobber!
  outputs = read_outputs
  return nil if outputs.nil?

  outputs.each do |relative_path|
    full_path = safe_build_path(relative_path)
    full_path.delete if full_path&.exist?
  end

  manifest_path.delete if manifest_path.exist?

  outputs
end

#prune_stale!(current_outputs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opal/rails/outputs_manifest.rb', line 25

def prune_stale!(current_outputs)
  previous_outputs = read_outputs
  return false if previous_outputs.nil?

  stale_outputs(previous_outputs, current_outputs).each do |relative_path|
    full_path = safe_build_path(relative_path)
    full_path.delete if full_path&.exist?
  end

  true
end

#read_outputsObject



17
18
19
20
21
22
23
# File 'lib/opal/rails/outputs_manifest.rb', line 17

def read_outputs
  return [] unless manifest_path.exist?

  JSON.parse(manifest_path.read).fetch('outputs')
rescue JSON::ParserError, KeyError
  nil
end

#write!(outputs) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/opal/rails/outputs_manifest.rb', line 37

def write!(outputs)
  FileUtils.mkdir_p(build_path)

  Tempfile.create(['opal-build-manifest', '.json'], build_path.to_s) do |file|
    file.write(JSON.pretty_generate('version' => 1, 'outputs' => outputs.sort))
    file.flush
    FileUtils.mv(file.path, manifest_path)
  end
end