update_version_stamp.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env lua
  2. --
  3. -- Script to update the Git version stamp during build.
  4. -- This is called via the custom update_version_stamp target in
  5. -- src/nvim/CMakeLists.txt.
  6. --
  7. -- arg[1]: file in which to update the version string
  8. -- arg[2]: prefix to use always ("vX.Y.Z")
  9. local function die(msg)
  10. io.stderr:write(string.format('%s: %s\n', arg[0], msg))
  11. -- No error, fall back to using generated "-dev" version.
  12. os.exit(0)
  13. end
  14. local function iswin()
  15. return package.config:sub(1,1) == '\\'
  16. end
  17. if #arg ~= 2 then
  18. die(string.format("Expected two args, got %d", #arg))
  19. end
  20. local versiondeffile = arg[1]
  21. local prefix = arg[2]
  22. local dev_null = iswin() and 'NUL' or '/dev/null'
  23. local described = io.popen('git describe --first-parent --dirty 2>'..dev_null):read('*l')
  24. if not described then
  25. described = io.popen('git describe --first-parent --tags --always --dirty'):read('*l')
  26. end
  27. if not described then
  28. io.open(versiondeffile, 'w'):write('\n')
  29. die('git-describe failed, using empty include file.')
  30. end
  31. -- `git describe` annotates the most recent tagged release; for pre-release
  32. -- builds we append that to the dev version
  33. local with_prefix = prefix
  34. if prefix:match('-dev$') ~= nil then
  35. with_prefix = prefix .. '+' .. described:gsub('^v%d+%.%d+%.%d+-', '')
  36. end
  37. -- Read existing include file.
  38. local current = io.open(versiondeffile, 'r')
  39. if current then
  40. current = current:read('*l')
  41. end
  42. -- Write new include file, if different.
  43. local new = '#define NVIM_VERSION_MEDIUM "'..with_prefix..'"'
  44. if current ~= new then
  45. io.open(versiondeffile, 'w'):write(new .. '\n')
  46. end