Module: Drakkon::Skeleton::Deploy

Defined in:
lib/drakkon/skeleton/deploy.rb

Overview

Run Command for CLI

Class Method Summary collapse

Class Method Details

.collect_skeleton_templates(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/drakkon/skeleton/deploy.rb', line 67

def self.collect_skeleton_templates(name)
  data = Hub.skeletons[name]
  Hub.skeletons[name][:templates].map do |template_name|
    template_config_file = case data[:source].to_sym
                           when :local
                             "#{data[:path]}/#{template_name}/skeleton.json"
                           end

    template_config = JSON.parse(File.read(template_config_file), { symbolize_names: true })
    template_config[:path] = case data[:source].to_sym
                             when :local
                               "#{data[:path]}/#{template_name}"
                             end

    template_config
  end
end

.collect_variables(template) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/drakkon/skeleton/deploy.rb', line 49

def self.collect_variables(template)
  variables = {}
  template[:variables].each do |var|
    question = var[:prompt] || var[:key]
    variables[var[:key].to_sym] = prompt.ask(question, **var[:ask])
  end

  variables
end

.process_variables(src, variables) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/drakkon/skeleton/deploy.rb', line 40

def self.process_variables(src, variables)
  result = src.clone
  variables.each do |key, value|
    result.gsub!(/DRAKKON_#{key}/, value)
  end

  result
end

.promptObject



100
101
102
# File 'lib/drakkon/skeleton/deploy.rb', line 100

def self.prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
end

.skeleton_selectObject



59
60
61
62
63
64
65
# File 'lib/drakkon/skeleton/deploy.rb', line 59

def self.skeleton_select
  prompt.select('Which Skeleton?', filter: true, per_page: 25) do |menu|
    Hub.skeletons.each_key { |sk|  menu.choice name: sk, value: sk }
  end
rescue TTY::Reader::InputInterrupt
  exit 0
end

.skeleton_template_select(name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/drakkon/skeleton/deploy.rb', line 85

def self.skeleton_template_select(name)
  templates = collect_skeleton_templates(name)

  prompt.select('Which Template?', filter: true, per_page: 25) do |menu|
    templates.each do |template|
      name = template[:name].pastel(:bright_blue)
      name += " - #{template[:description]}".pastel(:bright_black)

      menu.choice name: name, value: template
    end
  end
rescue TTY::Reader::InputInterrupt
  exit 0
end

.start(args) ⇒ Object



5
6
7
8
9
10
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
# File 'lib/drakkon/skeleton/deploy.rb', line 5

def self.start(args)
  name = args.shift&.to_sym
  name = skeleton_select if name.nil?

  template_name = args.shift
  template = collect_skeleton_templates(name.to_sym).find { |x| x[:name] == template_name } if template_name
  template = skeleton_template_select(name) if template.nil?

  variables = collect_variables(template)

  template[:files].each do |files|
    file_src = "#{template[:path]}/#{files[:source]}"
    unless File.exist?(file_src)
      LogBot.fatal('Skeleton', "Missing File! File: #{file_src}")
      exit 0
    end

    src = File.read(file_src)
    result = process_variables(src, variables)

    # Assume Src is Target Unless specified
    files[:target] ||= files[:source]

    # Allow for Dynamic Targets
    target = process_variables(files[:target], variables)

    # Map Names
    target = target.downcase if files[:downcase]

    # Create Subdirectories as needed
    FileUtils.mkdir_p File.dirname(target)
    File.write(target, result)
  end
end