-
Notifications
You must be signed in to change notification settings - Fork 21
/
newgem
262 lines (221 loc) · 7.59 KB
/
newgem
1
2
3
4
5
6
7
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
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
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
262
#!/usr/bin/env ruby
begin
require 'rubygems'
rescue LoadError
# no rubygems to load, so we fail silently
end
require 'fileutils'
require 'active_support'
require 'optparse'
require 'newgem'
templates = File.dirname(__FILE__) + '/../templates/'
OPTIONS = {
:import_path => nil,
:version => '0.0.1',
:full_name => nil,
:email => nil,
:bin_name => nil,
:test_framework => 'test::unit'
}
parser = OptionParser.new do |opts|
opts.banner = <<BANNER
Take any library or Rails plugin or command line application,
gemify it, and easily share it with the Ruby world.
Usage: #{File.basename($0)} [options] <name of gem>
Options are:
BANNER
opts.separator ""
opts.on("-b", "--bin-name=BIN_NAME[,BIN_NAME2]", String,
"Sets up executable scripts in the bin folder.",
"Default: none") { |OPTIONS[:bin_name]| }
opts.on("-i", "--import_path=PATH", String,
"Path where your files could be copied from.",
"Default: none") { |OPTIONS[:import_path]| }
opts.on("-n", "--full_name=PATH", String,
"Your name to be inserted into generated files.",
"Default: ~/.rubyforge/user-config.yml[user_name]") { |OPTIONS[:full_name]| }
opts.on("-e", "--email=PATH", String,
"Your email to be inserted into generated files.",
"Default: ~/.rubyforge/user-config.yml[email]") { |OPTIONS[:email]| }
opts.on("-t", "--test-with=TEST_FRAMEWORK", String,
"Your preferred testing framework: Options: test::unit (default), rspec.",
"Default: test::unit") { |OPTIONS[:test_framework]| }
opts.on("-v", "--version=YOUR_VERSION", String,
"Version of the gem you are creating.",
"Default: 0.0.1") { |OPTIONS[:version]| }
opts.on("-h", "--help",
"Show this help message.") { puts opts; exit }
opts.parse!(ARGV)
end
OPTIONS[:version] = OPTIONS[:version].to_s.split(/\./)
version_str = OPTIONS[:version].join('.')
author = OPTIONS[:full_name]
email = OPTIONS[:email]
unless author && email
rubyforge_config = Newgem::Rubyforge.new
author ||= rubyforge_config.full_name
email ||= rubyforge_config.email
end
test_framework = OPTIONS[:test_framework]
gem_name = project_name = ARGV[0]
parser.parse!(["-h"]) unless gem_name
module_name = project_name.classify
lib = project_name + '/lib'
lib_project_name = project_name + '/lib/' + project_name
version = project_name + "/lib/" + project_name + "/version.rb"
readme, setup = %w(README.txt setup.rb).
collect {|f| project_name + '/' + f}
template_files = %w(Rakefile History.txt License.txt Manifest.txt scripts/txt2html website/index.txt website/index.html website/template.rhtml)
copy_files = %w(website/javascripts/rounded_corners_lite.inc.js website/stylesheets/screen.css)
main = project_name + "/lib/" + project_name + ".rb"
examples = project_name + '/examples'
bin = project_name + '/bin'
folders = %w( scripts website website/javascripts website/stylesheets)
FileUtils.rm_rf project_name
puts "creating: " + project_name
Dir.mkdir project_name
puts "creating: " + readme
File.open(readme, 'w') do |file|
file << <<-eos
README for #{project_name}
===========#{'=' * project_name.length}
eos
end
puts "creating: " + lib
FileUtils.mkdir_p lib
folders.each do |folder|
new_folder = project_name + '/' + folder
puts "creating: " + new_folder
FileUtils.mkdir_p new_folder
end
if OPTIONS[:import_path]
require 'fileutils'
OPTIONS[:import_path].chomp! if OPTIONS[:import_path][-1] == 47 # /
FileUtils.cp_r OPTIONS[:import_path] + '/.', lib, :preserve => true
end
puts "creating: " + lib_project_name
FileUtils.mkdir_p lib_project_name
puts "creating: " + main
File.open(main, 'w') do |file|
file << <<-EOS
module #{module_name}
end
require '#{module_name}/version'
EOS
end
puts "creating: " + version
File.open(version, 'w') do |file|
file << <<-EOS
module #{module_name} #:nodoc:
module VERSION #:nodoc:
MAJOR = #{OPTIONS[:version][0] || '0'}
MINOR = #{OPTIONS[:version][1] || '0'}
TINY = #{OPTIONS[:version][2] || '0'}
STRING = [MAJOR, MINOR, TINY].join('.')
end
end
EOS
end
puts "creating: " + bin
Dir.mkdir bin
if OPTIONS[:bin_name]
bin_names_list = OPTIONS[:bin_name].split(',')
template = File.open(templates + 'app.rb','r') {|f| f.readlines.join}
bin_names_list.each do |bin_name|
puts "creating: " + bin + '/' + bin_name
File.open(bin + '/' + bin_name, 'w') do |file|
file << eval('"' + template.gsub(/"/, '\"') + '"')
end
end
bin_names = bin_names_list.join(' ')
else
bin_names = ''
bin_names_list = []
end
# Setup spec/test::unit environments
# Would like to refactor this...
if test_framework == 'rspec'
test = project_name + "/spec"
test_helper = test + "/spec_helper.rb"
test_template = 'spec.rb'
unit_test = test + "/" + project_name + "_spec.rb"
test_file_prefix = 'spec/NAME_spec'
spec_opts = 'spec.opts'
unit_test_template = <<-eos
require 'spec'
eos
requirements = <<-eos
begin
require 'spec/rake/spectask'
rescue LoadError
puts 'To use rspec for testing you must install rspec gem:'
puts '$ sudo gem install rspec'
exit
end
eos
test_task = <<-eos
desc "Run the specs under spec/models"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "spec/spec.opts"]
t.spec_files = FileList['spec/*_spec.rb']
end
desc "Default task is to run specs"
task :default => :spec
eos
else
test = project_name + "/test"
test_helper = test + "/test_helper.rb"
test_template = 'test.rb'
unit_test = test + "/test_" + project_name + ".rb"
test_file_prefix = 'test/test_NAME'
unit_test_template = <<-eos
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/#{project_name}'
eos
requirements = ''
test_task = ''
end
puts "creating: " + test
Dir.mkdir test
puts "creating: " + test_helper
File.open(test_helper, 'w') do |file|
file << unit_test_template
end
puts "creating: " + unit_test
template = File.open(templates + test_template,'r') {|f| f.readlines.join}
File.open(unit_test, 'w') do |file|
file << eval('"' + template.gsub(/"/, '\"') + '"')
end
if test_framework == 'rspec'
spec_opts_file = test + "/" + spec_opts
puts "creating: " + spec_opts_file
template = File.open(templates + spec_opts, 'r') { |f| f.readlines.join }
File.open(spec_opts_file, 'w') do |file|
file << eval('"' + template.gsub(/"/, '\""') + '"')
end
end
puts "creating: " + examples
Dir.mkdir examples
puts "creating: " + setup
template = File.open(templates + 'setup.rb','r') {|f| f.readlines.join}
File.open(setup, 'w') do |file|
file << template
end
bin_names_list = bin_names_list.collect {|name| 'bin/' + name }.join("\n")
bin_names_list += "\n" if bin_names_list.size > 0
test_names_list = ['helper.rb',"#{project_name}.rb"].sort.collect {|name| test_file_prefix.gsub('NAME', name) }.join("\n")
template_files.each do |template_file|
puts "creating: " + project_name + '/' + template_file
template = File.open(templates + template_file,'r') {|f| f.readlines.join}
File.open(project_name + '/' + template_file, 'w') do |file|
file << eval('"' + template.gsub(/"/, '\"') + '"')
end
end
copy_files.each do |file|
puts "copying: " + project_name + '/' + file
template = File.open(templates + file,'r') {|f| f.readlines.join}
File.open(project_name + '/' + file, 'w') do |file|
file << template
end
end
puts "NOW - update #{gem_name}/Rakefile with gem description, etc"