From 42d47821b69ecdcf0cd31ef4dadeb1ce9a51888d Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Mon, 6 Jul 2020 22:23:25 +0200 Subject: [PATCH] (GH-75) Template PackAsTool & nuspec->csproj --- build/Lifetime.cs | 4 - build/Tasks/PackageTemplate.cs | 19 +-- build/Utilities/ToolInstaller.cs | 79 ------------ template/Cake.Frosting.Template.csproj | 34 +++++ template/Cake.Frosting.Template.nuspec | 31 ----- template/build.ps1 | 120 ------------------ .../.template.config/template.json | 0 .../cakefrosting/build}/Build.csproj | 9 +- .../cakefrosting/build}/Context.cs | 0 .../cakefrosting/build}/Lifetime.cs | 0 .../cakefrosting/build}/Program.cs | 0 .../cakefrosting/build}/Tasks/Default.cs | 0 .../cakefrosting/build}/Tasks/Hello.cs | 0 13 files changed, 49 insertions(+), 247 deletions(-) create mode 100644 template/Cake.Frosting.Template.csproj delete mode 100644 template/Cake.Frosting.Template.nuspec delete mode 100644 template/build.ps1 rename template/{ => templates/cakefrosting}/.template.config/template.json (100%) rename template/{ => templates/cakefrosting/build}/Build.csproj (64%) rename template/{ => templates/cakefrosting/build}/Context.cs (100%) rename template/{ => templates/cakefrosting/build}/Lifetime.cs (100%) rename template/{ => templates/cakefrosting/build}/Program.cs (100%) rename template/{ => templates/cakefrosting/build}/Tasks/Default.cs (100%) rename template/{ => templates/cakefrosting/build}/Tasks/Hello.cs (100%) diff --git a/build/Lifetime.cs b/build/Lifetime.cs index 10b9352..a98dc50 100644 --- a/build/Lifetime.cs +++ b/build/Lifetime.cs @@ -38,10 +38,6 @@ public override void Setup(Context context) context.IsPrimaryBranch = StringComparer.OrdinalIgnoreCase.Equals(context.PrimaryBranchName, buildSystem.AppVeyor.Environment.Repository.Branch); context.BuildSystem = buildSystem; - // Install tools - context.Information("Installing tools..."); - context.InstallNuGetExe("5.6.0"); - // Install Global .Net Tools context.Information("Installing .Net Global Tools..."); context.DotNetCoreToolInstall("GitReleaseManager.Tool", "0.11.0", "dotnet-gitreleasemanager"); diff --git a/build/Tasks/PackageTemplate.cs b/build/Tasks/PackageTemplate.cs index 6ca53d8..3548c2c 100644 --- a/build/Tasks/PackageTemplate.cs +++ b/build/Tasks/PackageTemplate.cs @@ -1,7 +1,7 @@ -using System.Collections.Generic; -using Cake.Common.Tools.NuGet; -using Cake.Common.Tools.NuGet.Pack; +using Cake.Common.Tools.DotNetCore; +using Cake.Common.Tools.DotNetCore.Pack; using Cake.Common.Xml; +using Cake.Core.IO; using Cake.Frosting; public class PackageTemplate : FrostingTask @@ -11,17 +11,18 @@ public override void Run(Context context) if (context.AppVeyor) { context.XmlPoke( - "./template/Build.csproj", + "./template/templates/cakefrosting/build/Build.csproj", "/Project/ItemGroup/PackageReference[@Include = 'Cake.Frosting']/@Version", context.Version.SemVersion ); } - context.NuGetPack("./template/Cake.Frosting.Template.nuspec", new NuGetPackSettings + var path = new FilePath("./template/Cake.Frosting.Template.csproj"); + context.DotNetCorePack(path.FullPath, new DotNetCorePackSettings { - Version = context.Version.SemVersion, - OutputDirectory = context.Artifacts, - NoPackageAnalysis = true + Configuration = context.BuildConfiguration, + MSBuildSettings = context.MSBuildSettings, + OutputDirectory = context.Artifacts }); } -} \ No newline at end of file +} diff --git a/build/Utilities/ToolInstaller.cs b/build/Utilities/ToolInstaller.cs index b5e587a..e68364c 100644 --- a/build/Utilities/ToolInstaller.cs +++ b/build/Utilities/ToolInstaller.cs @@ -1,12 +1,6 @@ -using System; -using System.IO; -using Cake.Common.Diagnostics; using Cake.Common.IO; -using Cake.Common.Net; using Cake.Common.Tools.DotNetCore; using Cake.Common.Tools.DotNetCore.Tool; -using Cake.Common.Tools.NuGet; -using Cake.Common.Tools.NuGet.Install; using Cake.Core; using Cake.Core.IO; @@ -14,79 +8,6 @@ public static class ToolInstaller { private static DirectoryPath ToolsPath { get; } = "./tools"; - public static void InstallNuGetExe(this ICakeContext context, string version) - { - - var toolsPath = context.MakeAbsolute(ToolsPath); - - context.EnsureDirectoryExists(toolsPath); - - var nugetExePath = toolsPath.CombineWithFilePath("nuget.exe"); - - if (context.FileExists(nugetExePath)) - { - if (TryValidateVersion(version, nugetExePath, out var existingVersion)) - { - context.Tools.RegisterFile(nugetExePath); - return; - } - - context.Information( - "Found version {0} expected {1}, deleting {2}...", - existingVersion, - version, - toolsPath - ); - - context.DeleteFile(nugetExePath); - } - - var address = $"https://dist.nuget.org/win-x86-commandline/v{Uri.EscapeDataString(version)}/nuget.exe"; - context.Information("Downloading NuGet exe from {0} to {1}...", address, nugetExePath); - context.DownloadFile( - address, - nugetExePath - ); - - if (!context.FileExists(nugetExePath)) - { - throw new Exception("Failed to install NuGet exe."); - } - - - if (!TryValidateVersion(version, nugetExePath, out var downloadedFileVersion)) - { - throw new Exception($"Expected version {version} got {downloadedFileVersion}"); - } - - context.Tools.RegisterFile(nugetExePath); - context.Information("NuGet exe downloaded and registered successfully."); - } - - private static bool TryValidateVersion(string expectedVersion, FilePath nugetExePath, out string foundVersion) - { - try - { - var fileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(nugetExePath.FullPath); - foundVersion = FormattableString.Invariant($"{fileVersion.FileMajorPart}.{fileVersion.FileMinorPart}.{fileVersion.FileBuildPart}"); - return foundVersion == expectedVersion; - } - catch(Exception ex) - { - foundVersion = ex.Message; - return false; - } - } - - public static void Install(this ICakeContext context, string package, string version) - { - context.NuGetInstall(package, new NuGetInstallSettings { - Version = version, - ExcludeVersion = true, - OutputDirectory = ToolsPath - }); - } - public static FilePath DotNetCoreToolInstall( this ICakeContext context, string package, diff --git a/template/Cake.Frosting.Template.csproj b/template/Cake.Frosting.Template.csproj new file mode 100644 index 0000000..b9ed9c6 --- /dev/null +++ b/template/Cake.Frosting.Template.csproj @@ -0,0 +1,34 @@ + + + + Template + Cake.Frosting.Template + Cake.Frosting templates for the .NET SDK. + Cake.Frosting templates for the .NET SDK. + Patrik Svensson, Mattias Karlsson, Gary Ewan Park, Alistair Chapman, Martin Björkström and contributors + Copyright (c) .NET Foundation and contributors + Cake;Script;Build + cake-medium.png + MIT + git + https://github.com/cake-build/frosting + netcoreapp3.1 + + true + false + content + NU5110;NU5111;NU5128 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/template/Cake.Frosting.Template.nuspec b/template/Cake.Frosting.Template.nuspec deleted file mode 100644 index a5adb17..0000000 --- a/template/Cake.Frosting.Template.nuspec +++ /dev/null @@ -1,31 +0,0 @@ - - - - Cake.Frosting.Template - Cake.Frosting templates for the .NET SDK. - Cake (C# Make) is a build automation system with a C# DSL to do things like compiling code, copy files/folders, running unit tests, compress files and build NuGet packages. - Patrik Svensson, Mattias Karlsson, Gary Ewan Park, Alistair Chapman, Martin Björkström and contributors - MIT - https://github.com/cake-build/frosting - cake-medium.png - https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-medium.png - false - Copyright (c) .NET Foundation and contributors - Cake Script Build - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/template/build.ps1 b/template/build.ps1 deleted file mode 100644 index 8bd24ea..0000000 --- a/template/build.ps1 +++ /dev/null @@ -1,120 +0,0 @@ -<# -.SYNOPSIS -This is a Powershell script to bootstrap a Cake build. -.DESCRIPTION -This Powershell script will download NuGet if missing, restore NuGet tools (including Cake) -and execute your Cake build script with the parameters you provide. -.PARAMETER Target -The build script target to run. -.PARAMETER Configuration -The build configuration to use. -.PARAMETER Verbosity -Specifies the amount of information to be displayed. -.PARAMETER WhatIf -Performs a dry run of the build script. -No tasks will be executed. -.PARAMETER ScriptArgs -Remaining arguments are added here. -.LINK -https://cakebuild.net -#> - -[CmdletBinding()] -Param( - [string]$Target = "Default", - [ValidateSet("Release", "Debug")] - [string]$Configuration = "Release", - [ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")] - [string]$Verbosity = "Verbose", - [switch]$WhatIf, - [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)] - [string[]]$ScriptArgs -) - -$DotNetVersion = "3.1.301"; -$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1"; -$NugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" - -# 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 -} - -########################################################################### -# INSTALL .NET CORE CLI -########################################################################### - -Function Remove-PathVariable([string]$VariableToRemove) -{ - $path = [Environment]::GetEnvironmentVariable("PATH", "User") - $newItems = $path.Split(';') | Where-Object { $_.ToString() -inotlike $VariableToRemove } - [Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "User") - $path = [Environment]::GetEnvironmentVariable("PATH", "Process") - $newItems = $path.Split(';') | Where-Object { $_.ToString() -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 -Version $DotNetVersion -InstallDir $InstallPath; - - Remove-PathVariable "$InstallPath" - $env:PATH = "$InstallPath;$env:PATH" - $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 - $env:DOTNET_CLI_TELEMETRY_OPTOUT=1 -} - -########################################################################### -# INSTALL NUGET -########################################################################### - -# Make sure nuget.exe exists. -$NugetPath = Join-Path $ToolPath "nuget.exe" -if (!(Test-Path $NugetPath)) { - Write-Host "Downloading NuGet.exe..." - (New-Object System.Net.WebClient).DownloadFile($NugetUrl, $NugetPath); -} - -########################################################################### -# RUN BUILD SCRIPT -########################################################################### - -# Build the argument list. -$Arguments = @{ - target=$Target; - configuration=$Configuration; - verbosity=$Verbosity; - dryrun=$WhatIf; -}.GetEnumerator() | ForEach-Object { "--{0}=`"{1}`"" -f $_.key, $_.value }; - -try { - Push-Location - Set-Location build - Write-Host "Restoring packages..." - Invoke-Expression "dotnet restore" - if($LASTEXITCODE -eq 0) { - Write-Output "Compiling build..." - Invoke-Expression "dotnet publish -c Debug /v:q /nologo" - if($LASTEXITCODE -eq 0) { - Write-Output "Running build..." - Invoke-Expression "bin/Debug/net461/publish/Build.exe $Arguments" - } - } -} -finally { - Pop-Location - exit $LASTEXITCODE; -} diff --git a/template/.template.config/template.json b/template/templates/cakefrosting/.template.config/template.json similarity index 100% rename from template/.template.config/template.json rename to template/templates/cakefrosting/.template.config/template.json diff --git a/template/Build.csproj b/template/templates/cakefrosting/build/Build.csproj similarity index 64% rename from template/Build.csproj rename to template/templates/cakefrosting/build/Build.csproj index 04460f5..9a16e83 100644 --- a/template/Build.csproj +++ b/template/templates/cakefrosting/build/Build.csproj @@ -2,14 +2,15 @@ Exe - netcoreapp3.0 - - + netcoreapp3.1 + true + + $(MSBuildProjectDirectory) - + diff --git a/template/Context.cs b/template/templates/cakefrosting/build/Context.cs similarity index 100% rename from template/Context.cs rename to template/templates/cakefrosting/build/Context.cs diff --git a/template/Lifetime.cs b/template/templates/cakefrosting/build/Lifetime.cs similarity index 100% rename from template/Lifetime.cs rename to template/templates/cakefrosting/build/Lifetime.cs diff --git a/template/Program.cs b/template/templates/cakefrosting/build/Program.cs similarity index 100% rename from template/Program.cs rename to template/templates/cakefrosting/build/Program.cs diff --git a/template/Tasks/Default.cs b/template/templates/cakefrosting/build/Tasks/Default.cs similarity index 100% rename from template/Tasks/Default.cs rename to template/templates/cakefrosting/build/Tasks/Default.cs diff --git a/template/Tasks/Hello.cs b/template/templates/cakefrosting/build/Tasks/Hello.cs similarity index 100% rename from template/Tasks/Hello.cs rename to template/templates/cakefrosting/build/Tasks/Hello.cs