Class: AppArchetype::Template::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/app_archetype/template/manifest.rb

Overview

Manifest is a description of an archetype

Constant Summary collapse

MIN_ARCHETYPE_VERSION =

Minimum supported archetype version

'1.0.0'.freeze
SCHEMA =

Manifest JSON schema

{
  type: 'object',
  required: %w[name version metadata variables],

  properties: {
    name: {
      type: 'string'
    },
    version: {
      type: 'string'
    },
    metadata: {
      type: 'object',
      required: %w[app_archetype],

      properties: {
        app_archetype: {
          type: 'object',
          required: %w[version]
        }
      }
    },
    variables: {
      type: 'object'
    }
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, data) ⇒ Manifest

Creates a manifest and memoizes the manifest data hash as a Hashe::Map

On initialize the manifest variables are retrieved and memoized for use in rendering the templates.

Parameters:



99
100
101
102
103
104
# File 'lib/app_archetype/template/manifest.rb', line 99

def initialize(path, data)
  @path = path
  @data = OpenStruct.new(data)
  @variables = AppArchetype::Template::VariableManager
               .new(@data.variables)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



88
89
90
# File 'lib/app_archetype/template/manifest.rb', line 88

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



88
89
90
# File 'lib/app_archetype/template/manifest.rb', line 88

def path
  @path
end

#variablesObject (readonly)

Returns the value of attribute variables.



88
89
90
# File 'lib/app_archetype/template/manifest.rb', line 88

def variables
  @variables
end

Class Method Details

.incompatible?(manifest) ⇒ Boolean

Incompatible returns true if the current manifest is not compatible with this version of AppArchetype.

A manifest is not compatible if it was created with a version greater than this the installed version.

Parameters:

  • manifest (Hash)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/app_archetype/template/manifest.rb', line 79

def incompatible?(manifest)
  manifest_version = manifest['metadata']['app_archetype']['version']
  return true if manifest_version < MIN_ARCHETYPE_VERSION
  return true if manifest_version > AppArchetype::VERSION
rescue NoMethodError
  true
end

.new_from_file(file_path) ⇒ Object

Creates a [AppArchetype::Template] from a manifest json so long as the manifest is compatible with this version of AppArchetype.

Parameters:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/app_archetype/template/manifest.rb', line 52

def new_from_file(file_path)
  manifest = Jsonnet.evaluate(
    File.read(file_path)
  )

  if incompatible?(manifest)
    raise 'provided manifest is invalid or incompatible with '\
    'this version of app archetype'
  end

  new(
    file_path,
    manifest
  )
end

Instance Method Details

#metadataString

Manifest metadata getter

Returns:



129
130
131
# File 'lib/app_archetype/template/manifest.rb', line 129

def 
  @data.
end

#nameString

Manifest name getter

Returns:



111
112
113
# File 'lib/app_archetype/template/manifest.rb', line 111

def name
  @data.name
end

#parent_pathString

Parent path of the manifest (working directory)

Returns:



137
138
139
# File 'lib/app_archetype/template/manifest.rb', line 137

def parent_path
  File.dirname(@path)
end

#templateAppArchetype::Template::Source

Loads the template that is adjacent to the manifest.json or manifest.jsonnet file.

If the template cannot be found, a RuntimeError explaining that the template cannot be found is raised.

Loaded template is memoized for the current session.



161
162
163
164
165
166
167
168
# File 'lib/app_archetype/template/manifest.rb', line 161

def template
  unless File.exist?(template_path)
    raise "cannot find template for manifest #{name}"
  end

  @template ||= AppArchetype::Template::Source.new(template_path)
  @template
end

#template_pathString

Template files path

Returns:



146
147
148
# File 'lib/app_archetype/template/manifest.rb', line 146

def template_path
  File.join(parent_path, 'template')
end

#valid?Boolean

Returns true if manifest is valid

Returns:

  • (Boolean)


188
189
190
# File 'lib/app_archetype/template/manifest.rb', line 188

def valid?
  validate.empty?
end

#validateArray

Runs a schema validation on the given manifest to determine whether the schema is valid. Returns an array of validation messages.

Returns:

  • (Array)


175
176
177
178
179
180
181
# File 'lib/app_archetype/template/manifest.rb', line 175

def validate
  JSON::Validator.fully_validate(
    SCHEMA,
    @data.to_h.to_json,
    strict: true
  )
end

#versionString

Manifest version getter

Returns:



120
121
122
# File 'lib/app_archetype/template/manifest.rb', line 120

def version
  @data.version
end