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/ClassLength

Direct Known Subclasses

ModuleInitializer

Defined Under Namespace

Classes: Result

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Initializer.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/mxrb/initializer.rb', line 14

def initialize(name, subject: :project, mxrb_path: 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"
  @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 Method Details

#scaffold(into: Dir.pwd) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mxrb/initializer.rb', line 24

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)
  write_scaffold(staging)
  FileUtils.mv(staging, root)
  Result.new(root, relative_files.map { File.join(root, _1) }.freeze)
ensure
  FileUtils.rm_rf(staging) if staging && File.exist?(staging)
end