module Gyro::Template
Public Class Methods
config()
click to toggle source
Hash of Yaml config for templates
# File lib/gyro/template.rb, line 136 def self.config @config ||= YAML.load_file(Gyro::Template.directory + 'config.yml') end
directory()
click to toggle source
Returns the Pathname representing the directory where all bundled templates are located
# File lib/gyro/template.rb, line 79 def self.directory Pathname.new(File.dirname(__FILE__)) + '../templates' end
find(template_param, fail_on_error = true)
click to toggle source
@param [String] template_param
The name or path of the template to find
@return [Pathname]
The path to the template corresponding to that name or path
# File lib/gyro/template.rb, line 100 def self.find(template_param, fail_on_error = true) template = if template_param.include?('/') find_by_path(template_param) else find_by_name(template_param) end if template.nil? && fail_on_error Gyro::Log.fail!('You need to specify a valid template directory or name' \ ' using the --template parameter (see --help for more info)') end template end
find_by_name(name)
click to toggle source
@param [String] name
The name of the template to find among the templates bundled with gyro
@return [Pathname]
The path to the template corresponding to that name or path
# File lib/gyro/template.rb, line 128 def self.find_by_name(name) target = Gyro::Template.resolve_alias(name) template_dir = Gyro::Template.directory + (target || name) return template_dir if template_dir.directory? end
find_by_path(path)
click to toggle source
@param [String] path
The path to the template to find
@return [Pathname]
The path to the template corresponding to that name or path
# File lib/gyro/template.rb, line 118 def self.find_by_path(path) template_dir = Pathname.new(path) return template_dir if template_dir.directory? end
print_infos(template)
click to toggle source
# File lib/gyro/template.rb, line 68 def self.print_infos(template) template_dir = Gyro::Template.find(template, false) Gyro::Log.fail!("No template found at path or for name '#{template}'.") unless template_dir readme = template_dir + 'README.md' Gyro::Log.fail!("No README.md found for template #{template}.") unless readme.exist? puts readme.read end
print_list()
click to toggle source
Print template list representation
# File lib/gyro/template.rb, line 23 def self.print_list directories = Gyro::Template.directory.children.select(&:directory?).map(&:basename).map(&:to_s) deprecated = select_deprecated_templates(directories) non_deprecated = select_non_deprecated_templates(directories) print_templates(non_deprecated.sort + deprecated.sort) end
print_templates(array)
click to toggle source
# File lib/gyro/template.rb, line 52 def self.print_templates(array) array.each do |name| alias_target = Gyro::Template.resolve_alias(name) is_deprecated = config['deprecated'].include?(name) txt = [' - '] txt << name.colorize(is_deprecated ? :gray : :normal) if alias_target txt << ' (alias for '.colorize(:gray, :faint) txt << alias_target.colorize(:gray) txt << ')'.colorize(:gray, :faint) end txt << ' (deprecated)'.colorize(:yellow) if is_deprecated puts txt.join end end
resolve_alias(name)
click to toggle source
@param [Pathname] entry
The alias to resolve
@return [String]
The alias target name Or nil if the entry does not correspond to a valid template alias
# File lib/gyro/template.rb, line 89 def self.resolve_alias(name) target = config['alias'][name] return nil unless target target end
select_deprecated_templates(directories)
click to toggle source
Select deprecated templates from the config file
@param [Array<String>] directories
The array of directories name
@return [Array<String>]
The array of deprecated templates
# File lib/gyro/template.rb, line 37 def self.select_deprecated_templates(directories) config['deprecated'].select { |t| config['alias'].key?(t) || directories.include?(t) } end
select_non_deprecated_templates(directories)
click to toggle source
Select non deprecated templates from the template directory and the config file
@param [Array<String>] directories
The array of directories name
@return [Array<String>]
The array of non deprecated templates
# File lib/gyro/template.rb, line 48 def self.select_non_deprecated_templates(directories) (directories + config['alias'].keys).reject { |t| config['deprecated'].include?(t) } end