Skip to content

Instantly share code, notes, and snippets.

@colinangusmackay
Last active April 24, 2019 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinangusmackay/ddb0fe0b6c3f9d6f2958de8b8d8c4dfb to your computer and use it in GitHub Desktop.
Save colinangusmackay/ddb0fe0b6c3f9d6f2958de8b8d8c4dfb to your computer and use it in GitHub Desktop.
Demonstrate issue with Cake and using System.Data.SqlClient NuGet package
[CmdletBinding()]
Param(
[string]$Script = "build.cake",
[string]$Target,
[string]$Configuration,
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
[string]$Verbosity,
[switch]$ShowDescription,
[Alias("WhatIf", "Noop")]
[switch]$DryRun,
[switch]$Experimental,
[switch]$Mono,
[switch]$SkipToolPackageRestore,
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
[string[]]$ScriptArgs
)
$CakeVersion = "0.33.0"
$DotNetVersion = "2.2.106";
$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1";
# Make sure tools folder exists
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$ToolPath = Join-Path $PSScriptRoot "tools"
if (!(Test-Path $ToolPath)) {
Write-Verbose "Creating tools directory..."
New-Item -Path $ToolPath -Type directory | out-null
}
# Attempt to set highest encryption available for SecurityProtocol.
# PowerShell will not set this by default (until maybe .NET 4.6.x). This
# will typically produce a message for PowerShell v2 (just an info
# message though)
try {
# Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
# Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
# exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
# installed (.NET 4.5 is an in-place upgrade).
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
} catch {
Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3'
}
###########################################################################
# INSTALL .NET CORE CLI
###########################################################################
Function Remove-PathVariable([string]$VariableToRemove)
{
$path = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($path -ne $null)
{
$newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "User")
}
$path = [Environment]::GetEnvironmentVariable("PATH", "Process")
if ($path -ne $null)
{
$newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "Process")
}
}
# Get .NET Core CLI path if installed.
$FoundDotNetCliVersion = $null;
if (Get-Command dotnet -ErrorAction SilentlyContinue) {
$FoundDotNetCliVersion = dotnet --version;
}
if($FoundDotNetCliVersion -ne $DotNetVersion) {
$InstallPath = Join-Path $PSScriptRoot ".dotnet"
if (!(Test-Path $InstallPath)) {
mkdir -Force $InstallPath | Out-Null;
}
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, "$InstallPath\dotnet-install.ps1");
& $InstallPath\dotnet-install.ps1 -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;
Remove-PathVariable "$InstallPath"
$env:PATH = "$InstallPath;$env:PATH"
}
$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=0
$env:DOTNET_CLI_TELEMETRY_OPTOUT=1
###########################################################################
# INSTALL CAKE
###########################################################################
# Make sure Cake has been installed.
$CakePath = Join-Path $ToolPath ".store\cake.tool\$CakeVersion"
$CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)
if ((!(Test-Path -Path $CakePath -PathType Container)) -or (!(Test-Path $CakeExePath -PathType Leaf))) {
$AllCakeToolPath = Join-Path $CakePath "..";
if (Test-Path -Path $AllCakeToolPath){
$AllCakeToolPaths = Get-ChildItem -Path $AllCakeToolPath -Directory
if ($AllCakeToolPaths.Length -ge 1){
& dotnet tool uninstall Cake.Tool --tool-path $ToolPath
}
}
& dotnet tool install --tool-path $ToolPath --version $CakeVersion Cake.Tool
if ($LASTEXITCODE -ne 0)
{
'Failed to install cake'
exit 1
}
$CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)
}
###########################################################################
# RUN BUILD SCRIPT
###########################################################################
# Build Cake arguments
$bootstrapArguments = @("$Script");
if ($Verbosity) { $bootstrapArguments += "-verbosity=$Verbosity" }
$bootstrapArguments += "--bootstrap";
Write-Host "Bootstrapping $Script with arguments:";
Write-Host $bootstrapArguments;
& "$CakeExePath" $bootstrapArguments
if ($LASTEXITCODE -eq 0)
{
$cakeArguments = @("$Script");
if ($Target) { $cakeArguments += "-target=$Target" }
if ($Configuration) { $cakeArguments += "-configuration=$Configuration" }
if ($Verbosity) { $cakeArguments += "-verbosity=$Verbosity" }
if ($ShowDescription) { $cakeArguments += "-showdescription" }
if ($DryRun) { $cakeArguments += "-dryrun" }
$cakeArguments += $ScriptArgs
Write-Host "";
Write-Host "Running $Script with arguments:"
Write-Host $cakeArguments;
& "$CakeExePath" $cakeArguments
}
exit $LASTEXITCODE
#addin nuget:?package=System.Data.SqlClient&version=4.6.0&exclude=runtimes/**/*.dll
using System.Text.RegularExpressions;
using System.Data.SqlClient;
public string GetLocalConnectionString(string originalConnectionString)
{
Information("Original Connection String: "+originalConnectionString);
var builder = new SqlConnectionStringBuilder(originalConnectionString);
// Do stuff to modify the connection string.
return builder.ToString();
}
#load "common-migrations.cake"
Task("Publish-Migrations")
.Does(() => {
// Do things to publish the migrations to an artefacts folder.
});
Task("Run-Migrations")
.Does(()=>{
string connectionString = GetLocalConnectionString("Server=my-server;Database=my-database;trusted_connection=true;application name=MigrationViaCakeScript;");
Information("The local connection string being used is : "+connectionString);
// Other things to run in the migration.
});
Task("Migrate-Local")
.IsDependentOn("Publish-Migrations")
.IsDependentOn("Run-Migrations");
RunTarget("Migrate-Local");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment