2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / mconfig / Mono.MonoConfig / FeatureAction.cs
blob246ffeebe0845c239b1c9ecb93df058cfdbe0cf3
1 //
2 // Authors:
3 // Marek Habersack (mhabersack@novell.com)
4 //
5 // (C) 2007 Novell, Inc
6 //
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel;
31 using System.Diagnostics;
32 using System.IO;
33 using System.Text;
34 using System.Xml;
35 using System.Xml.XPath;
37 namespace Mono.MonoConfig
39 public enum ActionType
41 Message,
42 ShellScript,
43 Exec
46 public enum ActionWhen
48 Before,
49 After
52 public class FeatureAction
54 ActionType type;
55 ActionWhen when;
56 string command;
57 string commandArguments;
58 string message;
59 string script;
61 public ActionWhen When {
62 get { return when; }
65 public FeatureAction (XPathNavigator nav)
67 string val = Helpers.GetRequiredNonEmptyAttribute (nav, "type");
68 type = Helpers.ConvertEnum <ActionType> (val, "type");
70 val = Helpers.GetRequiredNonEmptyAttribute (nav, "when");
71 when = Helpers.ConvertEnum <ActionWhen> (val, "when");
73 XPathNodeIterator iter;
74 StringBuilder sb = new StringBuilder ();
76 switch (type) {
77 case ActionType.Message:
78 case ActionType.ShellScript:
79 iter = nav.Select ("./text()");
80 while (iter.MoveNext ())
81 sb.Append (iter.Current.Value);
82 if (type == ActionType.Message)
83 message = sb.ToString ();
84 else
85 script = sb.ToString ();
86 break;
88 case ActionType.Exec:
89 command = Helpers.GetRequiredNonEmptyAttribute (nav, "command");
90 commandArguments = Helpers.GetOptionalAttribute (nav, "commndArguments");
91 break;
95 public void Execute ()
97 switch (type) {
98 case ActionType.Message:
99 ExecuteMessage ();
100 break;
102 case ActionType.ShellScript:
103 ExecuteShellScript ();
104 break;
106 case ActionType.Exec:
107 ExecuteExec ();
108 break;
112 void ExecuteMessage ()
114 if (String.IsNullOrEmpty (message))
115 return;
117 string[] lines = message.Split ('\n');
118 string line;
119 int maxLineWidth = Console.WindowWidth;
120 StringBuilder sb = new StringBuilder ();
122 foreach (string l in lines) {
123 if (l.Length == 0) {
124 sb.Append ("\n");
125 continue;
128 line = l.Trim ();
129 if (line.Length > maxLineWidth)
130 sb.AppendFormat ("{0}\n", Helpers.BreakLongLine (line, String.Empty, maxLineWidth));
131 else
132 sb.AppendFormat ("{0}{1}\n", String.Empty, line);
134 Console.WriteLine (sb.ToString ());
137 void ExecuteShellScript ()
139 if (String.IsNullOrEmpty (script))
140 return;
142 string script_temp = Path.GetTempFileName ();
143 StreamWriter s = null;
145 try {
146 s = new StreamWriter (script_temp);
147 s.Write (script);
148 s.Flush ();
149 s.Close ();
150 RunCommand ("/bin/sh", script_temp);
151 } catch (Exception ex) {
152 throw new ApplicationException ("Error executing feature 'shell script' action.", ex);
153 } finally {
154 if (s != null)
155 s.Close ();
156 try {
157 File.Delete (script_temp);
158 } catch (Exception) {
159 // ignore
164 void ExecuteExec ()
166 if (String.IsNullOrEmpty (command))
167 return;
169 try {
170 RunCommand (command, commandArguments);
171 } catch (Exception ex) {
172 throw new ApplicationException ("Error executing feature 'exec' action.", ex);
176 void RunCommand (string commandPath, string format, params object[] arguments)
178 if (String.IsNullOrEmpty (commandPath))
179 return;
180 string args;
182 if (!String.IsNullOrEmpty (format))
183 args = String.Format (format, arguments);
184 else
185 args = String.Empty;
187 Process p = null;
189 try {
190 p = new Process ();
191 ProcessStartInfo pinfo = p.StartInfo;
193 pinfo.UseShellExecute = false;
194 pinfo.RedirectStandardOutput = true;
195 pinfo.RedirectStandardError = true;
196 pinfo.FileName = commandPath;
197 pinfo.Arguments = args;
198 p.Start ();
200 string stdout = p.StandardOutput.ReadToEnd ();
201 string stderr = p.StandardError.ReadToEnd ();
202 p.WaitForExit ();
204 if (!String.IsNullOrEmpty (stdout))
205 Console.WriteLine (stdout);
206 if (!String.IsNullOrEmpty (stderr))
207 Console.Error.WriteLine (stderr);
209 int exitCode = p.ExitCode;
210 if (exitCode != 0)
211 throw new ApplicationException (
212 String.Format ("Process signalled failure code: {0}", exitCode));
213 } finally {
214 if (p != null)
215 p.Close ();