Improved API.

This commit is contained in:
Martin Edenhofer 2012-12-27 15:41:28 +01:00
parent a7d970a9f5
commit 17e3ce1766
2 changed files with 53 additions and 56 deletions

View file

@ -2,13 +2,15 @@ require 'rexml/document'
class Package < ApplicationModel
@@root = Rails.root.to_s
def self.build_file(file)
xml = self._read_file(file, true)
def self.build(data)
if data[:file]
xml = self._read_file( data[:file], true )
package = self._parse(xml)
self.build(package)
elsif data[:string]
package = self._parse( data[:string] )
end
def self.build(package)
build_date = REXML::Element.new("build_date")
build_date.text = Time.now.utc.iso8601
build_host = REXML::Element.new("build_host")
@ -25,21 +27,16 @@ class Package < ApplicationModel
return package.to_s
end
def self.install_file(file)
xml = self._read_file( file, true )
def self.install(data)
if data[:file]
xml = self._read_file( data[:file], true )
package = self._parse(xml)
self.install(package)
elsif data[:string]
package = self._parse( data[:string] )
end
def self.install_string(xml)
package = self._parse(xml)
self.install(package)
end
def self.install(package)
# package meta data
data = {
meta = {
:name => package.elements["zpm/name"].text,
:version => package.elements["zpm/version"].text,
:vendor => package.elements["zpm/vendor"].text,
@ -49,26 +46,30 @@ class Package < ApplicationModel
}
# verify if package can get installed
package_db = Package.where( :name => data[:name] ).first
package_db = Package.where( :name => meta[:name] ).first
if package_db
if Gem::Version.new( package_db.version ) == Gem::Version.new( data[:version] )
raise "Package '#{data[:name]}' already installed!"
if Gem::Version.new( package_db.version ) == Gem::Version.new( meta[:version] )
raise "Package '#{meta[:name]}-#{meta[:version]}' already installed!"
end
if Gem::Version.new( package_db.version ) > Gem::Version.new( data[:version] )
raise "Newer version (#{package_db.version}) of package '#{data[:name]}-#{data[:version]}' already installed!"
if Gem::Version.new( package_db.version ) > Gem::Version.new( meta[:version] )
raise "Newer version (#{package_db.version}) of package '#{meta[:name]}-#{meta[:version]}' already installed!"
end
# uninstall old package
self.uninstall_name( package_db.name, package_db.version, false )
self.uninstall({
:name => package_db.name,
:version => package_db.version,
:migration_not_down => true,
})
end
# store package
record = Package.create( data )
record = Package.create( meta )
Store.add(
:object => 'Package',
:o_id => record.id,
:data => package.to_s,
:filename => data[:name] + '-' + data[:version] + '.zpm',
:filename => meta[:name] + '-' + meta[:version] + '.zpm',
:preferences => {},
)
@ -86,35 +87,31 @@ class Package < ApplicationModel
record.save
# up migrations
Package::Migration.migrate( data[:name] )
Package::Migration.migrate( meta[:name] )
# prebuild assets
return true
end
def self.uninstall_name( name, version, migration_down = true )
file = self._get_bin( name, version )
def self.uninstall( data )
if data[:string]
package = self._parse( data[:string] )
else
file = self._get_bin( data[:name], data[:version] )
package = self._parse(file)
self.uninstall( package, migration_down )
end
def self.uninstall_string(xml)
package = self._parse(xml)
self.uninstall(package)
end
def self.uninstall( package, migration_down = true )
# package meta data
data = {
meta = {
:name => package.elements["zpm/name"].text,
:version => package.elements["zpm/version"].text,
}
# down migrations
if migration_down
Package::Migration.migrate( data[:name], 'reverse' )
if !data[:migration_not_down]
Package::Migration.migrate( meta[:name], 'reverse' )
end
package.elements.each('zpm/filelist/file') do |element|
@ -129,8 +126,8 @@ class Package < ApplicationModel
# delete package
record = Package.where(
:name => data[:name],
:version => data[:version],
:name => meta[:name],
:version => meta[:version],
).first
record.destroy

View file

@ -159,7 +159,7 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
tests.each { |test|
if test[:action] == 'install'
begin
success = Package.install_string( test[:zpm] )
success = Package.install( :string => test[:zpm] )
rescue => e
puts 'ERROR: ' + e.inspect
success = false
@ -172,13 +172,13 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
elsif test[:action] == 'uninstall'
if test[:zpm]
begin
success = Package.uninstall_string( test[:zpm] )
success = Package.uninstall( :string => test[:zpm] )
rescue
success = false
end
else
begin
success = Package.uninstall_name( test[:name], test[:version] )
success = Package.uninstall( :name => test[:name], :version => test[:version] )
rescue
success = false
end