about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-09-09 08:56:21 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-09-09 08:56:21 -0700
commitbcd7f6b59828f6f5419c1a48394d12177ba35446 (patch)
tree69b1e1375c2d8a3c56364e2624c46d8a3ad60ff2
parent31266e23f5bbdf5887303bbcc0f69246e6e45f57 (diff)
downloadlines.love-bcd7f6b59828f6f5419c1a48394d12177ba35446.tar.gz
new primitives for reading/writing files
These are like versions in nativefs, but only support absolute paths.
I want to be thoughtful about the precise location at each call-site.

It's a little ugly that app.lua now has a dependency on file.lua. Or
source_file.lua for the source editor.
-rw-r--r--app.lua34
1 files changed, 34 insertions, 0 deletions
diff --git a/app.lua b/app.lua
index 03bc4de..b615ad0 100644
--- a/app.lua
+++ b/app.lua
@@ -288,6 +288,10 @@ function App.open_for_reading(filename)
   end
 end
 
+function App.read_file(filename)
+  return App.filesystem[filename]
+end
+
 function App.open_for_writing(filename)
   App.filesystem[filename] = ''
   return {
@@ -299,6 +303,11 @@ function App.open_for_writing(filename)
   }
 end
 
+function App.write_file(filename, contents)
+  App.filesystem[filename] = contents
+  return --[[status]] true
+end
+
 function App.mkdir(dirname)
   -- nothing in test mode
 end
@@ -435,6 +444,19 @@ function App.disable_tests()
           return ok, err
         end
       end
+  App.read =
+      function(path)
+        if not is_absolute_path(path) then
+          return --[[status]] false, 'Please use an unambiguous absolute path.'
+        end
+        local f, err = App.open_for_reading(path)
+        if err then
+          return --[[status]] false, err
+        end
+        local contents = f:read()
+        f:close()
+        return contents
+      end
   App.open_for_writing =
       function(filename)
         local result = nativefs.newFile(filename)
@@ -445,6 +467,18 @@ function App.disable_tests()
           return ok, err
         end
       end
+  App.write =
+      function(filename, contents)
+        if not is_absolute_path(path) then
+          return --[[status]] false, 'Please use an unambiguous absolute path.'
+        end
+        local f, err = App.open_for_writing(filename)
+        if err then
+          return --[[status]] false, err
+        end
+        f:write(contents)
+        f:close()
+      end
   App.files = nativefs.getDirectoryItems
   App.mkdir = nativefs.createDirectory
   App.remove = nativefs.remove