Skip to content

Commit 37bd867

Browse files
nirbarflcdrg
authored andcommitted
Handle null results (#13)
* Handle null results * Support overriding immediate actions scheduling
1 parent 89753dd commit 37bd867

3 files changed

Lines changed: 56 additions & 50 deletions

File tree

PowerShellActions/CustomAction.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static ActionResult ScriptsImmediate(Session session, int elevated, stri
8181
try
8282
{
8383
CustomActionData data;
84-
using ( View view = db.OpenView( string.Format( "SELECT `Id`, `Script` FROM `PowerShellScripts` WHERE `Elevated` = {0}", elevated ) ) )
84+
using (View view = db.OpenView(string.Format("SELECT `Id`, `Script` FROM `PowerShellScripts` WHERE `Elevated` = {0}", elevated)))
8585
{
8686
view.Execute();
8787

@@ -108,11 +108,11 @@ private static ActionResult ScriptsImmediate(Session session, int elevated, stri
108108
// length of the progress bar by the total number of ticks in
109109
// the custom action.
110110
MessageResult iResult;
111-
using ( var hProgressRec = new Record( 2 ) )
111+
using (var hProgressRec = new Record(2))
112112
{
113113
hProgressRec[1] = 3;
114114
hProgressRec[2] = TotalTicks;
115-
iResult = session.Message( InstallMessage.Progress, hProgressRec );
115+
iResult = session.Message(InstallMessage.Progress, hProgressRec);
116116
}
117117

118118
if (iResult == MessageResult.Cancel)
@@ -136,12 +136,12 @@ private static ActionResult ScriptsImmediate(Session session, int elevated, stri
136136
private static ActionResult ScriptsDeferred(Session session, string deferredProperty)
137137
{
138138
MessageResult iResult;
139-
using ( var hActionRec = new Record( 3 ) )
139+
using (var hActionRec = new Record(3))
140140
{
141141
hActionRec[1] = deferredProperty;
142142
hActionRec[2] = "PowerShell Scripts";
143143
hActionRec[3] = "[1] of [2], [3]";
144-
iResult = session.Message( InstallMessage.ActionStart, hActionRec );
144+
iResult = session.Message(InstallMessage.ActionStart, hActionRec);
145145
}
146146

147147
if (iResult == MessageResult.Cancel)
@@ -150,7 +150,7 @@ private static ActionResult ScriptsDeferred(Session session, string deferredProp
150150
}
151151

152152
// Tell the installer to use explicit progress messages.
153-
using ( var hProgressRec = new Record( 3 ) )
153+
using (var hProgressRec = new Record(3))
154154
{
155155
hProgressRec[1] = 1;
156156
hProgressRec[2] = 1;
@@ -175,11 +175,11 @@ private static ActionResult ScriptsDeferred(Session session, string deferredProp
175175
using (var task = new PowerShellTask(script, session))
176176
{
177177
var result = task.Execute();
178-
session.Log( "PowerShell non-terminating errors: {0}", !result );
178+
session.Log("PowerShell non-terminating errors: {0}", !result);
179179

180-
if ( !result )
180+
if (!result)
181181
{
182-
session.Log( "Returning Failure" );
182+
session.Log("Returning Failure");
183183

184184
return ActionResult.Failure;
185185
}
@@ -190,7 +190,7 @@ private static ActionResult ScriptsDeferred(Session session, string deferredProp
190190
}
191191
catch (Exception ex)
192192
{
193-
session.Log( "PowerShell terminating error, returning Failure" );
193+
session.Log("PowerShell terminating error, returning Failure");
194194
session.Log(ex.ToString());
195195
return ActionResult.Failure;
196196
}
@@ -207,11 +207,11 @@ private static ActionResult FilesImmediate(Session session, int elevated, string
207207
try
208208
{
209209
XDocument doc;
210-
using ( View view = db.OpenView( string.Format( "SELECT `Id`, `File`, `Arguments` FROM `{0}` WHERE `Elevated` = {1}", tableName, elevated ) ) )
210+
using (View view = db.OpenView(string.Format("SELECT `Id`, `File`, `Arguments` FROM `{0}` WHERE `Elevated` = {1}", tableName, elevated)))
211211
{
212212
view.Execute();
213213

214-
doc = new XDocument( new XDeclaration( "1.0", "utf-16", "yes" ), new XElement( "r" ) );
214+
doc = new XDocument(new XDeclaration("1.0", "utf-16", "yes"), new XElement("r"));
215215

216216
foreach (Record row in view)
217217
{
@@ -232,11 +232,11 @@ private static ActionResult FilesImmediate(Session session, int elevated, string
232232
// length of the progress bar by the total number of ticks in
233233
// the custom action.
234234
MessageResult iResult;
235-
using ( var hProgressRec = new Record( 2 ) )
235+
using (var hProgressRec = new Record(2))
236236
{
237237
hProgressRec[1] = 3;
238238
hProgressRec[2] = TotalTicks;
239-
iResult = session.Message( InstallMessage.Progress, hProgressRec );
239+
iResult = session.Message(InstallMessage.Progress, hProgressRec);
240240
}
241241

242242
if (iResult == MessageResult.Cancel)
@@ -266,12 +266,12 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
266266
// action is doing. Tell the installer to use this template and
267267
// text in progress messages.
268268
MessageResult iResult;
269-
using ( var hActionRec = new Record( 3 ) )
269+
using (var hActionRec = new Record(3))
270270
{
271271
hActionRec[1] = deferredProperty;
272272
hActionRec[2] = "PowerShell Files";
273273
hActionRec[3] = "[1] of [2], [3]";
274-
iResult = session.Message( InstallMessage.ActionStart, hActionRec );
274+
iResult = session.Message(InstallMessage.ActionStart, hActionRec);
275275
}
276276

277277
if (iResult == MessageResult.Cancel)
@@ -280,7 +280,7 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
280280
}
281281

282282
// Tell the installer to use explicit progress messages.
283-
using ( var hProgressRec = new Record( 3 ) )
283+
using (var hProgressRec = new Record(3))
284284
{
285285
hProgressRec[1] = 1;
286286
hProgressRec[2] = 1;
@@ -298,7 +298,7 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
298298
if (!session.CustomActionData.ContainsKey("xml"))
299299
{
300300
session.Log("Skipping as no CustomActionData key 'xml'");
301-
return ActionResult.NotExecuted;
301+
return ActionResult.NotExecuted;
302302
}
303303

304304
string content = session.CustomActionData["xml"];
@@ -313,11 +313,11 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
313313

314314
using (var task = new PowerShellTask(file, arguments, session))
315315
{
316-
var result = task.Execute();
316+
bool result = task.Execute();
317317
session.Log("PowerShell non-terminating errors: {0}", !result);
318-
if ( !result )
318+
if (!result)
319319
{
320-
session.Log( "Returning Failure" );
320+
session.Log("Returning Failure");
321321
return ActionResult.Failure;
322322
}
323323
}
@@ -327,10 +327,10 @@ private static ActionResult FilesDeferred(Session session, string deferredProper
327327
}
328328
catch (Exception ex)
329329
{
330-
session.Log( "PowerShell terminating error, returning Failure" );
331-
session.Log( ex.ToString() );
330+
session.Log("PowerShell terminating error, returning Failure");
331+
session.Log(ex.ToString());
332332
return ActionResult.Failure;
333333
}
334334
}
335335
}
336-
}
336+
}

PowerShellActions/PowerShellTask.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,42 @@ public bool Execute()
6262
{
6363
var results = _pipeline.Invoke();
6464

65-
var record = new Record(0);
66-
record[0] = string.Format("Exit code {0}", ExitCode);
67-
_session.Message(InstallMessage.Info, record);
65+
using (var record = new Record(0))
66+
{
67+
record[0] = string.Format("Exit code {0}", ExitCode);
68+
_session.Message(InstallMessage.Info, record);
6869

69-
if (results.Any())
70-
_session.Log("Output");
70+
if (results?.Any() ?? false)
71+
{
72+
_session.Log("Output");
7173

72-
foreach (var r in results)
73-
{
74-
_session.Log(r.BaseObject.ToString());
75-
}
74+
foreach (var r in results)
75+
{
76+
if (r?.BaseObject != null)
77+
{
78+
_session.Log("\t" + r?.BaseObject?.ToString() ?? "");
79+
}
80+
}
81+
}
7682

77-
var errors = Errors();
83+
var errors = Errors();
84+
if (errors != null)
85+
{
86+
_session.Log("Non-terminating errors");
7887

79-
if (errors != null)
80-
{
81-
_session.Log( "Non-terminating errors" );
88+
record[0] = errors;
89+
_session.Message(InstallMessage.Error, record);
90+
}
8291

83-
record[0] = errors;
84-
_session.Message(InstallMessage.Error, record);
92+
// Using .Error instead of .HadErrors to support any PS version.
93+
return (((_pipeline?.Error?.Count ?? 0) == 0) && (errors == null) && (ExitCode == 0));
8594
}
86-
87-
// Using .Error instead of .HadErrors to support any PS version.
88-
return ((_pipeline.Error == null) || (_pipeline.Error.Count == 0)) && errors == null && ExitCode == 0;
8995
}
9096

9197
public string Errors()
9298
{
9399
// check for errors (non-terminating)
94-
if (_pipeline.Error.Count > 0)
100+
if ((_pipeline?.Error?.Count ?? 0) > 0)
95101
{
96102
var builder = new StringBuilder();
97103

@@ -107,11 +113,11 @@ public string Errors()
107113
if (r != null)
108114
{
109115
// build whatever kind of message you want
110-
builder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message);
111-
builder.AppendLine(r.InvocationInfo.PositionMessage);
116+
builder.AppendLine(r.InvocationInfo?.MyCommand?.Name ?? "" + " : " + r.Exception.Message);
117+
builder.AppendLine(r.InvocationInfo?.PositionMessage ?? "");
112118
builder.AppendLine(string.Format("+ CategoryInfo: {0}", r.CategoryInfo));
113119
builder.AppendLine(
114-
string.Format("+ FullyQualifiedErrorId: {0}", r.FullyQualifiedErrorId));
120+
string.Format("+ FullyQualifiedErrorId: {0}", r.FullyQualifiedErrorId ?? ""));
115121
}
116122
}
117123
}

PowerShellLibrary/Library.wxs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@
5757

5858
<InstallExecuteSequence>
5959
<!-- These are the 'immediate' actions that prep the data for the 'deferred' actions-->
60-
<Custom Action="PowerShellScriptsImmediate" Before="PowerShellScriptsDeferred">NOT Installed</Custom>
60+
<Custom Action="PowerShellScriptsImmediate" Before="PowerShellScriptsDeferred" Overridable="yes">NOT Installed</Custom>
6161

62-
<Custom Action="PowerShellScriptsElevatedImmediate" Before="PowerShellScriptsElevatedDeferred">NOT Installed</Custom>
62+
<Custom Action="PowerShellScriptsElevatedImmediate" Before="PowerShellScriptsElevatedDeferred" Overridable="yes">NOT Installed</Custom>
6363

64-
<Custom Action="PowerShellFilesImmediate" Before="PowerShellFilesDeferred">NOT Installed</Custom>
64+
<Custom Action="PowerShellFilesImmediate" Before="PowerShellFilesDeferred" Overridable="yes">NOT Installed</Custom>
6565

66-
<Custom Action="PowerShellFilesElevatedImmediate" Before="PowerShellFilesElevatedDeferred">NOT Installed</Custom>
66+
<Custom Action="PowerShellFilesElevatedImmediate" Before="PowerShellFilesElevatedDeferred" Overridable="yes">NOT Installed</Custom>
6767

6868
<!-- 'Deferred' actions -->
6969
<Custom Action="PowerShellScriptsDeferred" After="InstallFiles" Overridable="yes">NOT Installed</Custom>

0 commit comments

Comments
 (0)