diff options
| author | ruki <[email protected]> | 2019-03-21 21:02:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-03-21 00:13:18 +0800 |
| commit | 858ac89b6ade76ea04701bbd6a610a9abe9a0638 (patch) | |
| tree | b41ca0dd6d7c254aeec995f97c591d87b05249d5 | |
| parent | 00bbf677845e44ff3def527d7c5e538e02d11d1e (diff) | |
improve to clean and scan packages
| -rw-r--r-- | xmake/actions/require/clean.lua | 102 | ||||
| -rw-r--r-- | xmake/actions/require/main.lua | 10 | ||||
| -rw-r--r-- | xmake/actions/require/scan.lua | 90 | ||||
| -rw-r--r-- | xmake/actions/require/xmake.lua | 7 |
4 files changed, 170 insertions, 39 deletions
diff --git a/xmake/actions/require/clean.lua b/xmake/actions/require/clean.lua index cb69d85a4..b646baa8d 100644 --- a/xmake/actions/require/clean.lua +++ b/xmake/actions/require/clean.lua @@ -27,47 +27,41 @@ import("core.base.option") import("core.project.cache") import("core.package.package") --- clean all installed package caches -function main() +-- clear the unused or invalid package directories +function _clear_packagedirs(packagedir) - -- trace - print("clear all package caches ..") - - -- clear cache directory - os.rm(package.cachedir()) - - -- clear require cache - local require_cache = cache("local.require") - require_cache:clear() - require_cache:flush() - - -- trace - print("clear all unused packages ..") - - -- clear all unused packages - local installdir = package.installdir() - for _, references_file in ipairs(os.files(path.join(installdir, "*", "*", "*", "*", "references.txt"))) do - local references = io.load(references_file) - if references then - local found = false - for projectdir, refdate in pairs(references) do - if os.isdir(projectdir) then - found = true - break + -- 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 - if not found then - - -- get package directory - local packagedir = path.directory(references_file) - print("remove %s ..", packagedir) - - -- get confirm + 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 confirm = option.get("yes") if confirm == nil then -- show tips - cprint("${bright color.warning}note: ${clear}no projects are using this package, remove it (pass -y to skip confirm)?") + cprint("${bright color.warning}note: ${clear}remove this ${magenta}%s-%s${clear}/${yellow}%s${clear} (${red}%s${clear}) (pass -y to skip confirm)?", package_name, version, hash, status) cprint("please input: y (y/n)") -- get answer @@ -78,10 +72,48 @@ function main() end end if confirm then - os.rm(packagedir) + 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 + +-- clean the given or all package caches +function main(package_names) + + -- trace + print("clearing caches ..") + + -- clear cache directory + os.rm(package.cachedir()) + + -- clear require cache + local require_cache = cache("local.require") + require_cache:clear() + require_cache:flush() + + -- trace + 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 end diff --git a/xmake/actions/require/main.lua b/xmake/actions/require/main.lua index 6311434cc..9ac5a4004 100644 --- a/xmake/actions/require/main.lua +++ b/xmake/actions/require/main.lua @@ -29,6 +29,7 @@ import("core.project.config") import("core.project.project") import("core.platform.platform") import("list") +import("scan") import("info") import("clean") import("search") @@ -76,7 +77,7 @@ function main() -- clean all installed packages cache if option.get("clean") then - clean(option.get("global")) + clean(option.get("requires")) -- search for the given packages from repositories elseif option.get("search") then @@ -93,11 +94,16 @@ function main() info(option.get("requires")) - -- list all package dependencies + -- list all package dependencies in project elseif option.get("list") then list() + -- scan the given or all packages + elseif option.get("scan") then + + scan(option.get("requires")) + -- install and update all outdated package dependencies by default if no arguments else install(option.get("requires")) diff --git a/xmake/actions/require/scan.lua b/xmake/actions/require/scan.lua new file mode 100644 index 000000000..12100023b --- /dev/null +++ b/xmake/actions/require/scan.lua @@ -0,0 +1,90 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional scanrmation +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file scan.lua +-- + +-- imports +import("core.base.option") +import("core.package.package") + +-- scan local package +function _scan_package(packagedir) + + -- show packages + local package_name = path.filename(packagedir) + for _, versiondir in ipairs(os.dirs(path.join(packagedir, "*"))) do + local version = path.filename(versiondir) + cprint("${magenta}%s-%s${clear}:", package_name, version) + + -- show package hash + 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 manifest = os.isfile(manifest_file) and io.load(manifest_file) or nil + cprintf(" -> ${yellow}%s${clear}:", hash) + if os.emptydir(hashdir) then + cprintf(" ${red}empty${clear}") + elseif not referenced then + cprintf(" ${red}unused${clear}") + elseif not manifest then + cprintf(" ${red}invalid${clear}") + end + print("") + if manifest and manifest.configs then + print(" -> %s", string.serialize(manifest.configs, true)) + end + end + end +end + +-- scan local packages +function main(package_names) + + -- trace + print("scanning packages ..") + + -- scan 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 + _scan_package(packagedir) + end + end + else + for _, packagedir in ipairs(os.dirs(path.join(installdir, "*", "*"))) do + _scan_package(packagedir) + end + end +end + diff --git a/xmake/actions/require/xmake.lua b/xmake/actions/require/xmake.lua index 85c84af2d..87cea29bc 100644 --- a/xmake/actions/require/xmake.lua +++ b/xmake/actions/require/xmake.lua @@ -45,7 +45,10 @@ task("require") -- options , options = { - {'c', "clean", "k", nil, "Clear all package caches and uninstall all not-referenced packages." } + {'c', "clean", "k", nil, "Clear all package caches and uninstall all not-referenced packages.", + "e.g.", + " $ xmake require --clean", + " $ xmake require --clean zlib tbox pcr*" } , {'f', "force", "k", nil, "Force to reinstall all package dependencies." } , {'l', "list", "k", nil, "List all package dependencies in project.", "e.g.", @@ -53,7 +56,7 @@ task("require") , {nil, "scan", "k", nil, "Scan the given or all installed packages.", "e.g.", " $ xmake require --scan", - " $ xmake require --scan zlib tbox" } + " $ xmake require --scan zlib tbox pcr*" } , { } , {nil, "info", "k", nil, "Show the given package info.", "e.g.", |
