5 // Chris Bacon (chrisbacon76@gmail.com)
7 // Copyright (C) 2010 Chris Bacon
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
36 using Mono.CompilerServices.SymbolWriter;
38 namespace Mono.CodeContracts.Rewrite {
39 public class Rewriter {
41 public static RewriterResults Rewrite (RewriterOptions options)
43 Rewriter rewriter = new Rewriter(options);
44 return rewriter.RewriteImpl();
47 private Rewriter(RewriterOptions options)
49 this.options = options;
52 private RewriterOptions options;
53 private List<string> warnings = new List<string> ();
54 private List<string> errors = new List<string> ();
55 private bool usingMdb = false;
56 private bool usingPdb = false;
58 private void LoadSymbolReader (AssemblyDefinition assembly) {
59 if (this.options.Assembly.IsStream && this.options.Assembly.Streams.Symbols == null) {
60 this.warnings.Add ("-debug specified, but no symbol stream provided.");
63 foreach (ModuleDefinition module in assembly.Modules) {
64 module.LoadSymbols ();
69 if (!this.usingMdb && !this.usingPdb) {
70 this.warnings.Add ("-debug specified, but no MDB or PDB symbol file found.");
75 private ISymbolWriter LoadSymbolWriter(AssemblyDefinition assembly, AssemblyRef output)
77 // TODO: Get symbol writing to work.
78 // ISymbolWriterProvider symProv = null;
79 // if (this.usingMdb) {
80 // symProv = new Mono.Cecil.Mdb.MdbWriterProvider ();
81 // } else if (this.usingPdb) {
82 // symProv = new Mono.Cecil.Pdb.PdbWriterProvider ();
84 // this.warnings.Add ("-writePDBFile specified, but no symbol file found, cannot write symbols.");
86 // if (symProv != null) {
87 // return output.IsFilename ?
88 // symProv.GetSymbolWriter (assembly.MainModule, output.Filename) :
89 // symProv.GetSymbolWriter (assembly.MainModule, output.Streams.Symbols);
95 private RewriterResults RewriteImpl ()
97 if (!this.options.Rewrite) {
98 return RewriterResults.Warning ("Not asked to rewrite");
101 if (!this.options.Assembly.IsSet) {
102 return RewriterResults.Error ("No assembly given to rewrite");
104 AssemblyDefinition assembly = this.options.Assembly.IsFilename ?
105 AssemblyFactory.GetAssembly (this.options.Assembly.Filename) :
106 AssemblyFactory.GetAssembly (this.options.Assembly.Streams.Assembly);
108 if (this.options.ForceAssemblyRename != null) {
109 assembly.Name.Name = this.options.ForceAssemblyRename;
110 } else if (this.options.OutputFile.IsSet && this.options.OutputFile.IsFilename) {
111 assembly.Name.Name = Path.GetFileNameWithoutExtension(this.options.OutputFile.Filename);
115 this.LoadSymbolReader (assembly);
118 var output = this.options.OutputFile.IsSet ? this.options.OutputFile : this.options.Assembly;
119 ISymbolWriter symWriter = null;
120 if (options.WritePdbFile) {
121 if (!options.Debug) {
122 return RewriterResults.Error ("Must specify -debug if using -writePDBFile.");
124 if (output.IsStream && output.Streams.Symbols==null){
125 return RewriterResults.Error ("-writePDFFile specified, but no output symbol stream provided.");
127 symWriter = this.LoadSymbolWriter (assembly, output);
131 PerformRewrite rewriter = new PerformRewrite (symWriter, this.options);
132 rewriter.Rewrite (assembly);
134 if (output.IsFilename) {
135 AssemblyFactory.SaveAssembly(assembly, output.Filename);
137 AssemblyFactory.SaveAssembly(assembly, output.Streams.Assembly);
140 if (symWriter != null) {
141 symWriter.Dispose ();
145 return new RewriterResults (warnings, errors);