Module: FoobarTemplates::CLI::DirToTemplate

Defined in:
lib/foobar_templates/cli/dir_to_template.rb

Constant Summary collapse

NO_PERSONAL_REPO_MSG =
"Unable to convert to template, no personal templates repo exists.  Run `foobar_templates --setup-personal-templates` to get that setup.".freeze

Class Method Summary collapse

Class Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/foobar_templates/cli/dir_to_template.rb', line 13

def go(input: $stdin, output: $stdout)
  personal_dir = FoobarTemplates::CLI::SetupPersonalTemplatesRepo.personal_templates_dir
  if personal_dir.nil? || !File.directory?(personal_dir)
    output.puts NO_PERSONAL_REPO_MSG
    raise FoobarTemplates::CLIError
  end

  validate_inside_git_repo!(output: output)

  project_name = File.basename(Dir.pwd)
  default_category = read_existing_category(Dir.pwd) || "misc"

  template_folder_name = prompt(input, output, "Template folder name", project_name)
  validate_folder_name!(template_folder_name, output: output)

  category = prompt(input, output, "Category", default_category)

  dest = File.join(personal_dir, template_folder_name)

  if File.exist?(dest)
    output.puts "Error: a template already exists at #{dest}.  Remove it or rename your project before re-running."
    raise FoobarTemplates::CLIError
  end

  copy_tracked_files_to(dest)
  write_foobar_yml(dest, category)

  files_changed = Dir.chdir(dest) do
    FoobarTemplates::Core::DirToTemplate.🧙🪄! Find.find('.'), template_name: project_name
  end

  output.puts "Template created at: #{dest}"
  output.puts "Tip: review the new template directory and remove any files that aren't helpful as a starting point (build artifacts, secrets, lockfiles, large fixtures, etc.)."
  files_changed
end