From 281e1a7559daa7053a07a86ed61fba07682b7890 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Fri, 6 Dec 2024 16:52:33 -0300 Subject: [PATCH] Add `PackageDownload` to download tools on the fly. --- Build/Build.csproj | 2 +- Build/IAzureSignTool.cs | 3 + CHANGELOG.md | 5 +- ricaun.Nuke/Tools/AzureSignToolUtils.cs | 94 +++++++++++++++++++++++++ ricaun.Nuke/ricaun.Nuke.csproj | 5 +- 5 files changed, 103 insertions(+), 6 deletions(-) diff --git a/Build/Build.csproj b/Build/Build.csproj index fd86e57..16ed451 100644 --- a/Build/Build.csproj +++ b/Build/Build.csproj @@ -19,6 +19,6 @@ - + diff --git a/Build/IAzureSignTool.cs b/Build/IAzureSignTool.cs index 31688b5..4f07aea 100644 --- a/Build/IAzureSignTool.cs +++ b/Build/IAzureSignTool.cs @@ -8,6 +8,9 @@ public interface IAzureSignTool : IClean, ICompile .Before(Compile) .Executes(() => { + ricaun.Nuke.Tools.AzureSignToolUtils.DownloadAzureSignTool(); + ricaun.Nuke.Tools.AzureSignToolUtils.DownloadNuGetKeyVaultSignTool(); + ricaun.Nuke.Tools.AzureSignToolUtils.EnsureAzureToolIsInstalled(); }); } diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ac584..3283d4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,12 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Enable sign files using `Azure Key Vault`. ### Build - Add `IAzureSignTool` to check if `AzureSignToolUtils` is installed. -- Add import `build` with `.targets` +- [ ] Add import `build` with `.targets` ### Updates - Add `AzureSignToolUtils` to sign files using `AzureSignToolTasks` or `NuGetKeyVaultSignToolTasks`. - Add `NuGetKeyVaultSignTool` for nuke version `8.*`. - Add `AzureKeyVaultConfig` with json file with `Azure Key Vault` without secrets. -- Add `build` with `.targets` to install packages `AzureSignTool` and `NuGetKeyVaultSignTool`. +- [ ] Add `build` with `.targets` to install packages `AzureSignTool` and `NuGetKeyVaultSignTool`. +- Add `PackageDownload` to download `AzureSignTool` and `NuGetKeyVaultSignTool` on the fly. ### Tests - Update `NuGetExtensionTests` - Add `AzureKeyVaultConfigTests` diff --git a/ricaun.Nuke/Tools/AzureSignToolUtils.cs b/ricaun.Nuke/Tools/AzureSignToolUtils.cs index 5e4dc22..f93098e 100644 --- a/ricaun.Nuke/Tools/AzureSignToolUtils.cs +++ b/ricaun.Nuke/Tools/AzureSignToolUtils.cs @@ -6,6 +6,8 @@ using ricaun.Nuke.Tools.NuGetKeyVaultSignTool; using Nuke.Common.Tools.AzureSignTool; using System.IO; +using Nuke.Common.Tools.DotNet; +using Nuke.Common.IO; namespace ricaun.Nuke.Tools { @@ -24,6 +26,9 @@ public class AzureSignToolUtils /// Thrown when the required packages are missing. public static void EnsureAzureToolIsInstalled() { + DownloadAzureSignTool(); + DownloadNuGetKeyVaultSignTool(); + try { _ = AzureSignToolTasks.AzureSignToolPath; @@ -41,6 +46,93 @@ public static void EnsureAzureToolIsInstalled() } } + private static AbsolutePath GetToolInstallationPath() + { + AbsolutePath folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + return folder / "Tools"; + } + + private static string PackageDownload(string packageId) + { + var toolFolder = GetToolInstallationPath(); + + DotNetTasks.DotNetToolInstall(x => x + .SetPackageName(packageId) + .SetToolInstallationPath(toolFolder) + ); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath absolutePath) + { + return absolutePath; + } + return null; + } + + /// + /// Download AzureSignTool if not already installed. + /// + public static void DownloadAzureSignTool() + { + try + { + _ = AzureSignToolTasks.AzureSignToolPath; + } + catch (Exception) + { + var packageId = AzureSignToolTasks.AzureSignToolPackageId; + var packageToolExe = PackageDownload(packageId); + + Environment.SetEnvironmentVariable(packageId.ToUpper() + "_EXE", packageToolExe); + } + + _ = AzureSignToolTasks.AzureSignToolPath; + } + + /// + /// Download NuGetKeyVaultSignTool if not already installed. + /// + public static void DownloadNuGetKeyVaultSignTool() + { + try + { + _ = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPath; + } + catch (Exception) + { + var packageId = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPackageId; + var packageToolExe = PackageDownload(packageId); + + Environment.SetEnvironmentVariable(packageId.ToUpper() + "_EXE", packageToolExe); + } + + _ = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPath; + } + + private static void DownloadNuGetKeyVaultSignTool_() + { + try + { + _ = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPath; + } + catch (Exception) + { + var toolFolder = GetToolInstallationPath(); + var packageId = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPackageId; + + DotNetTasks.DotNetToolInstall(x => x + .SetPackageName(packageId) + .SetToolInstallationPath(toolFolder) + ); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath packageToolPath) + { + Environment.SetEnvironmentVariable(packageId.ToUpper() + "_EXE", packageToolPath); + } + } + + _ = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPath; + } + /// /// Signs the specified file using Azure Sign Tool or NuGet Key Vault Sign Tool. /// @@ -56,6 +148,7 @@ public static void Sign(string fileName, { if (Path.GetExtension(fileName) == NugetPackageExtension) { + DownloadNuGetKeyVaultSignTool(); NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignTool(x => x .SetFile(fileName) .SetKeyVaultCertificateName(azureKeyVaultConfig.AzureKeyVaultCertificate) @@ -69,6 +162,7 @@ public static void Sign(string fileName, return; } + DownloadAzureSignTool(); AzureSignToolTasks.AzureSignTool(x => x .SetFiles(fileName) .SetKeyVaultCertificateName(azureKeyVaultConfig.AzureKeyVaultCertificate) diff --git a/ricaun.Nuke/ricaun.Nuke.csproj b/ricaun.Nuke/ricaun.Nuke.csproj index 28dabdf..b2aef83 100644 --- a/ricaun.Nuke/ricaun.Nuke.csproj +++ b/ricaun.Nuke/ricaun.Nuke.csproj @@ -8,7 +8,7 @@ ricaun.Nuke - 1.9.0-alpha.1 + 1.9.0-alpha.2 @@ -78,8 +78,7 @@ - - +