Class: NewGem

Inherits:
Object show all
Defined in:
lib/volt/cli/new_gem.rb

Overview

Creates a new “volt” gem, which can be used to easily repackage components.

Instance Method Summary collapse

Constructor Details

#initialize(thor, name, options) ⇒ NewGem

Returns a new instance of NewGem.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/volt/cli/new_gem.rb', line 8

def initialize(thor, name, options)
  @thor = thor
  @component_name = name.chomp('/')
  @name = 'volt-' + @component_name # remove trailing slash if present

  if gem_is_available?
    @thor.say("#{@name} is available!  Making gem files.", :green)
  else
    @thor.say("There is already a gem named #{@name}.  Please choose a different name.", :red)
    return
  end

  @options = options
  @namespaced_path = @name.tr('-', '/')
  @opts = gem_options
  @target = File.join(Dir.pwd, @name)

  copy_files
  copy_options
end

Instance Method Details

#copy_filesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/volt/cli/new_gem.rb', line 46

def copy_files
  @thor.directory('newgem/app/newgem', File.join("#{@target}", "app/#{@component_name}"), @opts)
  copy('newgem/Gemfile.tt', 'Gemfile')
  copy('newgem/Rakefile.tt', 'Rakefile')
  copy('newgem/README.md.tt', 'README.md')
  copy('newgem/gitignore.tt', '.gitignore')
  copy('newgem/newgem.gemspec.tt', "#{@name}.gemspec")
  copy('newgem/lib/newgem.rb.tt', "lib/#{@namespaced_path}.rb")
  copy('newgem/VERSION', 'VERSION')
  FileUtils.mkdir_p(File.join(@target, "lib/#{@namespaced_path}"))
end

#copy_optionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/volt/cli/new_gem.rb', line 58

def copy_options
  if @options[:bin]
    copy('newgem/bin/newgem.tt', "bin/#{@name}")
  end
  case @options[:test]
  when 'rspec'
    copy('newgem/rspec.tt', '.rspec')
    copy('newgem/spec/spec_helper.rb.tt', 'spec/spec_helper.rb')
    copy('newgem/spec/newgem_spec.rb.tt', "spec/#{@namespaced_path}_spec.rb")
  when 'minitest'
    copy('newgem/test/minitest_helper.rb.tt', 'test/minitest_helper.rb')
    copy('newgem/test/test_newgem.rb.tt', "test/test_#{@namespaced_path}.rb")
  end
  puts "Initializing git repo in #{@target}"
  Dir.chdir(@target) { `git init`; `git add .` }

  if @options[:edit]
    run("#{@options['edit']} \"#{gemspec_dest}\"")  # Open gemspec in editor
  end
end

#gem_is_available?Boolean

Check with the rubygems api to see if this gem name is available.

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/volt/cli/new_gem.rb', line 30

def gem_is_available?
  @thor.say("Check if #{@name} is available as a gem name.", :yellow)
  uri = URI.parse("https://rubygems.org/api/v1/gems/#{@name}.json")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  return response.code == '404'
rescue SocketError => e
  # rubygems is down, skip check
  return true
end