With some, limited, success, I'm able to automate VS to execute some arbitrary PS in the Package Manager Console. The following is roughly the code I have running so far:
MessageFilter.Register()
try
{
var type = Type.GetTypeFromProgID("VisualStudio.DTE.14.0");
var comObj = Activator.CreateInstance(type);
var dte = (DTE2) comObj;
try
{
dte.MainWindow.Activate();
Thread.Sleep(10000); // WAIT 1
dte.Solution.Open("path/to/sln");
dte.ExecuteCommand("View.PackageManagerConsole");
Thread.Sleep(10000); // WAIT 2
dte.ExecuteCommand("View.PackageManagerConsole", "Start-Sleep -Seconds 10")
Thread.Sleep(10000); // WAIT 3
dte.Solution.Close();
}
finally
{
dte.Quit()
}
}
finally
{
MessageFilter.Revoke();
}
- Is there some way to remove the waits and subscribe to some events when some operation has completed? I'm especially curious about
WAIT 2
andWAIT 3
above, which would occur after PMC commands (first to open it, and then to invoke the PS command). - Unrelatedly, is there a way of doing all this without having to make the VS window active? I suspect not, because I get an exception without
dte.MainWindow.Activate()
.
My end goal is to be able to run Update-Package with a specific version for a given package and solution, although running any arbitrary PMC command would have other advantages as well. Unfortunately nuget.exe doesn't give the option of passing in a version AFAIK. For a large solution, the update can take a while, so there's no good way of knowing when it has completed.