bytecode-generator.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. local root_path = arg[2]
  2. local dest_path = arg[4]
  3. -------------------------------------------------------------
  4. function get_files(path)
  5. local files = {}
  6. local tmp_file = 'tmp.txt'
  7. os.execute('ls -1 ' .. path .. '*.lua > ' .. tmp_file)
  8. local f = io.open(tmp_file)
  9. if not f then
  10. return
  11. end
  12. local i = 1;
  13. for line in f:lines() do
  14. files[i] = line
  15. i = i + 1
  16. end
  17. f.close()
  18. return files
  19. end
  20. --------------------------------------------------------------
  21. function prepare_destination(files)
  22. -- Portable bytecode
  23. local ext = ".raw"
  24. local raws = {}
  25. for i,s in ipairs(files) do
  26. raws[i] = string.sub(s, 1 + string.len(root_path))
  27. local len = string.len(raws[i])
  28. raws[i] = string.sub(raws[i], 1, len -4)
  29. raws[i] = raws[i] .. ext
  30. raws[i] = dest_path .. raws[i]
  31. end
  32. return raws
  33. end
  34. --------------------------------------------------------------
  35. function generate_bytecode(src, dest)
  36. local chunk = string.dump(loadfile(src), true)
  37. local f,err = io.open(dest, "w")
  38. if not f then
  39. return print(err)
  40. end
  41. f:write(chunk)
  42. f:close()
  43. end
  44. --------------------------------------------------------------
  45. local src = get_files(root_path)
  46. local dest = prepare_destination(src)
  47. for i,s in ipairs(src) do
  48. generate_bytecode(s, dest[i])
  49. end