From 3834432a2fa50afc439288a4f7862261aedf41ae Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 25 Aug 2018 00:51:54 +0800 Subject: improve to load repo and package --- xmake/actions/require/package.lua | 8 ++-- xmake/actions/require/repository.lua | 55 +++++----------------- xmake/actions/require/search.lua | 3 +- xmake/core/package/package.lua | 12 ++++- xmake/core/package/repository.lua | 43 ++++++++--------- .../modules/import/core/package/package.lua | 4 +- .../modules/import/core/package/repository.lua | 8 ++++ xmake/packages/cmake/xmake.lua | 19 -------- xmake/packages/git/xmake.lua | 40 ---------------- xmake/plugins/repo/main.lua | 12 +++-- xmake/repository/packages/cmake/xmake.lua | 19 ++++++++ xmake/repository/packages/git/xmake.lua | 40 ++++++++++++++++ xmake/repository/xmake.lua | 3 ++ 13 files changed, 131 insertions(+), 135 deletions(-) delete mode 100644 xmake/packages/cmake/xmake.lua delete mode 100644 xmake/packages/git/xmake.lua create mode 100644 xmake/repository/packages/cmake/xmake.lua create mode 100644 xmake/repository/packages/git/xmake.lua create mode 100644 xmake/repository/xmake.lua diff --git a/xmake/actions/require/package.lua b/xmake/actions/require/package.lua index e48ce37fc..f111470ae 100644 --- a/xmake/actions/require/package.lua +++ b/xmake/actions/require/package.lua @@ -150,10 +150,10 @@ end function _load_package_from_repository(packagename, reponame) -- get package directory from the given package name - local packagedir, is_global = repository.packagedir(packagename, reponame) + local packagedir, repo = repository.packagedir(packagename, reponame) if packagedir then -- load it - return core_package.load_from_repository(packagename, is_global, packagedir) + return core_package.load_from_repository(packagename, repo, packagedir) end end @@ -219,8 +219,8 @@ function _search_package_from_repository(name) -- search package directories from the given package name local packages = {} - for packagename, packageinfo in pairs(repository.searchdirs(name)) do - local package = core_package.load_from_repository(packagename, packageinfo.is_global, packageinfo.packagedir) + for _, packageinfo in ipairs(repository.searchdirs(name)) do + local package = core_package.load_from_repository(packageinfo.name, packageinfo.repo, packageinfo.packagedir) if package then table.insert(packages, package) end diff --git a/xmake/actions/require/repository.lua b/xmake/actions/require/repository.lua index 39ccd7e50..371e1b012 100644 --- a/xmake/actions/require/repository.lua +++ b/xmake/actions/require/repository.lua @@ -63,39 +63,12 @@ function packagedir(packagename, reponame) return foundir[1], foundir[2] end - -- find the package directory from the given repository - if reponame then - for _, from in ipairs({"local", "global"}) do - local is_global = (from == "global") - for _, repodir in ipairs(repository.directory(is_global)) do - local dir = path.join(repodir, reponame, "packages", (packagename:gsub('%.', path.seperator()))) - if os.isdir(dir) then - foundir = {dir, is_global} - break - end - end - if foundir then - break - end - end - else - -- find the package directory from all repositories - for _, repo in ipairs(repositories()) do - - -- the package directory - local dir = path.join(repo:directory(), "packages", (packagename:gsub('%.', path.seperator()))) - if os.isdir(dir) then - foundir = {dir, repo.global} - break - end - end - end - - -- not found? find this package from the builtin packages directory - if not foundir then - local dir = path.join(os.programdir(), "packages", (packagename:gsub('%.', path.seperator()))) - if os.isdir(dir) then - foundir = {dir, true} + -- find the package directory from repositories + for _, repo in ipairs(repositories()) do + local dir = path.join(repo:directory(), "packages", (packagename:gsub('%.', path.seperator()))) + if os.isdir(dir) and (not reponame or reponame == repo:name()) then + foundir = {dir, repo} + break end end @@ -120,20 +93,18 @@ function searchdirs(name) local subdirs = "**" .. table.concat(name:split('%.'), "*" .. path.seperator() .. "*") .. "*" -- find the package directories from all repositories + local unique = {} local packageinfos = {} for _, repo in ipairs(repositories()) do - - -- the package directory pattern - for _, file in ipairs(os.files(path.join(repository.directory(repo.global), repo.name, "packages", subdirs, "xmake.lua"))) do - packageinfos[path.basename(path.directory(file))] = {is_global = repo.global, packagedir = path.directory(file)} + for _, file in ipairs(os.files(path.join(repo:directory(), "packages", subdirs, "xmake.lua"))) do + local packagename = path.basename(path.directory(file)) + if not unique[packagename] then + table.insert(packageinfos, {name = packagename, repo = repo, packagedir = path.directory(file)}) + unique[packagename] = true + end end end - -- search the package directories from the builtin packages directory - for _, file in ipairs(os.files(path.join(os.programdir(), "packages", subdirs, "xmake.lua"))) do - packageinfos[path.basename(path.directory(file))] = {is_global = repo.global, packagedir = path.directory(file)} - end - -- ok? return packageinfos end diff --git a/xmake/actions/require/search.lua b/xmake/actions/require/search.lua index 53fee8220..1397e2aff 100644 --- a/xmake/actions/require/search.lua +++ b/xmake/actions/require/search.lua @@ -57,7 +57,8 @@ function main(names) -- show packages for _, instance in ipairs(packages) do - cprint(" -> ${magenta}%s${clear}: %s", instance:fullname(), instance:get("description") or "") + local repo = instance:repo() + cprint(" -> ${magenta}%s${clear}: %s %s", instance:fullname(), instance:get("description") or "", repo and ("(in " .. repo:name() .. ")") or "") end end end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index f26884f58..24f15a2f7 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -84,6 +84,11 @@ function _instance:name() return self._NAME end +-- get the repository of this package +function _instance:repo() + return self._REPO +end + -- get the package alias function _instance:alias() local requireinfo = self:requireinfo() @@ -545,7 +550,7 @@ function package.load_from_project(packagename, project) end -- load the package from the package directory or package description file -function package.load_from_repository(packagename, is_global, packagedir, packagefile) +function package.load_from_repository(packagename, repo, packagedir, packagefile) -- get it directly from cache first package._PACKAGES = package._PACKAGES or {} @@ -587,8 +592,11 @@ function package.load_from_repository(packagename, is_global, packagedir, packag return nil, errors end + -- save repository + instance._REPO = repo + -- mark as global/project package? - instance._FROMKIND = utils.ifelse(is_global, "global", "local") + instance._FROMKIND = repo:is_global() and "global" or "local" -- save instance to the cache package._PACKAGES[packagename] = instance diff --git a/xmake/core/package/repository.lua b/xmake/core/package/repository.lua index 88affe853..fdce0b7af 100644 --- a/xmake/core/package/repository.lua +++ b/xmake/core/package/repository.lua @@ -35,14 +35,13 @@ local config = require("project/config") local interpreter = require("base/interpreter") -- new an instance -function _instance.new(name, info, url, directory, is_global) +function _instance.new(name, url, directory, is_global) -- new an instance local instance = table.inherit(_instance) -- init instance instance._NAME = name - instance._INFO = info instance._URL = url instance._DIRECTORY = directory instance._IS_GLOBAL = is_global @@ -56,9 +55,26 @@ function _instance:get(name) -- the info local info = self._INFO + if not info then - -- get if from info first - local value = info[name] + -- attempt to load info from the repository script (xmake.lua) + local scriptpath = path.join(self:directory(), "xmake.lua") + if os.isfile(scriptpath) then + + -- load repository and disable filter + local results, errors = repository._interpreter():load(scriptpath, nil, true, false) + if not results and os.isfile(scriptpath) then + os.raise(errors) + end + + -- save repository info + info = results + self._INFO = info + end + end + + -- get if from info + local value = info and info[name] or nil if value ~= nil then return value end @@ -160,25 +176,10 @@ function repository.load(name, url, is_global) end -- the repository directory - local repodir = path.join(repository.directory(is_global), name) - - -- get the repository script path - local repoinfo = {} - local scriptpath = path.join(repodir, "xmake.lua") - if os.isfile(scriptpath) then - - -- load repository and disable filter - local results, errors = repository._interpreter():load(scriptpath, nil, true, false) - if not results and os.isfile(scriptpath) then - return nil, errors - end - - -- save repository info - repoinfo = results - end + local repodir = os.isdir(url) and url or path.join(repository.directory(is_global), name) -- new an instance - local instance, errors = _instance.new(name, repoinfo, url, repodir, is_global) + local instance, errors = _instance.new(name, url, repodir, is_global) if not instance then return nil, errors end diff --git a/xmake/core/sandbox/modules/import/core/package/package.lua b/xmake/core/sandbox/modules/import/core/package/package.lua index 5a507e20d..39da91a3f 100644 --- a/xmake/core/sandbox/modules/import/core/package/package.lua +++ b/xmake/core/sandbox/modules/import/core/package/package.lua @@ -67,10 +67,10 @@ function sandbox_core_package_package.load_from_system(packagename) end -- load the package from repositories -function sandbox_core_package_package.load_from_repository(packagename, is_global, packagedir, packagefile) +function sandbox_core_package_package.load_from_repository(packagename, repo, packagedir, packagefile) -- load package instance - local instance, errors = package.load_from_repository(packagename, is_global, packagedir, packagefile) + local instance, errors = package.load_from_repository(packagename, repo, packagedir, packagefile) if not instance then raise(errors) end diff --git a/xmake/core/sandbox/modules/import/core/package/repository.lua b/xmake/core/sandbox/modules/import/core/package/repository.lua index 2fc09e22b..16fab448a 100644 --- a/xmake/core/sandbox/modules/import/core/package/repository.lua +++ b/xmake/core/sandbox/modules/import/core/package/repository.lua @@ -121,6 +121,14 @@ function sandbox_core_package_repository.repositories(is_global) end end + -- load repository from builtin program directory + if is_global then + local repo = repository.load("builtin-repo", path.join(os.programdir(), "repository"), true) + if repo then + table.insert(repositories, repo) + end + end + -- get the repositories return repositories end diff --git a/xmake/packages/cmake/xmake.lua b/xmake/packages/cmake/xmake.lua deleted file mode 100644 index c78f590fd..000000000 --- a/xmake/packages/cmake/xmake.lua +++ /dev/null @@ -1,19 +0,0 @@ -package("cmake") - - set_kind("binary") - set_homepage("https://cmake.org") - set_description("A cross-platform family of tool designed to build, test and package software") - add_imports("package.manager.install") - - on_build(function (package) - end) - - on_install(function (package) - install("cmake") - end) - - on_install("windows", function (package) - -- the winenv with cmake has been installed when installing git - raise() - end) - diff --git a/xmake/packages/git/xmake.lua b/xmake/packages/git/xmake.lua deleted file mode 100644 index f6bbe8102..000000000 --- a/xmake/packages/git/xmake.lua +++ /dev/null @@ -1,40 +0,0 @@ -package("git") - - set_kind("binary") - set_homepage("https://git-scm.com/") - set_description("A free and open source distributed version control system") - - if os.host() == "windows" then - if os.arch() == "x64" then - add_urls("https://github.com/tboox/xmake-win64env/archive/$(version).zip", {alias = "github"}) - add_urls("https://gitlab.com/tboox/xmake-win64env/-/archive/$(version)/xmake-win64env-$(version).zip", {alias = "gitlab"}) - add_versions("github:v1.0.1", "95170b1d144ebb30961611fd87b1e9404bbe37d2d904adb15f3e202bb3e19c21") - add_versions("gitlab:v1.0.1", "00ec9a71d34725fb974fcdef3e6b21104a0b1681d0cd7d07ee64d4a73eed0c87") - else - add_urls("https://github.com/tboox/xmake-win32env/archive/$(version).zip", {alias = "github"}) - add_urls("https://gitlab.com/tboox/xmake-win32env/-/archive/$(version)/xmake-win32env-$(version).zip", {alias = "gitlab"}) - add_versions("github:v1.0.1", "3c46983379329a246596a2b5f0ff971b55e3eccbbfd34272e7121e13fb346db5") - add_versions("gitlab:v1.0.1", "c9bd70a5394b9d943607268f876b81d29c360f7f2860375cec1e4146b44023bf") - end - else - add_imports("package.manager.install") - end - - on_build(function (package) - end) - - on_install(function (package) - install("git") - end) - - on_install("windows", function (package) - - -- install winenv with git - local winenv_dir = path.translate("~/.xmake/winenv") - os.mkdir(winenv_dir) - os.cp("*", winenv_dir) - - -- load winenv - import("winenv", {rootdir = winenv_dir})(winenv_dir) - end) - diff --git a/xmake/plugins/repo/main.lua b/xmake/plugins/repo/main.lua index 25a405f2e..91c64225c 100644 --- a/xmake/plugins/repo/main.lua +++ b/xmake/plugins/repo/main.lua @@ -100,12 +100,16 @@ function _update() -- remove repeat and only pull the first repository if not pulled[repodir] then if os.isdir(repodir) then + + -- update the local repository with the remote url + if not os.isdir(repo:url()) then - -- trace - vprint("pulling repository(%s): %s to %s ..", repo:name(), repo:url(), repodir) + -- trace + vprint("pulling repository(%s): %s to %s ..", repo:name(), repo:url(), repodir) - -- pull it - git.pull({verbose = option.get("verbose"), branch = "master", repodir = repodir}) + -- pull it + git.pull({verbose = option.get("verbose"), branch = "master", repodir = repodir}) + end else -- trace vprint("cloning repository(%s): %s to %s ..", repo:name(), repo:url(), repodir) diff --git a/xmake/repository/packages/cmake/xmake.lua b/xmake/repository/packages/cmake/xmake.lua new file mode 100644 index 000000000..c78f590fd --- /dev/null +++ b/xmake/repository/packages/cmake/xmake.lua @@ -0,0 +1,19 @@ +package("cmake") + + set_kind("binary") + set_homepage("https://cmake.org") + set_description("A cross-platform family of tool designed to build, test and package software") + add_imports("package.manager.install") + + on_build(function (package) + end) + + on_install(function (package) + install("cmake") + end) + + on_install("windows", function (package) + -- the winenv with cmake has been installed when installing git + raise() + end) + diff --git a/xmake/repository/packages/git/xmake.lua b/xmake/repository/packages/git/xmake.lua new file mode 100644 index 000000000..f6bbe8102 --- /dev/null +++ b/xmake/repository/packages/git/xmake.lua @@ -0,0 +1,40 @@ +package("git") + + set_kind("binary") + set_homepage("https://git-scm.com/") + set_description("A free and open source distributed version control system") + + if os.host() == "windows" then + if os.arch() == "x64" then + add_urls("https://github.com/tboox/xmake-win64env/archive/$(version).zip", {alias = "github"}) + add_urls("https://gitlab.com/tboox/xmake-win64env/-/archive/$(version)/xmake-win64env-$(version).zip", {alias = "gitlab"}) + add_versions("github:v1.0.1", "95170b1d144ebb30961611fd87b1e9404bbe37d2d904adb15f3e202bb3e19c21") + add_versions("gitlab:v1.0.1", "00ec9a71d34725fb974fcdef3e6b21104a0b1681d0cd7d07ee64d4a73eed0c87") + else + add_urls("https://github.com/tboox/xmake-win32env/archive/$(version).zip", {alias = "github"}) + add_urls("https://gitlab.com/tboox/xmake-win32env/-/archive/$(version)/xmake-win32env-$(version).zip", {alias = "gitlab"}) + add_versions("github:v1.0.1", "3c46983379329a246596a2b5f0ff971b55e3eccbbfd34272e7121e13fb346db5") + add_versions("gitlab:v1.0.1", "c9bd70a5394b9d943607268f876b81d29c360f7f2860375cec1e4146b44023bf") + end + else + add_imports("package.manager.install") + end + + on_build(function (package) + end) + + on_install(function (package) + install("git") + end) + + on_install("windows", function (package) + + -- install winenv with git + local winenv_dir = path.translate("~/.xmake/winenv") + os.mkdir(winenv_dir) + os.cp("*", winenv_dir) + + -- load winenv + import("winenv", {rootdir = winenv_dir})(winenv_dir) + end) + diff --git a/xmake/repository/xmake.lua b/xmake/repository/xmake.lua new file mode 100644 index 000000000..01c381ed3 --- /dev/null +++ b/xmake/repository/xmake.lua @@ -0,0 +1,3 @@ + +-- set repository description +set_description("The official builtin package repository of xmake") -- cgit v1.3.1