From 20cf6c946fc6c3a09205dc8d4e768ab9d2b001ca Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 15 Jan 2020 22:51:18 +0800 Subject: download resources for package --- .../require/impl/action/download_resources.lua | 84 +++++++++++++++++++++ xmake/actions/require/impl/action/install.lua | 10 ++- xmake/actions/require/impl/action/patch.lua | 85 ---------------------- .../actions/require/impl/action/patch_sources.lua | 85 ++++++++++++++++++++++ xmake/core/package/package.lua | 25 ++++++- 5 files changed, 200 insertions(+), 89 deletions(-) create mode 100644 xmake/actions/require/impl/action/download_resources.lua delete mode 100644 xmake/actions/require/impl/action/patch.lua create mode 100644 xmake/actions/require/impl/action/patch_sources.lua diff --git a/xmake/actions/require/impl/action/download_resources.lua b/xmake/actions/require/impl/action/download_resources.lua new file mode 100644 index 000000000..1d9675e32 --- /dev/null +++ b/xmake/actions/require/impl/action/download_resources.lua @@ -0,0 +1,84 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file download_resources.lua +-- + +-- imports +import("core.base.option") +import("net.http") +import("utils.archive") + +-- download resources +function _download(package, resource_name, resource_url, resource_hash) + + -- trace + vprint("downloading resource(%s: %s) to %s-%s ..", resource_name, resource_url, package:name(), package:version_str()) + + -- get the resource file + local resource_file = assert(package:resourcefile(resource_name), "invalid resource file!") + + -- the package file have been downloaded? + local cached = true + if option.get("force") or not os.isfile(resource_file) or resource_hash ~= hash.sha256(resource_file) then + + -- no cached + cached = false + + -- attempt to remove the previous file first + os.tryrm(resource_file) + + -- download the resource file + if resource_url:find(string.ipattern("https-://")) or resource_url:find(string.ipattern("ftps-://")) then + http.download(resource_url, resource_file) + else + raise("invalid resource url(%s)", resource_url) + end + + -- check hash + if resource_hash and resource_hash ~= hash.sha256(resource_file) then + raise("resource(%s): unmatched checksum!", resource_url) + end + end + + -- extract the resource file + local resourcedir = package:resourcedir(resource_name) + local resourcedir_tmp = resourcedir .. ".tmp" + os.tryrm(resourcedir_tmp) + if archive.extract(resource_file, resourcedir_tmp) then + os.tryrm(resourcedir) + os.mv(resourcedir_tmp, resourcedir) + else + os.tryrm(resourcedir_tmp) + end +end + +-- download all resources of the given package +function main(package) + + + -- no resources? + local resources = package:resources() + if not resources then + return + end + + -- download all resources + for name, resourceinfo in pairs(resources) do + _download(package, name, resourceinfo.url, resourceinfo.sha256) + end +end diff --git a/xmake/actions/require/impl/action/install.lua b/xmake/actions/require/impl/action/install.lua index 463a82949..ca0a45da2 100644 --- a/xmake/actions/require/impl/action/install.lua +++ b/xmake/actions/require/impl/action/install.lua @@ -23,7 +23,8 @@ import("core.base.option") import("core.project.target") import("lib.detect.find_file") import("test") -import("patch") +import("patch_sources") +import("download_resources") import(".utils.filter") -- patch pkgconfig if not exists @@ -153,8 +154,11 @@ function main(package) dep:envs_enter() end - -- patch it first - patch(package) + -- download package resources + download_resources(package) + + -- patch source codes of package + patch_sources(package) -- do install for i = 1, 3 do diff --git a/xmake/actions/require/impl/action/patch.lua b/xmake/actions/require/impl/action/patch.lua deleted file mode 100644 index 66340b8d8..000000000 --- a/xmake/actions/require/impl/action/patch.lua +++ /dev/null @@ -1,85 +0,0 @@ ---!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-2020, TBOOX Open Source Group. --- --- @author ruki --- @file patch.lua --- - --- imports -import("core.base.option") -import("net.http") -import("devel.git") - --- do patch -function _patch(package, patch_url, patch_hash) - - -- trace - vprint("patching %s to %s-%s ..", patch_url, package:name(), package:version_str()) - - -- get the patch file - local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), (path.filename(patch_url):gsub("%?.+$", ""))) - - -- the package file have been downloaded? - local cached = true - if option.get("force") or not os.isfile(patch_file) or patch_hash ~= hash.sha256(patch_file) then - - -- no cached - cached = false - - -- attempt to remove the previous file first - os.tryrm(patch_file) - - -- download the patch file - if patch_url:find(string.ipattern("https-://")) or patch_url:find(string.ipattern("ftps-://")) then - http.download(patch_url, patch_file) - else - -- copy the patch file - if os.isfile(patch_url) then - os.cp(patch_url, patch_file) - else - local scriptdir = package:scriptdir() - if scriptdir and os.isfile(path.join(scriptdir, patch_url)) then - os.cp(path.join(scriptdir, patch_url), patch_file) - else - raise("patch(%s): not found!", patch_url) - end - end - end - - -- check hash - if patch_hash and patch_hash ~= hash.sha256(patch_file) then - raise("patch(%s): unmatched checksum!", patch_url) - end - end - - -- apply the patch file - git.apply(patch_file) -end - --- patch the given package -function main(package) - - -- no patches? - local patches = package:patches() - if not patches then - return - end - - -- do all patches - for _, patchinfo in ipairs(patches) do - _patch(package, patchinfo.url, patchinfo.sha256) - end -end diff --git a/xmake/actions/require/impl/action/patch_sources.lua b/xmake/actions/require/impl/action/patch_sources.lua new file mode 100644 index 000000000..55c21abf3 --- /dev/null +++ b/xmake/actions/require/impl/action/patch_sources.lua @@ -0,0 +1,85 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file patch_sources.lua +-- + +-- imports +import("core.base.option") +import("net.http") +import("devel.git") + +-- do patch +function _patch(package, patch_url, patch_hash) + + -- trace + vprint("patching %s to %s-%s ..", patch_url, package:name(), package:version_str()) + + -- get the patch file + local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), (path.filename(patch_url):gsub("%?.+$", ""))) + + -- the package file have been downloaded? + local cached = true + if option.get("force") or not os.isfile(patch_file) or patch_hash ~= hash.sha256(patch_file) then + + -- no cached + cached = false + + -- attempt to remove the previous file first + os.tryrm(patch_file) + + -- download the patch file + if patch_url:find(string.ipattern("https-://")) or patch_url:find(string.ipattern("ftps-://")) then + http.download(patch_url, patch_file) + else + -- copy the patch file + if os.isfile(patch_url) then + os.cp(patch_url, patch_file) + else + local scriptdir = package:scriptdir() + if scriptdir and os.isfile(path.join(scriptdir, patch_url)) then + os.cp(path.join(scriptdir, patch_url), patch_file) + else + raise("patch(%s): not found!", patch_url) + end + end + end + + -- check hash + if patch_hash and patch_hash ~= hash.sha256(patch_file) then + raise("patch(%s): unmatched checksum!", patch_url) + end + end + + -- apply the patch file + git.apply(patch_file) +end + +-- patch the given package +function main(package) + + -- no patches? + local patches = package:patches() + if not patches then + return + end + + -- do all patches + for _, patchinfo in ipairs(patches) do + _patch(package, patchinfo.url, patchinfo.sha256) + end +end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index df9ba6435..83e8f82ba 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -939,7 +939,8 @@ function _instance:resources() resources = {} resourceinfos = table.wrap(resourceinfos) for idx = 1, #resourceinfos, 3 do - table.insert(resources, {name = resourceinfos[idx], url = resourceinfos[idx + 1], sha256 = resourceinfos[idx + 2]}) + local name = resourceinfos[idx] + resources[name] = {url = resourceinfos[idx + 1], sha256 = resourceinfos[idx + 2]} end end end @@ -948,6 +949,28 @@ function _instance:resources() return resources and resources or nil end +-- get the the given resource +function _instance:resource(name) + local resources = self:resources() + return resources and resources[name] or nil +end + +-- get the the given resource file +function _instance:resourcefile(name) + local resource = self:resource(name) + if resource and resource.url then + return path.join(self:cachedir(), "resources", name, (path.filename(resource.url):gsub("%?.+$", ""))) + end +end + +-- get the the given resource directory +function _instance:resourcedir(name) + local resource = self:resource(name) + if resource and resource.url then + return path.join(self:cachedir(), "resources", name, (path.filename(resource.url):gsub("%?.+$", "")) .. ".dir") + end +end + -- has the given c funcs? -- -- @param funcs the funcs -- cgit v1.3.1