Class: FoobarTemplates::CLI::TemplateGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, gem_name) ⇒ TemplateGenerator

Returns a new instance of TemplateGenerator.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/foobar_templates/cli/template_generator.rb', line 14

def initialize(options, gem_name)
  @options = options
  @gem_name = resolve_name(gem_name)

  @name = @gem_name
  @target = Pathname.pwd.join(gem_name)
  @template_src = ::FoobarTemplates::TemplateManager.get_template_src(options)
  @configurator = ::FoobarTemplates::Configurator.new

  @tconf = load_template_configs
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



12
13
14
# File 'lib/foobar_templates/cli/template_generator.rb', line 12

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/foobar_templates/cli/template_generator.rb', line 12

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/foobar_templates/cli/template_generator.rb', line 12

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



12
13
14
# File 'lib/foobar_templates/cli/template_generator.rb', line 12

def target
  @target
end

Instance Method Details

#build_interpolation_configObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/foobar_templates/cli/template_generator.rb', line 76

def build_interpolation_config
  title = name.tr('-', '_').split('_').map(&:capitalize).join(" ")
  pascal_name = name.tr('-', '_').split('_').map(&:capitalize).join
  unprefixed_name = name.sub(/^#{@tconf[:prefix]}/, '')
  underscored_name = name.tr('-', '_')
  constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] unless p.empty?}.join
  constant_name = constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
  constant_array = constant_name.split('::')
  git_user_name = `git config user.name`.chomp
  git_user_email = `git config user.email`.chomp

  # Resolve domain values from ~/.foobar/config, prompting if needed
  required_domains = scan_template_for_required_domains
  prompt_for_missing_domains(required_domains)

  registry_domain = @configurator.domain('registry_domain')
  k8s_domain = @configurator.domain('k8s_domain')
  git_repo_domain = @configurator.domain('repo_domain') || 'github.com'

  if git_user_name.empty?
    raise FoobarTemplates::CLIError, [
      "Error: git config user.name didn't return a value.  You'll probably want to make sure that's configured with your github username:",
      "",
      "git config --global user.name YOUR_GH_NAME",
    ].join("\n")
  else
    # git_repo_path = provider.com/user/name
    git_repo_path = "#{git_repo_domain}/#{git_user_name}/#{name}".downcase # downcasing for languages like go that are creative
  end

  # git_repo_url = https://provider.com/user/name
  git_repo_url = "https://#{git_repo_domain}/#{git_user_name}/#{name}"

  image_path = "#{git_user_name}/#{name}".downcase
  registry_repo_path = "#{registry_domain}/#{image_path}".downcase

  config = {
    :name             => name,
    :title            => title,
    :unprefixed_name  => unprefixed_name,
    unprefixed_pascal: unprefixed_name.tr('-', '_').split('_').map(&:capitalize).join,
    underscored_name:  underscored_name,
    :pascal_name      => pascal_name,
    :camel_name       => pascal_name.sub(/^./, &:downcase),
    :screamcase_name  => name.tr('-', '_').upcase,
    :namespaced_path  => name.tr('-', '/'),
    :makefile_path    => "#{underscored_name}/#{underscored_name}",
    :constant_name    => constant_name,
    :constant_array   => constant_array,
    :author           => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
    :email            => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
    :git_repo_domain  => git_repo_domain,
    :git_repo_url     => git_repo_url,
    :git_repo_path    => git_repo_path,
    :image_path       => image_path,
    :registry_domain  => registry_domain,
    :registry_repo_path => registry_repo_path,
    :k8s_domain       => k8s_domain,
    :template         => @options[:template],
    :test             => @options[:test],
  }
end

#configObject



26
27
28
# File 'lib/foobar_templates/cli/template_generator.rb', line 26

def config
  @config ||= build_interpolation_config
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/foobar_templates/cli/template_generator.rb', line 30

def run
  puts "Beginning run" if $TRACE
  raise_project_with_that_name_already_exists! if File.exist?(target)

  puts "ensure_safe_project_name" if $TRACE
  ensure_safe_project_name(name, config[:constant_array])

  puts "run_name_validation" if $TRACE
  run_name_validation

  template_src = match_template_src

  puts "dynamically_generate_template_directories" if $TRACE
  time_it("dynamically_generate_template_directories") do
    @template_directories = dynamically_generate_template_directories
  end

  puts "dynamically_generate_templates_files" if $TRACE
  templates = dynamically_generate_templates_files

  puts "Creating new project folder '#{name}'\n\n"
  create_template_directories(@template_directories, target)

  templates.each do |src, dst|
    template("#{template_src}/#{src}", target.join(dst), config)
  end

  Dir.chdir(target) do
    if @configurator.always_perform_git_init || !inside_git_work_tree?
      `git init`
    end
    `git add .`
  end

  if @tconf[:bootstrap_command]
    puts "Executing bootstrap_command"
    cmd = safe_gsub_template_variables(@tconf[:bootstrap_command])
    puts cmd
    Dir.chdir(target) do
      `#{cmd}`
    end
  end

  puts "\nComplete."
end