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

View file

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