Class: AppArchetype::Commands::RenderTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/app_archetype/commands/render_template.rb

Overview

Prompts user for variable values and renders template to disk

Instance Method Summary collapse

Constructor Details

#initialize(manager, destination_path, options = Hashie::Mash.new) ⇒ RenderTemplate

Returns a new instance of RenderTemplate.



7
8
9
10
11
12
# File 'lib/app_archetype/commands/render_template.rb', line 7

def initialize(manager, destination_path, options = Hashie::Mash.new)
  @manager = manager
  @destination_path = destination_path
  @options = options
  @prompt = TTY::Prompt.new
end

Instance Method Details

#boolean_variable_prompt_for(var) ⇒ Boolean

Prompts and then asks for boolean input for a boolean variable

Parameters:

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
# File 'lib/app_archetype/commands/render_template.rb', line 135

def boolean_variable_prompt_for(var)
  puts "#{var.name} (#{var.description})"

  @prompt.yes?(
    "Enter value for `#{var.name}` variable:",
    default: var.default
  )
end

#integer_variable_prompt_for(var) ⇒ Integer

Prompts and then asks for integer input for a integer variable

Parameters:

Returns:

  • (Integer)


152
153
154
155
156
157
158
# File 'lib/app_archetype/commands/render_template.rb', line 152

def integer_variable_prompt_for(var)
  puts "#{var.name} (#{var.description})"

  @prompt.ask("Enter value for `#{var.name}` variable:",
              convert: :int,
              default: var.default)
end

#render_template(manifest, template, overwrite: false) ⇒ Object

Builds plan to render template and executes it - essentially rendering the template to the output location

Parameters:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/app_archetype/commands/render_template.rb', line 88

def render_template(
  manifest,
  template,
  overwrite: false
)
  plan = AppArchetype::Template::Plan.new(
    template,
    manifest.variables,
    destination_path: @destination_path,
    overwrite: overwrite
  )

  plan.devise
  plan.execute
end

#resolve_variables(manifest) ⇒ Object

Prompts user for values for each variable specified in the given manifest. And then sets the value of those variables to the answers to the prompts.

@param manifest



72
73
74
75
76
77
# File 'lib/app_archetype/commands/render_template.rb', line 72

def resolve_variables(manifest)
  manifest.variables.all.each do |var|
    value = variable_prompt_for(var)
    var.set!(value)
  end
end

#runObject

Renders a template with instructions described in the manifest.

First it looks to the options to determine the name of the manifest. If one is not provided then the user will be prompted to choose a manifest from the list.

The manager will then attempt to find the manifest. if one is not found then a RuntimeError will be raised.

Once the manifest is loaded the template will be loaded into memory.

Then the variables specified in the manifest are resolved. This involves the command prompting for values.

A plan can then be constructed with the template and variables, this plan is then devised and executed.

When the render is successful a success message is sent to STDOUT to confirm the operation was successful.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/app_archetype/commands/render_template.rb', line 40

def run
  name = @options.name
  name ||= @prompt.select('Please choose manifest', @manager.manifest_names)

  manifest = @manager.find_by_name(name)

  unless manifest
    puts "✖ No template with name `#{name}` found."
    return
  end

  template = manifest.template
  template.load

  resolve_variables(manifest)
  render_template(
    manifest,
    template,
    overwrite: @options.overwrite
  )

  puts("✔ Rendered #{name} to #{@destination_path}")
end

#string_variable_prompt_for(var) ⇒ String

Prompts and then asks for string input for a string variable

Parameters:

Returns:



168
169
170
171
172
173
174
175
# File 'lib/app_archetype/commands/render_template.rb', line 168

def string_variable_prompt_for(var)
  puts "#{var.name} (#{var.description})"

  @prompt.ask(
    "Enter value for `#{var.name}` variable:",
    default: var.default
  )
end

#variable_prompt_for(var) ⇒ Object

Resolver for a given variable

First, it will set the value if the value is set in the manifest.

Otherwise it will call a function that prompts a user for input depending on type.

By default it will call the string variable prompt

Parameters:

Returns:

  • (Object)


119
120
121
122
123
124
125
# File 'lib/app_archetype/commands/render_template.rb', line 119

def variable_prompt_for(var)
  return var.value if var.value?
  return boolean_variable_prompt_for(var) if var.type == 'boolean'
  return integer_variable_prompt_for(var) if var.type == 'integer'

  string_variable_prompt_for(var)
end