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.
This commit is contained in:
Kartik K. Agaram 2023-09-09 08:56:21 -07:00
parent 31266e23f5
commit bcd7f6b598
1 changed files with 34 additions and 0 deletions

34
app.lua
View File

@ -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