Where I work we use Visual Studio as IDE to run our main app that compile the Visual Studio solution .sln
and the IIS Sever.
Since I never liked Visual Studio as it's an heavy and bloated IDE, with many things that I will never use, I wanted to to find a more lightweight approach for my machine just using a code editor like Sublime Text/Visual Studio Code.
So, I wrote a small script that does switch to the project repository directory then use the msbuild tools and start a browser instance while running IIS Express Server.
Requirements:
- Visual Studio build tools 2019
- IIS 10+ Express
You can install those dependency with Chocolatey package manager for Windows
choco install visualstudio2019buildtools -y
choco install iisexpress -y
vsbuild.bat
@ECHO OFF
CD /D "C:\gitrepos\my-repo"
call "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/Common7/Tools/VsDevCmd.bat"
msbuild
start msedge http://localhost:49797/index.aspx
CD /D "C:/Program Files (x86)/IIS Express"
iisexpress /path:C:\gitrepos\my-repo /port:49797 /clr:v4.0
CMD
Place the vsbuild.bat
in your app root folder and then add an alias
If you don't know what
.bashrc
and alias is, please refer to this post: How to speed up Git terminal workflow with bash
.bashrc
#alias
vsb="./vsbuild.bat"
Run the vsbuild
vsb
Solve the same problem with a Powershell script
I wrote 2 version of this script.
First one just run the solution and IIS on Edge browser, the second let you pick between Edge or Firefox and the type of msbuild.
vsbuild-1.ps1
# Msbuild - Run Visual studio solution from command line, then start IIS Web Server and Open the site on localhost
# Paniconi Fabio
# 15-03-2021
# Define variables
$folder = $PSScriptRoot
$sln = "$folder/app.sln"
$url = "http://localhost:49797/index.aspx"
$firefox = "firefox"
$edge = "msedge.exe"
$port = "49797"
$iisExpressExe = '"C:\Program Files (x86)\IIS Express\iisexpress.exe"'
$path = (Resolve-path .)
$params = "/port:$port /path:$path"
$command = "$iisExpressExe $params"
function buildVS
{
param
(
[parameter(Mandatory=$true)]
[String] $path,
[parameter(Mandatory=$false)]
[bool] $nuget = $true,
[parameter(Mandatory=$false)]
[bool] $clean = $true
)
process
{
$msBuildExe = "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"
if ($nuget) {
Write-Host "`r"
Write-Host "Restoring NuGet packages" -foregroundcolor green
Write-Host "`r"
nuget restore "$($path)"
}
if ($clean) {
Write-Host "`r"
Write-Host "Cleaning $($path)" -foregroundcolor green
Write-Host "`r"
& "$($msBuildExe)" "$($path)" /t:Clean /m
}
Write-Host "`r"
Write-Host "Building $($path)" -foregroundcolor green
Write-Host "`r"
& "$($msBuildExe)" "$($path)" /t:Build /m
}
}
function vsBuild($witch)
{
if($witch -eq "rcb")
{
# Restore clean and build
buildVS $sln
}
if($witch -eq "cb")
{
# Cleab and build
buildVS $sln $false $true
}
if($witch -eq "b")
{
# Build
buildVS $sln -nuget $false -clean $false
}
if($witch -eq "rb")
{
# Restore and build
buildVS $sln -clean $false
}
}
vsBuild("b")
# Open firefox and start IIS express web server
Start-Process $edge $url
Write-Output "`r"
Write-Host $path
Write-Output "`r"
Write-host "Starting site on port: $port" -foregroundcolor green
cmd /c start cmd /k "$command"
Start-Sleep -m 1000
Write-Output "`r"
Write-Host "App started" -foregroundcolor green
Write-Output "`r"
vsbuild-2.ps1
# Msbuild - Run Visual studio solution from command line, then start IIS Web Server and Open the site on localhost
# Paniconi Fabio
# 15-03-2021
# Define variables
$folder = $PSScriptRoot
$sln = "$folder/app.sln"
$url = "http://localhost:49797/index.aspx"
$firefox = "firefox"
$edge = "msedge.exe"
$port = "49797"
$iisExpressExe = '"C:\Program Files (x86)\IIS Express\iisexpress.exe"'
$path = (Resolve-path .)
$params = "/port:$port /path:$path"
$command = "$iisExpressExe $params"
function buildVS
{
param
(
[parameter(Mandatory=$true)]
[String] $path,
[parameter(Mandatory=$false)]
[bool] $nuget = $true,
[parameter(Mandatory=$false)]
[bool] $clean = $true
)
process
{
$msBuildExe = "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"
if ($nuget) {
Write-Host "`r"
Write-Host "Restoring NuGet packages" -foregroundcolor green
Write-Host "`r"
nuget restore "$($path)"
}
if ($clean) {
Write-Host "`r"
Write-Host "Cleaning $($path)" -foregroundcolor green
Write-Host "`r"
& "$($msBuildExe)" "$($path)" /t:Clean /m
}
Write-Host "`r"
Write-Host "Building $($path)" -foregroundcolor green
Write-Host "`r"
& "$($msBuildExe)" "$($path)" /t:Build /m
}
}
function vsBuild($witch)
{
if($witch -eq "rcb")
{
# Restore clean and build
buildVS $sln
}
if($witch -eq "cb")
{
# Cleab and build
buildVS $sln $false $true
}
if($witch -eq "b")
{
# Build
buildVS $sln -nuget $false -clean $false
}
if($witch -eq "rb")
{
# Restore and build
buildVS $sln -clean $false
}
}
function RunServer($browser)
{
if($browser -eq "f")
{
Start-Process $firefox $url
}
if($browser -eq "e")
{
Start-Process $edge $url
}
}
Write-Host "`r"
Write-Host "If you have problem with restore builds due to nuget,please install:`r" -foregroundcolor yellow
Write-Host "`r"
Write-Host "choco install -y nuget.commandline" -foregroundcolor yellow
Write-Host "`r"
Write-Host "then restart the terminal and try again." -foregroundcolor yellow
Write-Host "`r"
Write-Host "Available MS-Builds:" -foregroundcolor green
Write-Host "`r"
Write-Host "rcb -- restore, clean and build" -foregroundcolor green
Write-Host "cb -- clean and build" -foregroundcolor green
Write-Host "b -- build" -foregroundcolor green
Write-Host "rb -- resotre and build" -foregroundcolor green
Write-Host "`r"
$buildType = Read-Host "Type the build to run"
if(($buildType -eq "rcb") -or ($buildType -eq "cb") -or ($buildType -eq "b") -or ($buildType -eq "rb"))
{
Write-Host "`r"
Write-Host "Available Browsers:" -foregroundcolor green
Write-Host "`r"
Write-Host "f -- Firefox" -foregroundcolor green
Write-Host "e -- Edge" -foregroundcolor green
Write-Host "`r"
$browser = Read-Host "Pick a browser"
}
else
{
Write-Host "`r"
Write-Host "This build do not exist, will now exit" -foregroundcolor red
Write-Host "`r"
Write-Host "To run again '.\ms-build'"
Write-Host "`r"
exit
}
if($browser -eq "f" -or $browser -eq "e")
{
vsBuild($buildType)
}
else
{
Write-Host "`r"
Write-Host "This browser do not exist, will now exit" -foregroundcolor red
Write-Host "`r"
Write-Host "To run again '.\ms-build'"
Write-Host "`r"
exit
}
# Open browser and start IIS express web server
RunServer($browser)
Write-Output "`r"
Write-Host $path
Write-Output "`r"
Write-host "Starting site on port: $port" -foregroundcolor green
cmd /c start cmd /k "$command"
Start-Sleep -m 1000
Write-Output "`r"
Write-Host "App started" -foregroundcolor green
Write-Output "`r"