Sha256: ceaf814a74980aa7c9b189457ae5009c8e2d89d3944b843b0398873571650a18

Contents?: true

Size: 1.76 KB

Versions: 46

Compression:

Stored size: 1.76 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

46 entries across 46 versions & 1 rubygems

Version Path
bake-toolkit-2.35.3 lib/common/ext/file.rb
bake-toolkit-2.35.2 lib/common/ext/file.rb
bake-toolkit-2.35.1 lib/common/ext/file.rb
bake-toolkit-2.35.0 lib/common/ext/file.rb
bake-toolkit-2.34.4 lib/common/ext/file.rb
bake-toolkit-2.34.3 lib/common/ext/file.rb
bake-toolkit-2.34.2 lib/common/ext/file.rb
bake-toolkit-2.34.1 lib/common/ext/file.rb
bake-toolkit-2.34.0 lib/common/ext/file.rb
bake-toolkit-2.33.0 lib/common/ext/file.rb
bake-toolkit-2.32.0 lib/common/ext/file.rb
bake-toolkit-2.31.7 lib/common/ext/file.rb
bake-toolkit-2.31.5 lib/common/ext/file.rb
bake-toolkit-2.31.4 lib/common/ext/file.rb
bake-toolkit-2.31.2 lib/common/ext/file.rb
bake-toolkit-2.31.0 lib/common/ext/file.rb
bake-toolkit-2.30.0 lib/common/ext/file.rb
bake-toolkit-2.29.4 lib/common/ext/file.rb
bake-toolkit-2.29.3 lib/common/ext/file.rb
bake-toolkit-2.29.2 lib/common/ext/file.rb