require 'repertoire/media/exceptions/unknown_media' require 'repertoire/media/exceptions/program_not_found' require 'repertoire/extensions/uri' require 'repertoire/compat' require 'fileutils' module Repertoire class Media # # Returns the Hash of all registered Media types. # def Media.types @types ||= {} end # # Returns +true+ if a Media type was registered for the specified # _scheme_, returns +false+ otherwise. # def Media.supported?(scheme) Media.types.has_key?(scheme.to_s) end # # Get the Media type that was registered for the specified _scheme_. # def Media.get(scheme) unless Media.supported?(scheme) raise(UnknownMedia,"media type #{scheme.dump} is unsupported",caller) end return Media.types[scheme] end # # Get the Media type that was registered for the scheme of the # specified _uri_. # def Media.get_for_uri(uri) Media.get(URI.parse(uri).scheme) end # # Attempts to determine the correct Media type for the specified _path_. # def Media.guess(path) path = File.expand_path(path) Media.types.values.uniq.each do |media| if media.respond_to?(:is_repo?) return media if media.is_repo?(path) end end raise(UnknownMedia,"the media type for #{path.dump} is unknown",caller) end # # Checkout the repository at the specified _uri_ and the given _path_. # def Media.checkout(uri,path=nil) if path path = File.expand_path(path) else path = URI.repo_name(uri) end return Media.get_for_uri(uri).checkout(uri,path) end # # Update the repository at the specified _path_ and the given _uri_. # If _uri_ is not given Repertoire will attempt to guess the media # type of the repository, see Media.guess. # def Media.update(path,uri=nil) path = File.expand_path(path) if uri.nil? media = Media.guess(path) else media = Media.get_for_uri(uri) end return media.update(path,uri) end # # Delete the repository at the specified _path_. # def Media.delete(path) FileUtils.rm_r(path.to_s,:force => true, :secure => true) end protected # # Register a Media type for the specified _schemes_. # # uses_schemes 'cvs' # # uses_schemes 'svn', 'svn+ssh' # def self.uses_schemes(*schemes) schemes.each do |scheme| Media.types[scheme.to_s] = self end end # # Runs the command specified by _program_ and the given _args_. # If _program_ cannot be found on the system, a ProgramNotFound # exception will be raised. # # sh('ls','-la','/') # def self.sh(program,*args) program = program.to_s program_path = Compat.find_program(program) unless program_path raise(ProgramNotFound,"the program #{program.dump} was not found",caller) end # stringify the args args = args.map { |arg| arg.to_s } return system(program_path,*args) end end end