Skip to content

Commit

Permalink
Refactor resource resoution and downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Nullreff committed Jan 19, 2014
1 parent 45a1cd6 commit 250d628
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 119 deletions.
4 changes: 3 additions & 1 deletion lib/bukin/bukget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Bukin
# BukGet api
# Docs: http://bukget.org/pages/docs/API3.html
class Bukget
attr_reader :url, :server

VERSION = 'release'
URL = 'http://api.bukget.org'
SERVER = 'bukkit'
Expand Down Expand Up @@ -50,7 +52,7 @@ def find(data)
version_data = versions.find {|data| jar_filename(data)} || versions.first
raise NoDownloadError.new(name, version) unless version_data

Resource.new(name, version_data['version'], version_data['download'])
return version_data['version'], version_data['download']
end

private
Expand Down
4 changes: 3 additions & 1 deletion lib/bukin/bukkit_dl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Bukin
# Bukkit download api
# Docs: http://dl.bukkit.org/about/
class BukkitDl
attr_reader :url

VERSION = 'latest-rb'
URL = 'http://dl.bukkit.org'
GOOD_VERSIONS = "'latest', 'latest-rb', 'latest-beta', 'latest-dev', "\
Expand All @@ -30,7 +32,7 @@ def find(data)

download = @url + info['file']['url']

Resource.new(name, "build-#{info['build_number']}", download)
return "build-#{info['build_number']}", download
end

def correct_version_format?(version)
Expand Down
40 changes: 19 additions & 21 deletions lib/bukin/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,18 @@ def prepare_resources(raw_resources)
downloads = {}
final_resources = []

raw_resources.each do |data|
name, provider = prepare(data)

unless provider.is_a?(Download)
# If the provider is set, we add it to the list of datas that
# require information to be downloaded from a server before they can
# be downloaded themselves
url = data[name]
downloads[url] ||= []
downloads[url] << data
else
# Otherwise we push it to the final list
final_datas << data
raw_resources.group_by {|data| get_provider(data)}.each do |name, datas|
datas.group_by {|data| data[name]}.each do |url, data|
downloads[PROVIDERS[name].new(url)] = data
end
end

downloads.each do |provider, datas|
section "Fetching information from #{url}" do
fetching provider do
datas.each do |data|
begin
final_resources << provider.find_resource(data)
version, download = provider.find(data)
final_resources << Resource.new(data, version, download)
rescue OpenURI::HTTPError => ex
raise Bukin::BukinError,
"There was an error fetching information about "\
Expand All @@ -81,7 +72,7 @@ def install_resources(resources)
installer = Bukin::Installer.new(Dir.pwd, true)

resources.each do |resource|
downloading resource[:name], resource[:version] do
downloading resource.name, resource.version do
begin
installer.install(resource)
rescue OpenURI::HTTPError => ex
Expand All @@ -108,18 +99,17 @@ def install_resources(resources)
plugin: :bukget
}

def prepare(data)
name, provider = PROVIDERS.find {|n, p| data[n]}
def get_provider(data)
name = PROVIDERS.keys.find {|n| data[n]}

# If this resource doesn't have a provider, we assign a default
unless name
name = DEFAULTS[data[:type]]
raise MissingProviderError.new(data) unless name
provider = PROVIDERS[name]
data[name] = provider::URL
data[name] = PROVIDERS[name]::URL
end

return name, provider
name
end

def section(message)
Expand All @@ -137,5 +127,13 @@ def downloading(name, version, &block)
msg << " (#{version})" if version
section(msg, &block)
end

def fetching(provider, &block)
if provider.is_a?(Download)
yield
else
section("Fetching information from #{provider.url}", &block)
end
end
end
end
8 changes: 6 additions & 2 deletions lib/bukin/download.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module Bukin
# Straight file downloads
class Download
attr_accessor :url

def initialize(url)
@url = url
end

def find(name, version = nil, options = {})
Resource.new(name, version, @url)
def find(data)
version = data[:version]

return version, @url
end
end
end
17 changes: 6 additions & 11 deletions lib/bukin/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ def initialize(path, use_lockfile = false)
@lockfile = Bukin::Lockfile.new if use_lockfile
end

def install(data)
path = PATHS[data[:type]]
def install(resource)
path = PATHS[resource.type]
file_names = []
dl_data, dl_name = download_file(data[:download])
dl_data, dl_name = download_file(resource.download)

