Skip to content

Instantly share code, notes, and snippets.

@tinohager
Last active May 18, 2018 21:57
Show Gist options
  • Save tinohager/cc811866172b694c8762301133ed1e68 to your computer and use it in GitHub Desktop.
Save tinohager/cc811866172b694c8762301133ed1e68 to your computer and use it in GitHub Desktop.
Build script with unit test and nuget push, for visual studio 2017 community and BuildTools
$buildEnvironmentVisualStudio2017BuildTools = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\";
$buildEnvironmentVisualStudio2017Community = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\";
$msbuild = "";
$vstest = "";
$nuget = "..\nuget.exe";
$nugetDownloadUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
# Detect build environment
if (Test-Path $buildEnvironmentVisualStudio2017BuildTools -PathType Container) {
# https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15
Write-Host "Visual Studio 2017 BuildTools detected";
$msbuild = $buildEnvironmentVisualStudio2017BuildTools + "\MSBuild\15.0\Bin\MSBuild.exe";
$vstest = $buildEnvironmentVisualStudio2017BuildTools + "\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe";
} elseif (Test-Path $buildEnvironmentVisualStudio2017Community -PathType Container) {
Write-Host "Visual Studio 2017 Community detected";
$msbuild = $buildEnvironmentVisualStudio2017Community + "\MSBuild\15.0\Bin\MSBuild.exe";
$vstest = $buildEnvironmentVisualStudio2017Community + "\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe";
} else {
Write-Error "No compatible build environment detect";
exit 1;
}
# Check needed file are exists
if (-Not ([System.IO.File]::Exists($msbuild))) {
Write-Error "msbuild not found (MSBuild.exe)";
exit 1;
}
if (-Not ([System.IO.File]::Exists($vstest))) {
Write-Error "vstest not found (vstest.console.exe)";
exit 1;
}
# Check is nuget available otherwise download
if (-Not (Test-Path -Path $nuget)) {
Write-Host "nuget.exe not found download start"
Invoke-WebRequest -Uri $nugetDownloadUrl -OutFile $nuget;
}
# Find Solution file and build this
Get-ChildItem -Path .\ -Filter *.sln -Recurse -File -Name| ForEach-Object {
# info -> $_ = filename
# restore nuget package for solution
# ----------------------------------------------
# add custom nuget source
&$nuget sources Add -Name "nuget.privatenuget.com" -source "http://nuget.privatenuget.com/nuget";
# restore microsoft nuget server
&$nuget restore $_;
# build project
# -----------------------------------------------
#&$msbuild ($_, '/p:Configuration=Release', '/t:Clean,Build');
&$msbuild ($_, '/verbosity:q', '/p:Configuration=Release', '/t:Clean,Build');
# info -> $? = Returns True or False value indicating whether previous command ended with an error
if (!$?) {
Write-Error "error build $_";
exit 1;
}
Write-Host "build successful $_";
}
# start unit tests
Get-ChildItem -Path .\ -Filter *.UnitTest.dll -Recurse -File -Name| ForEach-Object {
if ($_ -like '*\bin\Release\*') {
Write-Host "start unit test $_";
&$vstest $_;
if (!$?) {
Write-Error "error unit test $_";
exit 1;
}
} else {
Write-Host "ignore unit test dll, wrong directory $_";
}
}
# Find all projects with nuspec files and create a nuget package
Get-ChildItem -Path .\ -Filter *.nuspec -Recurse -File -Name| ForEach-Object {
Write-Host "$_";
$file = (Get-Item $_);
$projectFile = "$($file.DirectoryName)\$($file.Basename).csproj";
#&$nuget pack $projectFile -Build -Properties Configuration=Release;
&$nuget pack $projectFile -Properties Configuration=Release;
}
# Publish nuget packages to nuget.privatenuget.com
Get-ChildItem -Path .\ -Filter *.nupkg -File -Name| ForEach-Object {
$file = (Get-Item $_);
# Push to nuget
&$nuget push $file.Name -ApiKey XXXXXXXXXXXXXX -Source http://nuget.privatenuget.com/;
# Remove package from disk
Remove-Item $_;
}
@tinohager
Copy link
Author

Optimize visual studio selection with this script
https://github.com/ilyabreev/Bia.Countries/blob/master/nuget%20deploy.cmd

convert to powershell

for /f "usebackq tokens=1* delims=: " %%i in ("%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -requires Microsoft.Component.MSBuild) do (
if /i "%%i"=="installationPath" set InstallDir=%%j
)

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