powershell - 如何在文件更改时在 Powershell 中运行 MSBuild 任务

标签 powershell msbuild

我正在尝试在 PowerShell 中编写类似 CSharp watch 任务的东西。所以,我想要发生的是当某个目录中的 CSharp 文件发生更改时,它会尝试找到 csproj 文件并启动构建。

这是我目前拥有的

    function Watch-CSharp-Files() {
    set-location "D:\path\to\csharp\files\"

    $originalPath = Get-Location

    Write-host "Welcome to The Watcher. It keeps track of changing files in this solution directory (including subdirectories) and triggers a build when something changes..."

    $existingEvents = get-eventsubscriber
    foreach ($item in $existingEvents) {
        Unregister-event -SubscriptionId $item.SubscriptionId
        write-host "Unsubscribed existing event:" $item.Action.Name
    }

    $folder = get-location
    $filter = '*.*'
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

    Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action {
        $path = $Event.SourceEventArgs.FullPath  

        if ($path -match "(\.cs~|.cs$)") {
            write-host "file changed: $path"
            Invoke-Expression -Command "Find-And-Build-Project '$path'"
        }
    }
}

function Find-And-Build-Project([string]$path) {
    write-host "BUILD PROJECT REPORTING FOR DUTY"

    $pathParts = "$path".Split("\\")
    $end = $pathParts.Count - 2 # skip the file name to the parent directory
    $testPath = $pathParts[0..$end] -join "\"
    write-host "testing path $testPath"
    $csproj = Get-ChildItem -path $testPath *.csproj

    For ($i = 0; $i -le 10; $i++) {    
        $newEnd = $end - $i
        $newPath = $pathParts[0..$newEnd] -join "\"
        $csproj = Get-ChildItem -path $newPath *.csproj
        write-host "$i. trying: $newPath, csproj: $csproj"
        if ($csproj) {
            write-host "found on $i, at $newPath, $csproj"
            break
        }
    }

    write-host "Ready: $newPath\$csproj"
    $msbuild = "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
    write-host "trying to MSBUILD"    
    & $msbuild ("$newPath\$csproj", "/target:Build", "/p:configuration=debug", "/verbosity:n")    
}

Watch-CSharp-Files

我发现在函数 Find-And-Build-Project , & $msbuild不会被调用。但是,我不明白为什么。

有任何想法吗?

最佳答案

好的,这对我有帮助:https://stackoverflow.com/a/37724701/1326235 .

此脚本针对已保存的 .cs文件(或 Visual Studio 似乎创建的 .cs~tmp[0-9].cs),然后搜索备份目录树以找到 .csproj文件并构建它。

我将模块发布到 PowerShell 库,它被称为 CSharp-Watch

关于powershell - 如何在文件更改时在 Powershell 中运行 MSBuild 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594042/

相关文章:

PowerShell: `New-Object` `-ArgumentList` vs 调用构造函数 vs 类型转换

azure - PowerShell 获取 Azure AD 应用程序证书信息

powershell - 可选字符串参数(应为 NULL)

c# - 使用 TFS 2017 BuildAgent 构建解决方案时出现错误 CS0234

.net - 如何使用 Team Build 2010 部署 Windows 服务项目

azure - 未为 "CSPack"任务指定所需参数 "ServiceDefinitionFile"的值

Powershell:启动进程输出和脚本

powershell - Jenkins 错误 : Powershell Integration with Jenkins

MSBuild 找不到已通过 web.config 绑定(bind)重定向的 DLL

java - Ant .NET 库文档? (设置控制台记录器参数)