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



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

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



46
47
48
49
50
51
52
53
54
# File 'lib/drakkon/skeleton/deploy.rb', line 46

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



37
38
39
40
41
42
43
44
# File 'lib/drakkon/skeleton/deploy.rb', line 37

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

  result
end

.promptObject



97
98
99
# File 'lib/drakkon/skeleton/deploy.rb', line 97

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

.skeleton_selectObject



56
57
58
59
60
61
62
# File 'lib/drakkon/skeleton/deploy.rb', line 56

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/drakkon/skeleton/deploy.rb', line 82

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
# 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]}"
    raise 'Missing File!' unless File.exist?(file_src)

    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