Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
uribase pear package manager written in capistrano
# -*- mode: ruby; coding: utf-8 -*-
require 'hpricot'
require 'open-uri'
=begin
required configuration
set :package, {
:name => 'PACKAGE',
# $1 is used to specify the version number
:filename => /PACKAGE-(.*)\.tgz/,
:uri => 'http://example.com/path/to/yourpackage/'
}
=end
desc "force upgrade to latest version"
task :upgrade do
package = UribasePackage.new( fetch( :package ) )
if ( !package.installed? or package.current != package.latest )
puts package.upgrade
else
puts 'no need to do.'
end
end
desc "downgrade to previous version"
task :downgrade do
package = UribasePackage.new( fetch( :package ) )
if ( !package.installed? )
puts "install first"
elsif ( package.current == package.initial )
puts "no need to do."
else
puts package.downgrade
end
end
desc "install latest version"
task :install do
package = UribasePackage.new( fetch( :package ) )
if ( package.installed? )
puts "no need to do."
else
puts package.install
end
end
desc "list up Package packages"
task :list do
puts UribasePackage.new( fetch( :package ) ).collect.join( "\n" )
end
desc "check current installed version"
task "check-current" do
package = UribasePackage.new( fetch( :package ) )
if ( package.installed? )
puts package.current
else
puts "install first"
end
end
desc "check latest version"
task "check-latest" do
puts UribasePackage.new( fetch( :package ) ).latest
end
desc "uninstall Package"
task :uninstall do
package = UribasePackage.new( fetch( :package ) )
if ( package.installed? )
package.uninstall
else
puts "no need to do."
end
end
class UribasePackage
APACHE_OPTION = 'C=N;O=A;V=1;F=0'
def initialize( opt = {} )
@base_uri ||= opt[:uri]
@filename_re ||= opt[:filename]
@name ||= opt[:name]
@filelist = nil
@current = nil
end
def collect
if ( @filelist.nil? )
u = URI.parse( @base_uri )
u.query = "?#{APACHE_OPTION}"
d = Hpricot( u.read )
@filelist = d.search( 'a' ).map { |e|
m = e.get_attribute( 'href' ).match( @filename_re )
( m.nil? ) ? nil : m.to_s
}.compact
end
return @filelist
end
def current
if ( @current.nil? )
@current = pear_message.to_s.split( /\s+/ )[1] if installed?
end
return @current
end
def latest
version( collect.last )
end
def initial
version( collect.first )
end
def absolute_uri( package )
return @base_uri + package
end
def installed?
return ( pear_message.size > 0 ) ? true : nil
end
def pear_message
StringIO.new( `pear list -c __uri` ).entries.grep( /\b#{@name}\b/ )
end
def version( filename )
filename.match( @filename_re )[1]
end
def uri( filename )
return @base_uri + filename
end
def install
return `sudo pear install #{uri( latest_filename )}#`
end
def upgrade
return `sudo pear upgrade #{uri( latest_filename )}`
end
def downgrade
if ( prev_filename )
return `sudo pear upgrade --force #{uri( prev_filename )}`
end
end
def uninstall
return `sudo pear uninstall __uri/#{@name}`
end
def prev_filename
index = current_index
return ( index == 0 ) ? nil : collect[index - 1]
end
def next_filename
index = current_index
return ( index == collect.size - 1 ) ? nil : collect[index + 1]
end
def latest_filename
return collect.last
end
def current_index
current_file = collect.map { |e|
e.include?( current ) ? e : nil
}.compact.to_s
return collect.index( current_file )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment