Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/common/ext/file.rb

Constant Summary collapse

SLASH =
'/'
@@warnedCase =
[]

Class Method Summary collapse

Class Method Details

.add_prefix(prefix, file) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/common/ext/file.rb', line 93

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

.cleanupWarningsObject



9
10
11
# File 'lib/common/ext/file.rb', line 9

def self.cleanupWarnings
  @@warnedCase.clear
end

.is_absolute?(filename) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/common/ext/file.rb', line 13

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

.normalize(filename) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/common/ext/file.rb', line 17

def self.normalize(filename)
  if filename.length > 1
    toIsWindowsAbs = filename[1] == ':'
    return filename[0].downcase + filename[1..-1] if toIsWindowsAbs
  end
  return filename
end

.rel_from_to_project(from, to, endWithSlash = true) ⇒ Object

seems both are rel or both are abs in all cases



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/common/ext/file.rb', line 26

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
    if toSplitted[i] != fromSplitted[i]
      if Bake.options.verbose >= 1 && Bake.options.caseSensitivityCheck
        if toSplitted[i].casecmp(fromSplitted[i]) == 0
          if !@@warnedCase.include?(fromSplitted[0..i].join("/"))
            fromsj = fromSplitted[0..i].join("/")
            tosj = toSplitted[0..i].join("/")
            @@warnedCase << fromsj
            @@warnedCase << tosj
            Bake.formatter.printWarning("Warning: different cases for folders \"#{fromsj}\" and \"#{tosj}\" detected.")
          end
        end
      end
      break
    end
    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

.which(cmd) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/common/ext/file.rb', line 101

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