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'
    },
    next_steps: {
      type: 'array'
    }
  }
}.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:



102
103
104
105
106
107
# File 'lib/app_archetype/template/manifest.rb', line 102

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.



91
92
93
# File 'lib/app_archetype/template/manifest.rb', line 91

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



91
92
93
# File 'lib/app_archetype/template/manifest.rb', line 91

def path
  @path
end

#variablesObject

Returns the value of attribute variables.



91
92
93
# File 'lib/app_archetype/template/manifest.rb', line 91

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)


82
83
84
85
86
87
88
# File 'lib/app_archetype/template/manifest.rb', line 82

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:



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

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:



132
133
134
# File 'lib/app_archetype/template/manifest.rb', line 132

def 
  @data.
end

#nameString

Manifest name getter

Returns:



114
115
116
# File 'lib/app_archetype/template/manifest.rb', line 114

def name
  @data.name
end

#next_stepsString

Next steps getter

Returns:



141
142
143
144
145
146
147
# File 'lib/app_archetype/template/manifest.rb', line 141

def next_steps
  steps = @data.next_steps

  return [] unless steps

  steps
end

#parent_pathString

Parent path of the manifest (working directory)

Returns:



153
154
155
# File 'lib/app_archetype/template/manifest.rb', line 153

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.



177
178
179
180
181
182
183
184
# File 'lib/app_archetype/template/manifest.rb', line 177

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:



162
163
164
# File 'lib/app_archetype/template/manifest.rb', line 162

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

#valid?Boolean

Returns true if manifest is valid

Returns:

  • (Boolean)


204
205
206
# File 'lib/app_archetype/template/manifest.rb', line 204

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)


191
192
193
194
195
196
197
# File 'lib/app_archetype/template/manifest.rb', line 191

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

#versionString

Manifest version getter

Returns:



123
124
125
# File 'lib/app_archetype/template/manifest.rb', line 123

def version
  @data.version
end