1+ <#
2+ . SYNOPSIS
3+ This is a Powershell script to bootstrap a Cake build.
4+ . DESCRIPTION
5+ This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
6+ and execute your Cake build script with the parameters you provide.
7+ . PARAMETER Target
8+ The build script target to run.
9+ . PARAMETER Configuration
10+ The build configuration to use.
11+ . PARAMETER Verbosity
12+ Specifies the amount of information to be displayed.
13+ . PARAMETER WhatIf
14+ Performs a dry run of the build script.
15+ No tasks will be executed.
16+ . PARAMETER ScriptArgs
17+ Remaining arguments are added here.
18+ . LINK
19+ http://cakebuild.net
20+ #>
21+
22+ [CmdletBinding ()]
23+ Param (
24+ [string ]$Target = " Default" ,
25+ [ValidateSet (" Release" , " Debug" )]
26+ [string ]$Configuration = " Release" ,
27+ [ValidateSet (" Quiet" , " Minimal" , " Normal" , " Verbose" , " Diagnostic" )]
28+ [string ]$Verbosity = " Verbose" ,
29+ [switch ]$WhatIf ,
30+ [Parameter (Position = 0 , Mandatory = $false , ValueFromRemainingArguments = $true )]
31+ [string []]$ScriptArgs
32+ )
33+
34+ $CakeVersion = " 0.19.3"
35+ $DotNetChannel = " preview" ;
36+ $DotNetVersion = " 1.0.3" ;
37+ $DotNetInstallerUri = " https://dot.net/v1/dotnet-install.ps1" ;
38+ $NugetUrl = " https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
39+
40+ # Make sure tools folder exists
41+ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
42+ $ToolPath = Join-Path $PSScriptRoot " tools"
43+ if (! (Test-Path $ToolPath )) {
44+ Write-Verbose " Creating tools directory..."
45+ New-Item - Path $ToolPath - Type directory | out-null
46+ }
47+
48+ # ##########################################################################
49+ # INSTALL .NET CORE CLI
50+ # ##########################################################################
51+
52+ Function Remove-PathVariable ([string ]$VariableToRemove )
53+ {
54+ $path = [Environment ]::GetEnvironmentVariable(" PATH" , " User" )
55+ if ($path -ne $null )
56+ {
57+ $newItems = $path.Split (' ;' , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
58+ [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join(' ;' , $newItems ), " User" )
59+ }
60+
61+ $path = [Environment ]::GetEnvironmentVariable(" PATH" , " Process" )
62+ if ($path -ne $null )
63+ {
64+ $newItems = $path.Split (' ;' , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
65+ [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join(' ;' , $newItems ), " Process" )
66+ }
67+ }
68+
69+ # Get .NET Core CLI path if installed.
70+ $FoundDotNetCliVersion = $null ;
71+ if (Get-Command dotnet - ErrorAction SilentlyContinue) {
72+ $FoundDotNetCliVersion = dotnet -- version;
73+ }
74+
75+ if ($FoundDotNetCliVersion -ne $DotNetVersion ) {
76+ $InstallPath = Join-Path $PSScriptRoot " .dotnet"
77+ if (! (Test-Path $InstallPath )) {
78+ mkdir - Force $InstallPath | Out-Null ;
79+ }
80+ (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri , " $InstallPath \dotnet-install.ps1" );
81+ & $InstallPath \dotnet- install.ps1 - Channel $DotNetChannel - Version $DotNetVersion - InstallDir $InstallPath ;
82+
83+ Remove-PathVariable " $InstallPath "
84+ $env: PATH = " $InstallPath ;$env: PATH "
85+ $env: DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
86+ $env: DOTNET_CLI_TELEMETRY_OPTOUT = 1
87+ }
88+
89+ # ##########################################################################
90+ # INSTALL NUGET
91+ # ##########################################################################
92+
93+ # Make sure nuget.exe exists.
94+ $NugetPath = Join-Path $ToolPath " nuget.exe"
95+ if (! (Test-Path $NugetPath )) {
96+ Write-Host " Downloading NuGet.exe..."
97+ (New-Object System.Net.WebClient).DownloadFile($NugetUrl , $NugetPath );
98+ }
99+
100+ # ##########################################################################
101+ # INSTALL CAKE
102+ # ##########################################################################
103+
104+ # Make sure Cake has been installed.
105+ $CakePath = Join-Path $ToolPath " Cake.$CakeVersion /Cake.exe"
106+ if (! (Test-Path $CakePath )) {
107+ Write-Host " Installing Cake..."
108+ Invoke-Expression " &`" $NugetPath `" install Cake -Version $CakeVersion -OutputDirectory `" $ToolPath `" " | Out-Null ;
109+ if ($LASTEXITCODE -ne 0 ) {
110+ Throw " An error occured while restoring Cake from NuGet."
111+ }
112+ }
113+
114+ # ##########################################################################
115+ # RUN BUILD SCRIPT
116+ # ##########################################################################
117+
118+ # Build the argument list.
119+ $Arguments = @ {
120+ target = $Target ;
121+ configuration = $Configuration ;
122+ verbosity = $Verbosity ;
123+ dryrun = $WhatIf ;
124+ }.GetEnumerator() | % {" --{0}=`" {1}`" " -f $_.key , $_.value };
125+
126+ # Start Cake
127+ Write-Host " Running build script..."
128+ Invoke-Expression " & `" $CakePath `" `" build.cake`" $Arguments $ScriptArgs "
129+ exit $LASTEXITCODE
0 commit comments