summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-23 23:53:52 +0800
committerruki <[email protected]>2021-02-23 23:53:52 +0800
commit10460870888ae181b7e9d0937063b789451e9c70 (patch)
treeb40b573f0d6c71772b782fcd2e438a41c538aff2
parent789015d123d0492195b28966dd13e416bccd6e57 (diff)
support xrepo remove --all
-rw-r--r--xmake/modules/private/action/require/clean.lua60
-rw-r--r--xmake/modules/private/action/require/impl/remove_packages.lua93
-rw-r--r--xmake/modules/private/xrepo/action/remove.lua11
3 files changed, 105 insertions, 59 deletions
diff --git a/xmake/modules/private/action/require/clean.lua b/xmake/modules/private/action/require/clean.lua
index fb93e6304..57b549810 100644
--- a/xmake/modules/private/action/require/clean.lua
+++ b/xmake/modules/private/action/require/clean.lua
@@ -22,52 +22,7 @@
import("core.base.option")
import("core.package.package")
import("core.cache.localcache")
-
--- clear the unused or invalid package directories
-function _clear_packagedirs(packagedir)
-
- -- clear them
- local package_name = path.filename(packagedir)
- for _, versiondir in ipairs(os.dirs(path.join(packagedir, "*"))) do
- local version = path.filename(versiondir)
- for _, hashdir in ipairs(os.dirs(path.join(versiondir, "*"))) do
- local hash = path.filename(hashdir)
- local references_file = path.join(hashdir, "references.txt")
- local referenced = false
- local references = os.isfile(references_file) and io.load(references_file) or nil
- if references then
- for projectdir, refdate in pairs(references) do
- if os.isdir(projectdir) then
- referenced = true
- break
- end
- end
- end
- local manifest_file = path.join(hashdir, "manifest.txt")
- local status = nil
- if os.emptydir(hashdir) then
- status = "empty"
- elseif not referenced then
- status = "unused"
- elseif not os.isfile(manifest_file) then
- status = "invalid"
- end
- if status then
- local description = string.format("remove this ${magenta}%s-%s${clear}/${yellow}%s${clear} (${red}%s${clear})", package_name, version, hash, status)
- local confirm = utils.confirm({default = true, description = description})
- if confirm then
- os.rm(hashdir)
- end
- end
- end
- if os.emptydir(versiondir) then
- os.rm(versiondir)
- end
- end
- if os.emptydir(packagedir) then
- os.rm(packagedir)
- end
-end
+import("private.action.require.impl.remove_packages")
-- clean the given or all package caches
function main(package_names)
@@ -76,18 +31,7 @@ function main(package_names)
print("clearing packages ..")
-- clear all unused packages
- local installdir = package.installdir()
- if package_names then
- for _, package_name in ipairs(package_names) do
- for _, packagedir in ipairs(os.dirs(path.join(installdir, package_name:sub(1, 1), package_name))) do
- _clear_packagedirs(packagedir)
- end
- end
- else
- for _, packagedir in ipairs(os.dirs(path.join(installdir, "*", "*"))) do
- _clear_packagedirs(packagedir)
- end
- end
+ remove_packages(package_names, {clean = true})
-- trace
print("clearing caches ..")
diff --git a/xmake/modules/private/action/require/impl/remove_packages.lua b/xmake/modules/private/action/require/impl/remove_packages.lua
new file mode 100644
index 000000000..f2248fb16
--- /dev/null
+++ b/xmake/modules/private/action/require/impl/remove_packages.lua
@@ -0,0 +1,93 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file remove_packages.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.package.package")
+import("core.cache.localcache")
+
+-- remove package directories
+function _remove_packagedirs(packagedir, opt)
+
+ -- clear them
+ local package_name = path.filename(packagedir)
+ for _, versiondir in ipairs(os.dirs(path.join(packagedir, "*"))) do
+ local version = path.filename(versiondir)
+ for _, hashdir in ipairs(os.dirs(path.join(versiondir, "*"))) do
+ local hash = path.filename(hashdir)
+ local references_file = path.join(hashdir, "references.txt")
+ local referenced = false
+ local references = os.isfile(references_file) and io.load(references_file) or nil
+ if references then
+ for projectdir, refdate in pairs(references) do
+ if os.isdir(projectdir) then
+ referenced = true
+ break
+ end
+ end
+ end
+ local manifest_file = path.join(hashdir, "manifest.txt")
+ local status = nil
+ if os.emptydir(hashdir) then
+ status = "empty"
+ elseif not referenced then
+ status = "unused"
+ elseif not os.isfile(manifest_file) then
+ status = "invalid"
+ end
+ if not opt.clean or status then
+ local description = string.format("remove this ${magenta}%s-%s${clear}/${yellow}%s${clear} (${red}%s${clear})", package_name, version, hash, status and status or "used")
+ local confirm = utils.confirm({default = true, description = description})
+ if confirm then
+ os.rm(hashdir)
+ end
+ end
+ end
+ if os.emptydir(versiondir) then
+ os.rm(versiondir)
+ end
+ end
+ if os.emptydir(packagedir) then
+ os.rm(packagedir)
+ end
+end
+
+-- remove the given or all packages
+--
+-- @param package_names the package names list, support lua pattern
+-- @param opt the options, only clean unused packages if pass `{clean = true}`
+--
+function main(package_names, opt)
+ opt = opt or {}
+ local installdir = package.installdir()
+ if package_names then
+ for _, package_name in ipairs(package_names) do
+ for _, packagedir in ipairs(os.dirs(path.join(installdir, package_name:sub(1, 1), package_name))) do
+ _remove_packagedirs(packagedir, opt)
+ end
+ end
+ else
+ for _, packagedir in ipairs(os.dirs(path.join(installdir, "*", "*"))) do
+ _remove_packagedirs(packagedir, opt)
+ end
+ end
+end
+
+
diff --git a/xmake/modules/private/xrepo/action/remove.lua b/xmake/modules/private/xrepo/action/remove.lua
index 4a6446c91..7624fa9cf 100644
--- a/xmake/modules/private/xrepo/action/remove.lua
+++ b/xmake/modules/private/xrepo/action/remove.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.option")
+import("private.action.require.impl.remove_packages", {alias = "remove_all_packages"})
-- get menu options
function menu_options()
@@ -41,6 +42,12 @@ function menu_options()
" - xrepo remove -f \"vs_runtime=MD\" zlib",
" - xrepo remove -f \"regex=true,thread=true\" boost"},
{},
+ {nil, "all", "k", nil, "Remove all packages and ignore extra package configs.",
+ "If `--all` is enabled, the package name parameter will support lua pattern",
+ "e.g.",
+ " - xrepo remove --all",
+ " - xrepo remove --all zlib boost",
+ " - xrepo remove --all zl* boo*"},
{nil, "packages", "vs", nil, "The packages list.",
"e.g.",
" - xrepo remove zlib boost",
@@ -147,7 +154,9 @@ end
-- main entry
function main()
local packages = option.get("packages")
- if packages then
+ if option.get("all") then
+ remove_all_packages(packages)
+ elseif packages then
_remove_packages(packages)
else
raise("please specify the packages to be removed.")