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.



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

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.



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

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#build_interpolation_configObject



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/foobar_templates/cli/template_generator.rb', line 89

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 = time_it("scan_template_for_required_domains") do
    scan_template_for_required_domains
  end
  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



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

def config
  @config ||= time_it("build_interpolation_config") { build_interpolation_config }
end

#runObject



29
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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/foobar_templates/cli/template_generator.rb', line 29

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

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

    puts "run_name_validation" if $TRACE
    time_it("run_name_validation") { run_name_validation }

    template_src = time_it("match_template_src") { match_template_src }

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

    puts "dynamically_generate_templates_files" if $TRACE
    templates = time_it("dynamically_generate_templates_files") do
      dynamically_generate_templates_files
    end

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

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

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

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

    puts "\nComplete."
  end
end