Skip to content

Commit 0cb8b75

Browse files
byroothsbt
authored andcommitted
Make Pathname#mkpath builtin
[Feature #17473]
1 parent 2bc9cb3 commit 0cb8b75

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

lib/pathname.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,34 @@ def sub_ext(repl)
349349

350350
# :startdoc:
351351

352+
# Creates a full path, including any intermediate directories that don't yet
353+
# exist.
354+
#
355+
# See FileUtils.mkpath and FileUtils.mkdir_p
356+
def mkpath(mode: nil)
357+
path = @path == '/' ? @path : @path.chomp('/')
358+
359+
stack = []
360+
until File.directory?(path) || File.dirname(path) == path
361+
stack.push path
362+
path = File.dirname(path)
363+
end
364+
365+
stack.reverse_each do |dir|
366+
dir = dir == '/' ? dir : dir.chomp('/')
367+
if mode
368+
Dir.mkdir dir, mode
369+
File.chmod mode, dir
370+
else
371+
Dir.mkdir dir
372+
end
373+
rescue SystemCallError
374+
raise unless File.directory?(dir)
375+
end
376+
377+
self
378+
end
379+
352380
# chop_basename(path) -> [pre-basename, basename] or nil
353381
def chop_basename(path) # :nodoc:
354382
base = File.basename(path)
@@ -1158,16 +1186,6 @@ def find(ignore_error: true) # :yield: pathname
11581186

11591187

11601188
class Pathname # * FileUtils *
1161-
# Creates a full path, including any intermediate directories that don't yet
1162-
# exist.
1163-
#
1164-
# See FileUtils.mkpath and FileUtils.mkdir_p
1165-
def mkpath(mode: nil)
1166-
require 'fileutils'
1167-
FileUtils.mkpath(@path, mode: mode)
1168-
self
1169-
end
1170-
11711189
# Recursively deletes a directory, including all directories beneath it.
11721190
#
11731191
# See FileUtils.rm_rf

0 commit comments

Comments
 (0)