Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open .sln file from cmder?

Tags:

cmder

Is it possible to launch devenv.exe and open a .sln file from cmder? After I clone a git repo from the command line, I'd like to quickly open the solution in Visual Studio without having to leave cmder.

like image 963
spacemonkey Avatar asked Mar 08 '23 00:03

spacemonkey


1 Answers

Danny is correct, you simply type xxx.sln.

I'm a bit lazy and developed the script below to be able to type vs and it will find and open the first sln file it finds in the current directory.

Here are some example commands:

  1. vs - Will open the first sln file it finds, if it doesn't find one it will just open Visual Studio without a solution. I default to opening with Visual Studio 2017. Just change the default case in the Get-VisualStudioCommand function if you'd rather default to a different version.
  2. vs 15 - I have a bunch of versions of Visual Studio on my machine, so this will open similarly to vs, but will open with Visual Studio 2015.
  3. vs 13 'helloWorld.sln' - this will open a specific sln file, i.e., helloWorld.sln. One of the projects I worked on recently had multiple solutions in the same directory.
  4. vs -WhatIf -Verbose - this will show you what the script would do if it ran and will print out the Write-Verbose messages. Handy for debugging. I show and example of this in the image below.

For this solution, I'm assuming that you're using a PowerShell console. If so, you can define a .ps1 file that will run before the console opens via the -File option. It's like defining a different PowerShell profile with different commands per console window (very powerful).

See the image below where I show going to settings and modify the PowerShell:PowerShell task to execute PowerShell -NoExit -NoLogo -File C:\src\ps\Cmder\general_setup.ps1 -new_console:d:"C:\src"

Here is the image (note, although I use an alias for ls that outputs in PowerShell like a Linux ls command...this is a PowerShell console): cmder setup

If you add the following script to the C:\src\ps\Cmder\general_setup.ps1 file (or wherever you want to put it)...you can then type in vs and it will execute. Notice in the Get-VisualStudioCommand function, I've put all the file paths...not the most elegant looking code, but if your file paths to devenv.exe are different than mine, just change them there.

Note, you could also get fancy and add a pass-through to the -ArgumentList or flag for the /SafeMode command to be able to open Visual Studio in safe mode when needed, I've just created these as a quick and dirty shortcut.

function Get-VisualStudioCommand
{
    [CmdletBinding()]
    param ( [AllowNull()][String] $vsVersion )
    $vs10 = """${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe"""
    $vs13 = """${env:ProgramFiles(x86)}\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe"""
    $vs15 = """${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"""
    $vs17 = """${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"""

    switch ($vsVersion) 
    { 
        '10' {$vs10}
        '13' {$vs13}
        '15' {$vs15} 
        '17' {$vs17} 
        default {$vs17} 
    }
}

function Get-SolutionName
{
    [CmdletBinding()]
    param ( [AllowNull()][String] $Name )

    if (!$Name)
    {
        $Name = Get-ChildItem -Filter *.sln
    }

    if ($Name)
    {
        $Name = ('"{0}"' -f $Name)
    }

    return $Name
}

function Start-VisualStudioProcess
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param([String]$Version, [String]$Sln)

    $VsCommand = Get-VisualStudioCommand -vsVersion $Version
    $Sln = Get-SolutionName -Name $Sln

    Write-Verbose -Message ('Starting: command={0} solutionName={1}' -f $VsCommand, $Sln)

    if ($PSCmdlet.ShouldProcess($VsCommand, 'Start-Process'))
    {
        if ($Sln)
        {
            Start-Process -FilePath $vsCommand -ArgumentList $sln
        }
        else
        {
            Start-Process -FilePath $vsCommand 
        }

    }
}
Set-Alias -Name vs -Value Start-VisualStudioProcess

Let me know if you have any questions. I hope it helps.

like image 135
Michael Erpenbeck Avatar answered Jun 13 '23 20:06

Michael Erpenbeck