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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With