2

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();
}
  1. 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 and WAIT 3 above, which would occur after PMC commands (first to open it, and then to invoke the PS command).
  2. 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.

Andrew
  • 1,494
  • 1
  • 17
  • 27
  • Did you find any way? – Alvaro Rivoir Nov 16 '16 at 21:34
  • No, this was something I was playing around with on the side, so I gave up on it. In our case we have some automation with nuget.exe for now (being aware of its limitations), but of course being able to execute PMC commands outside of VS would be ideal. – Andrew Nov 22 '16 at 08:56

1 Answers1

0

One idea is that you could still use the Sleep method but with short time, for example, add a "for" loop for 10 times, just wart for 1000 ms every time, and use a method to check that whether one process like the VS windows was running using C# code(sample), at least, it will optimize the performance.

For the Nuget package update, the similar steps as above suggestion, you could get the package version number using C# code, just compare the version number.

Community
  • 1
  • 1
Jack Zhai
  • 6,230
  • 1
  • 12
  • 20