diff --git a/.vscode/settings.json b/.vscode/settings.json index d31f9d8..e21c6be 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,8 +7,8 @@ "editor.acceptSuggestionOnEnter": "off" }, "cSpell.words": [ - "aireone", "aire-one", + "aireone", "ansicolors", "awesomewm", "buildx", @@ -21,6 +21,7 @@ "lldebugger", "Luacheck", "luacheckrc", + "luafilesystem", "lualogging", "Luarocks", "luasec", diff --git a/rockspecs/awesomewm.d.tl-dev-1.rockspec b/rockspecs/awesomewm.d.tl-dev-1.rockspec index cc2af55..0e9fdc1 100644 --- a/rockspecs/awesomewm.d.tl-dev-1.rockspec +++ b/rockspecs/awesomewm.d.tl-dev-1.rockspec @@ -16,6 +16,7 @@ dependencies = { "penlight 1.13.1", "luasocket 3.1.0-1", "luasec 1.2.0-1", + "luafilesystem 1.8.0-1", } build = { type = "builtin", diff --git a/src/awesomewm.d.tl/filesystem/file_writer.tl b/src/awesomewm.d.tl/filesystem/file_writer.tl index a68581f..de44d8a 100644 --- a/src/awesomewm.d.tl/filesystem/file_writer.tl +++ b/src/awesomewm.d.tl/filesystem/file_writer.tl @@ -1,14 +1,35 @@ local file = require "pl.file" +local lfs = require "lfs" local logger = require "logger" local path = require "pl.path" local log = logger.log("file_writer") +local function mkdir(dir_path: string) + local sep, parent_dir = "/", "" + for dir in dir_path:gmatch("[^" .. sep .. "]+") do + parent_dir = parent_dir .. dir .. sep + if not path.isdir(parent_dir) then + local success, error_message = lfs.mkdir(parent_dir) + if not success then + log:error(logger.message_with_metadata( + "Failed to create directory", + { + directory = parent_dir, + error = error_message, + } + )) + error("Failed to create directory " .. parent_dir) + end + end + end +end + local function write_file(file_content: string, file_path: string): boolean, nil | string -- Make sure the directory we want to write the file to exists local directory = path.dirname(file_path) if not path.isdir(directory) then - path.mkdir(directory) + mkdir(directory) end return file.write(file_path, file_content, false) diff --git a/tlconfig.lua b/tlconfig.lua index aafa9c4..0fa854d 100644 --- a/tlconfig.lua +++ b/tlconfig.lua @@ -4,5 +4,6 @@ return { include_dir = { "src/awesomewm.d.tl", "types", + "generated", -- used to remove require error when visualizing the generated type definitions }, } diff --git a/types/lfs.d.tl b/types/lfs.d.tl new file mode 100644 index 0000000..12535ca --- /dev/null +++ b/types/lfs.d.tl @@ -0,0 +1,82 @@ +local record lfs + + enum FileMode + "file" + "directory" + "link" + "socket" + "named pipe" + "char device" + "block device" + "other" + end + + record Attributes + dev: number + ino: number + mode: FileMode + nlink: number + uid: number + gid: number + rdev: number + access: number + modification: number + change: number + size: number + permissions: string + blocks: number + blksize: number + end + + enum OpenFileMode + "binary" + "text" + end + + enum LockMode + "r" + "w" + end + + record Lock + free: function() + end + + dir: function(string): function(): string + + chdir: function(string): boolean, string + + lock_dir: function(string, number): Lock, string + + -- returns number on success, really!? this should be fixed in the lfs library + link: function(string, string, boolean): number, string + + mkdir: function(string): boolean, string + + rmdir: function(string): boolean, string + + setmode: function(string, OpenFileMode): boolean, string + + currentdir: function(): string + + attributes: function(string): Attributes + attributes: function(string, string): string + attributes: function(string, string): number + attributes: function(string, string): FileMode + attributes: function(string, Attributes): Attributes + + symlinkattributes: function(string): Attributes + symlinkattributes: function(string, string): string + symlinkattributes: function(string, string): number + symlinkattributes: function(string, string): FileMode + symlinkattributes: function(string, Attributes): Attributes + + touch: function(string, number, number): boolean, string + + -- TODO: FILE needs to be renamed to io.FILE in tl itself + lock: function(FILE, LockMode, number, number): boolean, string + unlock: function(FILE, number, number): boolean, string + +end + +return lfs