简体   繁体   English

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

[英]How can I run an MSBuild task in Powershell on file changed

I am trying to write something like a CSharp watch task in PowerShell. 我正在尝试在PowerShell中编写类似CSharp监视任务的内容。 So, what I want to happen is when a CSharp file changes in a certain directory, it tries to find the csproj file and initiates a build. 所以,我想发生的是当CSharp文件在某个目录中更改时,它试图找到csproj文件并启动构建。

Here's what I have currently 这是我目前所拥有的

    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

What I have found is that within the function Find-And-Build-Project , the & $msbuild doesn't get invoked. 我发现的是,在Find-And-Build-Project函数中, & $msbuild不会被调用。 But, I don't understand why. 但是,我不明白为什么。

Any ideas? 有任何想法吗?

Ok, this helped me: https://stackoverflow.com/a/37724701/1326235 . 好的,这对我有所帮助: https : //stackoverflow.com/a/37724701/1326235

This script targets the saved .cs file (or .cs~tmp[0-9].cs as Visual Studio seems to create), then searches back up the directory tree to find a .csproj file and builds it. 该脚本以保存的.cs文件(或Visual Studio似乎创建的.cs~tmp[0-9].cs )为目标,然后在目录树中搜索以找到.csproj文件并进行构建。

I published the module to the PowerShell Gallery, it's called CSharp-Watch 我将该模块发布到了PowerShell画廊,称为CSharp-Watch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Powershell 的后台任务中运行带有变量的管道? - How can I run a pipeline with variables in a background task in Powershell? 如何仅在使用 powershell 更改文件时才下载文件 - How can I download a file only if it has changed using powershell 如何使用PowerShell读取MSBuild PropertyGroup的值? - How can I read the value of an MSBuild PropertyGroup using PowerShell? 如何在批处理文件中运行此 powershell 脚本 - How can I run this powershell script in a batch file 如何以管理员身份和批处理文件中的参数运行PowerShell? - How can I run PowerShell as an administrator AND with arguments from a batch file? 如何使用 windows 的 powershell 运行 Haskell 文件? - How can I run a Haskell file using window's powershell? 如何在不显示窗口的情况下将Powershell脚本作为后台任务运行? - How can I run a powershell script as a background task without displaying a window? 从任务计划程序运行时如何重定向 PowerShell 输出? - How can I redirect PowerShell output when run from Task Scheduler? 如何说服 powershell(通过任务调度程序运行)找到我的网络驱动器? - How can I convince powershell (run through task scheduler) to find my network drive? 我如何使用powershell和schedule.service运行计划的任务 - How I could run a scheduled task with powershell and schedule.service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM