Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1186 Fixing signtool.exe path for new windows kits #1188

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
11 changes: 4 additions & 7 deletions build/Build.GlobalSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Nuke.Common.Utilities;
using Nuke.Utilities.Text.Yaml;
using static Nuke.Common.ControlFlow;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.ProjectModel.SolutionModelTasks;
using static Nuke.Common.Tools.Git.GitTasks;

Expand Down Expand Up @@ -67,18 +66,16 @@ IEnumerable<GitRepository> ExternalRepositories

if ((RootDirectory / $"{Solution.FileName}.DotSettings").FileExists())
{
CopyFile(
source: RootDirectory / $"{Solution.FileName}.DotSettings",
(RootDirectory / $"{Solution.FileName}.DotSettings").Copy(
target: RootDirectory / $"{global.FileName}.DotSettings",
FileExistsPolicy.Overwrite);
policy: ExistsPolicy.FileOverwrite);
}

if ((RootDirectory / $"{Solution.FileName}.DotSettings.user").FileExists())
{
CopyFile(
source: RootDirectory / $"{Solution.FileName}.DotSettings.user",
(RootDirectory / $"{Solution.FileName}.DotSettings.user").Copy(
target: RootDirectory / $"{global.FileName}.DotSettings.user",
FileExistsPolicy.Overwrite);
policy: ExistsPolicy.FileOverwrite);
}
});
}
9 changes: 9 additions & 0 deletions source/Nuke.Common/IO/FileSystemTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public static void DeleteFile(string file)
File.Delete(file);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Copy)}")]
public static void CopyFile(AbsolutePath source, AbsolutePath target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
{
if (!ShouldCopyFile(source, target, policy))
Expand All @@ -200,6 +201,7 @@ public static void CopyFile(AbsolutePath source, AbsolutePath target, FileExists
File.Copy(source, target, overwrite: true);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.CopyToDirectory)}")]
public static void CopyFileToDirectory(
AbsolutePath source,
AbsolutePath targetDirectory,
Expand All @@ -209,6 +211,7 @@ public static void CopyFileToDirectory(
CopyFile(source, Path.Combine(targetDirectory, Path.GetFileName(source).NotNull()), policy, createDirectories);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Move)}")]
public static void MoveFile(AbsolutePath source, AbsolutePath target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
{
if (!ShouldCopyFile(source, target, policy))
Expand All @@ -224,6 +227,7 @@ public static void MoveFile(AbsolutePath source, AbsolutePath target, FileExists
File.Move(source, target);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.MoveToDirectory)}")]
public static void MoveFileToDirectory(
AbsolutePath source,
AbsolutePath targetDirectory,
Expand All @@ -233,6 +237,7 @@ public static void MoveFileToDirectory(
MoveFile(source, Path.Combine(targetDirectory, Path.GetFileName(source).NotNull()), policy, createDirectories);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Rename)}")]
public static void RenameFile(AbsolutePath file, string newName, FileExistsPolicy policy = FileExistsPolicy.Fail)
{
if (Path.GetFileName(file) == newName)
Expand All @@ -241,6 +246,7 @@ public static void RenameFile(AbsolutePath file, string newName, FileExistsPolic
MoveFile(file, Path.Combine(Path.GetDirectoryName(file).NotNull(), newName), policy);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Move)}")]
public static void MoveDirectory(
AbsolutePath source,
AbsolutePath target,
Expand All @@ -264,6 +270,7 @@ public static void MoveDirectory(
}
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.MoveToDirectory)}")]
public static void MoveDirectoryToDirectory(
AbsolutePath source,
AbsolutePath targetDirectory,
Expand All @@ -273,6 +280,7 @@ public static void MoveDirectoryToDirectory(
MoveDirectory(source, Path.Combine(targetDirectory, new DirectoryInfo(source).Name), directoryPolicy, filePolicy);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Rename)}")]
public static void RenameDirectory(
string directory,
string newName,
Expand All @@ -282,6 +290,7 @@ public static void RenameDirectory(
MoveDirectory(directory, Path.Combine(Path.GetDirectoryName(directory).NotNull(), newName), directoryPolicy, filePolicy);
}

[Obsolete($"Use {nameof(AbsolutePath)}.{nameof(AbsolutePathExtensions.Copy)}")]
public static void CopyDirectoryRecursively(
AbsolutePath source,
AbsolutePath target,
Expand Down
8 changes: 3 additions & 5 deletions source/Nuke.Common/Tools/ReSharper/ReSharperTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ private static void PreProcess<T>(ref T toolSettings) where T : ReSharperSetting
var wave = GetWave(toolSettings).NotNull("wave != null");
var shadowDirectory = GetShadowDirectory(toolSettings, wave);

FileSystemTasks.CopyDirectoryRecursively(
Path.GetDirectoryName(toolSettings.ProcessToolPath).NotNull(),
shadowDirectory,
DirectoryExistsPolicy.Merge,
FileExistsPolicy.OverwriteIfNewer);
((AbsolutePath)toolSettings.ProcessToolPath.NotNull()).Copy(
target: shadowDirectory,
policy: ExistsPolicy.MergeAndOverwriteIfNewer);

toolSettings.Plugins
.Select(x => (Plugin: x.Key, Version: x.Value == ReSharperPluginLatest ? null : x.Value))
Expand Down
34 changes: 21 additions & 13 deletions source/Nuke.Common/Tools/SignTool/SignToolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Linq;
using JetBrains.Annotations;
using NuGet.Configuration;
using Nuke.Common.IO;

namespace Nuke.Common.Tools.SignTool;
Expand All @@ -20,18 +20,26 @@ private static string GetToolPath()
: SpecialFolders.ProgramFiles).NotNull();

var platformIdentifier = EnvironmentInfo.Is64Bit ? "x64" : "x86";
const string windowsKitLastVersion = "10";
const string windowsKitVersionWildcard = windowsKitLastVersion + ".*";
const string signtoolExe = "signtool.exe";
const string microsoftBuildToolsNugetPackage = "microsoft.windows.sdk.buildtools";

return new[]
{
programDirectory / "Windows Kits" / "10" / "bin" / "10.0.15063.0",
programDirectory / "Windows Kits" / "10" / "App Certification Kit",
programDirectory / "Windows Kits" / "10" / "bin" / platformIdentifier,
programDirectory / "Windows Kits" / "8.1" / "bin" / platformIdentifier,
programDirectory / "Windows Kits" / "8.0" / "bin" / platformIdentifier,
programDirectory / "Microsoft SDKs" / "Windows" / "v7.1A" / "Bin"
}
.Select(x => x / "signtool.exe")
.WhereFileExists()
.FirstOrDefault();
var windowsKitsRootDirectory = programDirectory / "Windows Kits" / windowsKitLastVersion;

var signToolPath = windowsKitsRootDirectory.GlobFiles($"bin/{windowsKitVersionWildcard}/{platformIdentifier}/{signtoolExe}").LastOrDefault();

if(signToolPath == null)
{
var nugetPackagesPath = SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(null));

signToolPath = AbsolutePath.Create(nugetPackagesPath)
.GlobFiles($"{microsoftBuildToolsNugetPackage}/{windowsKitVersionWildcard}/bin/{windowsKitVersionWildcard}/{platformIdentifier}/{signtoolExe}")
Copy link
Author

@gustavocalheiros gustavocalheiros Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path structure for nuget packages is different, for example :
C:\Users\MyUser\ .nuget\packages\microsoft.windows.sdk.buildtools\10.0.20348\bin\10.0.20348\x64\signtool.exe

.LastOrDefault();

signToolPath ??= windowsKitsRootDirectory.GlobFiles($"App Certification Kit/{signtoolExe}").SingleOrDefault();
}

return signToolPath;
}
}
10 changes: 2 additions & 8 deletions source/Nuke.Common/Utilities/TemplateUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,14 @@ private static void FillTemplateDirectoryRecursivelyInternal(
FillTemplateFile(file, tokens);

if (ShouldMove(file))
FileSystemTasks.RenameFile(file, file.Name.Replace(tokens), FileExistsPolicy.OverwriteIfNewer);
file.Rename(file.Name.Replace(tokens), ExistsPolicy.FileOverwriteIfNewer);
}

directory.GetDirectories()
.ForEach(x => FillTemplateDirectoryRecursivelyInternal(x, tokens, excludeDirectory, excludeFile));

if (ShouldMove(directory))
{
FileSystemTasks.RenameDirectory(
directory,
directory.Name.Replace(tokens),
DirectoryExistsPolicy.Merge,
FileExistsPolicy.OverwriteIfNewer);
}
directory.Rename(directory.Name.Replace(tokens), ExistsPolicy.MergeAndOverwriteIfNewer);
}

public static void FillTemplateFile(
Expand Down
2 changes: 1 addition & 1 deletion source/Nuke.Utilities.Tests/IO/FileSystemDependentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected FileSystemDependentTest(ITestOutputHelper testOutputHelper)
ExecutionDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).NotNull();
RootDirectory = Constants.TryGetRootDirectoryFrom(EnvironmentInfo.WorkingDirectory);
TestProjectDirectory = ExecutionDirectory.FindParentOrSelf(x => x.ContainsFile("*.csproj"));
TestTempDirectory = ExecutionDirectory / "temp" / $"{GetType().Name}.{TestName}";
TestTempDirectory = ExecutionDirectory / "temp" / $"{GetType().Name}.{TestName}";

TestTempDirectory.CreateOrCleanDirectory();
}
Expand Down
135 changes: 135 additions & 0 deletions source/Nuke.Utilities.Tests/IO/MoveCopyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2024 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Linq;
using FluentAssertions;
using Nuke.Common.IO;
using Nuke.Common.Utilities.Collections;
using Xunit;
using Xunit.Abstractions;

