Skip to content

Commit 8dab4c8

Browse files
authored
Merge pull request #5616 from xmake-io/ps
download file using powershell
2 parents 8cfa4e6 + 5994ea5 commit 8dab4c8

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

xmake/modules/net/http/download.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,25 @@ function _wget_download(tool, url, outputfile, opt)
202202
os.vrunv(tool.program, argv)
203203
end
204204

205+
-- download url using powershell
206+
-- e.g.
207+
-- powershell -ExecutionPolicy Bypass -File "D:\scripts\download.ps1" "url" "outputfile"
208+
function _powershell_download(tool, url, outputfile, opt)
209+
210+
-- get the script file
211+
local scriptfile = path.join(os.programdir(), "scripts", "download.ps1")
212+
213+
-- ensure output directory
214+
local outputdir = path.directory(outputfile)
215+
if not os.isdir(outputdir) then
216+
os.mkdir(outputdir)
217+
end
218+
219+
-- download it
220+
local argv = {"-ExecutionPolicy", "Bypass", "-File", scriptfile, url, outputfile}
221+
os.vrunv(tool.program, argv)
222+
end
223+
205224
-- download url
206225
--
207226
-- @param url the input url
@@ -227,5 +246,13 @@ function main(url, outputfile, opt)
227246
return _wget_download(tool, url, outputfile, opt)
228247
end
229248

249+
-- download url using powershell
250+
if is_host("windows") then
251+
tool = find_tool("pwsh") or find_tool("powershell")
252+
if tool then
253+
return _powershell_download(tool, url, outputfile, opt)
254+
end
255+
end
256+
230257
assert(tool, "curl or wget not found!")
231258
end

xmake/modules/private/action/require/impl/environment.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import("private.action.require.impl.install_packages")
3333
function enter()
3434

3535
-- unzip or 7zip is necessary
36-
if not find_tool("unzip") and not find_tool("7z") then
36+
if not is_host("windows") and not find_tool("unzip") and not find_tool("7z") then
3737
raise("failed to find unzip or 7zip! please install one of them first")
3838
end
3939

xmake/modules/utils/archive/extract.lua

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
-- imports
2222
import("core.base.option")
2323
import("lib.detect.find_file")
24+
import("lib.detect.find_tool")
2425
import("detect.tools.find_xz")
2526
import("detect.tools.find_7z")
2627
import("detect.tools.find_tar")
@@ -331,6 +332,46 @@ function _extract_using_unzip(archivefile, outputdir, extension, opt)
331332
return true
332333
end
333334

335+
-- extract archivefile using powershell
336+
-- powershell -ExecutionPolicy Bypass -File "D:\scripts\unzip.ps1" "archivefile" "outputdir"
337+
function _extract_using_powershell(archivefile, outputdir, extension, opt)
338+
339+
-- find powershell
340+
local powershell = find_tool("pwsh") or find_tool("powershell")
341+
if not powershell then
342+
return false
343+
end
344+
345+
-- get the script file
346+
local scriptfile = path.join(os.programdir(), "scripts", "unzip.ps1")
347+
348+
-- extract to *.tar file first
349+
local outputdir_old = nil
350+
if extension:startswith(".tar.") then
351+
outputdir_old = outputdir
352+
outputdir = os.tmpfile({ramdisk = false}) .. ".tar"
353+
end
354+
355+
-- ensure output directory
356+
if not os.isdir(outputdir) then
357+
os.mkdir(outputdir)
358+
end
359+
360+
-- extract it
361+
local argv = {"-ExecutionPolicy", "Bypass", "-File", scriptfile, archivefile, outputdir}
362+
os.vrunv(powershell.program, argv)
363+
364+
-- continue to extract *.tar file
365+
if outputdir_old then
366+
local tarfile = find_file("**.tar", outputdir)
367+
if tarfile and os.isfile(tarfile) then
368+
return _extract(tarfile, outputdir_old, ".tar", {_extract_using_tar, _extract_using_7z}, opt)
369+
end
370+
end
371+
return true
372+
end
373+
374+
334375
-- extract archivefile using bzip2
335376
function _extract_using_bzip2(archivefile, outputdir, extension, opt)
336377

@@ -425,7 +466,7 @@ function main(archivefile, outputdir, opt)
425466
-- tar/windows can not extract .bz2 ...
426467
extractors =
427468
{
428-
[".zip"] = {_extract_using_7z, _extract_using_unzip, _extract_using_tar}
469+
[".zip"] = {_extract_using_7z, _extract_using_unzip, _extract_using_tar, _extract_using_powershell}
429470
, [".7z"] = {_extract_using_7z}
430471
, [".gz"] = {_extract_using_7z, _extract_using_gzip, _extract_using_tar}
431472
, [".xz"] = {_extract_using_7z, _extract_using_xz, _extract_using_tar}

xmake/scripts/download.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env pwsh
2+
#Requires -version 5
3+
4+
param (
5+
[string]$url,
6+
[string]$outputfile
7+
)
8+
9+
& {
10+
function writeErrorTip($msg) {
11+
Write-Host $msg -BackgroundColor Red -ForegroundColor White
12+
}
13+
14+
$temppath = ([System.IO.Path]::GetTempPath(), $env:TMP, $env:TEMP, "$(Get-Location)" -ne $null)[0]
15+
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
16+
17+
function download {
18+
try {
19+
Invoke-Webrequest $url -OutFile $outputfile -UseBasicParsing
20+
} catch {
21+
writeErrorTip 'Download failed!'
22+
throw
23+
}
24+
}
25+
26+
download
27+
}

xmake/scripts/unzip.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env pwsh
2+
#Requires -version 5
3+
4+
param (
5+
[string]$archivefile,
6+
[string]$outputdir
7+
)
8+
9+
& {
10+
function writeErrorTip($msg) {
11+
Write-Host $msg -BackgroundColor Red -ForegroundColor White
12+
}
13+
14+
function unzip {
15+
try {
16+
Expand-Archive -Path $archivefile -DestinationPath $outputdir
17+
} catch {
18+
writeErrorTip 'Unzip failed!'
19+
throw
20+
}
21+
}
22+
23+
unzip
24+
}

0 commit comments

Comments
 (0)