Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Author: Kingdaro
- doughnut steel
- Description:
- implements the code of another file
- into the top of whatever file(s) you choose
- useful for, as an example, storing a checkFuel() function
- and implementing it into turtle programs
- as opposed to rewriting it every time
- ]]
- local args = {...}
- local function implement( source, ... )
- local dir = shell.dir()
- source = source..dir
- local sourceFile = fs.open(source, 'r')
- if not sourceFile then
- return false, 'could not open source file'
- end
- local code = sourceFile.readAll()
- sourceFile.close()
- -- format code for reimplementation if already implemented, for easy updating
- code = '--IMPLEMENT '..source..'\n'..
- code..
- '\n--END'
- local files = {...}
- for i=1,#files do
- files[i] = dir .. files[i]
- local file = fs.open(files[i], 'r')
- if file then
- local fileContent = file.readAll()
- file.close()
- -- remove current implementation if it exists
- fileContent = fileContent:gsub('%-%-IMPLEMENT '..source..'.-%-%-END\n*', '')
- file = fs.open(files[i], 'w')
- file.write(code .. '\n\n' .. fileContent)
- file.close()
- print ('successfully written ', files[i])
- else
- print ('could not open ', files[i])
- end
- end
- end
- if #args >= 3 and args[2] == 'into' then
- table.remove(args, 2)
- implement(unpack(args))
- else
- print 'Usage: implement <source> into <file> (<file2> <file3> ...)'
- end
Advertisement
Add Comment
Please, Sign In to add comment