Class: Mxrb::Initializer

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

Overview

Creates the minimum editable Ruby project accepted by mxrb generate. rubocop:disable Metrics

Direct Known Subclasses

ModuleInitializer

Defined Under Namespace

Classes: Result

Constant Summary collapse

NAME =
/\A[A-Za-z][A-Za-z0-9_-]*\z/
MODES =
%i[mendix ruby].freeze
COMPOUND_SUFFIXES =
%w[service clinic portal demo api app kit].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, subject: :project, mxrb_path: nil, mode: :mendix, stack: nil) ⇒ Initializer

Returns a new instance of Initializer.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mxrb/initializer.rb', line 17

def initialize(name, subject: :project, mxrb_path: nil, mode: :mendix, stack: nil)
  @dir_name = name.to_s
  raise ArgumentError, "#{subject} name must be snake_case or PascalCase" unless NAME.match?(@dir_name)

  @module_name = to_pascal_case(@dir_name)
  @mpr_name = "#{@module_name}.mpr"
  @mode = mode.to_sym
  raise ArgumentError, 'project mode must be mendix or ruby' unless MODES.include?(@mode)

  @stack = stack&.to_sym
  raise ArgumentError, 'Ruby stack presets require --mode ruby' if @stack && @mode != :ruby
  if @stack && !RubyApp::Preset::NAMES.include?(@stack)
    raise ArgumentError, "Ruby stack must be #{RubyApp::Preset::NAMES.join(' or ')}"
  end

  @mxrb_path = File.expand_path(mxrb_path) if mxrb_path
  raise ArgumentError, "mxrb path does not exist: #{@mxrb_path}" if @mxrb_path && !File.directory?(@mxrb_path)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



15
16
17
# File 'lib/mxrb/initializer.rb', line 15

def mode
  @mode
end

Instance Method Details

#scaffold(into: Dir.pwd) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mxrb/initializer.rb', line 36

def scaffold(into: Dir.pwd)
  parent = File.expand_path(into)
  root = File.join(parent, @dir_name)
  abort "#{root}: directory already exists" if File.exist?(root)

  FileUtils.mkdir_p(parent)
  staging = Dir.mktmpdir(".#{@dir_name}.mxrb-init-", parent)
  mode == :ruby ? write_ruby_scaffold(staging) : write_scaffold(staging)
  files = mode == :ruby ? generated_files(staging) : relative_files
  FileUtils.mv(staging, root)
  Result.new(root, files.map { File.join(root, _1) }.freeze)
ensure
  FileUtils.rm_rf(staging) if staging && File.exist?(staging)
end