Skip to content

Commit

Permalink
Create VS shortcut with SDK build environment automatically loaded (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MiYanni authored Jul 7, 2023
2 parents 6908ca1 + 4fbb0b5 commit 4834962
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion documentation/project-docs/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ The build script will output a `dotnet` installation to `artifacts\bin\redist\De

As part of the build, some intermediate files will get generated which may run into long-path issues. If you encounter a build failure with an error message similar to `Resource file [filename].resx cannot be found.`, [enable long paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later) and try again.

To open the solution in Visual Studio, be sure to build with `build.cmd` and run the generated environment for your shell. If you're using `cmd`, then run `artifacts\sdk-build-env.bat`. If you're using powershell, you need to 'dot source' `artifacts/sdk-build-env.ps1`. Finally, open Visual Studio with `devenv sdk.sln`.
#### Using Visual Studio

The simple way to launch Visual Studio after building via `build.cmd` is to double-click the `VS with sdk.sln` Windows shortcut in the `artifacts` folder. This will load the generated environment automatically and launch Visual Studio with the `sdk.sln` solution.

Alternatively, to open the solution in Visual Studio, be sure to build with `build.cmd` and run the generated environment for your shell. If you're using `cmd`, then run `artifacts\sdk-build-env.bat`. If you're using PowerShell, you need to 'dot source' `artifacts/sdk-build-env.ps1`. Finally, open Visual Studio with `devenv sdk.sln`.

In addition, Visual Studio must have the following option set:

Expand Down
35 changes: 35 additions & 0 deletions eng/restore-toolset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function InitializeCustomSDKToolset {
InstallDotNetSharedFramework "7.0.0"

CreateBuildEnvScripts
CreateVSShortcut
InstallNuget
}

Expand Down Expand Up @@ -83,6 +84,40 @@ function killdotnet {
Out-File -FilePath $scriptPath -InputObject $scriptContents -Encoding ASCII
}

function CreateVSShortcut()
{
# https://github.com/microsoft/vswhere/wiki/Installing
$installerPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer"
if(-Not (Test-Path -Path $installerPath))
{
return
}

# Note: The VS version in this call would need to be updated as new VS major versions release.
$vsVersion = 17.0
$devenvPath = (& "$installerPath\vswhere.exe" -all -prerelease -latest -version $vsVersion -find Common7\IDE\devenv.exe) | Select-Object -First 1
if(-Not $devenvPath)
{
return
}

$scriptPath = Join-Path $ArtifactsDir 'sdk-build-env.ps1'
$slnPath = Join-Path $RepoRoot 'sdk.sln'
$commandToLaunch = "& '$scriptPath'; & '$devenvPath' '$slnPath'"
$powershellPath = '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe'
$shortcutPath = Join-Path $ArtifactsDir 'VS with sdk.sln.lnk'

# https://stackoverflow.com/a/9701907/294804
# https://learn.microsoft.com/en-us/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh
$wsShell = New-Object -ComObject WScript.Shell
$shortcut = $wsShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $powershellPath
$shortcut.Arguments = "-WindowStyle Hidden -Command ""$commandToLaunch"""
$shortcut.IconLocation = $devenvPath
$shortcut.WindowStyle = 7 # Minimized
$shortcut.Save()
}

function InstallDotNetSharedFramework([string]$version) {
$dotnetRoot = $env:DOTNET_INSTALL_DIR
$fxDir = Join-Path $dotnetRoot "shared\Microsoft.NETCore.App\$version"
Expand Down

0 comments on commit 4834962

Please sign in to comment.