namespace Nuke.Common.Tests;

public class MoveCopyTest : FileSystemDependentTest
{
public MoveCopyTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
AbsolutePathExtensions.DefaultEofLineBreak = false;
}

[Fact]
public void TestCopyFile()
{
var source = TestTempDirectory / "source.txt";
source.WriteAllText("foobar");

var target = TestTempDirectory / "target.txt";
source.Copy(target);

target.FileExists().Should().BeTrue();

new Action(() => source.Copy(target))
.Should().Throw<Exception>().WithMessage("* already exists");

new Action(() => source.Copy(target, policy: ExistsPolicy.FileFail | ExistsPolicy.FileOverwrite))
.Should().Throw<Exception>().WithMessage("Multiple file policies *");

source.WriteAllText("fizzbuzz");
source.Copy(target, policy: ExistsPolicy.FileOverwrite)
.Should().Be(target);
target.ReadAllText().Should().Be("fizzbuzz");
}

[Fact]
public void TestMoveFile()
{
var source1 = (TestTempDirectory / "source1.txt").TouchFile();
var source2 = (TestTempDirectory / "source2.txt").TouchFile();
var source3 = (TestTempDirectory / "source3.txt").TouchFile();

var target = TestTempDirectory / "target.txt";
source2.Move(target);

target.FileExists().Should().BeTrue();
source2.FileExists().Should().BeFalse();

new Action(() => source1.Move(target, policy: ExistsPolicy.FileFail))
.Should().Throw<Exception>().WithMessage("* already exists");

source1.Move(target, policy: ExistsPolicy.FileSkip).Should().Be(source1);
source1.Move(target, policy: ExistsPolicy.FileOverwriteIfNewer).Should().Be(source1);

source3.TouchFile();
source3.Move(target, policy: ExistsPolicy.FileOverwriteIfNewer).Should().Be(target);
}

[Fact]
public void TestCopyDirectory()
{
var source = TestTempDirectory / "source";
var sourceFiles = new[]
{
source / "source1.txt",
source / "source2.txt",
source / "sub" / "source3.txt",
source / "sub" / "source4.txt",
};
sourceFiles.ForEach(x => x.WriteAllText("source"));

var target = TestTempDirectory / "target";
source.Copy(target);
target.GetFiles(depth: int.MaxValue).Select(x => target.GetRelativePathTo(x).ToString())
.Should().BeEquivalentTo(sourceFiles.Select(x => source.GetRelativePathTo(x).ToString()));

target.CreateOrCleanDirectory();
var target0 = (target / "source0.txt").TouchFile();
var target3 = (target / "sub" / "source3.txt").WriteAllText("target");
var target4 = (target / "sub" / "source4.txt").WriteAllText("target");
(source / target.GetRelativePathTo(target4)).TouchFile();

new Action(() => source.Copy(target, ExistsPolicy.DirectoryFail))
.Should().Throw<Exception>().WithMessage("Policy disallows merging directories");
target.GetFiles(depth: int.MaxValue).Should().HaveCount(3);

source.Copy(target, ExistsPolicy.MergeAndSkip);
target0.FileExists().Should().BeTrue();
target3.ReadAllText().Should().Be("target");
target4.ReadAllText().Should().Be("target");

source.Copy(target, ExistsPolicy.MergeAndOverwriteIfNewer);
target3.ReadAllText().Should().Be("target");
target4.ReadAllText().Should().Be("source");

source.Copy(target, ExistsPolicy.MergeAndOverwrite);
target3.ReadAllText().Should().Be("source");
}

[Fact]
public void TestMoveDirectory()
{
var source = TestTempDirectory / "source";
var sourceFiles = new[]
{
source / "source1.txt",
source / "source2.txt",
source / "sub" / "source3.txt",
source / "sub" / "source4.txt",
};
sourceFiles.ForEach(x => x.WriteAllText("source"));

var target = TestTempDirectory / "target";
(target / "source1.txt").TouchFile();
(target / "sub" / "source3.txt").TouchFile();

new Action(() => source.Move(target)).Should().Throw<Exception>();

source.Move(target, ExistsPolicy.MergeAndSkip);
source.GetFiles(depth: int.MaxValue).Should().HaveCount(2);

source.Move(target, ExistsPolicy.MergeAndSkip, deleteRemainingFiles: true)
.Should().Be(target);
source.DirectoryExists().Should().BeFalse();
}
}
9 changes: 9 additions & 0 deletions source/Nuke.Utilities/Collections/Enumerable.WhereNotNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T> enumerable)
{
return enumerable.Where(x => x != null);
}

/// <summary>
/// Filters the collection to elements that don't meet the condition.
/// </summary>
public static IEnumerable<T> WhereNot<T>(this IEnumerable<T> enumerable, Func<T, bool> condition)
where T : class
{
return enumerable.Where(x => condition == null || !condition(x));
}
}
Loading