tugberk
tugberk

Reputation: 58454

Running the Clean, Build and Rebuild through the Package Manager Console

I want to run the Clean, Build and Rebuild commands through the Package Manager Console in Visual Studio but so far I haven't been able to find how.

The following command gets me the first project inside the solution:

$project = Get-Project | select -First 1

When I run the $project | Get-Member, I can see the members of $project item.

#Members of the $project ($project | Get-Member)

#   TypeName: System.__ComObject#{866311e6-c887-4143-9833-645f5b93f6f1}
#
#Name                 MemberType            Definition                                                                                                                                   
#----                 ----------            ----------                                                                                                                                   
#ProjectName          CodeProperty          System.String ProjectName{get=GetCustomUniqueName;}                                                                                          
#Delete               Method                void Delete ()                                                                                                                               
#Save                 Method                void Save (string)                                                                                                                           
#SaveAs               Method                void SaveAs (string)                                                                                                                         
#Extender             ParameterizedProperty IDispatch Extender (string) {get}                                                                                                            
#CodeModel            Property              CodeModel CodeModel () {get}                                                                                                                 
#Collection           Property              Projects Collection () {get}                                                                                                                 
#ConfigurationManager Property              ConfigurationManager ConfigurationManager () {get}                                                                                           
#DTE                  Property              DTE DTE () {get}                                                                                                                             
#ExtenderCATID        Property              string ExtenderCATID () {get}                                                                                                                
#ExtenderNames        Property              Variant ExtenderNames () {get}                                                                                                               
#FileName             Property              string FileName () {get}                                                                                                                     
#FullName             Property              string FullName () {get}                                                                                                                     
#Globals              Property              Globals Globals () {get}                                                                                                                     
#IsDirty              Property              bool IsDirty () {get} {set}                                                                                                                  
#Kind                 Property              string Kind () {get}                                                                                                                         
#Name                 Property              string Name () {get} {set}                                                                                                                   
#Object               Property              IDispatch Object () {get}                                                                                                                    
#ParentProjectItem    Property              ProjectItem ParentProjectItem () {get}                                                                                                       
#ProjectItems         Property              ProjectItems ProjectItems () {get}                                                                                                           
#Properties           Property              Properties Properties () {get}                                                                                                               
#Saved                Property              bool Saved () {get} {set}                                                                                                                    
#UniqueName           Property              string UniqueName () {get}                                                                                                                   
#Type                 ScriptProperty        System.Object Type {get=switch ($this.Kind) {...       

I am not sure if I can get to the Clean Build and Rebuild method through the $project item or if I should I directly run the msbuild by targeting the project path.

Any idea?

Upvotes: 10

Views: 3433

Answers (2)

Matt Ward
Matt Ward

Reputation: 47967

Visual Studio's object model provides a way to build the entire solution or a single project via the SolutionBuild object.

Building the solution is straightforward from the NuGet Package Manager Console.

$dte.Solution.SolutionBuild.Clean($true)
$dte.Solution.SolutionBuild.Build($true)

The $true flag indicates that the command should wait for the clean/build to finish.

Building an individual project is not as straightforward. The SolutionBuild object provides a BuildProject method which takes three parameters.

$project = Get-Project | select -First 1
$dte.Solution.SolutionBuild.BuildProject("Debug", $project.FullName, $true)

It also does not allow you to run a Clean build.

If you want to build an individual project then, as Pavel suggests, using MSBuild seems to be more straighforward.

Upvotes: 16

Pavel Bakshy
Pavel Bakshy

Reputation: 7747

According nuget docs there are no commands to do this actions. I think the simplest way to run msbuild directly.

$project = Get-Project | select -First 1
$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
& $msbuild $project.FullName /t:Clean`;Build`;Rebuild

Based on this you can write your PS module and pack it like nuget package to share it with other developers and reuse in other projects. Good example how to add PS commands by nuget.

Upvotes: 5

Related Questions