Class: GLI::Commands::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/gli/commands/scaffold.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.create_scaffold(root_dir, create_test_dir, create_ext_dir, project_name, commands, force = false, dry_run = false, create_rvmrc = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gli/commands/scaffold.rb', line 8

def self.create_scaffold(root_dir,
                         create_test_dir,
                         create_ext_dir,
                         project_name,
                         commands,
                         force=false,
                         dry_run=false,
                         create_rvmrc=false)
  dirs = [File.join(root_dir,project_name,'lib')]
  dirs << File.join(root_dir,project_name,'bin')
  dirs << File.join(root_dir,project_name,'test') if create_test_dir
  dirs << File.join(root_dir,project_name,'ext') if create_ext_dir

  if mkdirs(dirs,force,dry_run)
    mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
    mk_readme(root_dir,dry_run,project_name)
    mk_gemspec(root_dir,dry_run,project_name)
    mk_rakefile(root_dir,dry_run,project_name,create_test_dir)
    mk_lib_files(root_dir,dry_run,project_name)
    if create_rvmrc
      rvmrc = File.join(root_dir,project_name,".rvmrc")
      File.open(rvmrc,'w') do |file|
        file.puts "rvm use #{ENV['rvm_ruby_string']}@#{project_name} --create"
      end
      puts "Created #{rvmrc}"
    end
    init_git(root_dir, project_name)
  end
end

.init_git(root_dir, project_name) ⇒ Object



373
374
375
376
377
378
379
# File 'lib/gli/commands/scaffold.rb', line 373

def self.init_git(root_dir, project_name)
  project_dir = "#{root_dir}/#{project_name}"

  unless system("git", "init", "--quiet", project_dir)
    puts "There was a problem initializing Git. Your gemspec may not work until you have done a successful `git init`"
  end
end

.mk_binfile(root_dir, create_ext_dir, force, dry_run, project_name, commands) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/gli/commands/scaffold.rb', line 263

def self.mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
  bin_file = File.join(root_dir,project_name,'bin',project_name)
  if !File.exist?(bin_file) || force
    if !dry_run
      File.open(bin_file,'w') do |file|
        file.chmod(0755)
        file.puts '#!/usr/bin/env ruby'
        file.puts "require 'gli'\nbegin # XXX: Remove this begin/rescue before distributing your app\nrequire '\#{project_name}'\nrescue LoadError\n  STDERR.puts \"In development, you need to use `bundle exec bin/\#{project_name}` to run your app\"\n  STDERR.puts \"At install-time, RubyGems will make sure lib, etc. are in the load path\"\n  STDERR.puts \"Feel free to remove this message from bin/\#{project_name} now\"\n  exit 64\nend\n\nclass App\n  extend GLI::App\n\n  program_desc 'Describe your application here'\n\n  version \#{project_name_as_module_name(project_name)}::VERSION\n\n  subcommand_option_handling :normal\n  arguments :strict\n\n  desc 'Describe some switch here'\n  switch [:s,:switch]\n\n  desc 'Describe some flag here'\n  default_value 'the default'\n  arg_name 'The name of the argument'\n  flag [:f,:flagname]\n"
        first = true
        commands.each do |command|
          file.puts "\n  desc 'Describe \#{command} here'\n  arg_name 'Describe arguments to \#{command} here'\n"
          if first
            file.puts "  command :\#{command} do |c|\nc.desc 'Describe a switch to \#{command}'\nc.switch :s\n\nc.desc 'Describe a flag to \#{command}'\nc.default_value 'default'\nc.flag :f\nc.action do |global_options,options,args|\n\n  # Your command logic here\n\n  # If you have any errors, just raise them\n  # raise \"that command made no sense\"\n\n  puts \"\#{command} command ran\"\nend\n  end\n"
          else
            file.puts "  command :\#{command} do |c|\nc.action do |global_options,options,args|\n  puts \"\#{command} command ran\"\nend\n  end\n"
          end
          first = false
        end
        file.puts "\n  pre do |global,command,options,args|\n# Pre logic here\n# Return true to proceed; false to abort and not call the\n# chosen command\n# Use skips_pre before a command to skip this block\n# on that command only\ntrue\n  end\n\n  post do |global,command,options,args|\n# Post logic here\n# Use skips_post before a command to skip this\n# block on that command only\n  end\n\n  on_error do |exception|\n# Error logic here\n# return false to skip default error handling\ntrue\n  end\nend\n\nexit App.run(ARGV)\n"
        puts "Created #{bin_file}"
      end
    end
  else
    puts bin_file + " exists; use --force to override"
    return false
  end
  true
end

.mk_gemspec(root_dir, dry_run, project_name) ⇒ Object



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
# File 'lib/gli/commands/scaffold.rb', line 53

def self.mk_gemspec(root_dir,dry_run,project_name)
  return if dry_run
  File.open("#{root_dir}/#{project_name}/#{project_name}.gemspec",'w') do |file|
    file.puts "# Ensure we require the local version and not one we might have installed already\nrequire File.join([File.dirname(__FILE__),'lib','\#{project_name}','version.rb'])\nspec = Gem::Specification.new do |s|\n  s.name = '\#{project_name}'\n  s.version = \#{project_name_as_module_name(project_name)}::VERSION\n  s.author = 'Your Name Here'\n  s.email = '[email protected]'\n  s.homepage = 'http://your.website.com'\n  s.platform = Gem::Platform::RUBY\n  s.summary = 'A description of your project'\n  s.files = `git ls-files`.split(\"\\n\")\n  s.require_paths << 'lib'\n  s.extra_rdoc_files = ['README.rdoc','\#{project_name}.rdoc']\n  s.rdoc_options << '--title' << '\#{project_name}' << '--main' << 'README.rdoc' << '-ri'\n  s.bindir = 'bin'\n  s.executables << '\#{project_name}'\n  s.add_development_dependency('rake')\n  s.add_development_dependency('rdoc')\n  s.add_development_dependency('aruba')\n  s.add_runtime_dependency('gli','\#{GLI::VERSION}')\nend\n"
  end
  puts "Created #{root_dir}/#{project_name}/#{project_name}.gemspec"
end

.mk_lib_files(root_dir, dry_run, project_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gli/commands/scaffold.rb', line 87

def self.mk_lib_files(root_dir,dry_run,project_name)
  return if dry_run
  FileUtils.mkdir("#{root_dir}/#{project_name}/lib/#{project_name}")
  File.open("#{root_dir}/#{project_name}/lib/#{project_name}/version.rb",'w') do |file|
    file.puts "module \#{project_name_as_module_name(project_name)}\n  VERSION = '0.0.1'\nend\n"
  end
  puts "Created #{root_dir}/#{project_name}/lib/#{project_name}/version.rb"
  File.open("#{root_dir}/#{project_name}/lib/#{project_name}.rb",'w') do |file|
    file.puts "require '\#{project_name}/version.rb'\n\n# Add requires for other files you add to your project here, so\n# you just need to require this one file in your bin file\n"
  end
  puts "Created #{root_dir}/#{project_name}/lib/#{project_name}.rb"
end

.mk_rakefile(root_dir, dry_run, project_name, create_test_dir) ⇒ Object



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/gli/commands/scaffold.rb', line 108

def self.mk_rakefile(root_dir,dry_run,project_name,create_test_dir)
  return if dry_run
  File.open("#{root_dir}/#{project_name}/Rakefile",'w') do |file|
    file.puts "require 'rake/clean'\nrequire 'rubygems'\nrequire 'rubygems/package_task'\nrequire 'rdoc/task'\n"
    if create_test_dir
      file.puts "require 'cucumber'\nrequire 'cucumber/rake/task'\n"
    end
    file.puts "Rake::RDocTask.new do |rd|\n  rd.main = \"README.rdoc\"\n  rd.rdoc_files.include(\"README.rdoc\",\"lib/**/*.rb\",\"bin/**/*\")\n  rd.title = 'Your application title'\nend\n\nspec = eval(File.read('\#{project_name}.gemspec'))\n\nGem::PackageTask.new(spec) do |pkg|\nend\n"
    if create_test_dir
      file.puts "CUKE_RESULTS = 'results.html'\nCLEAN << CUKE_RESULTS\ndesc 'Run features'\nCucumber::Rake::Task.new(:features) do |t|\n  opts = \"features --format html -o \\\#{CUKE_RESULTS} --format progress -x\"\n  opts += \" --tags \\\#{ENV['TAGS']}\" if ENV['TAGS']\n  t.cucumber_opts =  opts\n  t.fork = false\nend\n\ndesc 'Run features tagged as work-in-progress (@wip)'\nCucumber::Rake::Task.new('features:wip') do |t|\n  tag_opts = ' --tags ~@pending'\n  tag_opts = ' --tags @wip'\n  t.cucumber_opts = \"features --format html -o \\\#{CUKE_RESULTS} --format pretty -x -s\\\#{tag_opts}\"\n  t.fork = false\nend\n\ntask :cucumber => :features\ntask 'cucumber:wip' => 'features:wip'\ntask :wip => 'features:wip'\n"
    end
    if create_test_dir
      file.puts "require 'rake/testtask'\nRake::TestTask.new do |t|\n  t.libs << \"test\"\n  t.test_files = FileList['test/*_test.rb']\nend\n\ntask :default => [:test,:features]\n"
      File.open("#{root_dir}/#{project_name}/test/default_test.rb",'w') do |test_file|
        test_file.puts "require 'test_helper'\n\nclass DefaultTest < Test::Unit::TestCase\n\n  def setup\n  end\n\n  def teardown\n  end\n\n  def test_the_truth\nassert true\n  end\nend\n"
      end
      puts "Created #{root_dir}/#{project_name}/test/default_test.rb"
      File.open("#{root_dir}/#{project_name}/test/test_helper.rb",'w') do |test_file|
        test_file.puts "require 'test/unit'\n\n# Add test libraries you want to use here, e.g. mocha\n\nclass Test::Unit::TestCase\n\n  # Add global extensions to the test case class here\n\nend\n"
      end
      puts "Created #{root_dir}/#{project_name}/test/test_helper.rb"
    else
      file.puts "task :default => :package\n"
    end
  end
  puts "Created #{root_dir}/#{project_name}/Rakefile"
  File.open("#{root_dir}/#{project_name}/Gemfile",'w') do |bundler_file|
    bundler_file.puts "source 'https://rubygems.org'"
    bundler_file.puts "gemspec"
  end
  puts "Created #{root_dir}/#{project_name}/Gemfile"
  if create_test_dir
    features_dir = File.join(root_dir,project_name,'features')
    FileUtils.mkdir features_dir
    FileUtils.mkdir File.join(features_dir,"step_definitions")
    FileUtils.mkdir File.join(features_dir,"support")
    File.open(File.join(features_dir,"#{project_name}.feature"),'w') do |file|
      file.puts "Feature: My bootstrapped app kinda works\n  In order to get going on coding my awesome app\n  I want to have aruba and cucumber setup\n  So I don't have to do it myself\n\n  Scenario: App just runs\nWhen I get help for \"\#{project_name}\"\nThen the exit status should be 0\n"
    end
    File.open(File.join(features_dir,"step_definitions","#{project_name}_steps.rb"),'w') do |file|
      file.puts "When /^I get help for \"([^\"]*)\"$/ do |app_name|\n  @app_name = app_name\n  step %(I run `\\\#{app_name} help`)\nend\n\n# Add more step definitions here\n"
    end
    File.open(File.join(features_dir,"support","env.rb"),'w') do |file|
      file.puts "require 'aruba/cucumber'\n\nENV['PATH'] = \"\\\#{File.expand_path(File.dirname(__FILE__) + '/../../bin')}\\\#{File::PATH_SEPARATOR}\\\#{ENV['PATH']}\"\nLIB_DIR = File.join(File.expand_path(File.dirname(__FILE__)),'..','..','lib')\n\nBefore do\n  # Using \"announce\" causes massive warnings on 1.9.2\n  @puts = true\n  @original_rubylib = ENV['RUBYLIB']\n  ENV['RUBYLIB'] = LIB_DIR + File::PATH_SEPARATOR + ENV['RUBYLIB'].to_s\nend\n\nAfter do\n  ENV['RUBYLIB'] = @original_rubylib\nend\n"
    end
    puts "Created #{features_dir}"
  end
end

.mk_readme(root_dir, dry_run, project_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gli/commands/scaffold.rb', line 38

def self.mk_readme(root_dir,dry_run,project_name)
  return if dry_run
  File.open("#{root_dir}/#{project_name}/README.rdoc",'w') do |file|
    file << "= #{project_name}\n\n"
    file << "Describe your project here\n\n"
    file << ":include:#{project_name}.rdoc\n\n"
  end
  puts "Created #{root_dir}/#{project_name}/README.rdoc"
  File.open("#{root_dir}/#{project_name}/#{project_name}.rdoc",'w') do |file|
    file << "= #{project_name}\n\n"
    file << "Generate this with\n    #{project_name} _doc\nAfter you have described your command line interface"
  end
  puts "Created #{root_dir}/#{project_name}/#{project_name}.rdoc"
end

.mkdirs(dirs, force, dry_run) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/gli/commands/scaffold.rb', line 381

def self.mkdirs(dirs,force,dry_run)
  exists = false
  if !force
    dirs.each do |dir|
      if File.exist? dir
        raise "#{dir} exists; use --force to override"
        exists = true
      end
    end
  end
  if !exists
    dirs.each do |dir|
      puts "Creating dir #{dir}..."
      if dry_run
        puts "dry-run; #{dir} not created"
      else
        FileUtils.mkdir_p dir
      end
    end
  else
    puts "Exiting..."
    return false
  end
  true
end

.project_name_as_module_name(project_name) ⇒ Object



83
84
85
# File 'lib/gli/commands/scaffold.rb', line 83

def self.project_name_as_module_name(project_name)
  project_name.split(/[_-]/).map { |part| part[0..0].upcase + part[1..-1] }.join('')
end