Skip to content

Instantly share code, notes, and snippets.

@Keyaku
Last active March 7, 2016 06:21
Show Gist options
  • Save Keyaku/35e96df01ac2255e2200 to your computer and use it in GitHub Desktop.
Save Keyaku/35e96df01ac2255e2200 to your computer and use it in GitHub Desktop.
Lets you work extensively with files and directories via Sparkling
/*
The MIT License (MIT)
Copyright (c) 2016 by António Sarmento
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
let workdir = {
buffer : {
get : fn (self) -> self["buffer"]["value"],
set : fn (self, new) {
assert(typeof new == "string", "Buffer must be of type string");
self["buffer"]["value"] = new;
},
value : ""
},
update_buffer : fn (self) {
if system("test -s buf.txt") == 0 { // if file isn't empty
let str = readfile("buf.txt");
let len = str.length - 1; // Remove linefeed
self["buffer"]["value"] = str.substrto(len);
}
remove("buf.txt");
},
pwd : fn -> exec_command("pwd"),
ls : fn {
let args = $.slice(1).join(" ");
return exec_command("ls", args);
},
mkdir : fn {
let args = $.slice(1).join(" ");
return exec_command("mkdir", args);
},
zip : fn {
let args = $.slice(1).join(" ");
return exec_command("zip", args);
},
dirname : fn (self, path) -> exec_command("dirname", path),
basename : fn {
let args = $.slice(1).join(" ");
return exec_command("basename", args);
},
test : fn {
let expr = $.slice(1).join(" ");
return exec_command("test", expr);
},
// TESTING
exists : fn (self, path) -> self.test("-e", path) == 0,
isdir : fn (self, path) -> self.test("-d", path) == 0,
isfile : fn (self, path) -> self.test("-f", path) == 0,
issymlink : fn (self, path) -> self.test("-L", path) == 0,
isreadable : fn (self, path) -> self.test("-r", path) == 0,
iswriteable : fn (self, path) -> self.test("-w", path) == 0,
isexecutable : fn (self, path) -> self.test("-x", path) == 0,
isempty : fn (self, path) -> self.test("-s", path) == 1
// End of TESTING
};
extern exec_command = fn {
let ret = system("fn() {\n\t" .. $.join(" ") .. " 2>&1 | tee buf.txt\n\treturn ${PIPESTATUS[0]}\n}\nfn");
workdir.update_buffer();
return ret;
};
return workdir;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment