Class: Mxrb::Compiler::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/compiler/adapter.rb

Overview

Version-specific deployment contract. Compiler stages dispatch through this object instead of scattering major-version checks through the code.

Defined Under Namespace

Classes: V10, V11, V6, V7, V9

Constant Summary collapse

REQUIRED_FILES =
%w[
  model/model.mdp
  model/metadata.json
  model/bundles/project.jar
  web/index.html
].freeze
ROOTS =
%w[model web native sass tmp].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, source: nil) ⇒ Adapter

Returns a new instance of Adapter.



31
32
33
34
35
# File 'lib/mxrb/compiler/adapter.rb', line 31

def initialize(version, source: nil)
  @version = version.to_s
  @major = @version.split('.').first.to_i
  @source = source
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



18
19
20
# File 'lib/mxrb/compiler/adapter.rb', line 18

def major
  @major
end

#versionObject (readonly)

Returns the value of attribute version.



18
19
20
# File 'lib/mxrb/compiler/adapter.rb', line 18

def version
  @version
end

Class Method Details

.for(version, source: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/mxrb/compiler/adapter.rb', line 20

def self.for(version, source: nil)
  major = version.to_s.split('.').first.to_i
  klass = { 6 => V6, 7 => V7, 9 => V9, 10 => V10, 11 => V11 }[major]
  unless klass
    raise UnsupportedVersion,
          "audited native compilation supports Mendix 6.x, 7.x, 9.x, 10.x, and 11.x; got #{version}"
  end

  klass.new(version, source:)
end

Instance Method Details

#compatible_runtime?(runtime_version) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/mxrb/compiler/adapter.rb', line 68

def compatible_runtime?(runtime_version)
  target = version.split('.').first(3)
  actual = runtime_version.split('.').first(3)
  target == actual
end

#read_metadata(root) ⇒ Object



64
65
66
# File 'lib/mxrb/compiler/adapter.rb', line 64

def (root)
  JSON.parse(File.read(File.join(root, 'model', 'metadata.json')))
end

#validate_deployment!(root) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mxrb/compiler/adapter.rb', line 37

def validate_deployment!(root)
  validate_required_files!(root)
   = (root)
  runtime_version = .fetch('RuntimeVersion').to_s
  return  if compatible_runtime?(runtime_version)

  raise CompilationError,
        "deployment targets Mendix #{runtime_version}, but MPR targets #{version}"
rescue JSON::ParserError, KeyError => e
  raise CompilationError, "invalid model/metadata.json: #{e.message}"
end

#validate_freshness!(mpr_path, root) ⇒ Object

Raises:



49
50
51
52
53
54
55
# File 'lib/mxrb/compiler/adapter.rb', line 49

def validate_freshness!(mpr_path, root)
  compiled_model = File.join(root, 'model', 'model.mdp')
  return if File.mtime(compiled_model) >= File.mtime(mpr_path)

  raise CompilationError,
        "deployment is stale: #{compiled_model} is older than #{File.expand_path(mpr_path)}"
end

#validate_required_files!(root) ⇒ Object

Raises:



57
58
59
60
61
62
# File 'lib/mxrb/compiler/adapter.rb', line 57

def validate_required_files!(root)
  missing = REQUIRED_FILES.reject { File.file?(File.join(root, _1)) }
  return if missing.empty?

  raise CompilationError, "deployment is not materialized; missing #{missing.join(', ')}"
end

#web_profilesObject



74
# File 'lib/mxrb/compiler/adapter.rb', line 74

def web_profiles = [:dojo]