1 module api.fs;
2 
3 import lumars, std.file, std.exception, std.conv, std.algorithm, std.array;
4 
5 void registerFsApi(LuaState* lua)
6 {
7     lua.register!(
8         "append", (LuaState* l, string file, LuaValue v) {
9             enforce(v.isTable || v.isText, "Expected parameter 2 to be a table or a string.");
10             if(v.isText)
11                 append(file, v.textValue);
12             else
13             {
14                 auto t = v.tableValue;
15                 if(t.length == 0)
16                     return;
17                 t.push();
18                 scope(exit) l.pop(1);
19                 append(file, l.get!(ubyte[])(-1));
20             }
21         },
22         "chdir",            chdir!string,
23         "copy",             (string from, string to)    => copy(from, to),
24         "dirEntries",       (string path, string mode)  => dirEntries(path, mode.to!SpanMode).map!(de => de.name).array,
25         "dirEntriesGlob",   (string path, string pattern, string mode)  
26                                                         => dirEntries(path, pattern, mode.to!SpanMode).map!(de => de.name).array,
27         "exists",           exists!string,
28         "getSize",          getSize!string,
29         "isDir",            isDir!string,
30         "isFile",           isFile!string,
31         "mkdir",            mkdir!string,
32         "mkdirRecurse",     mkdirRecurse,
33         "readString",       (string file)               => readText(file),
34         "readBytes",        (string str)                => cast(ubyte[])read(str),
35         "remove",           std.file.remove!string,
36         "rename",           rename!(string, string),
37         "rmdir",            rmdir!string,
38         "rmDirRecurse",     rmdirRecurse,
39         "tempDir",          tempDir,
40         "write", (LuaState* l, string file, LuaValue v) {
41             enforce(v.isTable || v.isText, "Expected parameter 2 to be a table or a string.");
42             if(v.isText)
43                 write(file, v.textValue);
44             else
45             {
46                 auto t = v.tableValue;
47                 if(t.length == 0)
48                     return;
49                 t.push();
50                 scope(exit) l.pop(1);
51                 write(file, l.get!(ubyte[])(-1));
52             }
53         },
54 
55     )("sh.fs");
56 }