Search code examples
windowsvisual-studiocommand-linevisual-studio-2017

visual studio 2017: open AND build solution from a command line call


In visual studio 2017 (or probably any version) I want to open a solution in the IDE and start it building from the command line. IF the solution is already open then I just want it to start building (in the IDE).

So I can open the solution like this:

devenv solution.sln

Or I can build it like this:

devenv /build solution.sln

There is also this:

devenv solution.sln /command ...

But the documentation on what "commands" there are is very difficult to find out about... The example is some user made macro, but I assume there are other built in commands? - this may help...?

But I am not sure how to:

  • Open a solution in the IDE and have it build straight away
  • If its already open just get it to start building.

Is there some way to do this?

My use case is to kick off the build from within IBM Rhapsody. In MSVS2012 it supported a Rhapsody addin which did these tasks... but addins have been deprecated since 2013 so I can get Rhapsody to do what I want by re-writing its make file content - the makefile will just call a batch file script which will do the commands that I am trying to do in this question - and then Rhapsody plugin done :)


Solution

  • Actually note that:

    1. devenv solution.sln only opens a solution in a new VS IDE instance.
    2. devenv /build solution.sln only builds projects that have changed since the last build without opening VS IDE. To build all projects in a solution, use /rebuild instead.

    So, if you want to

    Open a solution in the IDE and have it build straight away

    you should run the two commands consecutively:

    devenv solution.sln  
    devenv /rebuild solution.sln
    

    Then at the second time use /rebuild only to avoid opening a new VS instance.

    Update: you can make a .cmd or .bat file contains the following:

    tasklist /fi "imagename eq devenv.exe" /v | find /i "solution" 2>NUL
        if "%ERRORLEVEL%"=="0" goto solution_is_running
        if "%ERRORLEVEL%"=="1" goto solution_is_closed
    
    :solution_is_running
    devenv /rebuild solution.sln
    goto:eof
    
    :solution_is_closed
    devenv solution.sln
    devenv /rebuild solution.sln
    goto:eof
    

    Or use:

    Taskkill /IM devenv.exe /F
    devenv solution.sln  
    devenv /rebuild solution.sln