add cil-strip tool
[mono.git] / mcs / tools / cil-strip / cilstrip.cs
1 //
2 // mono-cil-strip
3 //
4 // Author(s):
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.IO;
12
13 using Mono.Cecil;
14
15 namespace Mono.CilStripper {
16
17         class Program {
18
19                 static void Main (string [] args)
20                 {
21                         Header ();
22
23                         if (args.Length == 0)
24                                 Usage ();
25
26                         string file = args [0];
27                         string output = args.Length > 1 ? args [1] : file;
28
29                         try {
30                                 AssemblyDefinition assembly = AssemblyFactory.GetAssembly (file);
31                                 StripAssembly (assembly, output);
32
33                                 if (file != output)
34                                         Console.WriteLine ("Assembly {0} stripped out into {1}", file, output);
35                                 else
36                                         Console.WriteLine ("Assembly {0} stripped", file);
37                         } catch (Exception e) {
38                                 Console.WriteLine ("Error: {0}", e);
39                         }
40                 }
41
42                 static void StripAssembly (AssemblyDefinition assembly, string output)
43                 {
44                         Type stripper = typeof (AssemblyDefinition).Assembly.GetType ("Mono.Cecil.AssemblyStripper");
45                         if (stripper == null)
46                                 throw new NotSupportedException ("Cecil doesn't have support for mono-cil-strip");
47
48                         stripper.GetMethod ("StripAssembly").Invoke (null, new object [] { assembly, output });
49                 }
50
51                 static void Header ()
52                 {
53                         Console.WriteLine ("Mono CIL Stripper");
54                         Console.WriteLine ();
55                 }
56
57                 static void Usage ()
58                 {
59                         Console.WriteLine ("Usage: mono-cil-strip file [output]");
60                         Environment.Exit (1);
61                 }
62         }
63 }