Class: Capsium::Package::Manifest

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
sig/capsium/package/manifest.rbs,
lib/capsium/package/manifest.rb

Overview

Loads, generates and writes manifest.json (ARCHITECTURE.md section 3).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Manifest

Returns a new instance of Manifest.

Parameters:

  • path (String)


16
17
18
19
20
21
22
23
24
# File 'lib/capsium/package/manifest.rb', line 16

def initialize(path)
  @path = path
  @content_path = File.join(File.dirname(@path), Package::CONTENT_DIR)
  @config = if File.exist?(path)
              ManifestConfig.from_json(File.read(path))
            else
              ManifestConfig.new(resources: generate_manifest)
            end
end

Instance Attribute Details

#configManifestConfig (readonly)

Returns the value of attribute config.

Returns:



12
13
14
# File 'lib/capsium/package/manifest.rb', line 12

def config
  @config
end

#content_pathString (readonly)

Returns the value of attribute content_path.

Returns:

  • (String)


12
13
14
# File 'lib/capsium/package/manifest.rb', line 12

def content_path
  @content_path
end

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


12
13
14
# File 'lib/capsium/package/manifest.rb', line 12

def path
  @path
end

Instance Method Details

#content_file_exists?(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)


63
64
65
# File 'lib/capsium/package/manifest.rb', line 63

def content_file_exists?(path)
  File.exist?(path_to_content_file(path))
end

#generate_manifestHash[String, Resource]

Auto-generation: scan content/ recursively, detect MIME types, default visibility "exported", deterministic (sorted) output.

Returns:



29
30
31
32
33
34
# File 'lib/capsium/package/manifest.rb', line 29

def generate_manifest
  content_files.sort.to_h do |file_path|
    [relative_path(file_path),
     Resource.new(type: mime_from_path(file_path), visibility: "exported")]
  end
end

#lookup(path) ⇒ Resource?

Parameters:

  • path (String)

Returns:



40
41
42
# File 'lib/capsium/package/manifest.rb', line 40

def lookup(path)
  resources[path]
end

#path_to_content_file(path) ⇒ Pathname

Parameters:

  • path (String)

Returns:

  • (Pathname)

Raises:

  • (TypeError)


57
58
59
60
61
# File 'lib/capsium/package/manifest.rb', line 57

def path_to_content_file(path)
  raise TypeError, "Path cannot be nil" if path.nil?

  Pathname.new(File.dirname(@path)).join(path)
end

#relative_path(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


67
68
69
# File 'lib/capsium/package/manifest.rb', line 67

def relative_path(path)
  Pathname.new(path).relative_path_from(File.dirname(@path)).to_s
end

#resourcesHash[String, Resource]

Returns:



36
37
38
# File 'lib/capsium/package/manifest.rb', line 36

def resources
  @config.resources
end

#save_to_file(output_path = @path) ⇒ void

This method returns an undefined value.

Parameters:

  • output_path (String) (defaults to: @path)


53
54
55
# File 'lib/capsium/package/manifest.rb', line 53

def save_to_file(output_path = @path)
  File.write(output_path, to_json)
end

#to_hashHash[String, untyped]

Returns:

  • (Hash[String, untyped])


23
# File 'sig/capsium/package/manifest.rbs', line 23

def to_hash: () -> Hash[String, untyped]

#to_json(*_args) ⇒ String

Parameters:

  • args (Object)

Returns:

  • (String)


49
50
51
# File 'lib/capsium/package/manifest.rb', line 49

def to_json(*_args)
  @config.to_json
end

#type_for(path) ⇒ String?

Parameters:

  • path (String)

Returns:

  • (String, nil)


44
45
46
47
# File 'lib/capsium/package/manifest.rb', line 44

def type_for(path)
  resource = lookup(path)
  resource&.type
end