Module: Cri::Scaffold

Defined in:
lib/cri/scaffold.rb,
lib/cri/scaffold/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.build_command(root:, name: nil, parent: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cri/scaffold.rb', line 11

def self.build_command(root:, name: nil, parent: nil)
  raise "#{root} does not exist." unless Dir.exist?(root)
  raise "name must be a String or nil, or parent must be nil." \
    if !parent.nil? && !name.is_a?(String)

  file = File.join(root, [name, "cli.rb"].compact.join("."))

  cmd =
    if parent.nil?
      raise "#{file} must exist." unless File.exist?(file)

      Cri::Command.new_basic_root.modify do
        program_name = File.basename($PROGRAM_NAME)
        name program_name

        instance_eval File.read(file), file
      end
    else
      parent.define_command(name.gsub("_", "-")) do |dsl|
        dsl.instance_eval File.read(file), file
      end
    end

  subdir = "#{root}/#{name || "cli"}"
  if Dir.exist?(subdir)
    Dir["#{subdir}/*.cli.rb"].each do |path|
      new_root = File.dirname(path)
      new_name = File.basename(path, ".cli.rb")

      Scaffold.build_command(root: new_root, name: new_name, parent: cmd)
    end
  end

  cmd
end