Skip to content

Commit

Permalink
feat: auto create parent dirs on write_file
Browse files Browse the repository at this point in the history
  • Loading branch information
Aylur committed Dec 30, 2024
1 parent 998b57d commit ce9ba0d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/astal/io/file.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public async string read_file_async(string path) throws Error {
*/
public void write_file(string path, string content) {
try {
var dir = Path.get_dirname(path);
if (!FileUtils.test(dir, FileTest.IS_DIR)) {
File.new_for_path(dir).make_directory_with_parents(null);
}

FileUtils.set_contents(path, content);
} catch (Error error) {
critical(error.message);
Expand All @@ -36,6 +41,11 @@ public void write_file(string path, string content) {
* Write content to a file asynchronously.
*/
public async void write_file_async(string path, string content) throws Error {
var dir = Path.get_dirname(path);
if (!FileUtils.test(dir, FileTest.IS_DIR)) {
File.new_for_path(dir).make_directory_with_parents(null);
}

yield File.new_for_path(path).replace_contents_async(
content.data,
null,
Expand Down

0 comments on commit ce9ba0d

Please sign in to comment.