Class: Jenkins::Plugin::Tools::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins/plugin/tools/resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec, dir) ⇒ Resolver

Returns a new instance of Resolver.



8
9
10
11
12
# File 'lib/jenkins/plugin/tools/resolver.rb', line 8

def initialize(spec, dir)
  @spec = spec
  @dir = dir
  FileUtils.mkdir_p(dir) unless File.directory? @dir
end

Instance Method Details

#fetch(uri, limit = 10) ⇒ Object

download with redirect support

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jenkins/plugin/tools/resolver.rb', line 46

def fetch(uri, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  response = Net::HTTP.get_response(URI.parse(uri))
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

#resolve(short_name, version) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jenkins/plugin/tools/resolver.rb', line 20

def resolve(short_name,version)
  # this is where we expect the retrieved file to be
  cache = File.expand_path "~/.jenkins/cache/plugins/#{short_name}/#{version}/#{short_name}.hpi"

  return cache if File.exists?(cache)

  # now we start looking for places to find them

  # is it in the local maven2 repository?
  maven = File.expand_path "~/.m2/repository/org/jenkins-ci/plugins/#{short_name}/#{version}/#{short_name}-#{version}.hpi"
  return maven if File.exists?(maven)

  # download from the community update center
  FileUtils.mkdir_p(File.dirname(cache))
  open(cache+".tmp","wb") do |f|
    puts "Downloading #{short_name} #{version}"
    url = "http://updates.jenkins-ci.org/download/plugins/#{short_name}/#{version}/#{short_name}.hpi?for=ruby-plugin"
    puts "  from #{url}"
    f.write fetch(url).body
  end
  FileUtils.mv cache+".tmp", cache

  return cache
end

#resolve!Object



14
15
16
17
18
# File 'lib/jenkins/plugin/tools/resolver.rb', line 14

def resolve!
  @spec.dependencies.each do |name, version|
    FileUtils.cp resolve(name, version), @dir
  end
end