class URI::FILE

File URL. Keep in mind that file URLs take the form of file://host/path, although the host is not used, so typically all you will see are three backslashes. This methods accept common variants, like file:/path but always returns a valid URL.

Constants

COMPONENT

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/buildr/core/transports.rb, line 495
def initialize(*args)
  super
  # file:something (opaque) becomes file:///something
  if path.nil?
    set_path "/#{opaque}"
    unless opaque.nil?
      set_opaque nil
      warn "#{caller[2]}: We'll accept this URL, but just so you know, it needs three slashes, as in: #{to_s}"
    end
  end
  # Sadly, file://something really means file://something/ (something being server)
  set_path '/' if path.empty?

  # On windows, file://c:/something is not a valid URL, but people do it anyway, so if we see a drive-as-host,
  # we'll just be nice enough to fix it. (URI actually strips the colon here)
  if host =~ /^[a-zA-Z]$/
    set_path "/#{host}:#{path}"
    set_host nil
  end
end

Public Instance Methods

read(options = nil, &block) click to toggle source

See URI::Generic#read

# File lib/buildr/core/transports.rb, line 517
def read(options = nil, &block)
  options ||= {}
  raise ArgumentError, 'Either you\re attempting to read a file from another host (which we don\t support), or you used two slashes by mistake, where you should have file:///<path>.' if host

  path = real_path
  # TODO: complain about clunky URLs
  raise NotFoundError, "Looking for #{self} and can't find it." unless File.exists?(path)
  raise NotFoundError, "Looking for the file #{self}, and it happens to be a directory." if File.directory?(path)
  File.open path, 'rb' do |input|
    with_progress_bar options[:progress], path.split('/').last, input.stat.size do |progress|
      block ? block.call(input.read) : input.read
    end
  end
end
to_s() click to toggle source
# File lib/buildr/core/transports.rb, line 532
def to_s
  "file://#{host}#{path}"
end
upload(source, options = nil) click to toggle source
Calls superclass method URI::Generic#upload
# File lib/buildr/core/transports.rb, line 488
def upload(source, options = nil)
  super
  if File === source then
    File.chmod(source.stat.mode, real_path)
  end
end