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;
146 s
= new StreamWriter (script_temp
);
150 RunCommand ("/bin/sh", script_temp
);
151 } catch (Exception ex
) {
152 throw new ApplicationException ("Error executing feature 'shell script' action.", ex
);
157 File
.Delete (script_temp
);
158 } catch (Exception
) {
166 if (String
.IsNullOrEmpty (command
))
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
))
182 if (!String
.IsNullOrEmpty (format
))
183 args
= String
.Format (format
, arguments
);
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
;
200 string stdout
= p
.StandardOutput
.ReadToEnd ();
201 string stderr
= p
.StandardError
.ReadToEnd ();
204 if (!String
.IsNullOrEmpty (stdout
))
205 Console
.WriteLine (stdout
);
206 if (!String
.IsNullOrEmpty (stderr
))
207 Console
.Error
.WriteLine (stderr
);
209 int exitCode
= p
.ExitCode
;
211 throw new ApplicationException (
212 String
.Format ("Process signalled failure code: {0}", exitCode
));