示例#1
0
        internal TreeShakingRewriter(IMetadataHost host, AssemblyReport analysisReport, bool dryRun, bool removeMethods, bool debugStubs, StubMethodBodyEmitter stubEmitter)
            : base(host)
        {
            this.analysisReport            = analysisReport;
            this.removeMethodsWhenPossible = removeMethods;
            this.fullDebugStubs            = debugStubs;
            this.dryRun = dryRun;

            this.stubMethodBodyEmitter = stubEmitter;
        }
    private static void RewriteBinary(
        Assembly copy,
        AssemblyReport assemblyReport,
        MetadataReaderHost host,
        string outputPath,
        MethodRemoval methodRemoval,
        StubMethodBodyEmitter stubEmitter) {


      /* This is an attempt to decouple the MethodRemoval commandline options
       * from the tree shaker, but it doesn't really seem to be working.
       * Might be better to just pass the method removal directly to
       * the rewriter.
       */

      bool removeMethods = (methodRemoval == MethodRemoval.Remove);
      bool fullDebugStubs = (methodRemoval == MethodRemoval.Debug);
      bool dryRun = (methodRemoval == MethodRemoval.None);

      PdbReader/*?*/ pdbReader = null;
      string pdbFile = Path.ChangeExtension(copy.Location, "pdb");
      if (File.Exists(pdbFile)) {
        using (var pdbStream = File.OpenRead(pdbFile)) {
          pdbReader = new PdbReader(pdbStream, host);
        }
      }
      else {
        Console.WriteLine("Could not load the PDB file for '" + copy.Name.Value + "' . Proceeding anyway.");
      }

      using (pdbReader) {
        var localScopeProvider = pdbReader == null ? null : new ILGenerator.LocalScopeProvider(pdbReader);
        var outputFileName = Path.GetFileNameWithoutExtension(outputPath);
        using (var peStream = File.Create(outputPath)) {
          using (var pdbWriter = new PdbWriter(outputFileName + ".pdb", pdbReader)) {
            var rewriter = new TreeShakingRewriter(host, assemblyReport, dryRun, removeMethods, fullDebugStubs, stubEmitter);
            IAssembly rewrittenCopy = rewriter.Rewrite(copy);

            PeWriter.WritePeToStream(rewrittenCopy, host, peStream, pdbReader, localScopeProvider, pdbWriter);
          }
        }
      }
    }
    internal TreeShakingRewriter(IMetadataHost host, AssemblyReport analysisReport, bool dryRun, bool removeMethods, bool debugStubs, StubMethodBodyEmitter stubEmitter)
      : base(host) {
      this.analysisReport = analysisReport;
      this.removeMethodsWhenPossible = removeMethods;
      this.fullDebugStubs = debugStubs;
      this.dryRun = dryRun;

      this.stubMethodBodyEmitter = stubEmitter;
    }