Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
brutaldev committed Dec 16, 2022
2 parents 9e6a119 + 7abeeef commit f2a611f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="PowerArgs, Version=2.6.0.1, Culture=neutral, PublicKeyToken=26a276264bbd55b8, processorArchitecture=MSIL">
<HintPath>..\..\packages\PowerArgs.2.6.0.1\lib\net40\PowerArgs.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions src/Brutal.Dev.StrongNameSigner.Console/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Cecil" version="0.11.4" targetFramework="net472" />
<package id="PowerArgs" version="2.6.0.1" targetFramework="net472" />
<package id="StyleCop.MSBuild" version="6.2.0" targetFramework="net472" developmentDependency="true" />
</packages>
23 changes: 15 additions & 8 deletions src/Brutal.Dev.StrongNameSigner/AutomaticBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class AutomaticBuildTask : Microsoft.Build.Utilities.Task
[Required]
public ITaskItem[] References { get; set; }

[Required]
public ITaskItem OutputPath { get; set; }

public ITaskItem[] CopyLocalPaths { get; set; }
Expand Down Expand Up @@ -43,12 +42,6 @@ public override bool Execute()
return true;
}

if (OutputPath == null || string.IsNullOrEmpty(OutputPath.ItemSpec))
{
Log.LogError("Task parameter 'OutputPath' not provided.");
return false;
}

if (!string.IsNullOrEmpty(KeyFile) && !File.Exists(KeyFile))
{
Log.LogError($"The Key File '{KeyFile}' does not exist.");
Expand Down Expand Up @@ -94,7 +87,21 @@ public override bool Execute()
}
}

SigningHelper.SignAssemblies(assembliesToSign, KeyFile, Password, probingPaths);
if (string.IsNullOrEmpty(OutputPath?.ItemSpec))
{
Log.LogMessage("Task parameter 'OutputPath' not provided - signed files will overwrite source files.");
SigningHelper.SignAssemblies(assembliesToSign, KeyFile, Password, probingPaths);
}
else
{
if(!Directory.Exists(OutputPath.ItemSpec))
{
Directory.CreateDirectory(OutputPath.ItemSpec);
}
var inoutassemblies = assembliesToSign.Select(assm => new InputOutputFilePair(assm, Path.Combine(OutputPath.ItemSpec, Path.GetFileName(assm))));
SigningHelper.SignAssemblies(inoutassemblies, KeyFile, Password, probingPaths);
Log.LogMessage(MessageImportance.Normal, $"Signing files to output folder '{OutputPath.ItemSpec}'");
}

if (CopyLocalPaths != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<Company>Brutal.Dev.StrongNameSigner</Company>
<Version>3.2.0.0</Version>
<Version>3.2.0</Version>
<Description>Simple API to sign .NET assemblies with a strong-name key and fix assembly references.</Description>
<Copyright>Copyright © 2013-2022</Copyright>
<PackageProjectUrl>https://brutaldev.com/post/net-assembly-strong-name-signer</PackageProjectUrl>
Expand Down
36 changes: 36 additions & 0 deletions src/Brutal.Dev.StrongNameSigner/OutputFileManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Mono.Cecil;

namespace Brutal.Dev.StrongNameSigner
{
Expand Down Expand Up @@ -216,8 +219,41 @@ private static void CopyFile(string source, string target, bool move)
}
catch
{
try
{
var dir = Path.Combine(Path.GetDirectoryName(target), "tmp");
Directory.CreateDirectory(dir);
var tmptarget = Path.Combine(dir, Path.GetFileName(target));

File.Copy(source, tmptarget,true);
FailedCopies.Enqueue(tmptarget);
}
catch
{

}
// Ignore file operation failures.
}
}

private static Queue<string> FailedCopies = new Queue<string>();

public static void FixFailedCopies()
{
while (FailedCopies.Count>0)
{
var failed = FailedCopies.Dequeue();
try
{
var target = failed.Replace(".tmp","");
File.Copy(failed, target,true);
}
catch
{

}

}
}
}
}
1 change: 1 addition & 0 deletions src/Brutal.Dev.StrongNameSigner/SigningHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
{
assembly.Dispose();
}
OutputFileManager.FixFailedCopies();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Brutal.Dev.StrongNameSigner/StrongNameSigner.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<UsingTask TaskName="Brutal.Dev.StrongNameSigner.AutomaticBuildTask" AssemblyFile="$(MSBuildThisFileDirectory)Brutal.Dev.StrongNameSigner.dll" />

<Target Name="StrongNameSignerTarget" AfterTargets="AfterResolveReferences" Condition="$(EnableStrongNameSigner) != 'false'">
<Brutal.Dev.StrongNameSigner.AutomaticBuildTask References="@(ReferencePath)" CopyLocalPaths="@(ReferenceCopyLocalPaths)" OutputPath="$(IntermediateOutputPath)" KeyFile="$(StrongNameKeyFile)" Password="$(StrongNamePassword)">
<Brutal.Dev.StrongNameSigner.AutomaticBuildTask References="@(ReferencePath)" CopyLocalPaths="@(ReferenceCopyLocalPaths)" OutputPath="$(SignerOutputDirectory)" KeyFile="$(StrongNameKeyFile)" Password="$(StrongNamePassword)">
<Output TaskParameter="SignedAssembliesToReference" ItemName="AssembliesToReference" />
<Output TaskParameter="NewCopyLocalFiles" ItemName="NewCopyLocalFiles" />
</Brutal.Dev.StrongNameSigner.AutomaticBuildTask>
Expand Down

0 comments on commit f2a611f

Please sign in to comment.