Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msbuild] Improve altool task by logging execution errors #6815

Merged
merged 2 commits into from
Aug 23, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion msbuild/Xamarin.MacDev.Tasks.Core/Tasks/AlToolTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Text;

namespace Xamarin.MacDev.Tasks
{
public abstract class ALToolTaskBase : ToolTask
{
string sdkDevPath;
StringBuilder toolOutput;

public string SessionId { get; set; }

Expand Down Expand Up @@ -40,6 +42,17 @@ string DevicePlatformBinDir {
get { return Path.Combine (SdkDevPath, "usr", "bin"); }
}

public override bool Execute ()
{
toolOutput = new StringBuilder ();

base.Execute ();

LogErrorsFromOutput (toolOutput.ToString ());

return !HasLoggedErrors;
}

protected override string GenerateFullPathToTool ()
{
if (!string.IsNullOrEmpty (ToolPath))
Expand Down Expand Up @@ -70,6 +83,7 @@ protected override string GenerateCommandLineCommands ()

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
toolOutput.Append (singleLine);
Log.LogMessage (messageImportance, "{0}", singleLine);
}

Expand All @@ -82,5 +96,28 @@ string GetFileTypeValue ()
default: throw new NotSupportedException ($"Provided file type '{FileType}' is not supported by altool");
}
}

void LogErrorsFromOutput (string output)
{
try {
if (string.IsNullOrEmpty(output))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: space before (

return;

var plist = PObject.FromString (output) as PDictionary;
var errors = PObject.Create (PObjectType.Array) as PArray;
var message = PObject.Create (PObjectType.String) as PString;

if ((plist?.TryGetValue ("product-errors", out errors) == true)) {
foreach (var error in errors) {
var dict = error as PDictionary;
if (dict?.TryGetValue ("message", out message) == true) {
Log.LogError (ToolName, null, null, null, 0, 0, 0, 0, "{0}", message.Value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if the API (or some overloads) allows it but "{0}" seems to require a call to String.Format, can't you just provide message.Value ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spouliot What if message.Value contains something that can be a format string? Say "{0}" for instance, then things might fail (depending on LogError's implementation).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, bring us back to not sure if the API :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I don't really like it (using "{0}") but it seems to be safer, and that's how most of the XI tasks log errors that come from tool outputs or text we don't control.

}
}
}
} catch (Exception ex) {
Log.LogWarning ("Failed to parse altool output: {0}", ex.Message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you include output in the exception log ?
That way you can get a clue of what went wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also $"Failed to parse altool output: {ex.Message}" looks nicer :)

}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary space change