Method: ChefConfig::PathHelper.dirname

Defined in:
lib/chef-config/path_helper.rb

.dirname(path, windows: ChefUtils.windows?) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef-config/path_helper.rb', line 29

def self.dirname(path, windows: ChefUtils.windows?)
  if windows
    # Find the first slash, not counting trailing slashes
    end_slash = path.size
    loop do
      slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator(windows: windows))}]/, end_slash - 1)
      if !slash
        return end_slash == path.size ? "." : path_separator(windows: windows)
      elsif slash == end_slash - 1
        end_slash = slash
      else
        return path[0..slash - 1]
      end
    end
  else
    ::File.dirname(path)
  end
end