Skip to content

Commit

Permalink
Merge pull request #4956 from xmake-io/archive
Browse files Browse the repository at this point in the history
Improve archive
  • Loading branch information
waruqi authored Apr 10, 2024
2 parents 7e7b37e + bcc361f commit b559b4c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
16 changes: 5 additions & 11 deletions core/xpack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,12 @@ xpack("xmake")
local winenv_bindir = path.join(package:buildir(), "winenv", "bin")
os.mkdir(winenv_bindir)
http.download(url_7z, archive_7z, {insecure = global.get("insecure-ssl")})
if archive.extract(archive_7z, tmpdir_7z) then
os.cp(path.join(tmpdir_7z, "*"), winenv_bindir)
else
raise("extract 7z.zip failed!")
end
archive.extract(archive_7z, tmpdir_7z)
os.cp(path.join(tmpdir_7z, "*"), winenv_bindir)
http.download(url_curl, archive_curl, {insecure = global.get("insecure-ssl")})
if archive.extract(archive_curl, tmpdir_curl) then
os.cp(path.join(tmpdir_curl, "*", "bin", "*.exe"), winenv_bindir)
os.cp(path.join(tmpdir_curl, "*", "bin", "*.crt"), winenv_bindir)
else
raise("extract curl.zip failed!")
end
archive.extract(archive_curl, tmpdir_curl)
os.cp(path.join(tmpdir_curl, "*", "bin", "*.exe"), winenv_bindir)
os.cp(path.join(tmpdir_curl, "*", "bin", "*.crt"), winenv_bindir)
winenv = path.directory(winenv_bindir)
package:add("installfiles", path.join(winenv, "**"), {rootdir = path.directory(winenv)})
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ function _download(package, url, sourcedir, opt)
local sourcedir_tmp = sourcedir .. ".tmp"
os.rm(sourcedir_tmp)
local extension = archive.extension(packagefile)
if archive.extract(packagefile, sourcedir_tmp, {excludes = opt.url_excludes}) then
local ok = try {function() archive.extract(packagefile, sourcedir_tmp, {excludes = opt.url_excludes}); return true end}
if ok then
-- move to source directory and we skip it to avoid long path issues on windows if only one root directory
os.rm(sourcedir)
local filedirs = os.filedirs(path.join(sourcedir_tmp, "*"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ function _download(package, resource_name, resource_url, resource_hash)
local resourcedir_tmp = resourcedir .. ".tmp"
os.tryrm(resourcedir_tmp)
local extension = archive.extension(resource_file)
if archive.extract(resource_file, resourcedir_tmp) then
local ok = try {function() archive.extract(resource_file, resourcedir_tmp); return true end}
if ok then
os.tryrm(resourcedir)
os.mv(resourcedir_tmp, resourcedir)
elseif extension and extension ~= "" then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ function _patch(package, patchinfo)
local patchdir = patch_file .. ".dir"
local patchdir_tmp = patchdir .. ".tmp"
os.tryrm(patchdir_tmp)
if archive.extract(patch_file, patchdir_tmp) then
local ok = try {function() archive.extract(patch_file, patchdir_tmp); return true end}
if ok then
os.tryrm(patchdir)
os.mv(patchdir_tmp, patchdir)
else
Expand Down
23 changes: 16 additions & 7 deletions xmake/modules/utils/archive/archive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,25 @@ end

-- archive archive file using archivers
function _archive(archivefile, inputfiles, extension, archivers, opt)
local errors
for _, archive in ipairs(archivers) do
if archive(archivefile, inputfiles, extension, opt) then
local ok = try {
function ()
return archive(archivefile, inputfiles, extension, opt)
end,
catch {
function (errs)
if errs then
errors = tostring(errs)
end
end
}
}
if ok then
return true
end
end
return false
raise("cannot archive %s, %s!", path.filename(archivefile), errors or "archivers not found!")
end

-- only archive tar file
Expand All @@ -306,12 +319,8 @@ end
-- @param options the options, e.g.. {curdir = "/tmp", recurse = true, compress = "fastest|faster|default|better|best", excludes = {"*/dir/*", "dir/*"}}
--
function main(archivefile, inputfiles, opt)

-- init inputfiles
inputfiles = inputfiles or os.curdir()

-- init options
opt = opt or {}
inputfiles = inputfiles or os.curdir()
if opt.recurse == nil then
opt.recurse = true
end
Expand Down
22 changes: 15 additions & 7 deletions xmake/modules/utils/archive/extract.lua
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,25 @@ end

-- extract archive file using extractors
function _extract(archivefile, outputdir, extension, extractors, opt)
local errors
for _, extract in ipairs(extractors) do
local ok = try {function () return extract(archivefile, outputdir, extension, opt) end}
local ok = try {
function ()
return extract(archivefile, outputdir, extension, opt)
end,
catch {
function (errs)
if errs then
errors = tostring(errs)
end
end
}
}
if ok then
return true
end
end
return false
raise("cannot extract %s, %s!", path.filename(archivefile), errors or "extractors not found!")
end

-- extract archive file
Expand All @@ -389,12 +401,8 @@ end
-- @param options the options, e.g.. {excludes = {"*/dir/*", "dir/*"}}
--
function main(archivefile, outputdir, opt)

-- init outputdir
outputdir = outputdir or os.curdir()

-- init options
opt = opt or {}
outputdir = outputdir or os.curdir()

-- init extractors
local extractors
Expand Down

0 comments on commit b559b4c

Please sign in to comment.