summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-09-14 00:56:23 +0800
committerruki <[email protected]>2024-09-14 00:56:23 +0800
commit1be14dcae978f2969e708bb9224e36a8a7b6f77f (patch)
treecd6745d50d4877909cfd592463f9ace0105ab4af
parent8cfa4e691396c5c9ded6ed5ab45b8a45a73bd01f (diff)
download file using powershell
-rw-r--r--xmake/modules/net/http/download.lua27
-rwxr-xr-xxmake/scripts/download.ps127
2 files changed, 54 insertions, 0 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/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
+}