Class: Mxrb::Initializer
- Inherits:
-
Object
- Object
- Mxrb::Initializer
- Defined in:
- lib/mxrb/initializer.rb
Overview
Creates the minimum editable Ruby project accepted by mxrb generate.
rubocop:disable Metrics
Direct Known Subclasses
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
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#initialize(name, subject: :project, mxrb_path: nil, mode: :mendix, stack: nil) ⇒ Initializer
constructor
A new instance of Initializer.
- #scaffold(into: Dir.pwd) ⇒ Object
Constructor Details
#initialize(name, subject: :project, mxrb_path: nil, mode: :mendix, stack: nil) ⇒ Initializer
Returns a new instance of Initializer.
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.(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
#mode ⇒ Object (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.(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 |