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:
parent
31266e23f5
commit
bcd7f6b598
34
app.lua
34
app.lua
|
@ -288,6 +288,10 @@ function App.open_for_reading(filename)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function App.read_file(filename)
|
||||||
|
return App.filesystem[filename]
|
||||||
|
end
|
||||||
|
|
||||||
function App.open_for_writing(filename)
|
function App.open_for_writing(filename)
|
||||||
App.filesystem[filename] = ''
|
App.filesystem[filename] = ''
|
||||||
return {
|
return {
|
||||||
|
@ -299,6 +303,11 @@ function App.open_for_writing(filename)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function App.write_file(filename, contents)
|
||||||
|
App.filesystem[filename] = contents
|
||||||
|
return --[[status]] true
|
||||||
|
end
|
||||||
|
|
||||||
function App.mkdir(dirname)
|
function App.mkdir(dirname)
|
||||||
-- nothing in test mode
|
-- nothing in test mode
|
||||||
end
|
end
|
||||||
|
@ -435,6 +444,19 @@ function App.disable_tests()
|
||||||
return ok, err
|
return ok, err
|
||||||
end
|
end
|
||||||
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 =
|
App.open_for_writing =
|
||||||
function(filename)
|
function(filename)
|
||||||
local result = nativefs.newFile(filename)
|
local result = nativefs.newFile(filename)
|
||||||
|
@ -445,6 +467,18 @@ function App.disable_tests()
|
||||||
return ok, err
|
return ok, err
|
||||||
end
|
end
|
||||||
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.files = nativefs.getDirectoryItems
|
||||||
App.mkdir = nativefs.createDirectory
|
App.mkdir = nativefs.createDirectory
|
||||||
App.remove = nativefs.remove
|
App.remove = nativefs.remove
|
||||||
|
|
Loading…
Reference in New Issue