在VS擴展工具中使用powershell腳本發佈 dotnetcore單一文件

參考:dotnet publish 命令 - .NET CLI | Microsoft Docs

最近使用VS2019和VS2022,發佈 AOT時,總是提示失敗,要好幾回才成功,沒得辦法,自己搞吧,反覆重試總是能成功的,改改就可以在持續集成中打包了,也免得每個項目都要創建發佈腳本FolderProfile.pubxml

拿走不謝

在外部工具中創建一個命令



   

命令和參數如下

@@@code

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

-ExecutionPolicy RemoteSigned -File "c:\qoushuidll\tools\publish_core.ps1" "$(ProjectDir)$(ProjectFileName)"

@@#

PS腳本內容如下:

@@@code

Param(

[string]$path

)

# $path="D:\code\Zooy\PatrolCore\src\PatrolPlugins\PatrolPlugins.DBCore\PatrolPlugins.DBCore.csproj "

# 變量定義

$rarTool="C:\Program Files\WinRAR\winrar.exe"

$publishDir="d:\publish"

$dotnetVer="net5.0" # netcoreapp3.1 net48

 

$toolPath= Split-Path -Parent $MyInvocation.MyCommand.Definition

$slnPath =[System.IO.Path]::GetDirectoryName($path)

$name = [System.IO.Path]::GetFileNameWithoutExtension($path)

 

# 以項目名爲發佈文件夾名

$publishDir = [System.IO.Path]::Combine($publishDir,$name)

Write-Host "發佈NETCORE項目" $path "至" $publishDir -ForegroundColor Yellow

 

# 選擇發佈模式

Write-Host "選擇發佈模式,1 可移植 2 單文件 4 單文件獨立運行時 7 全部 "

$type = Read-Host "請選擇"

 

 

Set-Location $slnPath

 

$cmd = " -c Release "

[System.Collections.Generic.Dictionary[int,String]]$mDic = @{}

# 發佈可移植包

$mDic.Add(1,"-f $dotnetVer")

# 發佈可移植單一包

$mDic.Add(2,"-r win-x64 --self-contained false /p:PublishSingleFile=true")

# 發佈包含運行時的可移植單一包

$mDic.Add(4,"-r win-x64 --self-contained true /p:PublishSingleFile=true")

 

[System.Collections.Generic.Dictionary[int,String]]$mDicOutput = @{}

$mDicOutput.Add(1,"$name"+"_portable")

# 發佈可移植單一包

$mDicOutput.Add(2,"$name"+"_portable_aot")

# 發佈包含運行時的可移植單一包

$mDicOutput.Add(4,"$name"+"_aot")

 

$numbers = (1,2,4)

foreach($i in $numbers){

if($type -eq $i -or $type -eq 7)

{

$retry =5

$v= $mDicOutput[$i]

$trg= [System.IO.Path]::Combine($publishDir, $v)

# 清除現有文件

if ([System.IO.Directory]::Exists($trg))

{

Remove-Item $trg -Recurse -Force

}

 

$v = $mDic[$i]

# Write-Host $v

$tmp =" dotnet publish `"$path`" -o `"$trg`" $cmd $v "

# Write-Host $tmp

# 檢查是否失敗,重試5次

do {

$retry = $retry -1

$b =0

$v= $mDicOutput[$i]

if(-not [System.IO.Directory]::Exists($trg)){

$b=1

Write-Host "$v 生成" -ForegroundColor Yellow

}else{

if ([System.IO.Directory]::GetFiles($trg,"*").Length -lt 2){

$b=2

Write-Host "$v 生成失敗,重試" -ForegroundColor Red

}

}

if($b -gt 0)

{

& cmd /c " taskkill /F /IM `"dotnet.exe`""

& timeout /T 5 /NOBREAK

& cmd /c " $tmp "

}

} while( $retry -gt 0)

}

}

# 統計彙報以及壓縮

foreach($i in $numbers){

$v= $mDicOutput[$i]

$trg= [System.IO.Path]::Combine($publishDir, $v)

if($type -eq $i -or $type -eq 7)

{

$b =1

if(-not [System.IO.Directory]::Exists($trg)){

$b= 0

Write-Host "$v 未生成" -ForegroundColor Red

}else{

if ([System.IO.Directory]::GetFiles($trg,"*").Length -lt 2){

$b=2

Write-Host "$v 生成失敗" -ForegroundColor Red

}

}

if ($b -eq 1){

$zip="$publishDir\$v.zip"

if ([System.IO.Directory]::Exists($zip))

{

Remove-Item $zip -Force

}

& cmd /c " `"$rarTool`" a -EP1 -r -x*.Development.json -x*.pdb $zip $trg "

}

}

 

}

 

 

Start-Process explorer "$publishDir"

 

pause

 

@@#

   



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章