b5d7ff13094ed08265f32c380850c127bc31644a
[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.Reflection;
12
13 using Mono.Cecil;
14
15 namespace Mono.CilStripper {
16
17         class Program {
18
19                 static int 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                         bool replace = file == output;
29
30                         try {
31                                 using (var assembly = AssemblyDefinition.ReadAssembly (file, new ReaderParameters { ReadWrite = replace })) {
32                                         StripAssembly (assembly, replace ? null : output);
33                                 }
34
35                                 if (replace)
36                                         Console.WriteLine ("Assembly {0} stripped", file);
37                                 else
38                                         Console.WriteLine ("Assembly {0} stripped out into {1}", file, output);
39                                 return 0;
40                         } catch (TargetInvocationException tie) {
41                                 Console.WriteLine ("Error: {0}", tie.InnerException);
42                         } catch (Exception e) {
43                                 Console.WriteLine ("Error: {0}", e);
44                         }
45                         return 1;
46                 }
47
48                 static void StripAssembly (AssemblyDefinition assembly, string output)
49                 {
50                         AssemblyStripper.StripAssembly (assembly, output);
51                 }
52
53                 static void Header ()
54                 {
55                         Console.WriteLine ("Mono CIL Stripper");
56                         Console.WriteLine ();
57                 }
58
59                 static void Usage ()
60                 {
61                         Console.WriteLine ("Usage: mono-cil-strip file [output]");
62                         Environment.Exit (1);
63                 }
64         }
65 }