Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / tools / mdbrebase / mdbrebase.cs
1 using System;
2 using System.Reflection;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.IO;
6
7 using Mono.CompilerServices.SymbolWriter;
8 using Mono.Options;
9
10 namespace Mono.MdbRebase
11 {
12
13 class Settings
14 {
15         public string OutputDirectory { get; set; }
16         public string InputPattern { get; set; }
17         public string OutputPattern { get; set; }
18
19         public bool Validate ()
20         {
21                 return InputPattern != null && OutputPattern != null;
22         }
23
24         public string Replace (string input)
25         {
26                 if (input.StartsWith (InputPattern))
27                         return input.Replace (InputPattern, OutputPattern);
28                 return input;
29         }
30 }
31
32 class MdbRebase
33 {
34         Settings settings;
35
36         public MdbRebase (Settings settings)
37         {
38                 this.settings = settings;
39         }
40
41         public void RewriteMdbFile (string inputFile)
42         {
43                 Console.WriteLine ("Processing {0}", inputFile);
44                 var input = MonoSymbolFile.ReadSymbolFile (inputFile);
45
46                 var output = new MonoSymbolFile ();
47
48                 foreach (var s in input.Sources) {
49                         s.FileName = settings.Replace (s.FileName);
50                         output.AddSource (s);
51                 }
52
53                 foreach (var cu in input.CompileUnits) {
54                         cu.ReadAll ();
55                         output.AddCompileUnit (cu);
56                 }
57         
58                 foreach (var m in input.Methods) {
59                         m.ReadAll ();
60                         output.AddMethod (m);
61                 }
62
63
64                 var mdbName = new FileInfo (inputFile).Name;
65                 var tmpMdb = Path.Combine (Path.GetTempPath (), mdbName);
66                 var finalMdb = inputFile;
67                 if (settings.OutputDirectory != null)
68                         finalMdb = Path.Combine (settings.OutputDirectory, mdbName);
69
70                 using (var stream = new FileStream (tmpMdb, FileMode.Create)) {
71                         output.CreateSymbolFile (input.Guid, stream);
72                 }
73                 input.Dispose ();
74
75                 File.Delete (finalMdb);
76                 new FileInfo (tmpMdb).MoveTo (finalMdb);
77         }
78 }
79
80 class Driver {
81         static void Usage (OptionSet options)
82         {
83                 Console.WriteLine (@"Usage: mdbrebase [options] <ASSEMBLY_TO_FIX>");
84                 if (options != null) {
85                         Console.WriteLine ();
86                         Console.WriteLine ("Available options:");
87                         options.WriteOptionDescriptions (Console.Out);
88                 }
89                 Console.WriteLine ();
90                 
91                 Environment.Exit (-1);
92         }
93
94         static int Main (string[] args) {
95                 var s = new Settings ();
96                 bool showHelp = false;
97
98                 var p = new OptionSet () {
99                         { "d=|output=",  "Output directory to the mdb file, replace existing one if ommited", v => s.OutputDirectory = v },
100                         { "i=|input-pattern=", "Input pattern to replace (must not be a prefix to output-pattern)(required)", v => s.InputPattern = v },
101                         { "o=|output-pattern=", "Output pattern to replace (required)", v => s.OutputPattern = v },
102                         { "h|?|help", v => showHelp = true },
103                 };
104
105                 List <string> extra = p.Parse (args);
106
107                 if (showHelp || extra.Count < 1 || !s.Validate ())
108                         Usage (p);
109
110                 var m = new MdbRebase (s);
111                 foreach (var a in extra)
112                         m.RewriteMdbFile (a);
113                 return 0;
114
115         }
116 }
117 }