Class: FoobarTemplates::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/foobar_templates/template_manager.rb

Overview

This class handles the logic for finding templates in the user’s dir, the gem’s builtin templates (and on the web some day)

Class Method Summary collapse

Class Method Details

.collect_leaf_templates(dir) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/foobar_templates/template_manager.rb', line 70

def collect_leaf_templates(dir)
  config = load_template_config(dir)
  return [] if config.nil?

  if config[:monorepo] == true
    immediate_subdirectories(dir).flat_map { |child| collect_leaf_templates(child) }
  else
    [dir]
  end
end

.custom_template_locationObject



14
# File 'lib/foobar_templates/template_manager.rb', line 14

def custom_template_location() = File.expand_path("~/.foobar/templates")

.default_template_nameObject



15
# File 'lib/foobar_templates/template_manager.rb', line 15

def default_template_name() = "ruby-cli-gem"

.file_in_source?(target) ⇒ Boolean

Get’s path to ‘target’ from within the gem’s “templates” folder within the gem’s source

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/foobar_templates/template_manager.rb', line 112

def file_in_source?(target)
  src_in_source_path = "#{File.dirname(__FILE__)}/templates/#{target}"
  File.exist?(src_in_source_path)
end

.find_in_source_paths(target) ⇒ Object



105
106
107
108
# File 'lib/foobar_templates/template_manager.rb', line 105

def find_in_source_paths(target)
  path = "#{__dir__}/templates/#{target}"
  File.exist?(path) ? path : target
end

.get_template_src(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/foobar_templates/template_manager.rb', line 17

def get_template_src(options)
  template_name = options[:template] || default_template_name

  if template_exists_within_repo?(template_name)
    return resolve_template_path(internal_template_location, template_name)
  end

  custom_src = resolve_template_path(custom_template_location, template_name)
  return custom_src if File.exist?(custom_src)

  monorepo_match = resolve_monorepo_leaf_template(custom_template_location, template_name)
  return monorepo_match if monorepo_match

  raise FoobarTemplates::CLIError, "Error: template '#{template_name}' could not be found. Run the below command to list all available templates.\n\n  foobar_templates -l"
end

.immediate_subdirectories(dir) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/foobar_templates/template_manager.rb', line 96

def immediate_subdirectories(dir)
  return [] unless File.exist?(dir)

  Dir.children(dir).filter_map do |entry|
    full_path = File.join(dir, entry)
    File.directory?(full_path) ? full_path : nil
  end
end

.internal_template_locationObject



13
# File 'lib/foobar_templates/template_manager.rb', line 13

def internal_template_location() = File.expand_path("#{File.dirname(__FILE__)}/templates")

.load_template_config(dir) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/foobar_templates/template_manager.rb', line 86

def load_template_config(dir)
  path = File.join(dir, "foobar.yml")
  return nil unless File.exist?(path)

  raw = YAML.load_file(path, symbolize_names: true)
  raw.is_a?(Hash) ? raw : {}
rescue StandardError
  {}
end

.monorepo_directory?(dir) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/foobar_templates/template_manager.rb', line 81

def monorepo_directory?(dir)
  config = load_template_config(dir)
  !config.nil? && config[:monorepo] == true
end

.resolve_monorepo_leaf_template(root, requested_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/foobar_templates/template_manager.rb', line 47

def resolve_monorepo_leaf_template(root, requested_name)
  monorepo_roots = immediate_subdirectories(root).select { |dir| monorepo_directory?(dir) }
  return nil if monorepo_roots.empty?

  requested_leaf_name = requested_name.sub(/^template-/, "")
  leaf_paths = monorepo_roots.flat_map { |dir| collect_leaf_templates(dir) }
  matches = leaf_paths.select do |leaf_path|
    File.basename(leaf_path).sub(/^template-/, "") == requested_leaf_name
  end

  if matches.length > 1
    readable_paths = matches.map { |path| path.sub(/^#{Regexp.escape(root)}\//, "") }.sort.join(", ")
    raise FoobarTemplates::CLIError, "Ambiguous template name '#{requested_name}'. Matching leaf templates: #{readable_paths}. Rename one of the conflicting templates."
  end

  if matches.empty?
    readable_leaves = leaf_paths.map { |path| File.basename(path).sub(/^template-/, "") }.uniq.sort
    raise FoobarTemplates::CLIError, "Template '#{requested_name}' not found in monorepo leaf templates. Available leaf templates: #{readable_leaves.join(', ')}. Run 'foobar_templates -l' to list all available templates."
  end

  matches.first
end

.resolve_template_path(location, name) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/foobar_templates/template_manager.rb', line 37

def resolve_template_path(location, name)
  basic = "#{location}/#{name}"
  prefixed = "#{location}/template-#{name}"

  return basic if File.exist?(basic)
  return prefixed if File.exist?(prefixed)

  basic # fallback, even if it doesn't exist, will be caught by get_template_src
end

.template_exists_within_repo?(template_name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/foobar_templates/template_manager.rb', line 33

def template_exists_within_repo?(template_name)
  file_in_source?(template_name) || file_in_source?("template-#{template_name}")
end