clone url: git://git.m455.casa/ffs
ffs.lua
1 | #!/usr/bin/env lua |
2 | -- ffs - A Fennel and Lua library for handling files and directories |
3 | -- Author: Jesse Laprade (m455) |
4 | -- License: AGPL3 (https://www.gnu.org/licenses/agpl-3.0.en.html) |
5 | -- Source: https://git.m455.casa/m455/ffs |
6 | local ffs = {} |
7 | ffs["shell->sequence"] = function(command) |
8 | local file_handle = io.popen(command, "r") |
9 | local function close_handlers_0_(ok_0_, ...) |
10 | file_handle:close() |
11 | if ok_0_ then |
12 | return ... |
13 | else |
14 | return error(..., 0) |
15 | end |
16 | end |
17 | local function _0_() |
18 | local tbl_0_ = {} |
19 | for i in file_handle:lines() do |
20 | tbl_0_[(#tbl_0_ + 1)] = i |
21 | end |
22 | return tbl_0_ |
23 | end |
24 | return close_handlers_0_(xpcall(_0_, (package.loaded.fennel or debug).traceback)) |
25 | end |
26 | ffs["directory-exists?"] = function(directory) |
27 | local command = string.format("test -d %s && echo exists", directory) |
28 | if (ffs["shell->sequence"](command))[1] then |
29 | return true |
30 | else |
31 | return false |
32 | end |
33 | end |
34 | ffs["directory-create"] = function(dir) |
35 | return os.execute(string.format("mkdir -p %s", dir)) |
36 | end |
37 | ffs["directory-contents"] = function(dir) |
38 | return ffs["shell->sequence"](string.format("ls %s", dir)) |
39 | end |
40 | ffs["file-exists?"] = function(file) |
41 | local command = string.format("test -f %s && echo exists", file) |
42 | if (ffs["shell->sequence"](command))[1] then |
43 | return true |
44 | else |
45 | return false |
46 | end |
47 | end |
48 | ffs["file-create"] = function(file) |
49 | return io.close(io.open(file, "w")) |
50 | end |
51 | ffs["file-write"] = function(file, data, mode) |
52 | local file_out = io.open(file, mode) |
53 | local function close_handlers_0_(ok_0_, ...) |
54 | file_out:close() |
55 | if ok_0_ then |
56 | return ... |
57 | else |
58 | return error(..., 0) |
59 | end |
60 | end |
61 | local function _0_() |
62 | return file_out:write(data) |
63 | end |
64 | return close_handlers_0_(xpcall(_0_, (package.loaded.fennel or debug).traceback)) |
65 | end |
66 | ffs["file->lines"] = function(file) |
67 | local tbl_0_ = {} |
68 | for line in io.lines(file) do |
69 | tbl_0_[(#tbl_0_ + 1)] = line |
70 | end |
71 | return tbl_0_ |
72 | end |
73 | ffs["path-exists?"] = function(path) |
74 | return (ffs["file-exists?"](path) or ffs["directory-exists?"](path)) |
75 | end |
76 | ffs["path-copy"] = function(source, dest) |
77 | return os.execute(string.format("cp -r %s %s", source, dest)) |
78 | end |
79 | ffs["path-delete"] = function(path) |
80 | return os.execute(string.format("rm -rf %s", path)) |
81 | end |
82 | ffs["paths-missing"] = function(mode, required_paths) |
83 | local func = nil |
84 | do |
85 | local _0_0 = mode |
86 | if (_0_0 == "files") then |
87 | func = ffs["file-exists?"] |
88 | elseif (_0_0 == "directories") then |
89 | func = ffs["directory-exists?"] |
90 | else |
91 | func = nil |
92 | end |
93 | end |
94 | local tbl_0_ = {} |
95 | for _, path in ipairs(required_paths) do |
96 | local _1_ |
97 | if not func(path) then |
98 | _1_ = path |
99 | else |
100 | _1_ = nil |
101 | end |
102 | tbl_0_[(#tbl_0_ + 1)] = _1_ |
103 | end |
104 | return tbl_0_ |
105 | end |
106 | return ffs |