updatetext.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/env lua
  2. local basedir = ""
  3. if arg[0]:find("[/\\]") then
  4. basedir = arg[0]:gsub("(.*[/\\]).*$", "%1"):gsub("\\", "/")
  5. end
  6. if basedir == "" then basedir = "./" end
  7. -- Required by load_strings()
  8. function string.trim(s) -- luacheck: ignore
  9. return s:gsub("^%s*(.-)%s*$", "%1")
  10. end
  11. dofile(basedir.."/../lib.lua")
  12. local me = arg[0]:gsub(".*[/\\](.*)$", "%1")
  13. local function err(fmt, ...)
  14. io.stderr:write(("%s: %s\n"):format(me, fmt:format(...)))
  15. os.exit(1)
  16. end
  17. local output, outfile, template
  18. local catalogs = { }
  19. local function usage()
  20. print([[
  21. Usage: ]]..me..[[ [OPTIONS] TEMPLATE CATALOG...
  22. Update a catalog with new strings from a template.
  23. Available options:
  24. -h,--help Show this help screen and exit.
  25. -o,--output X Set output file (default: stdout).
  26. Messages in the template that are not on the catalog are added to the
  27. catalog at the end.
  28. This tool also checks messages that are in the catalog but not in the
  29. template, and reports such lines. It's up to the user to remove such
  30. lines, if so desired.
  31. ]])
  32. os.exit(0)
  33. end
  34. local i = 1
  35. while i <= #arg do
  36. local a = arg[i]
  37. if (a == "-h") or (a == "--help") then
  38. usage()
  39. elseif (a == "-o") or (a == "--output") then
  40. i = i + 1
  41. if i > #arg then
  42. err("missing required argument to `%s'", a)
  43. end
  44. output = arg[i]
  45. elseif a:sub(1, 1) ~= "-" then
  46. if not template then
  47. template = a
  48. else
  49. table.insert(catalogs, a)
  50. end
  51. else
  52. err("unrecognized option `%s'", a)
  53. end
  54. i = i + 1
  55. end
  56. if not template then
  57. err("no template specified")
  58. elseif #catalogs == 0 then
  59. err("no catalogs specified")
  60. end
  61. local f, e = io.open(template, "r")
  62. if not f then
  63. err("error opening template: %s", e)
  64. end
  65. local escapes = { ["\n"] = "\\n", ["="] = "\\=", ["\\"] = "\\\\", }
  66. local function escape(s)
  67. return s:gsub("[\\\n=]", escapes)
  68. end
  69. if output then
  70. outfile, e = io.open(output, "w")
  71. if not outfile then
  72. err("error opening file for writing: %s", e)
  73. end
  74. end
  75. local template_msgs = intllib.load_strings(template)
  76. for _, file in ipairs(catalogs) do
  77. print("Processing: "..file)
  78. local catalog_msgs = intllib.load_strings(file)
  79. local dirty_lines = { }
  80. if catalog_msgs then
  81. -- Add new entries from template.
  82. for k in pairs(template_msgs) do
  83. if not catalog_msgs[k] then
  84. print("NEW: "..k)
  85. table.insert(dirty_lines, escape(k).." =")
  86. end
  87. end
  88. -- Check for old messages.
  89. for k, v in pairs(catalog_msgs) do
  90. if not template_msgs[k] then
  91. print("OLD: "..k)
  92. table.insert(dirty_lines, "OLD: "..escape(k).." = "..escape(v))
  93. end
  94. end
  95. if #dirty_lines > 0 then
  96. local outf
  97. outf, e = io.open(file, "a+")
  98. if outf then
  99. outf:write("\n")
  100. for _, line in ipairs(dirty_lines) do
  101. outf:write(line)
  102. outf:write("\n")
  103. end
  104. outf:close()
  105. else
  106. io.stderr:write(("%s: WARNING: cannot write: %s\n"):format(me, e))
  107. end
  108. end
  109. else
  110. io.stderr:write(("%s: WARNING: could not load catalog\n"):format(me))
  111. end
  112. end