Class: Wabi::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/wabi/lockfile.rb

Overview

Manages config/wabi.lock.json in a user’s Rails app. Tracks installed components, versions, hashes, and registry origin.

Constant Summary collapse

DEFAULT_REGISTRY =
"https://wabikit.dev/r"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, data: {}) ⇒ Lockfile

Returns a new instance of Lockfile.



24
25
26
27
28
# File 'lib/wabi/lockfile.rb', line 24

def initialize(path:, data: {})
  @path       = path
  @registry   = data["registry"]   || DEFAULT_REGISTRY
  @components = data["components"] || {}
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



12
13
14
# File 'lib/wabi/lockfile.rb', line 12

def components
  @components
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/wabi/lockfile.rb', line 12

def path
  @path
end

#registryObject (readonly)

Returns the value of attribute registry.



12
13
14
# File 'lib/wabi/lockfile.rb', line 12

def registry
  @registry
end

Class Method Details

.load(path) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/wabi/lockfile.rb', line 14

def self.load(path)
  data =
    if File.exist?(path)
      JSON.parse(File.read(path))
    else
      {}
    end
  new(path: path, data: data)
end

Instance Method Details

#record(name, version:, hash:, files: nil, js_dependencies: nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/wabi/lockfile.rb', line 30

def record(name, version:, hash:, files: nil, js_dependencies: nil)
  entry = { "version" => version, "hash" => hash }
  entry["files"]           = files           if files
  entry["js_dependencies"] = js_dependencies if js_dependencies
  @components[name] = entry
end

#saveObject



37
38
39
40
41
42
43
# File 'lib/wabi/lockfile.rb', line 37

def save
  FileUtils.mkdir_p(File.dirname(@path))
  File.write(@path, JSON.pretty_generate({
    "registry"   => @registry,
    "components" => @components,
  }))
end