Class: Gem::Commands::InitCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/init_command.rb

Instance Method Summary collapse

Constructor Details

#initializeInitCommand

Returns a new instance of InitCommand.



5
6
7
8
9
10
11
12
# File 'lib/rubygems/commands/init_command.rb', line 5

def initialize
  super 'init', 'Create the barebones of a rubygem'
  
  add_option('-s', '--skip-check', 'Skip check to see if gem already exists') do |value, options|
    options[:skip_check] = true
  end
        
end

Instance Method Details

#argumentsObject

:nodoc:



18
19
20
# File 'lib/rubygems/commands/init_command.rb', line 18

def arguments # :nodoc:
  "GEMNAME          name of your new rubygem"
end

#create_gemspec(gem_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubygems/commands/init_command.rb', line 46

def create_gemspec(gem_name)
  File.open("#{gem_name}.gemspec", 'w+') do |f|
    f.puts <<-GEMSPEC
Gem::Specification.new do |s|
s.name        = "#{gem_name}"
s.version     = '0.1.0'
s.authors     = ["YOURNAME"]
s.email       = "TODO YOUREMAIL"
s.homepage    = "TODO HOMEPAGE"
s.summary     = "TODO SUMMARY"
s.description = "TODO DESCRIPTION"
s.required_rubygems_version = ">= 1.3.6"
s.files = ["lib/#{gem_name}.rb"]
# s.add_dependency 'some-gem'
# s.extra_rdoc_files = ['README.md', 'LICENSE']
# s.license = 'MIT'
end
    GEMSPEC
  end
end

#create_rakefileObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubygems/commands/init_command.rb', line 77

def create_rakefile
  Dir.mkdir("test")
  File.open("Rakefile", 'w+') do |f|
    f.puts <<-RAKEFILE
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << 'test'
end

desc "Run tests"
task :default => :test
    RAKEFILE
  end
end

#create_ruby_file(gem_name) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/rubygems/commands/init_command.rb', line 67

def create_ruby_file(gem_name)
  Dir.mkdir("lib")
  File.open("lib/#{gem_name}.rb", 'w+') do |f|
    f.puts <<-FILE
class #{gem_name.capitalize}
end
    FILE
  end
end

#descriptionObject

:nodoc:



14
15
16
# File 'lib/rubygems/commands/init_command.rb', line 14

def description # :nodoc:
  'Rubygems plugin to assist in creating a barebones gem'
end

#executeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubygems/commands/init_command.rb', line 26

def execute
  gem_name = get_new_gem_name
  
  gem_exists = options[:skip_check].nil? ? gem_exists?(gem_name) : false

  if gem_exists
    say "The #{gem_name} gem already exists on rubygems.org!\n\nChoose a new name or use the -s option to skip the check"
  else
    create_gemspec(gem_name)
    create_ruby_file(gem_name)
    create_rakefile
  end
end

#gem_exists?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/rubygems/commands/init_command.rb', line 40

def gem_exists?(gem_name)
  response = RestClient.get("http://rubygems.org/api/v1/gems/#{gem_name}.json") do |response, request, result|
    return response.code != 404
  end
end

#get_new_gem_nameObject

Taken from RubyGems get_one_gem_name



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rubygems/commands/init_command.rb', line 94

def get_new_gem_name
  args = options[:args]
  if args.nil? or args.empty?
    fail Gem::CommandLineError,
      "Please specify a gem name on the command line (e.g. gem init GEMNAME)"
  end
  if args.size > 1
    fail Gem::CommandLineError,
      "Too many gem names (#{args.join(', ')}); please specify only one"
  end
  args.first
end

#usageObject

:nodoc:



22
23
24
# File 'lib/rubygems/commands/init_command.rb', line 22

def usage # :nodoc:
  "#{program_name} GEMNAME"
end