aboutsummaryrefslogtreecommitdiff
path: root/bundler/platform.lua
diff options
context:
space:
mode:
Diffstat (limited to 'bundler/platform.lua')
-rw-r--r--bundler/platform.lua69
1 files changed, 69 insertions, 0 deletions
diff --git a/bundler/platform.lua b/bundler/platform.lua
new file mode 100644
index 0000000..2a69653
--- /dev/null
+++ b/bundler/platform.lua
@@ -0,0 +1,69 @@
+-- bundler. public domain.
+-- https://citrons.xyz/git/cc-bundler.git
+
+local cc_host = {}
+
+function cc_host.error(msg)
+ printError(msg)
+end
+
+function cc_host.list_dir(path)
+ local list = fs.list(shell.resolve(path))
+ local i = 0
+ return function()
+ i = i + 1
+ return list[i]
+ end
+end
+
+function cc_host.is_dir(path)
+ return fs.isDir(shell.resolve(path))
+end
+
+function cc_host.read(path)
+ local file = assert(fs.open(shell.resolve(path), "r"))
+ local data = assert(file.readAll())
+ file.close()
+ return data
+end
+
+function cc_host.write(path, content)
+ local file = assert(fs.open(shell.resolve(path), "w"))
+ file.write(content)
+ file.close()
+end
+
+local normal_host = {}
+
+function normal_host.error(msg)
+ io.stderr:write(tostring(msg).."\n")
+ os.exit(-1)
+end
+
+function normal_host.list_dir(path)
+ return require "lfs".dir(path)
+end
+
+function normal_host.is_dir(path)
+ return require "lfs".attributes(path).mode == "directory"
+end
+
+function normal_host.read(path)
+ local file = assert(io.open(path, "r"))
+ local data = assert(file:read "a")
+ file:close()
+ return data
+end
+
+function normal_host.write(path, content)
+ local file = assert(io.open(path, "w"))
+ assert(file:write(content))
+ file:close()
+end
+
+if _HOST and _HOST:match "ComputerCraft" then
+ return cc_host
+else
+ return normal_host
+end
+