Class: GLI::Scaffold

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

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
# 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)
  end
end

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



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

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/ruby'
        file.puts '$: << File.expand_path(File.dirname(__FILE__) + \'/../lib\')'
        file.puts '$: << File.expand_path(File.dirname(__FILE__) + \'/../ext\')' if create_ext_dir
        file.puts <<EOS
require 'rubygems'
require 'gli'

include GLI

desc 'Describe some switch here'
switch [:s,:switch]

desc 'Describe some flag here'
default_value 'the default'
arg_name 'The name of the argument'
flag [:f,:flagname]
EOS
        first = true
        commands.each do |command|
          file.puts <<EOS

desc 'Describe #{command} here'
arg_name 'Describe arguments to #{command} here'
EOS
          if first
            file.puts <<EOS
command :#{command} do |c|
  c.desc 'Describe a switch to #{command}'
  c.switch :s

  c.desc 'Describe a flag to #{command}'
  c.default_value 'default'
  c.flag :s
  c.action do |global_options,options,args|

# Your command logic here
 
# If you have any errors, just raise them
# raise "that command made no sense"
  end
end
EOS
          else
            file.puts <<EOS
command :#{command} do |c|
  c.action do |global_options,options,args|
  end
end
EOS
          end
          first = false
        end
        file.puts <<EOS

pre do |global,command,options,args|
  # Pre logic here
  # Return true to proceed; false to abourt and not call the
  # chosen command
  true
end

post do |global,command,options,args|
  # Post logic here
end

on_error do |exception|
  # Error logic here
  # return false to skip default error handling
  true
end

GLI.run(ARGV)
EOS
        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



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

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 <<EOS
spec = Gem::Specification.new do |s| 
  s.name = '#{project_name}'
  s.version = '0.0.01'
  s.author = 'Your Name Here'
  s.email = '[email protected]'
  s.homepage = 'http://your.website.com'
  s.platform = Gem::Platform::RUBY
  s.summary = 'A description of your project'
# Add your other files here if you make them
  s.files = %w(
bin/#{project_name}
  )
  s.require_paths << 'lib'
  s.has_rdoc = true
  s.extra_rdoc_files = ['README.rdoc','#{project_name}.rdoc']
  s.rdoc_options << '--title' << 'Git Like Interface' << '--main' << 'README.rdoc' << '-ri'
  s.bindir = 'bin'
  s.executables << '#{project_name}'
end
EOS
  end
end

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



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

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 <<EOS
require 'rake/clean'
require 'rubygems'
require 'rake/gempackagetask'
require 'rake/rdoctask'

Rake::RDocTask.new do |rd|
  rd.main = "README.rdoc"
  rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
  rd.title = 'Your application title'
end

spec = eval(File.read('#{project_name}.gemspec'))

Rake::GemPackageTask.new(spec) do |pkg|
end

EOS
    if create_test_dir
      file.puts <<EOS
require 'rake/testtask'
Rake::TestTask.new do |t|
  t.libs << "test"
  t.test_files = FileList['test/tc_*.rb']
end

task :default => :test
EOS
      File.open("#{root_dir}/#{project_name}/test/tc_nothing.rb",'w') do |test_file|
        test_file.puts <<EOS
require 'test/unit'
require 'test/unit/ui/console/testrunner'

class TC_testNothing < Test::Unit::TestCase

  def setup
  end

  def teardown
  end

  def test_the_truth
assert true
  end
end
EOS
      end
    else
      file.puts "task :default => :package\n"
    end
  end
end

.mk_readme(root_dir, dry_run, project_name) ⇒ Object



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

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
  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
end

.mkdirs(dirs, force, dry_run) ⇒ Object



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

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