Sha256: c5bca0aa774763378dc92346a9e26cc1c5d653dfc69934bbc977b8220cd4a229

Contents?: true

Size: 1.9 KB

Versions: 42

Compression:

Stored size: 1.9 KB

Contents

require 'common/utils'

class File

  SLASH = '/'

  def self.is_absolute?(filename)
    filename[0] == SLASH or filename[1] == ':'
  end

  # seems both are rel or both are abs in all cases
  def self.rel_from_to_project(from,to,endWithSlash = true)

    return nil if from.nil? or to.nil?
    
    toSplitted = to.split('/')
    fromSplitted = from.split('/')
    
    max = [toSplitted.length, fromSplitted.length].min
    
    
    return nil if max < 1
    
    i = 0
    
    # path letter in windows may be case different
    toIsWindowsAbs = false
    if toSplitted[0].length > 1 and fromSplitted[0].length > 1
      toIsWindowsAbs = toSplitted[0][1] == ':'
      i = 1  if toIsWindowsAbs and fromSplitted[0][1] == ':' and toSplitted[0][0].downcase == fromSplitted[0][0].downcase
    end
    
    if (toIsWindowsAbs and i==0)
      res = to
      res += "/" if endWithSlash 
      return res	  
    end
    
    while i < max
        break if toSplitted[i] != fromSplitted[i] 
      i += 1
    end
    j = i
    
    res = []
    while i < fromSplitted.length
      res << ".."
      i += 1
    end
    
    while j < toSplitted.length
      res << toSplitted[j]
      j += 1
    end
    
    if res.length == 0
      return ""
    end
    
    res = res.join('/')
    res += "/" if endWithSlash 
    res
  end

  
  def self.add_prefix(prefix, file)
    if not prefix or is_absolute?(file)
      file
    else
      prefix + file
    end
  end

  def self.which(cmd)
    return "" if not cmd
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
      exts.each { |ext|
        exe = File.join(path, "#{cmd}#{ext}")
        if File.executable?(exe) && !File.directory?(exe)
          return File.dirname(exe.gsub(/[\\]/,'/'))
        end 
      }
    end
    return ""
  end
  
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
bake-toolkit-2.20.4 lib/common/ext/file.rb
bake-toolkit-2.20.3 lib/common/ext/file.rb
bake-toolkit-2.20.2 lib/common/ext/file.rb
bake-toolkit-2.20.1 lib/common/ext/file.rb
bake-toolkit-2.19.2 lib/common/ext/file.rb
bake-toolkit-2.19.1 lib/common/ext/file.rb
bake-toolkit-2.19.0 lib/common/ext/file.rb
bake-toolkit-2.18.0 lib/common/ext/file.rb
bake-toolkit-2.17.4 lib/common/ext/file.rb
bake-toolkit-2.17.3 lib/common/ext/file.rb
bake-toolkit-2.17.2 lib/common/ext/file.rb
bake-toolkit-2.17.1 lib/common/ext/file.rb
bake-toolkit-2.16.1 lib/common/ext/file.rb
bake-toolkit-2.15.0 lib/common/ext/file.rb
bake-toolkit-2.14.0 lib/common/ext/file.rb
bake-toolkit-2.13.1 lib/common/ext/file.rb
bake-toolkit-2.13.0 lib/common/ext/file.rb
bake-toolkit-2.12.2 lib/common/ext/file.rb
bake-toolkit-2.12.1 lib/common/ext/file.rb
bake-toolkit-2.12.0 lib/common/ext/file.rb