diff --git a/Build/INuGetKeyVaultSign.cs b/Build/INuGetKeyVaultSign.cs index 4235287..6742ea0 100644 --- a/Build/INuGetKeyVaultSign.cs +++ b/Build/INuGetKeyVaultSign.cs @@ -9,21 +9,56 @@ using Nuke.Common.Tools.NuGetKeyVaultSignTool; using Nuke.NuGetKeyVaultSignTool; using System.Linq; +using Nuke.Common.Tools.NuGet; +using Nuke.Common.Tools.DotNet; public interface INuGetKeyVaultSign : IClean, ICompile { private static string AZURE_KEY_VAULT_FILE => Environment.GetEnvironmentVariable("AZURE_KEY_VAULT_FILE"); private static string AZURE_KEY_VAULT_PASSWORD => Environment.GetEnvironmentVariable("AZURE_KEY_VAULT_PASSWORD"); + 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(); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath packageToolExeExists) + { + Serilog.Log.Information("AzureSignTool"); + return packageToolExeExists; + } + + DotNetTasks.DotNetToolInstall(x => x + .SetPackageName(packageId) + .SetToolInstallationPath(toolFolder) + ); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath packageToolExe) + { + return packageToolExe; + } + return null; + } + Target NuGetKeyVaultSign => _ => _ .TriggeredBy(Clean) .Before(Compile) .Executes(() => { - new AzureSignToolTests().SetupEnvironmentToolPath(); - Serilog.Log.Information(AzureSignToolTasks.AzureSignToolPath); + Serilog.Log.Information("NuGetKeyVaultSign"); + + //new AzureSignToolTests().SetupEnvironmentToolPath(); + //new NuGetKeyVaultSignToolTests().SetupEnvironmentToolPath(); + + ricaun.Nuke.Tools.AzureSignToolUtils.DownloadAzureSignTool(); + ricaun.Nuke.Tools.AzureSignToolUtils.DownloadNuGetKeyVaultSignTool(); - new NuGetKeyVaultSignToolTests().SetupEnvironmentToolPath(); + Serilog.Log.Information(AzureSignToolTasks.AzureSignToolPath); Serilog.Log.Information(NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPath); if (string.IsNullOrEmpty(AZURE_KEY_VAULT_FILE)) @@ -83,5 +118,11 @@ public interface INuGetKeyVaultSign : IClean, ICompile var lengthAfter = (double)new System.IO.FileInfo(fullPath).Length; Serilog.Log.Warning($"Sign package {fullPath.Name} - {lengthAfter} {length}"); + + fullPath = fileNameToSign.Copy(rootAssembly / "package-ricaun.nupkg", ExistsPolicy.FileOverwrite); + length = (double)new System.IO.FileInfo(fullPath).Length; + ricaun.Nuke.Tools.AzureSignToolUtils.Sign(fullPath, ricaun.Nuke.Tools.AzureKeyVaultConfig.Create(AZURE_KEY_VAULT_FILE), AZURE_KEY_VAULT_PASSWORD); + lengthAfter = (double)new System.IO.FileInfo(fullPath).Length; + Serilog.Log.Warning($"Sign package {fullPath.Name} - {lengthAfter} {length}"); }); } diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eb775c..7b9a0ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Test `NuGetKeyVaultSignTool` in a package. - Test `AzureSignTool` in a assembly file, show subject with `GetSignedFileSubject`. - Test `AzureSignTool` in a install.exe file. +- Add `PackageDownloadUtils` to force download tool package in local tool folder. [vNext]: ../../compare/1.0.0...HEAD [1.0.0]: ../../compare/1.0.0 \ No newline at end of file diff --git a/Nuke.NuGetKeyVaultSignTool/AzureSignToolTests.cs b/Nuke.NuGetKeyVaultSignTool/AzureSignToolTests.cs index abd8d72..43a1c8a 100644 --- a/Nuke.NuGetKeyVaultSignTool/AzureSignToolTests.cs +++ b/Nuke.NuGetKeyVaultSignTool/AzureSignToolTests.cs @@ -24,11 +24,13 @@ public class AzureSignToolTests [OneTimeSetUp] public void SetupEnvironmentToolPath() { - var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - foreach (var item in EnvironmentToolPath) - { - Environment.SetEnvironmentVariable(item.Key, Path.Combine(userFolder, item.Value)); - } + //var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + //foreach (var item in EnvironmentToolPath) + //{ + // Environment.SetEnvironmentVariable(item.Key, Path.Combine(userFolder, item.Value)); + //} + var packageId = AzureSignToolTasks.AzureSignToolPackageId; + Environment.SetEnvironmentVariable(packageId.ToUpper() + "_EXE", PackageDownloadUtils.PackageDownload(packageId)); } public static void DefaultConsole(OutputType type, string output) diff --git a/Nuke.NuGetKeyVaultSignTool/NuGetKeyVaultSignToolTests.cs b/Nuke.NuGetKeyVaultSignTool/NuGetKeyVaultSignToolTests.cs index 64448c2..2e88443 100644 --- a/Nuke.NuGetKeyVaultSignTool/NuGetKeyVaultSignToolTests.cs +++ b/Nuke.NuGetKeyVaultSignTool/NuGetKeyVaultSignToolTests.cs @@ -25,11 +25,13 @@ public class NuGetKeyVaultSignToolTests [OneTimeSetUp] public void SetupEnvironmentToolPath() { - var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - foreach (var item in EnvironmentToolPath) - { - Environment.SetEnvironmentVariable(item.Key, Path.Combine(userFolder, item.Value)); - } + //var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + //foreach (var item in EnvironmentToolPath) + //{ + // Environment.SetEnvironmentVariable(item.Key, Path.Combine(userFolder, item.Value)); + //} + var packageId = NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignToolPackageId; + Environment.SetEnvironmentVariable(packageId.ToUpper() + "_EXE", PackageDownloadUtils.PackageDownload(packageId)); } public static void DefaultConsole(OutputType type, string output) diff --git a/Nuke.NuGetKeyVaultSignTool/PackageDownloadUtils.cs b/Nuke.NuGetKeyVaultSignTool/PackageDownloadUtils.cs new file mode 100644 index 0000000..9e0bc2c --- /dev/null +++ b/Nuke.NuGetKeyVaultSignTool/PackageDownloadUtils.cs @@ -0,0 +1,37 @@ +using Nuke.Common.IO; +using System.IO; +using Nuke.Common.Tools.DotNet; +using System.Linq; + +namespace Nuke.NuGetKeyVaultSignTool +{ + public class PackageDownloadUtils + { + private static AbsolutePath GetToolInstallationPath() + { + AbsolutePath folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + return folder / "Tools"; + } + + public static string PackageDownload(string packageId) + { + var toolFolder = GetToolInstallationPath(); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath packageToolExeExists) + { + return packageToolExeExists; + } + + DotNetTasks.DotNetToolInstall(x => x + .SetPackageName(packageId) + .SetToolInstallationPath(toolFolder) + ); + + if (Globbing.GlobFiles(toolFolder, $"{packageId}.exe").FirstOrDefault() is AbsolutePath packageToolExe) + { + return packageToolExe; + } + return null; + } + } +} \ No newline at end of file