#-- # ___ _ _ _ _ # | _ \_ _| |__| (_)__| |_ # | _/ || | '_ \ | (_-< ' \ # |_| \_,_|_.__/_|_/__/_||_| # #++ require 'shellwords' module Reap # = Publish # # Publish documents to hosting service. Three means of # publishing are current supported, ftp uploading, scp, # and web convenience option that recognizes particular # hosts (only RubyForge is currently supported). # # type Type of publishing, 'web', 'scp' or 'ftp'. # host Host server, default is 'rubyforge.org' # username Username for host. # dir Directory of files to publish. Or, # copy Files to publish using from and to. # project Project name (used for Rubyforge) # # If the type is 'scp' or 'ftp' you will also need to provide # the root directory parameter. # # root Document root directory at host. # # The dir parameter allows you to simply specify a local # directory, the contents of which will be published to # host's document root location (directory tree intact). # # If you need more control over which files to publish # where, you can use the copy parameter instead. Provide # an array of pattern strings in the form of "{from} {to}". # If the desitination is the host's document root you do # not need to specify the {to} part. For example: # # copy: [ 'web/*', 'doc/api/* doc/api' ] # # The first copies the files under your project's web directory # to the host's document root. The second copies your projects # doc/api files to the doc/api location on the host. # # When using the 'scp' type the internal template used for # the outbound destination is 'username@host:root/'. class Publish include TaskUtils attr_accessor :dir, :type, :host, :root, :project, :username, :copy def initialize( pub ) @type = 'web' @host = 'rubyforge.org' @root = 'var/www/gforge-projects/' super @copy ||= [ "#{@dir}/*" ] end # Upload files to host. def publish( target=nil, copy_from=nil, copy_to=nil ) return nil if target and target != @target # setup if copy_from @copy = [ "#{copy_from} #{copy_to}".strip ] end # validate unless @project puts "Project not specified." ; return nil end unless @username puts "Username not specified." ; return nil end # publish puts "Reap is passing off publishing work..." case @type when 'web', 'www' case @host when 'rubyforge', 'rubyforge.org' @host = "rubyforge.org" @root = "/var/www/gforge-projects/#{@project}" run_scp else puts %{Unrecognized publishing host '#{@host}'. Skipped.} return nil end when 'scp' run_scp when 'ftp' run_ftp else puts %{Unrecognized publishing type '#{@type}'. Skipped.} return nil end end private # Use scp to upload files. #-- # SHELL OUT! Need a net/ssh library to fix. #++ def run_scp @copy.each { |c| from, to = *Shellwords.shellwords(c) to = '' if to.nil? to = to[1..-1] if to[0,1] == '/' cmd = "scp -r #{from} #{@username}@#{@host}:#{@root}/#{to}" sh( cmd ) } end # Use ftp to upload files. def run_ftp require 'net/ftp' return if $PRETEND Net::FTP.open( @host ) { |ftp| ftp.login( @username ) ftp.chdir( @root ) @copy.each { |c| from, to = *Shellwords.shellwords(c) to = '' if to.nil? to = to[1..-1] if to[0,1] == '/' from.sub('*', '**/*') unless from =~ /\*\*/ files = FileList.new( from ) files.each { |f| t = File.join( to, f.basename ) ftp.putbinaryfile( f, t, 1024 ) } } } end end end #module Reap