diff --git a/.gitignore b/.gitignore index 3db78c4..83b96f5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,4 @@ obj *.suo *.nupkg .DS_Store - - +.vs/ \ No newline at end of file diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a13a591..f07e816 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -7,3 +7,7 @@ for fixing a command line parsing error Brian Fresen https://github.com/bfriesen for creating the NuGet package and fixing errors. + +Daniel Cazzulino https://github.com/kzu + +for porting to SDK style and migrating to SDK Pack \ No newline at end of file diff --git a/GitInfo.txt b/GitInfo.txt new file mode 100644 index 0000000..359a5b9 --- /dev/null +++ b/GitInfo.txt @@ -0,0 +1 @@ +2.0.0 \ No newline at end of file diff --git a/InjectModuleInitializer.csproj b/InjectModuleInitializer.csproj index 15a043c..c156ea8 100644 --- a/InjectModuleInitializer.csproj +++ b/InjectModuleInitializer.csproj @@ -1,132 +1,100 @@ - - + + - Debug - AnyCPU - 9.0.30729 - 2.0 - {8B2B6640-191B-4733-8243-5A4270162F86} Exe - Properties EinarEgilsson.Utilities.InjectModuleInitializer InjectModuleInitializer - v2.0 - 512 - EinarEgilsson.Utilities.InjectModuleInitializer.Program - true - $(PrivateKeys)\EinarEgilssonStrongName.snk - - - 3.5 - - - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true + net35 + *.binlog;Test\**\*.*;nuget\**\*.* + false + true + NU5105 + + Enables module initialization, as described here: http://einaregilsson.com/module-initializers-in-csharp + Einar Egilsson + Copyright © Einar Egilsson 2009-2018 + + module initializer assembly constructor + https://github.com/einaregilsson/InjectModuleInitializer/blob/master/license.txt + https://github.com/einaregilsson/InjectModuleInitializer + true + + true + true + tools + IncludeCecil - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - 1685 - AllRules.ruleset - false - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - 1685 - AllRules.ruleset - - - - lib\Mono.Cecil.dll - - - lib\Mono.Cecil.Mdb.dll - - - lib\Mono.Cecil.Pdb.dll - - - False - lib\nunit.framework.dll - - - - - - - - - - - - - Always - - - Always - - - - + - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - true - - - False - Windows Installer 3.1 - true - + + + - + + + - - - Always - Designer - - - PreserveNewest - - - - - + $([MSBuild]::Add('$(GitCommits)', '1')) + - + + + + + + + $(GitSemVerDashLabel)-pr$(BUILD_SOURCEBRANCH.Substring(10).TrimEnd('/merge')) + $(GitSemVerDashLabel)-pr$(APPVEYOR_PULL_REQUEST_NUMBER) + + + $(SYSTEM_PULLREQUEST_TARGETBRANCH) + $(BUILD_SOURCEBRANCHNAME) + $(APPVEYOR_REPO_BRANCH) + + + <_IndexOfBranchSlash>$(GitBranch.LastIndexOf('/')) + <_IndexOfBranchSubstring>$([MSBuild]::Add('$(_IndexOfBranchSlash)', '1')) + <_GitBranch Condition="'$(_IndexOfBranchSlash)' != '0'">$(GitBranch.Substring($(_IndexOfBranchSubstring))) + <_GitBranch Condition="'$(_IndexOfBranchSlash)' == '0'">$(GitBranch) + + + -$(_GitBranch) + $(_GitBranch). + $(SemVerMetadata)sha.$(GitCommit) + + $(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch) + $(AssemblyVersion) + $(FileVersion)$(GitSemVerDashLabel)+$(SemVerMetadata) + $(PackageVersion) + - --> + \ No newline at end of file diff --git a/InjectModuleInitializer.props b/InjectModuleInitializer.props new file mode 100644 index 0000000..3ce85b8 --- /dev/null +++ b/InjectModuleInitializer.props @@ -0,0 +1,7 @@ + + + + $([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)..\tools\net35\InjectModuleInitializer.exe')) + + + \ No newline at end of file diff --git a/InjectModuleInitializer.targets b/InjectModuleInitializer.targets new file mode 100644 index 0000000..35974dc --- /dev/null +++ b/InjectModuleInitializer.targets @@ -0,0 +1,23 @@ + + + + + + /k:"$(AssemblyOriginatorKeyFile)" + + + + + + + + + + + + + + diff --git a/Injector.cs b/Injector.cs index 87c6f5c..dcb0262 100644 --- a/Injector.cs +++ b/Injector.cs @@ -25,33 +25,11 @@ public class InjectionException : Exception public InjectionException(string msg) : base(msg) { } } - internal class Injector + internal class Injector : IDisposable { public const string DefaultInitializerClassName = "ModuleInitializer"; public const string DefaultInitializerMethodName = "Run"; - static Injector() - { - AppDomain.CurrentDomain.AssemblyResolve += LoadEmbeddedAssembly; - } - - static Assembly LoadEmbeddedAssembly(object sender, ResolveEventArgs args) - { - - string name = new AssemblyName(args.Name).Name; - string resourceName = typeof(Injector).Namespace + ".lib." + name + ".dll"; - Stream stream = typeof(Injector).Assembly.GetManifestResourceStream(resourceName); - if (stream == null) { - return null; - } - using (stream) { - byte[] buf = new byte[stream.Length]; - stream.Read(buf,0, buf.Length); - return System.Reflection.Assembly.Load(buf); - } - } - - private AssemblyDefinition Assembly { get; set; } private string PdbFile(string assemblyFile) @@ -65,7 +43,6 @@ private string PdbFile(string assemblyFile) return null; } - public void Inject(string assemblyFile, string moduleInitializer=null, string keyfile=null) { try @@ -93,7 +70,7 @@ public void Inject(string assemblyFile, string moduleInitializer=null, string ke private void InjectInitializer(MethodReference callee) { Debug.Assert(Assembly != null); - TypeReference voidRef = Assembly.MainModule.Import(callee.ReturnType); + TypeReference voidRef = Assembly.MainModule.ImportReference(callee.ReturnType); const MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; @@ -133,7 +110,8 @@ private void ReadAssembly(string assemblyFile) var readParams = new ReaderParameters(ReadingMode.Immediate) { - AssemblyResolver = resolver + AssemblyResolver = resolver, + InMemory = true }; if (PdbFile(assemblyFile) != null) @@ -209,6 +187,11 @@ private static T Find(Collection objects, Predicate condition) where T: } } return null; - } + } + + public void Dispose() + { + Assembly.Dispose(); + } } } diff --git a/Program.cs b/Program.cs index 75a2692..2f78d7f 100644 --- a/Program.cs +++ b/Program.cs @@ -10,7 +10,7 @@ Command line program to inject a module initializer into a .NET assembly. */ using System; using System.Text.RegularExpressions; -using EinarEgilsson.Utilities.InjectModuleInitializer.Test; +//using EinarEgilsson.Utilities.InjectModuleInitializer.Test; using System.Reflection; namespace EinarEgilsson.Utilities.InjectModuleInitializer @@ -19,14 +19,14 @@ internal class Program { static int Main(string[] args) { -#if DEBUG - //I only have VS Express at home and this is the easiest way to debug - //the unit tests since there's no test runner and I can't attach to NUnit. - if (args.Length == 1 && args[0] == "/runtests") - { - return TestRunner.RunTests(); - } -#endif +//#if DEBUG +// //I only have VS Express at home and this is the easiest way to debug +// //the unit tests since there's no test runner and I can't attach to NUnit. +// if (args.Length == 1 && args[0] == "/runtests") +// { +// return TestRunner.RunTests(); +// } +//#endif var injector = new Injector(); if (args.Length == 0 || args.Length > 3 || Regex.IsMatch(args[0], @"^((/|--?)(\?|h|help))$")) { @@ -71,6 +71,10 @@ static int Main(string[] args) Console.Error.WriteLine("error: " + ex.Message); return 1; } + finally + { + injector.Dispose(); + } } static void PrintHelp() diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs deleted file mode 100644 index 304f661..0000000 --- a/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,45 +0,0 @@ -/* -InjectModuleInitializer - -Command line program to inject a module initializer into a .NET assembly. - -Copyright (C) 2009-2016 Einar Egilsson -http://einaregilsson.com/module-initializers-in-csharp/ - -This program is licensed under the MIT license: http://opensource.org/licenses/MIT - */ -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("InjectModuleInitializer")] -[assembly: AssemblyDescription("Inject a module initializer into a .NET assembly")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Einar Egilsson")] -[assembly: AssemblyProduct("InjectModuleInitializer")] -[assembly: AssemblyCopyright("Copyright © Einar Egilsson 2009-2016")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("945b78d2-a443-48d5-ad51-1abb4fa1d9f3")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.0.0")] -[assembly: AssemblyFileVersion("1.5.0.0")] diff --git a/lib/Mono.Cecil.Mdb.dll b/lib/Mono.Cecil.Mdb.dll deleted file mode 100644 index 5b75b09..0000000 Binary files a/lib/Mono.Cecil.Mdb.dll and /dev/null differ diff --git a/lib/Mono.Cecil.Pdb.dll b/lib/Mono.Cecil.Pdb.dll deleted file mode 100644 index 38659aa..0000000 Binary files a/lib/Mono.Cecil.Pdb.dll and /dev/null differ diff --git a/lib/Mono.Cecil.dll b/lib/Mono.Cecil.dll deleted file mode 100644 index 8e3610f..0000000 Binary files a/lib/Mono.Cecil.dll and /dev/null differ diff --git a/lib/nunit.framework.dll b/lib/nunit.framework.dll deleted file mode 100644 index ed6550b..0000000 Binary files a/lib/nunit.framework.dll and /dev/null differ diff --git a/nuget/InjectModuleInitializer.nuspec b/nuget/InjectModuleInitializer.nuspec deleted file mode 100644 index d3200e2..0000000 --- a/nuget/InjectModuleInitializer.nuspec +++ /dev/null @@ -1,24 +0,0 @@ - - - - InjectModuleInitializer - 1.5 - Module Initializer - Einar Egilsson - Einar Egilsson - https://github.com/einaregilsson/InjectModuleInitializer/blob/master/license.txt - https://github.com/einaregilsson/InjectModuleInitializer - true - true - Enables module initialization, as described here: http://einaregilsson.com/module-initializers-in-csharp/. - Enables module initialization. - module initializer assembly constructor - - - - - - - - - \ No newline at end of file diff --git a/nuget/content/ModuleInitializer.cs.pp b/nuget/content/ModuleInitializer.cs.pp deleted file mode 100644 index 11519d9..0000000 --- a/nuget/content/ModuleInitializer.cs.pp +++ /dev/null @@ -1,16 +0,0 @@ -// This file can be modified in any way, with two exceptions. 1) The name of -// this class must be "ModuleInitializer". 2) This class must have a public or -// internal parameterless "Run" method that returns void. In addition to these -// modifications, this file may also be moved to any location, as long as it -// remains a part of its current project. - -namespace $rootnamespace$ -{ - internal static class ModuleInitializer - { - internal static void Run() - { - // TODO: Add assembly initialization logic. - } - } -} \ No newline at end of file diff --git a/nuget/tools/install.ps1 b/nuget/tools/install.ps1 deleted file mode 100644 index 151ba63..0000000 --- a/nuget/tools/install.ps1 +++ /dev/null @@ -1,61 +0,0 @@ -param($installPath, $toolsPath, $package, $project) - -function Get-Relative-Path ($from, $to) -{ - $path = $NULL - - $fromPath = "" - for ($fromDirectory = New-Object System.IO.DirectoryInfo([System.IO.Path]::GetDirectoryName($from)); - $fromDirectory -ne $NULL; - $fromDirectory = $fromDirectory.Parent) - { - $toPath = "" - for ($toDirectory = New-Object System.IO.DirectoryInfo($to); - $toDirectory -ne $NULL; - $toDirectory = $toDirectory.Parent) - { - If ($fromDirectory.FullName.ToLower() -eq $toDirectory.FullName.ToLower()) - { - $path = $fromPath + $toPath - break - } - - $toPath = $toDirectory.Name + "\" + $toPath - } - - if ($path -ne $NULL) - { - break - } - - $fromPath = $fromPath + "..\" - } - - $path -} - -function Check-ModuleInitializer($buildProject) -{ - $moduleInitializers = $buildProject.Items | where {$_.Xml.ItemType -eq "Compile" -and $_.Xml.Include.EndsWith("ModuleInitializer.cs")} - $count = ($moduleInitializers | measure).Count - - if ($count -gt 1) - { - Write-Host "ModuleInitializer.cs already exists in another location. Deleting template..." - $project.ProjectItems.Item("ModuleInitializer.cs").Delete() - } -} - -$relativePath = Get-Relative-Path $project.FileName $toolsPath - -Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' -$buildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1 - -$target = $buildProject.Xml.AddTarget("InjectModuleInitializer") -$target.AfterTargets = "AfterBuild" -$task = $target.AddTask("Exec") -$task.SetParameter("Command", $relativePath + "InjectModuleInitializer.exe `"`$(TargetPath)`"") - -Check-ModuleInitializer($buildProject) - -$project.Save() \ No newline at end of file diff --git a/nuget/tools/uninstall.ps1 b/nuget/tools/uninstall.ps1 deleted file mode 100644 index bdd9067..0000000 --- a/nuget/tools/uninstall.ps1 +++ /dev/null @@ -1,15 +0,0 @@ -param($installPath, $toolsPath, $package, $project) - -Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' -$buildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1 -$projectRoot = $buildProject.Xml; - -foreach ($target in $projectRoot.Targets) -{ - if ($target.Name -eq "InjectModuleInitializer") - { - $projectRoot.RemoveChild($target); - } -} - -$project.Save() \ No newline at end of file diff --git a/nuget/readme.txt b/readme.txt similarity index 93% rename from nuget/readme.txt rename to readme.txt index e27766d..48bfbe1 100644 --- a/nuget/readme.txt +++ b/readme.txt @@ -8,7 +8,16 @@ _________ _______ ______ _______ | | | (___) || (__/ )| (___) |(_) )_( (_______)(______/ (_______) -1) Open ModuleInitializer.cs. Note the documentation at the top. +1) Add the following class to your project: + +internal static class ModuleInitializer +{ + internal static void Run() + { + // TODO: Add assembly initialization logic. + } +} + 2) Add initialization logic to the Run method's body. ******************************************************************************** diff --git a/vsts.yaml b/vsts.yaml new file mode 100644 index 0000000..acdc9e9 --- /dev/null +++ b/vsts.yaml @@ -0,0 +1,32 @@ +queue: Hosted VS2017 + +steps: + +- checkout: self + clean: true + +- task: MSBuild@1 + displayName: Restore + inputs: + solution: InjectModuleInitializer.sln + msbuildArguments: /t:Restore + +- task: MSBuild@1 + displayName: Version + inputs: + solution: InjectModuleInitializer.csproj + msbuildArguments: /t:Version + +- task: MSBuild@1 + displayName: Build + inputs: + solution: InjectModuleInitializer.sln + msbuildArguments: /bl:"$(Build.ArtifactStagingDirectory)\build.binlog" /p:PackageOutputPath=$(Build.ArtifactStagingDirectory) /v:d /p:Configuration=Release + +- task: PublishBuildArtifacts@1 + displayName: Publish Artifact + inputs: + PathtoPublish: $(Build.ArtifactStagingDirectory) + ArtifactName: out + ArtifactType: Container + condition: always() \ No newline at end of file