diff options
| author | ruki <[email protected]> | 2024-09-14 06:51:23 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-09-14 06:51:23 +0800 |
| commit | 8dab4c87e8cd9e2a2ae106c7a0236294246478b2 (patch) | |
| tree | 43828dae7056131a4b717aefece411b9bd310f53 | |
| parent | 8cfa4e691396c5c9ded6ed5ab45b8a45a73bd01f (diff) | |
| parent | 5994ea556a485b2a6ddecaafb60d5528def6dddb (diff) | |
Merge pull request #5616 from xmake-io/ps
download file using powershell
| -rw-r--r-- | xmake/modules/net/http/download.lua | 27 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/environment.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/utils/archive/extract.lua | 43 | ||||
| -rwxr-xr-x | xmake/scripts/download.ps1 | 27 | ||||
| -rwxr-xr-x | xmake/scripts/unzip.ps1 | 24 |
5 files changed, 121 insertions, 2 deletions
diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua index b2802d219..601a38512 100644 --- a/xmake/modules/net/http/download.lua +++ b/xmake/modules/net/http/download.lua @@ -202,6 +202,25 @@ function _wget_download(tool, url, outputfile, opt) os.vrunv(tool.program, argv) end +-- download url using powershell +-- e.g. +-- powershell -ExecutionPolicy Bypass -File "D:\scripts\download.ps1" "url" "outputfile" +function _powershell_download(tool, url, outputfile, opt) + + -- get the script file + local scriptfile = path.join(os.programdir(), "scripts", "download.ps1") + + -- ensure output directory + local outputdir = path.directory(outputfile) + if not os.isdir(outputdir) then + os.mkdir(outputdir) + end + + -- download it + local argv = {"-ExecutionPolicy", "Bypass", "-File", scriptfile, url, outputfile} + os.vrunv(tool.program, argv) +end + -- download url -- -- @param url the input url @@ -227,5 +246,13 @@ function main(url, outputfile, opt) return _wget_download(tool, url, outputfile, opt) end + -- download url using powershell + if is_host("windows") then + tool = find_tool("pwsh") or find_tool("powershell") + if tool then + return _powershell_download(tool, url, outputfile, opt) + end + end + assert(tool, "curl or wget not found!") end diff --git a/xmake/modules/private/action/require/impl/environment.lua b/xmake/modules/private/action/require/impl/environment.lua index 8d7dbdcc0..02b700a0f 100644 --- a/xmake/modules/private/action/require/impl/environment.lua +++ b/xmake/modules/private/action/require/impl/environment.lua @@ -33,7 +33,7 @@ import("private.action.require.impl.install_packages") function enter() -- unzip or 7zip is necessary - if not find_tool("unzip") and not find_tool("7z") then + if not is_host("windows") and not find_tool("unzip") and not find_tool("7z") then raise("failed to find unzip or 7zip! please install one of them first") end 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/download.ps1 b/xmake/scripts/download.ps1 new file mode 100755 index 000000000..c19983699 --- /dev/null +++ b/xmake/scripts/download.ps1 @@ -0,0 +1,27 @@ +#!/usr/bin/env pwsh +#Requires -version 5 + +param ( + [string]$url, + [string]$outputfile +) + +& { + function writeErrorTip($msg) { + Write-Host $msg -BackgroundColor Red -ForegroundColor White + } + + $temppath = ([System.IO.Path]::GetTempPath(), $env:TMP, $env:TEMP, "$(Get-Location)" -ne $null)[0] + [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" + + function download { + try { + Invoke-Webrequest $url -OutFile $outputfile -UseBasicParsing + } catch { + writeErrorTip 'Download failed!' + throw + } + } + + download +} 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 +} |
