How to Overwrite a Project File Using Code? (Moved from Questions)
Previously posted in Questions here
Is there a way to replace a project file with different data using code? If I have an asset called foo
and try to do this:
system.project.deleteFile("assets/foo", function()
system.project.writeFile("assets/foo", "new content")
end)
I end up with no foo
asset but a foo2
asset containing "new content"
. I have checked, and the foo
asset is gone when writeFile is called. Is there anything I can do to fix this, or is it a MicroStudio bug?
This code adds code to the project.
First removes the existing .
Then adds .
The removal of code can be skipped if you add to the options calling
object replace = "true" end,
status = system.project.writeFile( "source/generated/src1", string, object replace = "true" end, .....
init = function()
string ="
DynamicClass = class
printString = function( s )
print( s )
end
end
Test = function()
print('Test function')
end
// registerDynamicClass = function()
// global.DynamicClass = DynamicClass
// end
// if not DynamicClass then
// registerDynamicClass()
// end
"
status = system.project.deleteFile( "source/generated/src1", function(result, error) print(result) end )
print( "status = " + status.ready )
while not status.ready end
print( "status = " + status.ready )
status = system.project.writeFile( "source/generated/src1", string, object replace = "true" end,
function(result, error) print("New file " + result)
// registerDynamicClass() d = new DynamicClass()
end )
print( "status = " + status.ready )
while not status.ready end
print( "status = " + status.ready )
dc = new DynamicClass()
dc.printString( system.time() )
end
update = function()
dc.printString( system.time() )
Test()
end
draw = function()
end
I once wanted to add JavaScript code to the project (instead of import - the library content would be in a txt file).
But it didn't work as I expected - the added JavaScript code was not available from MicroScript and I abandoned this idea.
If you don't delete the file before adding it, the changes are not visible in the editor until you go to another file and return to the code you added.