Skip to content

Commit

Permalink
Added wildcard support based on issue #19 and #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
brutaldev committed Apr 14, 2016
1 parent cabe868 commit 1c2cfe1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If you are making use of the [NuGet package](https://www.nuget.org/packages/Brut
```xml
<Target Name="BeforeBuild">
<Exec ContinueOnError="false"
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.6.1\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages&quot;" />
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.7.0\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages&quot;" />
</Target>
```

Expand All @@ -46,13 +46,31 @@ To add multiple directories to process at the same time (similar to how the UI c
```xml
<Target Name="BeforeBuild">
<Exec ContinueOnError="false"
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.6.1\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\elmah.corelibrary.1.2.2|..\packages\Elmah.MVC.2.1.1&quot;" />
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.7.0\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\elmah.corelibrary.1.2.2|..\packages\Elmah.MVC.2.1.1&quot;" />
</Target>
```

This way the core library can be signed and the MVC library can have it's reference to the new signed version updated since they will be processed together and each file can be verified against each other after signing.
As a rule of thumb, always include all libraries that will be affected by any signing in a single call.

You can also use wildcards for each of your input directories. The above example could also be written using a wildcard that will match all directories and versions.

```xml
<Target Name="BeforeBuild">
<Exec ContinueOnError="false"
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.7.0\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\elmah.*&quot;" />
</Target>
```

Wildcards can also be complex and placed anywhere in the path. This is useful if you only want a subset of directories as well as only certain framework specific lib directories to be signed.

```xml
<Target Name="BeforeBuild">
<Exec ContinueOnError="false"
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.7.0\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\Microsoft.*.Security*\*\net45&quot;" />
</Target>
```

Note that any files that are already strong-name signed will not be modified unless they reference a previously unsigned assembly. If you are using NuGet package restore then this works on build servers as well.

Another alternative is to simply call the `StrongNameSigner.Console.exe` with relevant argument as a pre-build step.
Expand All @@ -75,7 +93,7 @@ For example, ServiceStack's PostgreSQL NuGet package is not signed but other dep
```xml
<Target Name="BeforeBuild">
<Exec ContinueOnError="false"
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.6.1\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\ServiceStack.OrmLite.PostgreSQL.4.0.40\lib\net40|..\packages\ServiceStack.Text.Signed.4.0.40\lib\net40|..\packages\ServiceStack.OrmLite.Signed.4.0.40&quot;" />
Command="&quot;..\packages\Brutal.Dev.StrongNameSigner.1.7.0\tools\StrongNameSigner.Console.exe&quot; -in &quot;..\packages\ServiceStack.OrmLite.PostgreSQL.4.0.40\lib\net40|..\packages\ServiceStack.Text.Signed.4.0.40\lib\net40|..\packages\ServiceStack.OrmLite.Signed.4.0.40&quot;" />
</Target>
```

Expand Down
2 changes: 1 addition & 1 deletion src/Brutal.Dev.StrongNameSigner.Console/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Validate()
{
foreach (var inputDir in InputDirectory.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
if (!Directory.Exists(inputDir))
if (!inputDir.Contains("*") && !Directory.Exists(inputDir))
{
throw new ArgException(string.Format("Directory not found: '{0}'", inputDir));
}
Expand Down
42 changes: 36 additions & 6 deletions src/Brutal.Dev.StrongNameSigner.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using PowerArgs;
using C = System.Console;

Expand Down Expand Up @@ -103,17 +104,32 @@ private static Stats SignAssemblies(Options options)
int signedFiles = 0;
int referenceFixes = 0;

HashSet<string> filesToSign = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var filesToSign = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

if (!string.IsNullOrWhiteSpace(options.InputDirectory))
{
foreach (var inputDir in options.InputDirectory.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
foreach (var file in Directory.GetFiles(inputDir, "*.*", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".exe", StringComparison.OrdinalIgnoreCase) ||
Path.GetExtension(f).Equals(".dll", StringComparison.OrdinalIgnoreCase)))
if (inputDir.Contains("*"))
{
filesToSign.Add(file);
string firstWildCardPart = inputDir.Substring(0, inputDir.IndexOf("*"));
string searchPath = firstWildCardPart.Substring(0, firstWildCardPart.LastIndexOf(Path.DirectorySeparatorChar));

foreach (var dir in Directory.GetDirectories(searchPath, "*", SearchOption.AllDirectories)
.Where(d => Regex.IsMatch(d, "^" + Regex.Escape(inputDir).Replace("\\*", ".*").Replace("\\?", ".") + "$", RegexOptions.IgnoreCase)))
{
foreach (var file in GetFilesToSign(dir))
{
filesToSign.Add(file);
}
}
}
else
{
foreach (var file in GetFilesToSign(inputDir))
{
filesToSign.Add(file);
}
}
}
}
Expand Down Expand Up @@ -265,7 +281,21 @@ private static bool RemoveInvalidFriendAssemblyReferences(string assemblyPath, s

return false;
}


private static IEnumerable<string> GetFilesToSign(string directory)
{
var filesToSign = new HashSet<string>();

foreach (var file in Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".exe", StringComparison.OrdinalIgnoreCase) ||
Path.GetExtension(f).Equals(".dll", StringComparison.OrdinalIgnoreCase)))
{
filesToSign.Add(file);
}

return filesToSign;
}

private struct Stats
{
public int NumberOfSignedFiles { get; set; }
Expand Down

0 comments on commit 1c2cfe1

Please sign in to comment.