-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.rb
executable file
·390 lines (316 loc) · 9.78 KB
/
install.rb
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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env ruby
require 'fileutils'
def camelize(lower_case_and_underscored_word)
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
def append_to_ignore(text)
File.open("#{@gem_name}/.gitignore", 'a') { |f| f.puts(text) }
end
# RUBY_ENGINE doesn't exist in 1.8
def ruby_engine
defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"
end
print "\n\nPlease enter the name of your gem: "
@gem_name = gets.chomp
directory_name = @gem_name.downcase.gsub(" ", "_")
if @gem_name !~ /^\s*$/
module_name = camelize(@gem_name).gsub(" ", "::")
else
puts "I need a gem name that is not blank... c'mon..."
exit(1)
end
print "As well as your email: "
author_email = gets.chomp
unless author_email !~ /^\s*$/
puts "Need the email for rubygem deploy, don't worry, I won't spam you (others might though)..."
exit(1)
end
print "And your name please: "
author_name = gets.chomp
unless author_name !~ /^\s*$/
puts "You aren't going to tell me who you are? Sorry to offend you! But I sorta need it for rubygems... "
exit(1)
end
puts "\n\nThe git write URL is the git location where you will be storing your gem,"
puts "this needs read and write access, so be sure to enter the right one, for"
puts "example:"
puts " git@github.com:reInteractive/nugget.git"
puts "\nPlease enter the git write URL for your gem:"
giturl = gets.chomp
giturl =~ /^git@github.com:(\w+\/\w+).git$/
if $1
git_path = $1
else
puts "Does not look like a git read and write URL, sorry, I'm going to die now."
exit(1)
end
puts "\n\nThank you, building the gem structure now\n"
##############################################################
## Directory Structure
puts "Making directory structure..."
FileUtils.mkdir_p("#{@gem_name}/lib/#{@gem_name}")
FileUtils.mkdir_p("#{@gem_name}/spec/#{@gem_name}")
##############################################################
## Library Files
puts "Making top level library files..."
File.open("#{directory_name}/README.md", 'w') do |f|
file_contents=<<ENDFILE
#{@gem_name} readme
=========================
Decription
--------------------------
Usage
--------------------------
ENDFILE
f.write(file_contents)
end
File.open("#{directory_name}/CHANGELOG", 'w') do |f|
file_contents=<<ENDFILE
== #{Time.now.to_s} #{author_name} <#{author_email}>
* Created #{@gem_name}
ENDFILE
f.write(file_contents)
end
File.open("#{directory_name}/lib/#{directory_name}.rb", 'w') do |f|
file_contents=<<ENDFILE
# encoding: utf-8
module #{module_name}
require '#{directory_name}/version'
require '#{directory_name}/base'
end
ENDFILE
f.write(file_contents)
end
File.open("#{directory_name}/lib/#{directory_name}/base.rb", 'w') do |f|
file_contents=<<ENDFILE
# encoding: utf-8
module #{module_name}
class Base
# Put your code here
end
end
ENDFILE
f.write(file_contents)
end
##############################################################
## Version Files
puts "Making version files..."
File.open("#{directory_name}/lib/VERSION", 'w') do |f|
file_contents=<<ENDFILE
major:0
minor:0
patch:0
build:
ENDFILE
f.write(file_contents)
end
File.open("#{directory_name}/lib/#{directory_name}/version.rb", 'w') do |f|
file_contents=<<ENDFILE
# encoding: utf-8
module #{module_name}
module VERSION
version = {}
File.read(File.join(File.dirname(__FILE__), '../', 'VERSION')).each_line do |line|
type, value = line.chomp.split(":")
next if type =~ /^\s+$/ || value =~ /^\s+$/
version[type] = value
end
MAJOR = version['major']
MINOR = version['minor']
PATCH = version['patch']
BUILD = version['build']
STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
def self.version
STRING
end
end
end
ENDFILE
f.write(file_contents)
end
##############################################################
## Gemspec File
puts "Making #{directory_name}gemspec..."
File.open("#{directory_name}/#{directory_name}.gemspec", 'w') do |f|
file_contents=<<ENDFILE
require File.dirname(__FILE__) + "/lib/#{directory_name}/version"
Gem::Specification.new do |s|
s.name = "#{module_name}"
s.version = #{module_name}::VERSION::STRING
s.author = ["#{author_name}"]
s.email = ["#{author_email}"]
s.homepage = "http://github.com/#{git_path}"
s.license = 'MIT'
s.description = "" # Please fill in a short description
s.summary = "" # Please fill in a longer summary
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README.md", "CHANGELOG.md"]
# s.add_dependency('other_gem', ">= version")
s.add_development_dependency("rspec", ">= 2.5.1")
s.add_development_dependency("ZenTest", ">= 4.5.0")
s.add_development_dependency("rake", ">= 0.8.7")
s.add_development_dependency("bundler", ">= 1.0.12")
s.require_path = 'lib'
s.files = %w(README.md Rakefile) + Dir.glob("lib/**/*")
end
ENDFILE
f.write(file_contents)
end
##############################################################
## RakeFile
puts "Making Rakefile..."
File.open("#{directory_name}/Rakefile", 'w') do |f|
file_contents=<<ENDFILE
begin
require "rubygems"
require "bundler"
rescue LoadError
raise "Could not load the bundler gem. Install it with `gem install bundler`."
end
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("1.0.0")
raise RuntimeError, "Your bundler version is too old for Mail" +
"Run `gem install bundler` to upgrade."
end
begin
# Set up load paths for all bundled gems
ENV["BUNDLE_GEMFILE"] = File.expand_path("../Gemfile", __FILE__)
Bundler.setup
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." +
"Did you run `bundle install`?"
end
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
require "#{directory_name}/version"
task :build do
system "gem build #{directory_name}.gemspec"
end
task :release => :build do
system "gem push #{directory_name}-\#{#{module_name}::VERSION::STRING}"
end
ENDFILE
f.write(file_contents)
end
case
when defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
@ruby_type = 'rbx'
when RUBY_PLATFORM == 'java'
@ruby_type = 'jruby'
when RUBY_VERSION < '1.9'
@ruby_type = 'ruby'
else
# Skip it
end
##############################################################
## Gemfile
puts "Making Gemfile..."
File.open("#{directory_name}/Gemfile", 'w') do |f|
file_contents=<<ENDFILE
source :rubygems
# Add your run time dependencies here
group :test do
gem "ZenTest"
gem "rake"
gem "bundler"
gem "rspec"
gem "diff-lcs"
case
when ruby_engine == 'rbx'
# Skip it
when RUBY_PLATFORM == 'java'
# Skip it
when RUBY_VERSION < '1.9'
gem "ruby-debug"
else
# Skip it
end
end
ENDFILE
f.write(file_contents)
end
##############################################################
## RVM configuration
puts "Making .rvmrc..."
File.open("#{@gem_name}/.rvmrc", 'w') do |f|
f.puts "rvm --create use #{ruby_engine}-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}@#{@gem_name} > /dev/null"
end
##############################################################
## Git configuration
puts "Making .gitconfig..."
File.open("#{@gem_name}/.gitconfig", 'w') do |f|
file_contents=<<ENDFILE
[user]
email = #{author_email}
name = #{author_name}
ENDFILE
f.write(file_contents)
append_to_ignore('.gitconfig')
end
##############################################################
## Spec Folder
puts "Making Specs..."
File.open("#{directory_name}/spec/spec_helper.rb", 'w') do |f|
file_contents=<<ENDFILE
# encoding: utf-8
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
unless defined?(SPEC_ROOT)
SPEC_ROOT = File.join(File.dirname(__FILE__))
end
require '#{directory_name}'
ENDFILE
f.write(file_contents)
end
File.open("#{directory_name}/spec/#{directory_name}/base_spec.rb", 'w') do |f|
file_contents=<<ENDFILE
# encoding: utf-8
require 'spec_helper'
describe #{module_name}::Base do
it "does something"
end
ENDFILE
f.write(file_contents)
end
##############################################################
## Adding License
puts "Making Specs..."
File.open("#{directory_name}/LICENSE", 'w') do |f|
file_contents=<<ENDFILE
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ENDFILE
f.write(file_contents)
end
##############################################################
## Adding Git Remote
puts "Adding your git repository as origin..."
`cd #{directory_name} && git init`
`cd #{directory_name} && git remote add origin #{giturl}`
##############################################################
## Wrapping up
puts "\n\nCreation of gem #{@gem_name} is complete"
puts "\nPlease change into your gem directory in #{directory_name}, edit the README.md and run:"
puts "$ git add README.md"
puts '$ git commit README.md -m "First commit'
puts '$ git push origin master'
puts "\nThen to start testing:"
puts '$ bundle install'
puts '$ rake'
puts "\nEnjoy!\n"