Class: GLI::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/support/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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/support/scaffold.rb', line 7

def self.create_scaffold(root_dir,create_test_dir,create_ext_dir,project_name,commands,force=false,dry_run=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_version(root_dir,dry_run,project_name)
  end
end

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



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
# File 'lib/support/scaffold.rb', line 146

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 "# 1.9 adds realpath to resolve symlinks; 1.8 doesn't\n# have this method, so we add it so we get resolved symlinks\n# and compatibility\nunless File.respond_to? :realpath\n  class File #:nodoc:\ndef self.realpath path\n  return realpath(File.readlink(path)) if symlink?(path)\n  path\nend\n  end\nend\n"
        file.puts '$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + \'/../lib\')'
        file.puts '$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + \'/../ext\')' if create_ext_dir
        file.puts "require 'rubygems'\nrequire 'gli'\nrequire '\#{project_name}_version'\n\ninclude GLI\n\nprogram_desc 'Describe your application here'\n\nversion \#{project_name_as_module_name(project_name)}::VERSION\n\ndesc 'Describe some switch here'\nswitch [:s,:switch]\n\ndesc 'Describe some flag here'\ndefault_value 'the default'\narg_name 'The name of the argument'\nflag [:f,:flagname]\n"
        first = true
        commands.each do |command|
          file.puts "\ndesc 'Describe \#{command} here'\narg_name 'Describe arguments to \#{command} here'\n"
          if first
            file.puts "command :\#{command} do |c|\n  c.desc 'Describe a switch to \#{command}'\n  c.switch :s\n\n  c.desc 'Describe a flag to \#{command}'\n  c.default_value 'default'\n  c.flag :f\n  c.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  end\nend\n"
          else
            file.puts "command :\#{command} do |c|\n  c.action do |global_options,options,args|\n  end\nend\n"
          end
          first = false
        end
        file.puts "\npre do |global,command,options,args|\n  # Pre logic here\n  # Return true to proceed; false to abourt and not call the\n  # chosen command\n  # Use skips_pre before a command to skip this block\n  # on that command only\n  true\nend\n\npost 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\nend\n\non_error do |exception|\n  # Error logic here\n  # return false to skip default error handling\n  true\nend\n\nexit GLI.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



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
# File 'lib/support/scaffold.rb', line 37

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# Add your other files here if you make them\n  s.files = %w(\nbin/\#{project_name}\n  )\n  s.require_paths << 'lib'\n  s.has_rdoc = true\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')\nend\n"
  end
  puts "Created #{root_dir}/#{project_name}/#{project_name}.gemspec"
end

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



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
138
139
140
141
142
143
144
# File 'lib/support/scaffold.rb', line 84

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 'rake/gempackagetask'\nrequire 'rake/rdoctask'\n\nRake::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\nRake::GemPackageTask.new(spec) do |pkg|\nend\n\n"
    if create_test_dir
      file.puts "require 'rake/testtask'\nRake::TestTask.new do |t|\n  t.libs << \"test\"\n  t.test_files = FileList['test/tc_*.rb']\nend\n\ntask :default => :test\n"
      File.open("#{root_dir}/#{project_name}/test/tc_nothing.rb",'w') do |test_file|
        test_file.puts "require 'test/unit'\n\nclass TC_testNothing < 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/tc_nothing.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 :rubygems"
    bundler_file.puts "gemspec"
  end
  puts "Created #{root_dir}/#{project_name}/Gemfile"
end

.mk_readme(root_dir, dry_run, project_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/support/scaffold.rb', line 22

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} rdoc\nAfter you have described your command line interface"
  end
  puts "Created #{root_dir}/#{project_name}/#{project_name}.rdoc"
end

.mk_version(root_dir, dry_run, project_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/support/scaffold.rb', line 73

def self.mk_version(root_dir,dry_run,project_name)
  return if dry_run
  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"
end

.mkdirs(dirs, force, dry_run) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/support/scaffold.rb', line 257

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



69
70
71
# File 'lib/support/scaffold.rb', line 69

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