diff options
| author | ruki <[email protected]> | 2024-09-14 00:59:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-09-14 00:59:59 +0800 |
| commit | 86f30703b9d2ea74710aa4cd2a99b03568331724 (patch) | |
| tree | c1b81eb2aab7cf7fbd2a2549a62ce07e91f5af10 | |
| parent | 1be14dcae978f2969e708bb9224e36a8a7b6f77f (diff) | |
unzip file using powershell
| -rw-r--r-- | xmake/modules/utils/archive/extract.lua | 43 | ||||
| -rwxr-xr-x | xmake/scripts/unzip.ps1 | 24 |
2 files changed, 66 insertions, 1 deletions
diff --git a/xmake/modules/utils/archive/extract.lua b/xmake/modules/utils/archive/extract.lua index b5cb79033..7fd936041 100644 --- a/xmake/modules/utils/archive/extract.lua +++ b/xmake/modules/utils/archive/extract.lua @@ -21,6 +21,7 @@ -- imports import("core.base.option") import("lib.detect.find_file") +import("lib.detect.find_tool") import("detect.tools.find_xz") import("detect.tools.find_7z") import("detect.tools.find_tar") @@ -331,6 +332,46 @@ function _extract_using_unzip(archivefile, outputdir, extension, opt) return true end +-- extract archivefile using powershell +-- powershell -ExecutionPolicy Bypass -File "D:\scripts\unzip.ps1" "archivefile" "outputdir" +function _extract_using_powershell(archivefile, outputdir, extension, opt) + + -- find powershell + local powershell = find_tool("pwsh") or find_tool("powershell") + if not powershell then + return false + end + + -- get the script file + local scriptfile = path.join(os.programdir(), "scripts", "unzip.ps1") + + -- extract to *.tar file first + local outputdir_old = nil + if extension:startswith(".tar.") then + outputdir_old = outputdir + outputdir = os.tmpfile({ramdisk = false}) .. ".tar" + end + + -- ensure output directory + if not os.isdir(outputdir) then + os.mkdir(outputdir) + end + + -- extract it + local argv = {"-ExecutionPolicy", "Bypass", "-File", scriptfile, archivefile, outputdir} + os.vrunv(powershell.program, argv) + + -- continue to extract *.tar file + if outputdir_old then + local tarfile = find_file("**.tar", outputdir) + if tarfile and os.isfile(tarfile) then + return _extract(tarfile, outputdir_old, ".tar", {_extract_using_tar, _extract_using_7z}, opt) + end + end + return true +end + + -- extract archivefile using bzip2 function _extract_using_bzip2(archivefile, outputdir, extension, opt) @@ -425,7 +466,7 @@ function main(archivefile, outputdir, opt) -- tar/windows can not extract .bz2 ... extractors = { - [".zip"] = {_extract_using_7z, _extract_using_unzip, _extract_using_tar} + [".zip"] = {_extract_using_7z, _extract_using_unzip, _extract_using_tar, _extract_using_powershell} , [".7z"] = {_extract_using_7z} , [".gz"] = {_extract_using_7z, _extract_using_gzip, _extract_using_tar} , [".xz"] = {_extract_using_7z, _extract_using_xz, _extract_using_tar} diff --git a/xmake/scripts/unzip.ps1 b/xmake/scripts/unzip.ps1 new file mode 100755 index 000000000..46947bfd3 --- /dev/null +++ b/xmake/scripts/unzip.ps1 @@ -0,0 +1,24 @@ +#!/usr/bin/env pwsh +#Requires -version 5 + +param ( + [string]$archivefile, + [string]$outputdir +) + +& { + function writeErrorTip($msg) { + Write-Host $msg -BackgroundColor Red -ForegroundColor White + } + + function unzip { + try { + Expand-Archive -Path $archivefile -DestinationPath $outputdir + } catch { + writeErrorTip 'Unzip failed!' + throw + } + } + + unzip +} |
