2
0

build_artifacts.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import("core.package.package")
  2. import("core.base.semver")
  3. import("core.base.hashset")
  4. import("packages", {alias = "packages_util"})
  5. -- load package
  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 _need_artifact(instance)
  16. return (not instance:is_headeronly()) and (packages_util.is_supported(instance, "windows", "x64") or packages_util.is_supported(instance, "windows", "x86"))
  17. end
  18. function _build_artifacts(name, versions)
  19. local buildinfo = {name = name, versions = versions}
  20. print(buildinfo)
  21. os.tryrm("build-artifacts")
  22. os.exec("git clone [email protected]:xmake-mirror/build-artifacts.git -b build")
  23. local oldir = os.cd("build-artifacts")
  24. local trycount = 0
  25. while trycount < 2 do
  26. local ok = try
  27. {
  28. function ()
  29. io.save("build.txt", buildinfo)
  30. os.exec("git add -A")
  31. os.exec("git commit -a -m \"autobuild %s by xmake-repo/ci\"", name)
  32. os.exec("git push origin build")
  33. return true
  34. end,
  35. catch
  36. {
  37. function ()
  38. os.exec("git reset --hard HEAD^")
  39. os.exec("git pull origin build")
  40. end
  41. }
  42. }
  43. if ok then
  44. break
  45. end
  46. trycount = trycount + 1
  47. end
  48. assert(trycount < 2)
  49. os.cd(oldir)
  50. end
  51. function _get_latest_modified_packages()
  52. print("find latest modified packages ..")
  53. local instances = {}
  54. local files = os.iorun("git diff --name-only HEAD^")
  55. for _, file in ipairs(files:split('\n')) do
  56. file = file:trim()
  57. if file:find("packages", 1, true) and path.filename(file) == "xmake.lua" then
  58. assert(file == file:lower(), "%s must be lower case!", file)
  59. local packagedir = path.directory(file)
  60. local packagename = path.filename(packagedir)
  61. if #path.filename(path.directory(packagedir)) == 1 then
  62. local instance = _load_package(packagename, packagedir, file)
  63. if instance and _need_artifact(instance) then
  64. table.insert(instances, instance)
  65. print(" > %s", instance:name())
  66. end
  67. end
  68. end
  69. end
  70. print("%d found", #instances)
  71. return instances
  72. end
  73. function _get_all_packages()
  74. local packages = _g.packages
  75. if not packages then
  76. packages = {}
  77. for _, packagedir in ipairs(os.dirs(path.join("packages", "*", "*"))) do
  78. local packagename = path.filename(packagedir)
  79. local packagefile = path.join(packagedir, "xmake.lua")
  80. local instance = _load_package(packagename, packagedir, packagefile)
  81. local basename = instance:get("base")
  82. if instance and basename then
  83. local basedir = path.join("packages", basename:sub(1, 1):lower(), basename:lower())
  84. local basefile = path.join(basedir, "xmake.lua")
  85. instance._BASE = _load_package(basename, basedir, basefile)
  86. end
  87. if instance and _need_artifact(instance) then
  88. table.insert(packages, instance)
  89. end
  90. end
  91. _g.packages = packages
  92. end
  93. return packages
  94. end
  95. function _get_packagerefs_of(instance)
  96. local packagerefs = {}
  97. if instance:is_library() then
  98. local packages = _get_all_packages()
  99. for _, packageref in ipairs(packages) do
  100. local deps = packageref:get("deps")
  101. if deps and table.contains(table.wrap(deps), instance:name()) then
  102. table.insert(packagerefs, packageref)
  103. end
  104. end
  105. end
  106. return packagerefs
  107. end
  108. function _get_packagerefs_in_latest_24h()
  109. print("find packagerefs in latest 24h ..")
  110. local instances = {}
  111. local list = os.iorun("git log --since=\"24 hours ago\" --oneline")
  112. local lines = list:split('\n')
  113. if #lines > 0 then
  114. local line = lines[#lines]
  115. local commit = line:split(" ")[1]
  116. if commit and #commit == 8 then
  117. local files = os.iorun("git diff --name-only " .. commit .. "^")
  118. for _, file in ipairs(files:split('\n')) do
  119. file = file:trim()
  120. if file:find("packages", 1, true) and path.filename(file) == "xmake.lua" then
  121. assert(file == file:lower(), "%s must be lower case!", file)
  122. local packagedir = path.directory(file)
  123. local packagename = path.filename(packagedir)
  124. if #path.filename(path.directory(packagedir)) == 1 then
  125. local instance = _load_package(packagename, packagedir, file)
  126. if instance and _need_artifact(instance) then
  127. table.insert(instances, instance)
  128. end
  129. end
  130. end
  131. end
  132. end
  133. end
  134. local packagerefs = hashset.new()
  135. for _, instance in ipairs(instances) do
  136. print("%s: ", instance:name())
  137. for _, packageref in ipairs(_get_packagerefs_of(instance)) do
  138. packagerefs:insert(packageref)
  139. print(" -> %s", packageref:name())
  140. end
  141. end
  142. local result = {}
  143. for _, packageref in packagerefs:keys() do
  144. if #result < 24 then
  145. table.insert(result, packageref)
  146. end
  147. end
  148. print("%d found", #result)
  149. return result
  150. end
  151. function main(updaterefs)
  152. local instances = updaterefs and _get_packagerefs_in_latest_24h() or _get_latest_modified_packages()
  153. for _, instance in ipairs(instances) do
  154. local versions = instance:versions()
  155. if versions and #versions > 0 then
  156. table.sort(versions, function (a, b) return semver.compare(a, b) > 0 end)
  157. local version_latest = versions[1]
  158. _build_artifacts(instance:name(), table.wrap(version_latest))
  159. end
  160. end
  161. end