2
0

build_artifacts.lua 6.3 KB

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