Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Powershell中对已更改的文件运行MSBuild任务_Powershell_Msbuild - Fatal编程技术网

如何在Powershell中对已更改的文件运行MSBuild任务

如何在Powershell中对已更改的文件运行MSBuild任务,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 Watch

我正在尝试在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
不会被调用。但是,我不明白为什么

有什么想法吗?

好的,这对我有帮助:

此脚本以保存的
.cs
文件(或Visual Studio似乎创建的
.cs~tmp[0-9].cs
为目标),然后搜索备份目录树以查找
.csproj
文件并生成它

我将该模块发布到PowerShell Gallery,名为