Doliveras
Doliveras

Reputation: 1769

Visual studio 2008 macro to get source code

I have a macro that opens a solution, connects to SourceSafe and downloads the latest version of each file inside the solution. To this point all works correctly.

Now I want to execute devenv.exe with /command option to run this macro and close the Visual Studio environment once the source code is downloaded, and at this point I’m having some problems:

If I use a macro like the one following these lines, Visual Studio starts, loads the solution, starts getting source code and then exits without waiting for the code to be completely downloaded.

DTE.Solution.Open("C:\ApeironDev\Soluciones\SolucioApeiron\SolucioApeiron.sln")
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem("SolucioApeiron").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.ContextGetLatestVersion")
DTE.ExecuteCommand("File.Exit")

If I add a line to Sleep the macro for some time, say 20 minutes, this crashes with an error regarding error in COM call (I know, this is not an elegant way to do the job, but in order to test…).

DTE.Solution.Open("C:\ApeironDev\Soluciones\SolucioApeiron\SolucioApeiron.sln")
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem("SolucioApeiron").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.ContextGetLatestVersion")
Threading.Thread.Sleep(New System.TimeSpan(0, 20, 0))
DTE.ExecuteCommand("File.Exit")

What I’m searching for is a way to start Visual Studio, load a solution, get the latest version of the source code (waiting for the process to finish) and then close the Visual Studio environment. Any help?

Clarification, To run the macro I use the following command line:

devenv.exe /command "Macros.MyMacros.SourceControl.GetLastVersion"

Where Macros.MyMacros.SourceControl.GetLastVersion is the complete path to the macro containing the code above described.

Upvotes: 3

Views: 1042

Answers (2)

Doliveras
Doliveras

Reputation: 1769

Given the fact that I have not found the solution I was looking for, I finally implemented a partial solution. Is not the solution I was looking for nor is the most elegant solution, but works, and since this is a temporal solution…well is best than nothing.

I’m using the following macro:

Sub GestLastVersionOfSourceCode()
    DTE.Solution.Open("C:\ApeironDev\Soluciones\SolucioApeiron\SolucioApeiron.sln")
    DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
    DTE.ActiveWindow.Object.GetItem("SolucioApeiron").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.ContextGetLatestVersion")

    'Wait for 2 minutes
    For i As Integer = 0 To 2
        Threading.Thread.Sleep(New System.TimeSpan(0, 1, 0))
    Next
    DTE.ExecuteCommand("File.Exit")
End Sub

This macro runs without problems and give all the source code before closing the environment.

For some reason I don’t know, the loop between the command that obtain the source code and the command that closes the solution induce the environment to wait for the get source command to end, thing that does not happen if I put the exit command directly after the get source code command.

Upvotes: 1

Mark Sowul
Mark Sowul

Reputation: 10600

1) why not script SourceSafe itself? There is a command line tool for it
2) if you don't need to close VS, you can change the source control options to "Always get latest version of solution on load" and not need the macro

Upvotes: 1

Related Questions