3 // Marek Habersack (mhabersack@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.
29 using System.Collections.Generic;
30 using System.ComponentModel;
31 using System.Diagnostics;
35 using System.Xml.XPath;
37 namespace Mono.MonoConfig
39 public enum ActionType
46 public enum ActionWhen
52 public class FeatureAction
57 string commandArguments;
61 public ActionWhen 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 ();
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 ();
85 script = sb.ToString ();
89 command = Helpers.GetRequiredNonEmptyAttribute (nav, "command");
90 commandArguments = Helpers.GetOptionalAttribute (nav, "commndArguments");
95 public void Execute ()
98 case ActionType.Message:
102 case ActionType.ShellScript:
103 ExecuteShellScript ();
106 case ActionType.Exec:
112 void ExecuteMessage ()
114 if (String.IsNullOrEmpty (message))
117 string[] lines = message.Split ('\n');
119 int maxLineWidth = Console.WindowWidth;
120 StringBuilder sb = new StringBuilder ();
122 foreach (string l in lines) {
129 if (line.Length > maxLineWidth)
130 sb.AppendFormat ("{0}\n", Helpers.BreakLongLine (line, String.Empty, maxLineWidth));
132 sb.AppendFormat ("{0}{1}\n", String.Empty, line);
134 Console.WriteLine (sb.ToString ());
137 void ExecuteShellScript ()
139 if (String.IsNullOrEmpty (script))
142 string script_temp = Path.GetTempFileName ();
143 StreamWriter s = null;
145 s = new StreamWriter (script_temp);
148 Process p = new Process ();
149 ProcessStartInfo pinfo = p.StartInfo;
151 pinfo.UseShellExecute = false;
152 pinfo.RedirectStandardOutput = true;
153 pinfo.RedirectStandardError = true;
154 pinfo.FileName = "/bin/bash";
155 pinfo.Arguments = script_temp;
158 string stdout = p.StandardOutput.ReadToEnd ();
159 string stderr = p.StandardError.ReadToEnd ();
162 if (p.ExitCode != 0) {
163 Console.Error.Write (stderr);
164 throw new ApplicationException (
165 String.Format ("Script signalled failure code: {0}", p.ExitCode));
168 Console.Write (stdout);
170 } catch (Exception ex) {
171 Console.WriteLine (ex);
172 throw new ApplicationException ("Error executing feature shell action.", ex);
177 File.Delete (script_temp);
178 } catch (Exception) {