4

I am trying to create an Init.ps1 script for a NuGet package which adds items to a solution folder which may or may not already exist in the solution.

The script successfully adds or replaces the files in the filesystem and if there was no such solution folder, the items are successfully added to the newly created solution folder, however, if the solution folder already existed, a new solution folder named NewFolder1 (or 2 and on if that already exists) is created and no files are added to the solution.

param($installPath, $toolsPath, $package)

$solutionNode = Get-Interface $dte.Solution ([EnvDTE80.Solution2])

$solutionItemsNode = $solutionNode.FindProjectItem("FolderName")

$solutionItemsProjectItems = Get-Interface $solutionNode.ProjectItems ([EnvDTE.ProjectItems])

if (!$solutionItemsProjectItems) {

    $solutionItemsNode = $solutionNode.AddSolutionFolder("FolderName")

    $solutionItemsProjectItems = Get-Interface $solutionItemsNode.ProjectItems ([EnvDTE.ProjectItems])

}

$rootDir = (Get-Item $installPath).parent.parent.fullname

$deploySource = join-path $installPath '\sln\'
$deployTarget = join-path $rootDir '\FolderName\'

New-Item -ItemType Directory -Force -Path $deployTarget

ls $deploySource | foreach-object {

    $targetFile = join-path $deployTarget $_.Name

    Copy-Item $_.FullName $targetFile -Recurse -Force

    $solutionItemsProjectItems.AddFromFile($targetFile) > $null

} > $null

I have tried various alterations such as checking $solutionItemsNode instead of $solutionItemsProjectItems (same effect), adding directly to solution instead of under solution folder (did nothing) and the following change which acts the same as the original:

$solutionItemsNode = $solution.Projects | where-object { $_.ProjectName -eq "FolderName" } | select -first 1

if (!$solutionItemsNode) {
    $solutionItemsNode = $solutionNode.AddSolutionFolder("FolderName")
}


Any pointers?


Related Q&As and MSDN links:

Community
  • 1
  • 1
Danny Varod
  • 17,324
  • 5
  • 69
  • 111

1 Answers1

7

When you add a solution folder to a solution it is available from the EnvDTE80.Solution2's Projects property.

So using $solutionNode.Projects seems to work. The only modification to the code you had is the use of $solutionNode.Projects instead of $solution.Projects since $solution does not exist anywhere so the result would always be $null.

$solutionItemsNode = $solutionNode.Projects | where-object { $_.ProjectName -eq "FolderName" } | select -first 1

if (!$solutionItemsNode) {
    $solutionItemsNode = $solutionNode.AddSolutionFolder("FolderName")
}

The code above will not add the solution folder if it already exists.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • Thanks for finding the typo, however, it know acts the same as the original code - if the Solution Folder already exists creates NewFolder1 and does not add the file to the Solution. Updated the question accordingly. – Danny Varod Dec 15 '14 at 20:24
  • I tested the code directly in the Package Manager Console directly without creating a NuGet package and it worked for me. Also you are still using `$solution` in your code in the question. – Matt Ward Dec 15 '14 at 20:29
  • Missed that place - the lack of compiler + the late hours I work on this do not go along together. Testing again now. – Danny Varod Dec 15 '14 at 20:32