autoupdate.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import("core.package.package")
  2. import("core.base.semver")
  3. import("core.base.hashset")
  4. import("devel.git")
  5. import("packages", {alias = "packages_util"})
  6. function _load_package(packagename, packagedir, packagefile)
  7. local funcinfo = debug.getinfo(package.load_from_repository)
  8. if funcinfo and funcinfo.nparams == 3 then -- >= 2.7.8
  9. return package.load_from_repository(packagename, packagedir, {packagefile = packagefile})
  10. else
  11. -- deprecated
  12. return package.load_from_repository(packagename, nil, packagedir, packagefile)
  13. end
  14. end
  15. function _get_all_packages()
  16. local packages = _g.packages
  17. if not packages then
  18. packages = {}
  19. for _, packagedir in ipairs(os.dirs(path.join("packages", "*", "*"))) do
  20. local packagename = path.filename(packagedir)
  21. local packagefile = path.join(packagedir, "xmake.lua")
  22. local instance = _load_package(packagename, packagedir, packagefile)
  23. local basename = instance:get("base")
  24. if instance and basename then
  25. local basedir = path.join("packages", basename:sub(1, 1):lower(), basename:lower())
  26. local basefile = path.join(basedir, "xmake.lua")
  27. instance._BASE = _load_package(basename, basedir, basefile)
  28. end
  29. if instance then
  30. table.insert(packages, instance)
  31. end
  32. end
  33. _g.packages = packages
  34. end
  35. return packages
  36. end
  37. function _is_pending(instance, version)
  38. local branch = "autoupdate-" .. instance:name() .. "-" .. version
  39. local repourl = "[email protected]:xmake-io/xmake-repo.git"
  40. local is_pending = false
  41. local remote_branches = os.iorun("git ls-remote --head %s", repourl)
  42. if remote_branches then
  43. for _, remote_branch in ipairs(remote_branches:split("\n")) do
  44. remote_branch = remote_branch:split("%s")[2]
  45. if remote_branch == "refs/heads/" .. branch then
  46. is_pending = true
  47. break
  48. end
  49. end
  50. end
  51. return is_pending
  52. end
  53. function _update_version(instance, version, shasum)
  54. local branch = "autoupdate-" .. instance:name() .. "-" .. version
  55. local branch_current = os.iorun("git branch --show-current"):trim()
  56. local repourl = "[email protected]:xmake-io/xmake-repo.git"
  57. os.vexec("git reset --hard HEAD")
  58. os.vexec("git clean -fdx")
  59. os.execv("git", {"branch", "-D", branch}, {try = true})
  60. os.vexec("git checkout dev")
  61. os.vexec("git pull %s dev", repourl)
  62. os.vexec("git branch %s", branch)
  63. os.vexec("git checkout %s", branch)
  64. local scriptfile = path.join(instance:scriptdir(), "xmake.lua")
  65. if os.isfile(scriptfile) and not is_pending then
  66. local inserted = false
  67. local version_current
  68. io.gsub(scriptfile, "add_versions%(\"(.-)\",%s+\"(.-)\"%)", function (v, h)
  69. if not version_current or semver.compare(v, version_current) > 0 then
  70. version_current = v
  71. end
  72. if not inserted then
  73. inserted = true
  74. return string.format('add_versions("%s", "%s")\n add_versions("%s", "%s")', version, shasum, v, h)
  75. end
  76. end)
  77. local body = string.format("New version of %s detected (package version: %s, last github version: %s)",
  78. instance:name(), version_current, version)
  79. os.vexec("git add .")
  80. os.vexec("git commit -a -m \"Update %s to %s\"", instance:name(), version)
  81. os.vexec("git push %s %s:%s", repourl, branch, branch)
  82. os.vexec("gh pr create --label \"auto-update\" --title \"Auto-update %s to %s\" --body \"%s\" -R xmake-io/xmake-repo -B dev -H %s",
  83. instance:name(), version, body, branch)
  84. end
  85. os.vexec("git reset --hard HEAD")
  86. os.vexec("git checkout %s", branch_current)
  87. end
  88. function main(maxcount)
  89. local count = 0
  90. local maxcount = tonumber(maxcount or 10)
  91. local instances = _get_all_packages()
  92. math.randomseed(os.time())
  93. while count < maxcount do
  94. local instance = instances[math.random(#instances)]
  95. local checkupdate_filepath = path.join(instance:scriptdir(), "checkupdate.lua")
  96. if not os.isfile(checkupdate_filepath) then
  97. checkupdate_filepath = path.join(os.scriptdir(), "checkupdate.lua")
  98. end
  99. if os.isfile(checkupdate_filepath) then
  100. local checkupdate = import("checkupdate", {rootdir = path.directory(checkupdate_filepath), anonymous = true})
  101. local version, shasum = checkupdate(instance)
  102. if version and shasum and not _is_pending(instance, version) then
  103. cprint("package(%s): new version ${bright}%s${clear} found, shasum: ${bright}%s", instance:name(), version, shasum)
  104. _update_version(instance, version, shasum)
  105. count = count + 1
  106. end
  107. end
  108. end
  109. end