Module: SvnAuto::Path

Defined in:
lib/svnauto/path.rb

Class Method Summary collapse

Class Method Details

.absolute?(path) ⇒ Boolean

path is absolute?

Returns:

  • (Boolean)


105
106
107
# File 'lib/svnauto/path.rb', line 105

def self.absolute? (path)
  Pathname.new(path).absolute?
end

.absolute_from_home(path) ⇒ Object

make a relative path to home absolute



30
31
32
33
34
35
36
37
38
39
# File 'lib/svnauto/path.rb', line 30

def self.absolute_from_home (path)
  home = File.expand_path('~')
  path.strip!

  # path may start with ~
  path = File.expand_path(path) if path[0, 1] == '~'

  # allow path to be relative or absolute
  absolute?(path) ? path : File.join(home, path)
end

.case_insensitive_filesystem?Boolean

are we running under a case insensitive filesystem? (such as Windows)

Returns:

  • (Boolean)


129
130
131
132
133
# File 'lib/svnauto/path.rb', line 129

def self.case_insensitive_filesystem?
  ruby_version = Version.new(RUBY_VERSION) rescue nil
  return !File::FNM_SYSCASE.zero? if ruby_version >= Version.new('1.8.5')
  return windows?
end

.has_drive?(path) ⇒ Boolean

path has Windows drive letter?

Returns:

  • (Boolean)


117
118
119
# File 'lib/svnauto/path.rb', line 117

def self.has_drive? (path)
  path.match(/^[a-z]:/i)
end

.relative_to_home(path) ⇒ Object

make an absolute path relative to home



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/svnauto/path.rb', line 43

def self.relative_to_home (path)
  home = File.expand_path('~')
  absolute_path = path.dup

  if case_insensitive_filesystem?
    home.downcase!
    absolute_path.downcase!
  end

  absolute_path[0, home.length] == home ? absolute_path.sub(home, '~') : absolute_path
end

.to_path(url) ⇒ Object

extract local path element from url.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/svnauto/path.rb', line 71

def self.to_path (url)
  unless windows?
    return url.is_a?(URI) ? url.path : url
  end

  path = url.to_s
  return path unless url?(path)
  return path unless /^file:/ =~ path

  path = path.sub(/^file:/, "")
  return path unless path =~ /^\//

  if has_drive?(path.sub(/^\/+/, ""))
    path.sub(/^\/+/, "")
  elsif unc?(path)
    path
  else
    path.sub(/^\/+/, "/")
  end
end

.to_url(path) ⇒ Object

make file:// url from local path.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/svnauto/path.rb', line 57

def self.to_url (path)
  if windows?
    if unc?(path)
      return URI.escape("file:#{path}")
    elsif has_drive?(path)
      return URI.escape("file:///#{path}")
    end
  end

  url?(path) ? path : URI.escape("file://#{path}")
end

.unc?(path) ⇒ Boolean

path is UNC?

Returns:

  • (Boolean)


123
124
125
# File 'lib/svnauto/path.rb', line 123

def self.unc? (path)
  path.match(%r{^//[^/]})
end

.url?(path) ⇒ Boolean

path is URL?

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/svnauto/path.rb', line 94

def self.url? (path)
  begin
    u = URI.parse(path)
    return (u.scheme and u.scheme.length > 0)
  rescue URI::InvalidURIError
    false
  end
end

.windows?Boolean

Is this platform running the Windows OS?

Returns:

  • (Boolean)


111
112
113
# File 'lib/svnauto/path.rb', line 111

def self.windows?
  RUBY_PLATFORM.match(/djgpp|(cyg|ms|bcc)win|mingw/)
end