Skip to content

Commit 9d7ab46

Browse files
committed
ext: file
1 parent 3cc53ed commit 9d7ab46

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/ext/file.cr

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
21+
class File
22+
def self.write_p(path, contents)
23+
FileUtils.make_p File.dirname(path)
24+
File.write path, contents
25+
end
26+
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
30+
31+
def self.find_in_ancestors(base: String, name: String) : String?
32+
root = File.dirname(base)
33+
34+
loop do
35+
return nil if root == "." || root == "/"
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)