summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-01-15 22:51:18 +0800
committerruki <[email protected]>2020-01-15 11:07:48 +0800
commit20cf6c946fc6c3a09205dc8d4e768ab9d2b001ca (patch)
tree47b976574dc68d456537d45504c9afc41f9a3d52
parenteb1effdccd96425375af083d45a94a790f0c6d56 (diff)
download resources for package
-rw-r--r--xmake/actions/require/impl/action/download_resources.lua84
-rw-r--r--xmake/actions/require/impl/action/install.lua10
-rw-r--r--xmake/actions/require/impl/action/patch_sources.lua (renamed from xmake/actions/require/impl/action/patch.lua)2
-rw-r--r--xmake/core/package/package.lua25
4 files changed, 116 insertions, 5 deletions
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_sources.lua
index 66340b8d8..55c21abf3 100644
--- a/xmake/actions/require/impl/action/patch.lua
+++ b/xmake/actions/require/impl/action/patch_sources.lua
@@ -15,7 +15,7 @@
-- Copyright (C) 2015-2020, TBOOX Open Source Group.
--
-- @author ruki
--- @file patch.lua
+-- @file patch_sources.lua
--
-- imports
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