3 // Miguel de Icaza (miguel@novell.com)
5 // (C) 2007 Novell, Inc
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.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
34 using System.Configuration.Install;
36 public class InstallUtil {
38 static bool showcallstack = false;
39 static bool logtoconsole = false;
40 static string assembly = null;
42 static void ShowHelpForAssembly (string assembly)
44 Console.WriteLine ("Help for assembly not implemented");
47 static void ShowHelp ()
49 Console.WriteLine ("installutil -- Installs Assemblies that use System.Configuration.Install");
50 Console.WriteLine ("Usage is: installutil commands\n");
51 Console.WriteLine ("\n" +
52 " /help Shows help\n" +
53 " /help ASSEM Shows help for the given assembly\n" +
54 " /logfile[=out] Specifies a log file\n" +
55 " /uninstall ASSEM Uninstall the given assembly\n");
58 static void Call (Installer instance, string method, object arg)
60 Console.WriteLine ("M: " + method);
61 MethodInfo mi = typeof (Installer).GetMethod (method, BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Static);
62 mi.Invoke (instance, new object [] { arg });
65 static void Error (string st)
67 Console.Error.WriteLine (st);
70 static void Perform (bool install, string executable)
72 ArrayList order = new ArrayList ();
73 Hashtable states = new Hashtable ();
79 a = Assembly.Load (assembly);
81 a = Assembly.LoadFrom (executable);
83 Type [] types = a.GetTypes ();
85 // todo: pass arguments, they are kind of useless though.
86 InstallContext ctx = new InstallContext ();
88 foreach (Type t in types){
89 if (!t.IsSubclassOf (typeof (Installer)))
92 object [] attrs = t.GetCustomAttributes (typeof (RunInstallerAttribute), false);
93 if (attrs == null || attrs.Length == 0)
96 RunInstallerAttribute ria = attrs [0] as RunInstallerAttribute;
97 if (ria == null || !ria.RunInstaller)
101 Installer installer = (Installer) Activator.CreateInstance (t);
102 Hashtable state = new Hashtable ();
104 order.Add (installer);
105 states [installer] = state;
108 Call (installer, "OnBeforeInstall", state);
110 Call (installer, "OnBeforeUninstall", state);
112 installer.Install (state);
115 Call (installer, "OnAfterInstall", state);
117 Call (installer, "OnAfterUninstall", state);
119 } catch (Exception e) {
120 Error (String.Format ("Can not create installer of type {0}", t));
123 // According to the docs uninstall should not do rollback
126 foreach (Installer installer in order){
127 Hashtable state = (Hashtable) states [installer];
129 Call (installer, "OnBeforeRollback", state);
130 installer.Rollback (state);
131 Call (installer, "OnAfterRollback", state);
137 // Got it, now commit them
140 foreach (Installer inst in order){
141 Hashtable state = (Hashtable) states [inst];
143 Call (inst, "OnCommitting", state);
145 Call (inst, "OnCommitted", state);
149 Error (String.Format ("Unable to load assembly {0}", assembly));
153 static void Install (string assembly)
155 Perform (true, assembly);
158 static void Uninstall (string assembly)
160 Perform (false, assembly);
163 static int Main (string [] args)
165 bool did_something = false;
166 string logfile = null;
168 for (int i = 0; i < args.Length; i++){
169 string arg = args [i];
172 if (c == '/' || c == '-'){
173 switch (arg.ToLower ()){
174 case "/help": case "/h": case "/?":
175 case "-help": case "-h": case "-?":
176 if (i + 1 < args.Length){
178 ShowHelpForAssembly (args [i]);
185 case "-showcallstack":
186 case "/showcallstack":
187 showcallstack = true;
190 case "-logtoconsole":
191 case "/logtoconsole":
195 case "-u": case "-uninstall":
196 case "/u": case "/uninstall":
197 if (i + 1 < args.Length){
199 Uninstall (args [i]);
200 did_something = true;
207 case "-assemblyname":
208 case "/assemblyname":
209 if (i + 1 < args.Length){
224 did_something = true;