确定解决方案是否使用 MSBuild 和 PSake 进行编译

发布于 2024-08-12 20:02:15 字数 699 浏览 9 评论 0原文

我已经整理了一个 PSake (v2.0) 构建脚本,并且该脚本将 $psake.build_success 属性设置为 true 即使对 MSBuild 的调用失败。任何人都可以建议我如何更改脚本,以便在 MSBuild 调用失败时 $psake.build_success 属性正确返回 false 吗?

我的 PSake 构建脚本如下:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends BuildSolution 

task BuildSolution
{
    msbuild $solutionFile /t:Clean,Build
    if ($psake.build_success) 
    {
        $buildSuccessfulMessage
    } 
    else 
    {
        $buildFailureMessage
    }
}

I have put together a PSake (v2.0) build script, and the script is setting the $psake.build_success property as true even thought the call to MSBuild fails. Can anyone advise me on how to alter the script so that the $psake.build_success property will correctly return false when the MSBuild call fails?

My PSake build script is as follows:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends BuildSolution 

task BuildSolution
{
    msbuild $solutionFile /t:Clean,Build
    if ($psake.build_success) 
    {
        $buildSuccessfulMessage
    } 
    else 
    {
        $buildFailureMessage
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-08-19 20:02:15

PowerShell 的本机 $lastExitCode(即 WIn32 ExitCode)在上下文中有任何用处吗?我猜测内置的命令仅在您调用与 psake 相关的 cmdlet 时才相关。

即,将检查替换为

if($lastexitcode -eq 0) {

免责声明:只有播客级别的经验与 psake :D

Is PowerShell's native $lastExitCode (i.e., WIn32 ExitCode) any use in the context? I'd be guessing that the built in one is only relevant when you're invoking a psake-related cmdlet.

i.e., replace the check with

if($lastexitcode -eq 0) {

Disclaimer: Only podcast level experience with psake :D

苍景流年 2024-08-19 20:02:15

问题似乎是对 MSBuild 操作的调用实际上成功完成,而它启动的构建操作失败。我能够解决此问题的方法是将 MSBuild 调用的输出通过管道传输到文本文件,然后解析文件中的字符串“Build Failed”。如果它包含该字符串,显然构建失败。

我的 PSake 构建脚本如下:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends Build 

task Build -depends Clean {
    msbuild $solutionFile /t:Build /p:Configuration=Release >"MSBuildOutput.txt"
}

task Clean {
    msbuild $solutionFile /t:Clean 
}

在我的调用脚本中:

function Check-BuildSuccess()
{
    return (! (Find-StringInTextFile  -filePath .\MSBuildOutput.txt -searchTerm "Build Failed"))
}

function Is-StringInTextFile
(
    [string]$filePath = $(Throw "File Path Required!"),
    [string]$searchTerm = $(Throw "Search Term Required!")
)
{
    $fileContent = Get-Content $filePath    
    return ($fileContent -match $searchTerm)
}

The issue seems to be that the call to MSBuild operation actually completes successfully, whilst the build operation it initiates fails. The way I was able to get around this was to pipe the output of the MSBuild call to a text file, and then parse the file for the string "Build Failed". If it contained the string, obviously the build failed.

My PSake build script is as follows:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends Build 

task Build -depends Clean {
    msbuild $solutionFile /t:Build /p:Configuration=Release >"MSBuildOutput.txt"
}

task Clean {
    msbuild $solutionFile /t:Clean 
}

and in my calling script:

function Check-BuildSuccess()
{
    return (! (Find-StringInTextFile  -filePath .\MSBuildOutput.txt -searchTerm "Build Failed"))
}

function Is-StringInTextFile
(
    [string]$filePath = $(Throw "File Path Required!"),
    [string]$searchTerm = $(Throw "Search Term Required!")
)
{
    $fileContent = Get-Content $filePath    
    return ($fileContent -match $searchTerm)
}
完美的未来在梦里 2024-08-19 20:02:15

有 psake Exec 命令,您可以用它来包装 msbuild 和一个抛出 powershell 错误。

Exec {
     msbuild $solutionFile "/p:Configuration=$buildConfiguration;Platform=$buildPlatform;OutDir=$tempOutputDirectory"
}

There is the psake Exec command that you can wrap msbuild with and a powershell error is thrown.

Exec {
     msbuild $solutionFile "/p:Configuration=$buildConfiguration;Platform=$buildPlatform;OutDir=$tempOutputDirectory"
}
天煞孤星 2024-08-19 20:02:15

$LastExitCode 或 $_ 都不适合我。然而,这确实是:

$buildArgs = "MySolution.sln", "/t:Build", "/p:Configuration=Debug"
$procExitCode = 0
$process = Start-Process -FilePath "msbuild" -ArgumentList $buildArgs -NoNewWindow -PassThru
Wait-Process -InputObject $process
$procExitCode = $process.ExitCode

#aha! msbuild sets the process exit code but powershell doesn't notice
if ($procExitCode -ne 0)
{
    throw "msbuild failed with exit code $procExitCode."
}

PS 如果您在生产中使用它,我建议将 -timeout 处理添加到 Wait-Process

Neither $LastExitCode or $_ worked for me. This did however:

$buildArgs = "MySolution.sln", "/t:Build", "/p:Configuration=Debug"
$procExitCode = 0
$process = Start-Process -FilePath "msbuild" -ArgumentList $buildArgs -NoNewWindow -PassThru
Wait-Process -InputObject $process
$procExitCode = $process.ExitCode

#aha! msbuild sets the process exit code but powershell doesn't notice
if ($procExitCode -ne 0)
{
    throw "msbuild failed with exit code $procExitCode."
}

P.S. If you use this in production I recommend adding -timeout handling to Wait-Process

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文