new.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import("core.base.option")
  2. import("core.base.semver")
  3. import("core.base.json")
  4. import("core.base.hashset")
  5. import("lib.detect.find_tool")
  6. import("lib.detect.find_file")
  7. import("net.http")
  8. import("devel.git")
  9. import("utils.archive")
  10. local options = {
  11. {nil, "repo", "v", nil, "Set repository name.",
  12. "e.g. ",
  13. " - github:xmake-io/xmake",
  14. " - gitlab:xmake-io/xmake"}
  15. }
  16. -- function to get Gitlab data
  17. function get_gitlab_data(reponame)
  18. local glab = assert(find_tool("glab"), "glab not found!")
  19. local host = os.iorunv(glab.program, {"config", "get", "host"}):trim()
  20. local graphql_query = 'query={ project(fullPath: "' .. reponame .. '") { description webUrl sshUrlToRepo name } }'
  21. local repoinfo = os.iorunv(glab.program, {"api", "graphql", "-f", graphql_query})
  22. local data = {}
  23. if repoinfo then
  24. repoinfo = json.decode(repoinfo)
  25. if repoinfo.data and repoinfo.data.project then
  26. -- extract required data and restructure it
  27. local project_data = repoinfo.data.project
  28. data = {
  29. description = project_data.description,
  30. homepageUrl = project_data.webUrl,
  31. licenseInfo = "MIT", -- NOTE: Find a way to get the project license in gitlab
  32. url = project_data.webUrl,
  33. sshUrl = project_data.sshUrlToRepo,
  34. name = project_data.name,
  35. }
  36. repoinfo.data.project = data
  37. end
  38. end
  39. return {host = host, data = data}
  40. end
  41. local function get_github_data(reponame)
  42. local gh = assert(find_tool("gh"), "gh not found!")
  43. local host = "github.com"
  44. local data = os.iorunv(gh.program, {
  45. "repo",
  46. "view",
  47. reponame,
  48. "--json",
  49. "description,homepageUrl,licenseInfo,url,sshUrl,name,latestRelease",
  50. })
  51. if data then
  52. data = json.decode(data)
  53. end
  54. return {data = data, host = host}
  55. end
  56. function generate_package(reponame, get_data)
  57. local repo_data = get_data(reponame)
  58. local data = repo_data.data
  59. local host = repo_data.host
  60. -- generate package header
  61. local packagename = assert(data.name, "package name not found!"):lower()
  62. local packagefile = path.join("packages", string.sub(packagename, 1, 1), packagename, "xmake.lua")
  63. local file = io.open(packagefile, "w")
  64. -- define package and homepage
  65. file:print('package("%s")', packagename)
  66. local homepage = data.homepageUrl and data.homepageUrl ~= "" and data.homepageUrl or data.url
  67. if homepage then
  68. file:print(' set_homepage("%s")', homepage)
  69. end
  70. local description = data.description or ("The " .. packagename .. " package")
  71. file:print(' set_description("%s")', description)
  72. -- define license if available
  73. if type(data.licenseInfo) == "table" and data.licenseInfo.key then
  74. local licenses = {
  75. ["apache-2.0"] = "Apache-2.0",
  76. ["lgpl-2.0"] = "LGPL-2.0",
  77. ["lgpl-2.1"] = "LGPL-2.1",
  78. zlib = "zlib",
  79. mit = "MIT",
  80. }
  81. local license = licenses[data.licenseInfo.key]
  82. if license then
  83. file:print(' set_license("%s")', license)
  84. end
  85. end
  86. file:print("")
  87. -- define package URLs and versions
  88. local repodir
  89. local has_xmake, has_cmake, has_meson, has_bazel, has_autoconf, need_autogen
  90. local latest_release = data.latestRelease
  91. if type(latest_release) == "table" then
  92. local url = string.format("https://%s/%s/archive/refs/tags/%s.tar.gz", host, reponame, latest_release.tagName)
  93. local giturl = string.format("https://%s/%s.git", host, reponame)
  94. local tmpfile = os.tmpfile({ramdisk = false}) .. ".tar.gz"
  95. repodir = tmpfile .. ".dir"
  96. file:write(' add_urls("https://' .. host .. '/' .. reponame .. '/archive/refs/tags/$(version).tar.gz",\n')
  97. file:print(' "%s")\n', giturl)
  98. print("downloading %s", url)
  99. http.download(url, tmpfile)
  100. file:print(' add_versions("%s", "%s")', latest_release.tagName, hash.sha256(tmpfile))
  101. archive.extract(tmpfile, repodir)
  102. os.rm(tmpfile)
  103. else
  104. local giturl = string.format("https://%s/%s.git", host, reponame)
  105. repodir = os.tmpfile({ ramdisk = false })
  106. file:print(' add_urls("%s")', giturl)
  107. print("downloading %s", giturl)
  108. git.clone(giturl, { outputdir = repodir, depth = 1 })
  109. local commit = git.lastcommit({ repodir = repodir })
  110. local version = try {
  111. function()
  112. return os.iorunv("git", {
  113. "log",
  114. "-1",
  115. "--date=format:%Y.%m.%d",
  116. "--format=%ad",
  117. }, { curdir = repodir })
  118. end
  119. }
  120. if version then
  121. file:print(' add_versions("%s", "%s")', version:trim(), commit)
  122. end
  123. end
  124. local build_systems = {
  125. ["xmake.lua"] = {
  126. deps = {},
  127. priority = 1,
  128. install = function(configs, package)
  129. return [=[
  130. io.writefile("xmake.lua", [[
  131. add_rules("mode.release", "mode.debug")
  132. target("%s")
  133. set_kind("$(kind)")
  134. add_files("src/*.c")
  135. add_headerfiles("src/(*.h)")
  136. ]])
  137. if package:config("shared") then
  138. configs.kind = "shared"
  139. end
  140. import("package.tools.xmake").install(package, configs)]=]
  141. end,
  142. },
  143. ["CMakeLists.txt"] = {
  144. deps = {"cmake"},
  145. priority = 2,
  146. install = function(configs, package)
  147. return [[
  148. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  149. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  150. import("package.tools.cmake").install(package, configs)]]
  151. end,
  152. },
  153. ["configure,configure.ac,autogen.sh"] = {
  154. deps = {"autoconf", "automake", "libtool"},
  155. priority = 3,
  156. install = function(configs, package)
  157. return [[
  158. table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))
  159. if package:is_debug() then
  160. table.insert(configs, "--enable-debug")
  161. end
  162. import("package.tools.autoconf").install(package, configs)]]
  163. end,
  164. },
  165. ["meson.build"] = {
  166. deps = {"meson", "ninja"},
  167. priority = 4,
  168. install = function(configs, package)
  169. return [[
  170. table.insert(configs, "-Ddefault_library=" .. (package:config("shared") and "shared" or "static"))
  171. import("package.tools.meson").install(package, configs)]]
  172. end,
  173. },
  174. ["BUILD,BUILD.bazel"] = {
  175. deps = {"bazel"},
  176. priority = 5,
  177. install = function(configs, package)
  178. return [[
  179. import("package.tools.bazel").install(package, configs)]]
  180. end,
  181. }
  182. }
  183. -- detect build system
  184. local build_system_detected = {}
  185. if repodir then
  186. local files = os.files(path.join(repodir, "*")) or {}
  187. table.join2(files, os.files(path.join(repodir, "*", "*")))
  188. for _, file in ipairs(files) do
  189. local filename = path.filename(file)
  190. for k, v in pairs(build_systems) do
  191. local filenames = hashset.from(k:split(","))
  192. if filenames:has(filename) then
  193. table.insert(build_system_detected, v)
  194. end
  195. end
  196. end
  197. os.rm(repodir)
  198. end
  199. local build_system
  200. if #build_system_detected > 0 then
  201. table.sort(build_system_detected, function (a, b) return a.priority < b.priority end)
  202. build_system = build_system_detected[1]
  203. end
  204. if not build_system then
  205. build_system = build_systems["xmake.lua"]
  206. end
  207. -- add dependencies
  208. if build_system then
  209. local deps = table.wrap(build_system.deps)
  210. if deps and #deps > 0 then
  211. file:print('')
  212. file:print(' add_deps("' .. table.concat(deps, '", "') .. '")')
  213. end
  214. end
  215. -- generate install scripts
  216. file:print('')
  217. file:print(' on_install(function (package)')
  218. file:print(' local configs = {}')
  219. if build_system then
  220. file:print(build_system.install(configs, package))
  221. end
  222. file:print(' end)')
  223. -- generate test scripts
  224. file:print('')
  225. file:print(' on_test(function (package)')
  226. file:print(' assert(package:has_cfuncs("foo", {includes = "foo.h"}))')
  227. file:print(' end)')
  228. file:close()
  229. io.cat(packagefile)
  230. cprint("${bright}%s generated!", packagefile)
  231. end
  232. function main(...)
  233. local opt = option.parse(table.pack(...), options, "New a package.", "", "Usage: xmake l scripts/new.lua [options]")
  234. local repo = assert(opt.repo, "repository name must be set!")
  235. local reponame = repo:sub(8)
  236. if repo:startswith("github:") then
  237. generate_package(reponame, get_github_data)
  238. return
  239. end
  240. if repo:startswith("gitlab:") then
  241. generate_package(reponame, get_gitlab_data)
  242. return
  243. end
  244. raise("unsupported repository source. only 'github' and 'gitlab' are supported.")
  245. end