Resolve() public method

public Resolve ( IMetadataScope scope ) : AssemblyDefinition
scope IMetadataScope
return Mono.Cecil.AssemblyDefinition
Ejemplo n.º 1
0
        /// <summary>
        /// Performs packing.
        /// </summary>
        public void Run()
        {
            // Steps
            // 1) Mono.Cecil: Determine assembly dependencies
            // 2) ILMerge: Merge exe into a single assembly
            // 3) Mono.Linker
            var includeMergeListRegex = new string[] { @"SharpDX\..*" };

            // Step 1 : Mono.Cecil: Determine assembly dependencies
            var assembly = AssemblyDefinition.ReadAssembly(MainAssembly);
            var corlib = (AssemblyNameReference)assembly.MainModule.TypeSystem.Corlib;
            bool isNet40 = corlib.Version.Major == 4;

            var paths = new List<string>();

            var fromDirectory = Path.GetDirectoryName(assembly.MainModule.FullyQualifiedName);

            // Load SharpDX assemblies
            AddAssemblies(assembly, paths, fromDirectory, includeMergeListRegex);

            // Load assemblies to link
            foreach (var assemblyToLinkName in AssembliesToLink)
            {
                var assemblyToLink = AssemblyDefinition.ReadAssembly(assemblyToLinkName);
                paths.Add(assemblyToLink.MainModule.FullyQualifiedName);
            }


            // Step 2: ILMerge: Merge exe into a single assembly
            var merge = new ILMerge();

            String[] files = paths.ToArray();

            if (!Directory.Exists(OutputDirectory))
                Directory.CreateDirectory(OutputDirectory);

            //Here we get the first file name (which was the .exe file) and use that
            // as the output
            String strOutputFile = System.IO.Path.GetFileName(files[0]);

            merge.OutputFile = OutputDirectory + "\\" + strOutputFile;
            merge.SetInputAssemblies(files);
            merge.DebugInfo = false;
            merge.CopyAttributes = true;
            merge.AllowMultipleAssemblyLevelAttributes = true;
            merge.XmlDocumentation = false;

            // Special case for v4 framework
            // See http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
            if (isNet40)
            {
                // Retrieve the install root path for the framework
                string installRoot = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NetFramework", false).GetValue("InstallRoot").ToString();
                var directorties = Directory.GetDirectories(installRoot, "v4.*");
                if (directorties.Length == 0)
                    UsageError(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Cannot found any .Net 4.0 directory from [{0}] ", installRoot));
                merge.SetTargetPlatform("v4", directorties[0]);                
            }

            merge.Merge();

            // Step 3: Mono.Linker
            if (!NoLinker)
            {
                var pipeline = GetStandardPipeline();
                var context = new LinkContext(pipeline) {CoreAction = AssemblyAction.Skip, OutputDirectory = OutputDirectory};
                context.OutputDirectory = OutputDirectory;

                var mainAssemblyDirectory = new DirectoryInfo(Path.GetDirectoryName(Path.GetFullPath(MainAssembly)));
                context.Resolver.AddSearchDirectory(mainAssemblyDirectory.FullName);

                // Load assembly merged previously by ILMerge
                var mergedAssemblyDefinition = context.Resolve(merge.OutputFile);

                // Create Mono.Linker default pipeline
                pipeline = GetStandardPipeline();
                pipeline.PrependStep(new ResolveFromAssemblyStep(mergedAssemblyDefinition));

                // Add custom step for ComObject constructors
                pipeline.AddStepBefore(typeof (SweepStep), new ComObjectStep());

                pipeline.Process(context);
            }

            Console.WriteLine("Assembly successfully packed to [{0}]", merge.OutputFile);
        }