Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to make Using VSCODE on windows with MSVC and MSBUILD much less of a huge pain #6733

Closed
zenerbufen opened this issue Oct 11, 2023 · 1 comment

Comments

@zenerbufen
Copy link

zenerbufen commented Oct 11, 2023

#VS code does not play nice with MSBUILD tools, and GIT. There are instructions provided for manual set up that feel very hacky and work only in limited situations and are easy to break especially when trying to use multiple applications together, like developers do constantly.

#Here is a script that makes VSCODE act like a proper windows application, lets github use it as an editor, and makes sure you can double click (A short cut to) this script to launch vscode in the gui and have it just work

update: my script stopped working after some updates to VS, These updates fixed some issues with the dev environment "yay" but broke my script because it wasn't great. Here is an updated version that actually works, but even better now. I now have a shortcut to this script in my start menu to launch code, and have github pointed at it to use as an editor. and everything 'just works' how a proper app on windows should.

# 🌈🦄 This script is your fairy godmother for launching VS Code on Windows 🦄🌈
# 🎤 It sets the stage for MSVC, Git, and CLANG using MSBUILD from VS Community (or other editions) 🎤
# Version 2

# 🎸 To make VS Code your Git editor, follow the instructions in the comments 🎸
#
#    From a "Developer Command Prompt for VS 20xx" use:
#        git config --global core.editor "powershell -File path/to/your/script.ps1 -GitCall"
#    replacing "path/to/your/script.ps" with the path to this script.

# Define parameters for the script (arguments passed in, usually a file to edit but also other flags)
param (
    [string]$fileToEdit,
    [switch]$GitCall
)
# 🕵️‍♂️ Function to find that elusive vswhere, darling 🕵️‍♂️
function Find-MyGlassSlipper {
    $commonPaths = @(
        #fixed modern location
        "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer",
        #pre windows 10 32 bit
        "${env:ProgramFiles}\Microsoft Visual Studio\Installer",
        #chocolaty install support
        "${env:ProgramData}\chocolatey\lib\vswhere\tools",
        #backwards compatibility with some older studio versions user installs  
        "${env:LocalAppData}\Microsoft\VisualStudio\Installer",
        #backwards compatability with older bugged versions of chocolaty
        "${env:SystemDrive}\chocolatey\bin",
        "${env:ProgramData}\chocolatey\bin"
    )
    foreach ($fabulousPath in $commonPaths) {
        $possibleSlipper = Join-Path $fabulousPath "vswhere.exe"
        if (Test-Path $possibleSlipper) {
            Write-Host "👠 Found my glass slipper at $possibleSlipper 👠"
            return $possibleSlipper
        }
    }
    Write-Host "👠 My glass slipper is missing! Time to abort this mission. 👠"
    Read-Host "Smash that Enter key when you're ready to leave, but remember: you'll miss me. 😘"
    exit 1
}
# 🎬 Function to get the VS Install Path, because we need to know where the party is 🎬
function Get-PartyLocation {
    $vswherePath = Find-MyGlassSlipper
    if (Test-Path $vswherePath) {
        $vsPath = & $vswherePath -latest -property installationPath
        if ($null -ne $vsPath) {
            Write-Host "🎉 Found the party location: $vsPath 🎉"
            return $vsPath
        }
    }
    Write-Host "🎉 Party location not found. Time to abort this mission. 🎉"
    Read-Host "Smash that Enter key when you're ready to leave, but remember: you'll miss me. 😘"
    exit 1
}
# 🚀 Function to find where VS Code is hiding 🚀
function Get-VSCodeHideout {
    $vsCodeCommand = Get-Command code -ErrorAction SilentlyContinue
    if ($null -ne $vsCodeCommand) {
        return $vsCodeCommand.Source
    } else {
        Write-Host "🚀 VS Code is playing hide and seek. Time to abort this mission. 🚀"
        Read-Host "Smash that Enter key when you're ready to leave, but remember: you'll miss me. 😘"
        exit 1
    }
}
# 🎉 Let's get this party started! 🎉
$partyLocation = Get-PartyLocation
# Construct the full pumpkin carriage to take us to the party
$vsDevCmdPath = Join-Path $partyLocation "Common7\Tools\VsDevCmd.bat"

 Write-Host "🎉 Testing the $vsDevCmdPath🎉"
# See if the magic vegetable is road worthy
if (Test-Path $vsDevCmdPath) {
    # 🎶 Setting the mood with some environment variables 🎶
    $envVars = cmd /c "call `"$vsDevCmdPath`" && set"
    $envHash = @{}
    $envVars -split "`r`n" | ForEach-Object {
        $keyValue = $_ -split '=', 2
        $envHash[$keyValue[0]] = $keyValue[1]
    }

    # 🕺 Finding out where VS Code is hiding for the after-party 🕺
    $vsCodeHideout = Get-VSCodeHideout
     Write-Host "🎉 Found hidout:  $vsCodeHideout🎉"
    # 🏠 Setting the stage at your fabulous home directory 🏠
    $workingDir = $env:USERPROFILE
    Set-Location -Path $workingDir
    Write-Host "Set the stage: $vsCodeHideout"


    # 🎉 The Grand Finale: Let's get this party started! 🎉

    # 👀 Is VS Code already at the party? 👀
    $vsCodeProcess = Get-Process -Name "Code" -ErrorAction SilentlyContinue
    $argList = @()
    Write-Host "code process: $vsCodeProcess"

    if (-not [string]::IsNullOrEmpty($fileToEdit)) {
        $argList += $fileToEdit
    }


    # 🎉 If VS Code is already partying, let's not kill the vibe 🎉
    if ($null -ne $vsCodeProcess) {
        $argList = @("--reuse-window") + $argList
    
    }


    # Chain VsDevCmd.bat and code.cmd in a single cmd command line
    $cmdCommand = "$vsDevCmdPath"
    Write-Host "Launcher:  $cmdCommand"
    if ($argList.Count -gt 0) {
        $cmdArgs = [string]::Join(" ", $argList)
        $cmdCommand += " && $vsCodeHideout $cmdArgs"
    } else {
        $cmdCommand += " && $vsCodeHideout"
    }
# Fire it up!
    Write-Host "Launcher: cmd.exe -ArgumentList `"/k $cmdCommand`" -NoNewWindow"
Start-Process -FilePath "cmd.exe" -ArgumentList "/k $cmdCommand" -NoNewWindow

} else {
    # 😭 If we can't find the VsDevCmd.bat, the party's over 😭
    Write-Host "😭 VsDevCmd.bat is playing hide and seek. Time to abort this mission. 😭"
    exit 1
}
#copyright: public domain or whatever more or less permissive license you need to allow it to be integrated into your product I don't care. I just want VSCODE to function better. 
@zenerbufen zenerbufen changed the title Using VSCODE on windows with MSVC and MSBUILD is a huge pain Script to make Using VSCODE on windows with MSVC and MSBUILD much less of a huge pain Oct 25, 2023
@gregvanl
Copy link

Closing as not actionable.
Script is still available on GitHub vscode-docs repo in case others want to use this for VS Code and MSVC configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants