Skip to content

Commit

Permalink
Showing 4 changed files with 120 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.7.2] / 2023-10-05
### Features
- Enable `SignBinary` with `PathTooLong` path.
### Updated
- Create `PathTooLongUtils` to prevent `PathTooLongException`

## [1.7.1] / 2023-10-05
### Features
- Prerelease Nuget force to unlist.
@@ -319,6 +325,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- First Release

[vNext]: ../../compare/1.0.0...HEAD
[1.7.2]: ../../compare/1.7.1...1.7.2
[1.7.1]: ../../compare/1.7.0...1.7.1
[1.7.0]: ../../compare/1.6.1...1.7.0
[1.6.1]: ../../compare/1.6.0...1.6.1
80 changes: 80 additions & 0 deletions ricaun.Nuke/Extensions/PathTooLongUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.IO;

namespace ricaun.Nuke.Extensions
{
/// <summary>
/// PathTooLongUtils
/// </summary>
public class PathTooLongUtils
{
/// <summary>
/// MAX_PATH
/// </summary>
public const int MAX_PATH = 260;
/// <summary>
/// FileMoveToTempUtils
/// </summary>
public class FileMoveToTemp : IDisposable
{
private readonly string filePath;
private string tempFilePath;
/// <summary>
/// FileMoveToTempUtils
/// </summary>
/// <param name="filePath"></param>
public FileMoveToTemp(string filePath)
{
this.filePath = filePath;
Initialize();
}

private void Initialize()
{
if (IsPathTooLong())
{
tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(this.filePath));
File.Move(this.filePath, tempFilePath, true);
}
}

/// <summary>
/// GetFilePathLong
/// </summary>
/// <returns></returns>
public int GetFilePathLong()
{
return GetFilePath().Length;
}

/// <summary>
/// Check if path is too long
/// </summary>
/// <returns></returns>
public bool IsPathTooLong()
{
return GetFilePathLong() > MAX_PATH;
}

/// <summary>
/// GetFilePath
/// </summary>
/// <returns></returns>
public string GetFilePath()
{
return tempFilePath ?? filePath;
}

/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
if (tempFilePath is null)
return;

File.Move(tempFilePath, filePath, true);
}
}
}
}
61 changes: 32 additions & 29 deletions ricaun.Nuke/Extensions/SignExtension.cs
Original file line number Diff line number Diff line change
@@ -71,7 +71,6 @@ public static bool CreateCerFile(string fileNamePfx, string passwordPfx, string
if (File.Exists(cert)) return true;
return CreateCertificatesCer(fileNamePfx, passwordPfx, cert);
}

/// <summary>
/// https://github.com/DataDog/dd-trace-dotnet/blob/master/tracer/build/_build/Build.Gitlab.cs
/// </summary>
@@ -80,44 +79,48 @@ public static bool CreateCerFile(string fileNamePfx, string passwordPfx, string
/// <param name="binaryPath"></param>
public static void SignBinary(string certPath, string certPassword, string binaryPath)
{
if (binaryPath.Length > 260)
using (var utils = new PathTooLongUtils.FileMoveToTemp(binaryPath))
{
var messageError = $"FilePath.Length to big: {binaryPath}";
Serilog.Log.Error(messageError);
throw new PathTooLongException(messageError);
}
var filePath = utils.GetFilePath();
if (utils.IsPathTooLong())
{
var messageError = $"FilePath.Length too long: {filePath}";
Serilog.Log.Error(messageError);
throw new PathTooLongException(messageError);
}

if (HasSignature(binaryPath)) return;
if (HasSignature(filePath)) return;

Serilog.Log.Information($"Signing: {binaryPath}");
Serilog.Log.Information($"Signing [{utils.GetFilePathLong()}]: {filePath}");

if (!SignToolTasks.SignToolPath.SkipEmpty())
{
Serilog.Log.Error($"SignToolPath is not found, set SIGNTOOL_EXE enviroment variable path... {SignToolTasks.SignToolPath}");
return;
}

foreach (var timestampServer in timestampServers)
{
try
if (!SignToolTasks.SignToolPath.SkipEmpty())
{
SignToolTasks.SignTool(x => x
.SetFiles(binaryPath)
.SetFile(certPath)
.SetPassword(certPassword)
.SetTimestampServerUrl(timestampServer)
.SetFileDigestAlgorithm(SignToolDigestAlgorithm.SHA256)
.EnableQuiet()
);
Serilog.Log.Information($"Signing done with {timestampServer}");
Serilog.Log.Error($"SignToolPath is not found, set SIGNTOOL_EXE enviroment variable path... {SignToolTasks.SignToolPath}");
return;
}
catch (Exception)

foreach (var timestampServer in timestampServers)
{
Serilog.Log.Warning($"Failed to sign file with {timestampServer}");
try
{
SignToolTasks.SignTool(x => x
.SetFiles(filePath)
.SetFile(certPath)
.SetPassword(certPassword)
.SetTimestampServerUrl(timestampServer)
.SetFileDigestAlgorithm(SignToolDigestAlgorithm.SHA256)
.EnableQuiet()
);
Serilog.Log.Information($"Signing done with {timestampServer}");
return;
}
catch (Exception)
{
Serilog.Log.Warning($"Failed to sign file with {timestampServer}");
}
}
Serilog.Log.Error($"Failed to sign file {filePath}");
}
Serilog.Log.Error($"Failed to sign file {binaryPath}");
}

/// <summary>
2 changes: 1 addition & 1 deletion ricaun.Nuke/ricaun.Nuke.csproj
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>ricaun.Nuke</PackageId>
<Version>1.7.1</Version>
<Version>1.7.2-alpha</Version>
</PropertyGroup>

<PropertyGroup>

0 comments on commit f4bba26

Please sign in to comment.