Skip to content

Commit 512538d

Browse files
committed
ext: file
1 parent 9d7ab46 commit 512538d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/ext/file.cr

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@
1717
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
1818
# -----------------------------------------------------------------------
1919

20-
2120
class File
22-
def self.write_p(path, contents)
23-
FileUtils.make_p File.dirname(path)
24-
File.write path, contents
25-
end
21+
def self.write_p(path, contents)
22+
FileUtils.mkdir_p File.dirname(path)
23+
File.write path, contents
24+
end
2625

27-
def self.relative_path_from_ancestor(path: String, name: String) : String
28-
return path unless directory = File.find_in_ancestors(path, name)
29-
end
26+
def self.relative_path_from_ancestor(path : String, name : String) : String
27+
return path unless directory = File.find_in_ancestors(path, name)
28+
Path[path].relative_to(File.dirname(directory)).to_s
29+
end
30+
31+
def self.find_in_ancestors(base : String, name : String) : String?
32+
root = File.dirname(base)
3033

31-
def self.find_in_ancestors(base: String, name: String) : String?
32-
root = File.dirname(base)
34+
loop do
35+
return nil if root == "." || root == "/"
3336

34-
loop do
35-
return nil if root == "." || root == "/"
36-
end
37+
if File.exists?(path = Path[root, name])
38+
return path.to_s
39+
else
40+
root = File.dirname(root)
41+
end
3742
end
38-
end
43+
end
44+
end

0 commit comments

Comments
 (0)