Skip to content

Commit

Permalink
Add PackageDownloadUtils to force download tool package in local to…
Browse files Browse the repository at this point in the history
…ol folder
  • Loading branch information
ricaun committed Dec 7, 2024
1 parent 309a0f2 commit 50a05d3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 13 deletions.
47 changes: 44 additions & 3 deletions Build/INuGetKeyVaultSign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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}");
});
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions Nuke.NuGetKeyVaultSignTool/AzureSignToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions Nuke.NuGetKeyVaultSignTool/NuGetKeyVaultSignToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions Nuke.NuGetKeyVaultSignTool/PackageDownloadUtils.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 50a05d3

Please sign in to comment.