Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create or Get Solution folder in NuGet Init.ps1

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:

  • adding-solution-level-items-in-a-nuget-package
  • nuget-how-to-add-files-to-solution-folder
  • copy-files-to-solution-folder-with-init-ps1-and-nuget
  • link from above answer
  • MSDN: envdte80.solutionfolder
like image 735
Danny Varod Avatar asked Mar 17 '23 15:03

Danny Varod


1 Answers

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.

like image 149
Matt Ward Avatar answered Mar 29 '23 12:03

Matt Ward