Skip to content

Commit

Permalink
command line parameters added
Browse files Browse the repository at this point in the history
  • Loading branch information
ArsenShnurkov committed Jan 19, 2017
1 parent 863a5dd commit c57bc97
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
20 changes: 10 additions & 10 deletions mpt-core/04_CSProj/CSharpLibraryProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void InjectProjectImport(string import_name)
}
}

public void InjectVersioning()
public void InjectVersioning(string versionPropertyName)
{
/*
<Target Name="MyAssemblyVersion" Outputs="@(GeneratedVersion)">
Expand All @@ -60,14 +60,20 @@ public void InjectVersioning()
task.AddParameter("Directories", "$(IntermediateOutputPath)");
targ.AppendTask(task);
}
{
MSBuildPropertyGroup group = targ.CreatePropertyGroup();
group.Condition = " '$(" + versionPropertyName + ")' == '' ";
group.AddProperty(versionPropertyName, "1.0.0.0");
targ.AppendPropertyGroup(group);
}
{
MSBuildTask task = targ.CreateTask();
task.Name = "AssemblyInfo";
task.AddParameter("CodeLanguage", "CS");

task.AddParameter("AssemblyVersion", "$(VersionNumber)"); // System.Reflection.AssemblyVersion
task.AddParameter("AssemblyFileVersion", "$(VersionNumber)"); // System.Reflection.AssemblyFileVersion
task.AddParameter("AssemblyInformationalVersion", "$(VersionNumber)"); // System.Reflection.AssemblyInformationalVersion
task.AddParameter("AssemblyVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyVersion
task.AddParameter("AssemblyFileVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyFileVersion
task.AddParameter("AssemblyInformationalVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyInformationalVersion

task.AddParameter("OutputFile", "$(IntermediateOutputPath)AssemblyVersion.Generated.cs");
{
Expand All @@ -86,12 +92,6 @@ public void InjectInternalsVisibleTo(string assemblyName, string assemblyPublicK
{
MSBuildTarget targ = uo.CreateTarget();
targ.Name = "MyInsertInternalsTo";
{
MSBuildPropertyGroup group = targ.CreatePropertyGroup();
group.Condition = " '$(VersionNumber)' == '' ";
group.AddProperty("VersionNumber", "1.0.0.0");
targ.AppendPropertyGroup(group);
}
{
MSBuildTask task = targ.CreateTask(); // '$(SignAssembly)' == 'true'
task.Name = "AssemblyInfo";
Expand Down
42 changes: 42 additions & 0 deletions mpt-core/AssemblyName/PublicKeyUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.IO;
using System.Text;

public class PublicKeyUtils
{
public static byte[] GetPublicKey(byte[] snk)
{
var snkp = new System.Reflection.StrongNameKeyPair(snk);
byte[] publicKey = snkp.PublicKey;
return publicKey;
}

public static byte[] GetPublicKey(string container)
{
var snkp = new System.Reflection.StrongNameKeyPair(container);
byte[] publicKey = snkp.PublicKey;
return publicKey;
}

public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}

public static string GetPublicKeyStringFromContainer(string AssemblyKeyContainerName)
{
byte[] publicKey2 = GetPublicKey(AssemblyKeyContainerName);
string output2 = ByteArrayToString(publicKey2);
return ", PublicKey=" + output2;
}

public static string GetPublicKeyStringFromFilename(string AssemblyOriginatorKeyFile)
{
byte[] snk = File.ReadAllBytes(AssemblyOriginatorKeyFile);
byte[] publicKey1 = GetPublicKey(snk);
string output1 = ByteArrayToString(publicKey1);
return ", PublicKey=" + output1;
}
}
1 change: 1 addition & 0 deletions mpt-core/mpt-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="03_msbuild\MSBuildItem.cs" />
<Compile Include="03_msbuild\MSBuildPropertyGroup.cs" />
<Compile Include="03_msbuild\MSBuildItemGroup.cs" />
<Compile Include="AssemblyName\PublicKeyUtils.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="PackageProcessing\" />
Expand Down
49 changes: 47 additions & 2 deletions mpt-csproj/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static int Main (string[] args)
string reference_name = null;
bool bForceReferenceAppending = false;
string import_name = null;
string version_string = null;
string friend_assembly_name = null;
string AssemblyKeyContainerName = null;
string AssemblyOriginatorKeyFile = null;

var optionSet = new OptionSet()
{
Expand Down Expand Up @@ -68,6 +72,13 @@ public static int Main (string[] args)
{ "inject-reference=", str => { reference_name = str; bForceReferenceAppending = true; } },
// insert project import into .csproj files
{ "inject-import=", str => { import_name = str; } },
// insert task for versioning into .csproj files
{ "inject-versioning=", str => { version_string = str; } },
// insert project import into .csproj files
{ "inject-InternalsVisibleTo=", str => { friend_assembly_name = str; } },
// insert project import into .csproj files
{ "AssemblyKeyContainerName=", str => { AssemblyKeyContainerName = str; } },
{ "AssemblyOriginatorKeyFile=", str => { AssemblyOriginatorKeyFile = str; } },
};

var listOfUnparsedParameters = optionSet.Parse(args);
Expand Down Expand Up @@ -220,9 +231,38 @@ public static int Main (string[] args)
using (CSharpLibraryProject file = new CSharpLibraryProject(csproj_file))
{
file.InjectProjectImport(import_name);
file.InjectVersioning();
}
}
}
if (version_string != null)
{
Console.WriteLine($"Injecting version property {version_string}");
foreach (var csproj_file in listOfCsproj)
{
using (CSharpLibraryProject file = new CSharpLibraryProject(csproj_file))
{
file.InjectVersioning(version_string);
}
}
}
if (friend_assembly_name != null)
{
Console.WriteLine($"Injecting version property {version_string}");
foreach (var csproj_file in listOfCsproj)
{
using (CSharpLibraryProject file = new CSharpLibraryProject(csproj_file))
{
// null is ok - http://stackoverflow.com/questions/637308/why-is-adding-null-to-a-string-legal
file.InjectInternalsVisibleTo(import_name, null);
string publicKey = null;
if (String.IsNullOrEmpty(AssemblyOriginatorKeyFile) == false)
{
publicKey = PublicKeyUtils.GetPublicKeyStringFromFilename(AssemblyOriginatorKeyFile);
}
if (String.IsNullOrEmpty(AssemblyKeyContainerName) == false )
{
publicKey = PublicKeyUtils.GetPublicKeyStringFromContainer(AssemblyKeyContainerName);
}
file.InjectInternalsVisibleTo(import_name, publicKey);
}
}
}
Expand Down Expand Up @@ -252,6 +292,11 @@ public static void ShowHelp()
Console.WriteLine("\tmpt-csproj --inject-reference=\"MyDll,Version,Culture,PubKeyToken\"");
Console.WriteLine("\tmpt-csproj --inject-import='$(MSBuildToolsPath)\\MSBuild.Community.Tasks.Targets'");
Console.WriteLine("\t\tinserts new reference for MyDll of given version, or replaces the old one");
Console.WriteLine("\tmpt-csproj --inject - versioning=BuildVersion");
Console.WriteLine("\t\tinserts property with given name $(BuildVersion), and default value 1.0.0.0");
Console.WriteLine("\tmpt-csproj --inject-InternalsVisibleTo=mytest.dll --AssemblyKeyContainerName=mono");
Console.WriteLine("\tmpt-csproj --inject-InternalsVisibleTo=mytest.dll --AssemblyOriginatorKeyFile=mono.snk");
Console.WriteLine("\t\tinserts InternalsVisibleToAttribute");
}
static List<string> listOfCsproj = new List<string>();
static void AddProjectFile(string filename)
Expand Down

0 comments on commit c57bc97

Please sign in to comment.