Module: FoobarTemplates

Defined in:
lib/foobar_templates.rb,
lib/foobar_templates/strings.rb,
lib/foobar_templates/version.rb,
lib/foobar_templates/configurator.rb,
lib/foobar_templates/template_manager.rb

Defined Under Namespace

Modules: CLI, Core Classes: CLIError, Configurator, TemplateManager

Constant Summary collapse

HELP_MSG =
<<-HEREDOC
                  Foobar Templates version #{FoobarTemplates::VERSION}
Use foobar_templates to start a new project folder based on a predefined template.

Usage Examples:

# Download all my template files (configured in ~/.foobar/config)
$ foobar_templates --install-public-templates

# Create a personal mono-repo template for your projects
$ foobar_templates --setup-personal-templates

# Create a ruby gem project using the built in service template
$ foobar_templates --template ruby-cli-gem your_project_name

# Convert the current directory which represents a working project into a
# template by replacing project name variants with foo-bar placeholders
$ cd my_recently_built_project
$ foobar_templates --copy-to-templates
HEREDOC
VERSION =
"2.0.0"

Class Method Summary collapse

Class Method Details

.cheat_sheetObject



22
23
24
# File 'lib/foobar_templates.rb', line 22

def cheat_sheet
  CLI::CheatSheet.go
end

.convert_grouped_hashes_to_output(available_templates) ⇒ Object

input: [ { “predefined” => [“default”, “service”] },

  { "MISC" => ["my_thing"] }
]


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/foobar_templates.rb', line 114

def convert_grouped_hashes_to_output(available_templates)
  template_list_output = ""
  available_templates.each do |category, template_names|

    template_list_output << " #{category&.upcase || "UNSPECIFIED"}:\n"
    template_names.each do |el|
      template_list_output << "   #{el}\n"
    end
    template_list_output << "\n"
  end
  template_list_output
end

.dir_to_template(input: $stdin, output: $stdout) ⇒ Object



78
79
80
# File 'lib/foobar_templates.rb', line 78

def dir_to_template(input: $stdin, output: $stdout)
  CLI::DirToTemplate.go(input: input, output: output)
end

.generate_template(options, gem_name) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/foobar_templates.rb', line 82

def generate_template(options, gem_name)
  require 'foobar_templates/cli/template_generator'

  template_generator = CLI::TemplateGenerator.new(options, gem_name).run
rescue FoobarTemplates::CLIError => e
  msg = e.message
  $stderr.puts msg if msg && !msg.empty? && msg != e.class.name
end

.group_hashes_by_key(available_templates) ⇒ Object

input: [ { “predefined” => “default” },

  { "MISC" => "my_thing" },
  { "prdefined" => "service" }
]

output: [ { “predefined” => [“default”, “service”] },

  { "MISC" => ["my_thing"] }
]


100
101
102
103
104
105
106
107
108
109
# File 'lib/foobar_templates.rb', line 100

def group_hashes_by_key(available_templates)
  h = {}
  available_templates.each do |hash|
    k = hash.first.first
    v = hash.first.last
    h[k] = [] unless h.has_key?(k)
    h[k] << v
  end
  h
end

.install_public_templatesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/foobar_templates.rb', line 44

def install_public_templates
  validate_git_is_installed!
  configurator = Configurator.new
  config_file_data = configurator.config_file_data
  puts "Downloading templates from the following locations: \n  #{config_file_data['public_templates'].split(" ").join("\n  ")}"
  config_file_data['public_templates'].split.each do |url|
    uri = URI.parse(url)
    template_folder_name = File.basename(uri.path).sub(/\.git$/, "")
    if !File.exist?("#{ENV['HOME']}/.foobar/templates/#{template_folder_name}")
      cmd = "cd #{ENV['HOME']}/.foobar/templates && git clone #{url}"
      cmd += " 2> /dev/null" if $test_env
      `#{cmd}`
    else
      puts "Warning: Skipping, template already exists #{ENV['HOME']}/.foobar/templates/#{template_folder_name}"
    end
  end
end

.listObject

lists available templates



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/foobar_templates.rb', line 27

def list
  configurator = Configurator.new
  available_templates = configurator.collect_user_defined_templates

  available_templates = group_hashes_by_key(available_templates)
  output = convert_grouped_hashes_to_output(available_templates)

  if output.empty?
    empty_output_msg = "You have no templates.  You can install the public example templates with\n"
    empty_output_msg += "the below command:\n\n"
    empty_output_msg += "foobar_templates --install-public-templates"
    return empty_output_msg
  end

  mark_default_template(output, configurator.default_template)
end

.mark_default_template(output_string, default_template) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/foobar_templates.rb', line 127

def mark_default_template(output_string, default_template)
  output_string.lines.reverse.map do |l|
    if l.strip == default_template
      l[1]= "*"
      "#{l.chomp}       (default)\n"
    else
      l
    end
  end.reverse.join
end

.setup_personal_templates(input: $stdin, output: $stdout) ⇒ Object



74
75
76
# File 'lib/foobar_templates.rb', line 74

def setup_personal_templates(input: $stdin, output: $stdout)
  CLI::SetupPersonalTemplatesRepo.go(input: input, output: output)
end

.validate_git_is_installed!Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/foobar_templates.rb', line 62

def validate_git_is_installed!
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "git#{ext}")
      return if File.executable?(exe) && !File.directory?(exe)
    end
  end

  raise CLIError, "Error: `git` command not found. Please install Git and ensure it's in your PATH."
end

.versionObject



18
19
20
# File 'lib/foobar_templates.rb', line 18

def version
  FoobarTemplates::VERSION
end

.which(executable) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/foobar_templates.rb', line 138

def which(executable)
  if File.file?(executable) && File.executable?(executable)
    executable
  elsif ENV['PATH']
    path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p|
      abs_path = File.join(p, executable)
      File.file?(abs_path) && File.executable?(abs_path)
    end
    path && File.expand_path(executable, path)
  end
end