Skip to content

Commit

Permalink
rework generators to allow disambiguation template name duplication a…
Browse files Browse the repository at this point in the history
…cross sources, and to prepare for saving a list of all installed templates and fully qualifying templates names by source
  • Loading branch information
wr0ngway committed Aug 30, 2018
1 parent e0801d0 commit b06fa68
Show file tree
Hide file tree
Showing 9 changed files with 841 additions and 1,053 deletions.
62 changes: 29 additions & 33 deletions lib/simplygenius/atmos/commands/generate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'base_command'
require_relative '../../atmos/generator_factory'
require_relative '../../atmos/source_path'
require_relative '../../atmos/generator'
require_relative '../../atmos/utils'

module SimplyGenius
Expand Down Expand Up @@ -41,50 +42,45 @@ def self.description
def execute
signal_usage_error "template name is required" if template_list.blank? && ! list?

sourcepaths = []
sourcepath_list.each do |sp|
sourcepaths << SourcePath.new(File.basename(sp), sp)
end

# don't want to fail for new repo
if Atmos.config && Atmos.config.is_atmos_repo?
config_sourcepaths = Atmos.config['template_sources'].try(:collect, &:location) || []
sourcepath_list.concat(config_sourcepaths)
Atmos.config['template_sources'].try(:each) do |item|
sourcepaths << SourcePath.new(item.name, item.location)
end
end

# Always search for templates against the bundled templates directory
sourcepath_list << File.expand_path('../../../../../templates', __FILE__)
sourcepaths << SourcePath.new('bundled', File.expand_path('../../../../../templates', __FILE__))

g = GeneratorFactory.create(sourcepath_list,
force: force?,
pretend: dryrun?,
quiet: quiet?,
skip: skip?,
dependencies: dependencies?)
if list?
logger.info "Valid templates are:"
list_templates(g, template_list).each {|l| logger.info(l) }
sourcepaths.each do |sp|
logger.info("\tSourcepath #{sp}")
filtered_names = sp.template_names.select do |name|
template_list.blank? || template_list.any? {|f| name =~ /#{f}/ }
end
filtered_names.each {|n| logger.info ("\t\t#{n}")}
end
else
g.generate(template_list)
end

end

def list_templates(generator, name_filters)
# Format templates into comma-separated paragraph with limt of 70 characters per line
filtered_names = generator.valid_templates.select do |name|
name_filters.blank? || name_filters.any? {|f| name =~ /#{f}/ }
end

lines = ['']
filtered_names.each do |template_name|
line = lines.last
if line.size == 0
line << template_name
elsif line.size + template_name.size > 68
line << ','
lines << template_name # new line
else
line << ", " + template_name
g = Generator.new(*sourcepaths,
force: force?,
pretend: dryrun?,
quiet: quiet?,
skip: skip?,
dependencies: dependencies?)
begin
g.generate(template_list)
rescue ArgumentError => e
logger.error(e.message)
exit 1
end
end

return lines
end

end
Expand Down

0 comments on commit b06fa68

Please sign in to comment.