Class: Gem::Src

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(installer) ⇒ Src

Returns a new instance of Src.



8
9
10
# File 'lib/rubygems_plugin.rb', line 8

def initialize(installer)
  @installer, @tested_repositories = installer, []
end

Instance Attribute Details

#installer ⇒ Object (readonly)

Returns the value of attribute installer.



6
7
8
# File 'lib/rubygems_plugin.rb', line 6

def installer
  @installer
end

Instance Method Details

#api ⇒ Object



49
50
51
52
53
54
# File 'lib/rubygems_plugin.rb', line 49

def api
  require 'open-uri'
  @api ||= open("http://rubygems.org/api/v1/gems/#{installer.spec.name}.yaml", &:read)
rescue OpenURI::HTTPError
  ""
end

#clone_dir ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/rubygems_plugin.rb', line 12

def clone_dir
  @clone_dir ||= if ENV['GEMSRC_CLONE_ROOT']
    File.expand_path installer.spec.name, ENV['GEMSRC_CLONE_ROOT']
  elsif Gem.configuration[:gemsrc_clone_root]
    File.expand_path installer.spec.name, Gem.configuration[:gemsrc_clone_root]
  else
    gem_dir = installer.respond_to?(:gem_dir) ? installer.gem_dir : File.expand_path(File.join(installer.gem_home, 'gems', installer.spec.full_name))
    File.join gem_dir, 'src'
  end
end

#ghq_available? ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rubygems_plugin.rb', line 81

def ghq_available?
  system('which', 'ghq')
end

#git?(url) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rubygems_plugin.rb', line 37

def git?(url)
  !`git ls-remote #{url} 2> /dev/null`.empty?
end

#git_clone(repository) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubygems_plugin.rb', line 68

def git_clone(repository)
  return if repository.nil? || repository.empty?
  return if @tested_repositories.include? repository
  @tested_repositories << repository
  return if github?(repository) && !github_page_exists?(repository)

  if ghq_available?
    system 'ghq', 'get', repository
  else
    system 'git', 'clone', repository, clone_dir if git?(repository)
  end
end

#git_clone_homepage_or_source_code_uri_or_homepage_uri_or_github_organization_uri ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rubygems_plugin.rb', line 85

def git_clone_homepage_or_source_code_uri_or_homepage_uri_or_github_organization_uri
  return false if File.exist? clone_dir
  git_clone(installer.spec.homepage) ||
    git_clone(github_url(installer.spec.homepage)) ||
    git_clone(source_code_uri) ||
    git_clone(homepage_uri) ||
    git_clone(github_url(homepage_uri)) ||
    git_clone(github_organization_uri(installer.spec.name))
end

#github?(url) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rubygems_plugin.rb', line 41

def github?(url)
  URI.parse(url).host == 'github.com'
end

#github_organization_uri(name) ⇒ Object



64
65
66
# File 'lib/rubygems_plugin.rb', line 64

def github_organization_uri(name)
  "https://github.com/#{name}/#{name}"
end

#github_page_exists?(url) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rubygems_plugin.rb', line 45

def github_page_exists?(url)
  Net::HTTP.new('github.com', 443).tap {|h| h.use_ssl = true }.request_head(url).code != '404'
end

#github_url(url) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubygems_plugin.rb', line 23

def github_url(url)
  if url =~ /\Ahttps?:\/\/([^.]+)\.github.com\/(.+)/
    if $1 == 'www'
      "https://github.com/#{$2}"
    elsif $1 == 'wiki'
      # https://wiki.github.com/foo/bar => https://github.com/foo/bar
      "https://github.com/#{$2}"
    else
      # https://foo.github.com/bar => https://github.com/foo/bar
      "https://github.com/#{$1}/#{$2}"
    end
  end
end

#homepage_uri ⇒ Object



60
61
62
# File 'lib/rubygems_plugin.rb', line 60

def homepage_uri
  api[/^homepage_uri: (.*)$/, 1]
end

#source_code_uri ⇒ Object



56
57
58
# File 'lib/rubygems_plugin.rb', line 56

def source_code_uri
  api[/^source_code_uri: (.*)$/, 1]
end