if File.extname(dl_name) == '.zip'
match = self.get_match(data[:extract])
match = self.get_match(resource[:extract])
file_names = extract_files(dl_data, path, match)
raise Bukin::InstallError, "The resource #{data[:name]} (#{data[:version]}) has no jar files in it's download (zip file)." if file_names.empty?
raise Bukin::InstallError, "The resource #{resource.name} (#{resources.version}) has no jar files in it's download (zip file)." if file_names.empty?
else
self.save_download(dl_data, dl_name, path)
save_download(dl_data, dl_name, path)
file_names << dl_name
end

if @lockfile
data[:files] = file_names.map {|file_name| File.join(path, file_name)}
@lockfile.add(data)
end
end

def extract_files(file_data, path, match)
Expand Down
18 changes: 12 additions & 6 deletions lib/bukin/jenkins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@
module Bukin
# Api for downloading from jenkins
class Jenkins
attr_reader :url

VERSION = 'lastSuccessfulBuild'
GOOD_VERSIONS = "'125' or '#{VERSION}'"
GOOD_VERSIONS = "'build-125'"

def initialize(url)
@url = url
end

def find(data)
name = data[:name]
version = data[:version] || VERSION
version = data[:version]
match = data[:file] ? FileMatch.new(data[:file]) : FileMatch.any

unless correct_version_format?(version)
if version.nil? || version == VERSION
build = VERSION
elsif correct_version_format?(version)
build = version[/^build-([0-9]+)$/, 1]
else
raise VersionError.new(name, version, GOOD_VERSIONS)
end

base_path = "#{@url}/job/#{CGI.escape(name)}/#{CGI.escape(version)}"
base_path = "#{@url}/job/#{CGI.escape(name)}/#{CGI.escape(build)}"

info = Bukin.try_get_json("#{base_path}/api/json")
raise NoDownloadError.new(name, version) unless info
Expand All @@ -30,12 +36,12 @@ def find(data)
raise NoDownloadError.new(name, version) unless download_info

download = "#{base_path}/artifact/#{download_info['relativePath']}"
Resource.new(name, info['number'].to_s, download)
return "build-#{info['number']}", download
end

private
def correct_version_format?(version)
version == VERSION || /^[0-9]+$/.match(version)
version == VERSION || /^build-[0-9]+$/.match(version)
end
end
end
16 changes: 15 additions & 1 deletion lib/bukin/resource.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
module Bukin
Resource = Struct.new(:name, :version, :download)
class Resource
attr_reader :type, :name, :version, :download

def initialize(data, version, download)
@type = data[:type]
@name = data[:name]
@version = version
@download = download
@data = data
end

def [](key)
@data[key]
end
end
end
14 changes: 6 additions & 8 deletions spec/bukget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@

it 'installs the latest version of a resource' do
provider = Bukin::Bukget.new
resource = provider.find(name: @name)
version, download = provider.find(name: @name)

resource.name.should == @name
resource.version.should == @latest_version
version.should == @latest_version
end

it 'installs a specific version of a resource' do
provider = Bukin::Bukget.new
resource = provider.find(name: @name, version: @version)
version, download = provider.find(name: @name, version: @version)

resource.name.should == @name
resource.version.should == @version
version.should == @version
end

it 'returns an error when asked for a resource that doese not exist' do
Expand All @@ -51,8 +49,8 @@

it 'chooses the version with a .jar file when there are multiple versions' do
provider = Bukin::Bukget.new
resource = provider.find(name: @name, version: @version)
version, download = provider.find(name: @name, version: @version)

resource.download.should == @download
download.should == @download
end
end
14 changes: 6 additions & 8 deletions spec/bukkit_dl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@

it 'installs the latest version of a resource' do
provider = Bukin::BukkitDl.new
resource = provider.find(name: @name)
version, download = provider.find(name: @name)

resource.name.should == @name
resource.version.should == @latest_version
version.should == @latest_version
end

it 'installs a specific version of a resource' do
provider = Bukin::BukkitDl.new
resource = provider.find(name: @name, version: @version)
version, download = provider.find(name: @name, version: @version)

resource.name.should == @name
resource.version.should == @version
version.should == @version
end

it 'returns an error when asked for a resource that doese not exist' do
Expand All @@ -45,8 +43,8 @@

it 'chooses the first file when there are multiple files' do
provider = Bukin::BukkitDl.new
resource = provider.find(name: @name, version: @version)
version, download = provider.find(name: @name, version: @version)

resource.download.should == @download
download.should == @download
end
end
48 changes: 0 additions & 48 deletions spec/cli_spec.rb

This file was deleted.

Loading

0 comments on commit 250d628

Please sign in to comment.