Class: Ashiba::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/ashiba/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



48
49
50
# File 'lib/ashiba/cli.rb', line 48

def config
  @config
end

#packdataObject

Returns the value of attribute packdata.



45
46
47
# File 'lib/ashiba/cli.rb', line 45

def packdata
  @packdata
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ashiba/cli.rb', line 34

def self.exit_on_failure?
  true
end

.source_rootObject

add_runtime_options!



29
30
31
32
# File 'lib/ashiba/cli.rb', line 29

def self.source_root
  # File.dirname(__FILE__)
  File.expand_path('../../templates', __dir__)
end

Instance Method Details

#__print_versionObject



40
41
42
# File 'lib/ashiba/cli.rb', line 40

def __print_version
  say "Ashiba #{VERSION} (Ruby #{RUBY_VERSION}-#{RUBY_PLATFORM})"
end

#create(template, path) ⇒ Object

Raises:

  • (Error)


172
173
174
175
176
177
178
179
# File 'lib/ashiba/cli.rb', line 172

def create(template, path)
  path = File.expand_path(path)
  raise Error, set_color("ERROR: #{path} already exists.", :red) if File.exist?(path)

  @path = path
  parse_template_data(template)
  copy_template(template, path)
end

#info(template) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ashiba/cli.rb', line 211

def info(template)
  load_data_from_userconfig

  template_info = get_data_from_template(template)
  output = <<~OUTPUT
    Name       : #{template_info['name']}
    Version    : #{template_info['version']}
    Description: #{template_info['description'].chop}
    Origin     : #{template_info['origin']}

    Variables:
  OUTPUT
  say output

  align_to = template_info['variables'].keys.map(&:length).max # { |item| item.length }.max
  template_info['variables'].each do |name, default|
    default = '<unset>' if default.empty? || default == :mandatory

    outline =  "  #{name.ljust(align_to)}: "
    outline << "#{@config.settings[name]} (was: #{default})" if config.settings[name]
    outline << default unless config.settings[name]
    say outline
  end
end

#listObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ashiba/cli.rb', line 182

def list
  # @todo Should be in TemplateLoader
  dirs = TemplateLoader.new.locations
  entries = []
  dirs.each do |dir|
    yamls = Dir.glob('*.yaml', base: dir)
    basenames = yamls.map { |filename| filename.gsub(/\.yaml$/, '') }

    entries.concat(basenames)
  end

  say 'Available templates:'
  entries.uniq.sort.each do |template|
    data = get_data_from_template(template)
    say "  #{data['name']} (#{data['version']}) - #{data['summary']}"
  end
end

#locationsObject



201
202
203
204
205
206
207
208
# File 'lib/ashiba/cli.rb', line 201

def locations
  dirs = TemplateLoader.new.locations

  say 'Locations searched for templates'
  dirs.each do |dir|
    say "  #{dir}"
  end
end