Skip to content

Commit

Permalink
Add PackageDownload to download tools on the fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Dec 6, 2024
1 parent cef6ce4 commit 281e1a7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<ProjectReference Include="..\ricaun.Nuke\ricaun.Nuke.csproj" />
</ItemGroup>

<Import Project="..\ricaun.Nuke\build\ricaun.Nuke.targets" />
<!--<Import Project="..\ricaun.Nuke\build\ricaun.Nuke.targets" />-->

</Project>
3 changes: 3 additions & 0 deletions Build/IAzureSignTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
94 changes: 94 additions & 0 deletions ricaun.Nuke/Tools/AzureSignToolUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -24,6 +26,9 @@ public class AzureSignToolUtils
/// <exception cref="Exception">Thrown when the required packages are missing.</exception>
public static void EnsureAzureToolIsInstalled()
{
DownloadAzureSignTool();
DownloadNuGetKeyVaultSignTool();

try
{
_ = AzureSignToolTasks.AzureSignToolPath;
Expand All @@ -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;
}

/// <summary>
/// Download AzureSignTool if not already installed.
/// </summary>
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;
}

/// <summary>
/// Download NuGetKeyVaultSignTool if not already installed.
/// </summary>
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;
}

/// <summary>
/// Signs the specified file using Azure Sign Tool or NuGet Key Vault Sign Tool.
/// </summary>
Expand All @@ -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)
Expand All @@ -69,6 +162,7 @@ public static void Sign(string fileName,
return;
}

DownloadAzureSignTool();
AzureSignToolTasks.AzureSignTool(x => x
.SetFiles(fileName)
.SetKeyVaultCertificateName(azureKeyVaultConfig.AzureKeyVaultCertificate)
Expand Down
5 changes: 2 additions & 3 deletions ricaun.Nuke/ricaun.Nuke.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>ricaun.Nuke</PackageId>
<Version>1.9.0-alpha.1</Version>
<Version>1.9.0-alpha.2</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -78,8 +78,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="build\" />
<None Include="build\$(MSBuildProjectName).targets" PackagePath="build" Pack="true" />
<!--<None Include="build\$(MSBuildProjectName).targets" PackagePath="build" Pack="true" />-->
</ItemGroup>

</Project>

0 comments on commit 281e1a7

Please sign in to comment.