Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 506 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding solution-level items in a NuGet package

#1
I want to add solution folders and solution items (not projects) to a solution file via a NuGet package. I imagine this would be accomplished through Powershell. I've looked through the documentation for NuGet, Powershell, and EnvDTE and can't figure out:

1. Which commands/methods I would use?
2. Which standard script I would do this in, Init.ps1, Install.ps1, or somewhere else?
Reply

#2
Here is a PowerShell script that will create a solution folder called Parent and another solution folder called Child inside that one. It also adds a project file (MyProject.csproj) inside the Child solution folder.

# Get the open solution.
$solution = Get-Interface $dte.Solution ([EnvDTE80.Solution2])

# Create the parent solution folder.
$parentProject = $solution.AddSolutionFolder("Parent")

# Create a child solution folder.
$parentSolutionFolder = Get-Interface $parentProject.Object ([EnvDTE80.SolutionFolder])
$childProject = $parentSolutionFolder.AddSolutionFolder("Child")

# Add a file to the child solution folder.
$childSolutionFolder = Get-Interface $childProject.Object ([EnvDTE80.SolutionFolder])
$fileName = "D:\projects\MyProject\MyProject.csproj"
$projectFile = $childSolutionFolder.AddFromFile($fileName)

The two main Visual Studio interfaces being used here are [Solution2][1] and [SolutionFolder][2]. It also uses the Get-Interface function which is provided by NuGet.

For a solution-only package you should place your script in init.ps1 because [install.ps1][3] is only invoked for project-based packages. Init.ps1 runs once for a solution when the package is first installed and every time the solution is re-opened in Visual Studio.

To add arbitrary files (non-project files) to a solution folder you will need to do something similar to the following:

$vsSolution = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
$vsProject = $vsSolution.AddSolutionFolder("newFolder")
$projectItems = Get-Interface $vsProject.ProjectItems ([EnvDTE.ProjectItems])
$projectItems.AddFromFile("pathToFileToAdd.txt")

What is missing from this PowerShell script is the standard parameter declarations at the top of file.

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

What is also missing is checking whether the solution folder and folder item already exist. I shall leave that as an exercise for you to do.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through