Class: Testcloud::Generator::Commands::Gem

Inherits:
Base
  • Object
show all
Defined in:
lib/testcloud/generator/commands/gem.rb

Instance Attribute Summary

Attributes inherited from Base

#options, #parsed_options, #template_options, #thor

Instance Method Summary collapse

Methods inherited from Base

#abort_command, #initialize, #readline, #run

Constructor Details

This class inherits a constructor from Testcloud::Generator::Commands::Base

Instance Method Details

#confirmation_promptObject



8
9
10
11
12
13
14
15
# File 'lib/testcloud/generator/commands/gem.rb', line 8

def confirmation_prompt
  gemname = "testcloud-#{options['gemname']}"
  options['gemname'] = gemname
  $stdout.printf "Gem will be called: #{options['gemname']}\n"
  show_options
  $stdout.printf 'Is this correct? [y/n]: '
  abort_command if readline != 'y'
end

#execute_commandObject



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
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
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
# File 'lib/testcloud/generator/commands/gem.rb', line 17

def execute_command
  name = options['gemname']
  underscore_name = name.tr('-', '_')
  namespace_path  = name.tr('-', '/')
  constant_name   = name.split('-').map { |c| c[0..0].upcase + c[1..-1] }.join('::')
  constant_array  = constant_name.split('::')
  gem_email       = '[email protected]'
  gem_author      = 'testCloud Team'
  target          = File.join(Dir.pwd, name)

  @template_options = {
    'name' => name,
    'underscore_name' => underscore_name,
    'constant_name' => constant_name,
    'constant_array' => constant_array,
    'author' => gem_author,
    'email' => gem_email,
    'namespace_path' => namespace_path,
    'core' =>  options.fetch('core') { false },
    'rack' =>  options.fetch('rack') { false },
    'jobs' =>  options.fetch('jobs') { false }
  }

  # fixme: don't overwrite the options again :(
  options = @template_options

  templates = {
    'Gemfile.tt' => 'Gemfile',
    'gitignore.tt' => '.gitignore',
    'lib/newgem.rb.tt' => "lib/#{namespace_path}.rb",
    'lib/newgem_shortcut.rb.tt' => "lib/#{underscore_name}.rb",
    'lib/newgem/gem_version.rb.tt' => "lib/#{namespace_path}/gem_version.rb",
    'lib/newgem/version.rb.tt' => "lib/#{namespace_path}/version.rb",
    'lib/newgem/setup.rb.tt' => "lib/#{namespace_path}/setup.rb",
    'LICENSE.txt.tt' => 'LICENSE.txt',

    # testing
    'spec/spec_helper.rb.tt' => "spec/spec_helper.rb",
    'features/support/env.rb.tt' => 'features/support/env.rb',

    # rack
    'config.ru.tt' => 'config.ru',
    'newgem/lib/newgem/api.rb.tt' => "lib/#{namespace_path}/api.rb",
    'newgem/lib/newgem/api/application.rb.tt' => "lib/#{namespace_path}/api/application.rb",
    'newgem/lib/newgem/api/endpoint.rb.tt' => "lib/#{namespace_path}/api/endpoint.rb",

    # core
    'newgem/lib/newgem/params.rb.tt' => "lib/#{namespace_path}/params.rb",
    'newgem/lib/newgem/use_cases.rb.tt' => "lib/#{namespace_path}/use_cases.rb",
    'newgem/bin/console.tt' => "bin/console"
  }

  gemspec_dest = File.join(target, "#{name}.gemspec")

  # basic required files
  thor.template('newgem/Gemfile.tt', File.join(target, "Gemfile"), options)
  thor.template('newgem/LICENSE.txt', File.join(target, "LICENSE.txt"), options)
  thor.template('newgem/newgem.gemspec.tt', gemspec_dest, options)
  thor.template('newgem/lib/newgem.rb.tt', File.join(target, templates['lib/newgem.rb.tt']), options)
  thor.template('newgem/lib/newgem_shortcut.rb.tt', File.join(target, templates['lib/newgem_shortcut.rb.tt']), options)
  thor.template('newgem/lib/newgem/version.rb.tt', File.join(target, templates['lib/newgem/version.rb.tt']), options)
  thor.template('newgem/lib/newgem/gem_version.rb.tt', File.join(target, templates['lib/newgem/gem_version.rb.tt']), options)
  thor.template('newgem/lib/newgem/setup.rb.tt', File.join(target, templates['lib/newgem/setup.rb.tt']), options)

  # testing files
  thor.template('newgem/spec/spec_helper.rb.tt', File.join(target, templates['spec/spec_helper.rb.tt']), options)
  thor.template('newgem/features/support/env.rb.tt', File.join(target, templates['features/support/env.rb.tt']), options)

  # api/rack files
  if options['rack']
    thor.template('newgem/config.ru.tt', File.join(target, 'config.ru'), options)
    thor.template('newgem/lib/newgem/api.rb.tt', File.join(target, templates['newgem/lib/newgem/api.rb.tt']), options)
    thor.template('newgem/lib/newgem/api/application.rb.tt', File.join(target, templates['newgem/lib/newgem/api/application.rb.tt']), options)
    thor.template('newgem/lib/newgem/api/endpoint.rb.tt', File.join(target, templates['newgem/lib/newgem/api/endpoint.rb.tt']), options)
    ["lib/#{namespace_path}/api/resources"].each { |path| FileUtils.mkdir(File.join(target, path)) }
  end

  # core dependencies
  if options['core']
    thor.template('newgem/lib/newgem/params.rb.tt', File.join(target, templates['newgem/lib/newgem/params.rb.tt']), options)
    thor.template('newgem/lib/newgem/use_cases.rb.tt', File.join(target, templates['newgem/lib/newgem/use_cases.rb.tt']), options)
    ["lib/#{namespace_path}/params", "lib/#{namespace_path}/use_cases"].each { |path| FileUtils.mkdir(File.join(target, path)) }
  end

  # binary files
  ['cucumber', 'rspec', 'rake', 'puma', 'console'].each do |executable|
    executable_path = File.join(target, "bin/#{executable}")

    case executable
    when 'cucumber', 'rspec', 'rake'
      thor.template("newgem/bin/#{executable}.tt", executable_path, options)
      FileUtils.chmod('+x', executable_path)
    when 'console'
      if options['core']
        thor.template("newgem/bin/#{executable}.tt", executable_path, options)
        FileUtils.chmod('+x', executable_path)
      end
    when 'puma'
      if options['core']
        thor.template("newgem/bin/#{executable}.tt", executable_path, options)
        FileUtils.chmod('+x', executable_path)
      end
    end
  end

  # config files
  thor.template('newgem/.envrc', File.join(target, '.envrc'), options)
  thor.template('newgem/.rspec', File.join(target, '.rspec'), options)

  # All files are copied, let's initialize a new git repo
  Dir.chdir(target) do
    $stdout.printf "Initializing git repo in #{target}"
    `git init && git add .`
  end
end

#show_optionsObject



133
134
135
136
137
# File 'lib/testcloud/generator/commands/gem.rb', line 133

def show_options
  options.keys.select { |o| o != 'gemname' }.each do |key|
    $stdout.printf "* using #{key}\n" if options[key]
  end
end