make_resource.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/lua
  2. --------------------------------------------------------------------------------
  3. -- Script to be used on PC to build resource.lua file
  4. -- Parameters: [-o outputfile] file1 [file2] ...
  5. -- Example: ./make_resource resource/*
  6. -- creates resource.lua file with all files in resource directory
  7. --------------------------------------------------------------------------------
  8. local OUT = "resource.lua"
  9. local function readfile(file)
  10. local f = io.open(file, "rb")
  11. if f then
  12. local lines = f:read("*all")
  13. f:close()
  14. return lines
  15. end
  16. end
  17. -- tests the functions above
  18. print(string.format("make_resource script - %d parameter(s)", #arg))
  19. if #arg==0 or arg[1]=="--help" then
  20. print("parameters: [-o outputfile] file1 [file2] ...")
  21. return
  22. end
  23. local larg = {}
  24. local outpar = false
  25. for i, a in pairs(arg) do
  26. if i>0 then
  27. if outpar then
  28. OUT = a
  29. outpar = false
  30. else
  31. if a == "-o" then
  32. outpar = true
  33. else
  34. table.insert(larg, a)
  35. end
  36. end
  37. end
  38. end
  39. print(string.format("output set to: %s", OUT))
  40. local res = io.open(OUT, "w")
  41. res:write("-- luacheck: max line length 10000\nlocal arg = ...\n")
  42. res:close(file)
  43. local filelist = ""
  44. for _, a in pairs(larg) do
  45. local inp = string.match(a, ".*[/](.*)")
  46. if not inp then inp = a end
  47. local content = readfile(a)
  48. print(string.format("# processing %s", inp))
  49. if content then
  50. res = io.open(OUT, "a")
  51. filelist = filelist .. ('"%s",'):format(inp)
  52. res:write(('if arg == "%s" then return %q end\n\n'):format(inp, content))
  53. res:close(file)
  54. end
  55. end
  56. res = io.open(OUT, "a")
  57. res:write(('if arg == nil then return {%s} end\n'):format(filelist))
  58. res:close(file)