Search code examples
visual-studiopowershelltfsnuget-packageenvdte

ProjectItems.AddFromFile Adds File to Pending Changes


As part of my nuget package, I have an install.ps1 powershell script that I am using to add a reference file to the project (a couple text documents) from the package's tools folder.

Everything is working great, except that when the files are referenced in a TFS solution, they are added to the Team Explorer Pending Changes. How can I remove them from pending changes (or keep them from ever showing up)? I don't want these checked into TFS, since the packages folder shouldn't be there in the first place.

Here's my install.ps1 script:

param($installPath, $toolsPath, $package, $project)

#Add reference text files to the project and opens them

Get-ChildItem $toolsPath -Filter *.txt |
ForEach-Object {

    $projItem = $project.ProjectItems.AddFromFile($_.FullName)
    If ($projItem -ne $null) {
        $projItem.Properties.Item("BuildAction").Value = 0  # Set BuildAction to None
    }
}

Solution

  • I finally figured out how to do it using tf.exe. Calling tf vc undo with the full filename will undo pending changes for those files. And if the folder isn't tied to TFS, no harm done. It just continues on.

    This implementation does require VS 2015 to be installed (due to the hardcodes path to the IDE folder), so I'm looking for a better way to obtain the IDE path of the currently loaded IDE. For now though, this solves my current issue.

    param($installPath, $toolsPath, $package, $project)
    
    $idePath = "$env:VS140COMNTOOLS..\IDE"
    $tfPath = "$idePath\tf.exe"
    
    Get-ChildItem $toolsPath -Filter *.txt |
    ForEach-Object {
    
        $projItem = $project.ProjectItems.AddFromFile($_.FullName)
        If ($projItem -ne $null) {
            $projItem.Properties.Item("BuildAction").Value = 0  # Set BuildAction to None
    
            $filename = $_.FullName
    
            & $tfPath vc undo `"$filename`" # Remove File from TFS Pending Changes, as AddFromFile can automatically add it
        }
    }