Class: URI::FILE

Inherits:
Generic show all
Defined in:
lib/buildr/core/transports.rb

Overview

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.

Constant Summary collapse

COMPONENT =
[ :host, :path ].freeze

Instance Method Summary collapse

Methods inherited from Generic

#download, #write

Constructor Details

#initialize(*args) ⇒ FILE

Returns a new instance of FILE.



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/buildr/core/transports.rb', line 499

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

Instance Method Details

#read(options = nil, &block) ⇒ Object

See URI::Generic#read

Raises:

  • (ArgumentError)


521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/buildr/core/transports.rb', line 521

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

#real_pathObject

Returns the file system path based that corresponds to the URL path. On windows this method strips the leading slash off of the path. On all platforms this method unescapes the URL path.



543
544
545
546
# File 'lib/buildr/core/transports.rb', line 543

def real_path #:nodoc:
  real_path = Buildr::Util.win_os? && path =~ /^\/[a-zA-Z]:\// ? path[1..-1] : path
  URI.unescape(real_path)
end

#to_sObject



536
537
538
# File 'lib/buildr/core/transports.rb', line 536

def to_s
  "file://#{host}#{path}"
end

#upload(source, options = nil) ⇒ Object



492
493
494
495
496
497
# File 'lib/buildr/core/transports.rb', line 492

def upload(source, options = nil)
  super
  if File === source then
    File.chmod(source.stat.mode, real_path)
  end
end