in src/Sarif.Viewer.VisualStudio.Core/ErrorList/ErrorListService.cs [111:275]
public static async Task ProcessLogFileCoreAsync(string filePath, string toolFormat, bool promptOnLogConversions, bool cleanErrors, bool openInEditor)
{
SarifLog log = null;
string logText = null;
string outputPath = null;
bool saveOutputFile = true;
if (toolFormat.MatchesToolFormat(ToolFormat.None))
{
await RetryInvokeAsync(
async () =>
{
using (var logStreamReader = new StreamReader(filePath, Encoding.UTF8))
{
logText = await logStreamReader.ReadToEndAsync().ConfigureAwait(continueOnCapturedContext: false);
}
},
retryInterval: TimeSpan.FromMilliseconds(300),
maxAttemptCount: 5);
Match match = MatchVersionProperty(logText);
if (match.Success)
{
string inputVersion = match.Groups["version"].Value;
if (inputVersion == SarifUtilities.V1_0_0)
{
// They're opening a v1 log, so we need to transform it.
// Ask if they'd like to save the v2 log.
MessageDialogCommand response = promptOnLogConversions ?
await PromptToSaveProcessedLogAsync(Resources.TransformV1_DialogMessage).ConfigureAwait(continueOnCapturedContext: false) :
MessageDialogCommand.No;
if (response == MessageDialogCommand.Cancel)
{
return;
}
var settingsV1 = new JsonSerializerSettings()
{
ContractResolver = SarifContractResolverVersionOne.Instance,
};
SarifLogVersionOne v1Log = JsonConvert.DeserializeObject<SarifLogVersionOne>(logText, settingsV1);
var transformer = new SarifVersionOneToCurrentVisitor();
transformer.VisitSarifLogVersionOne(v1Log);
log = transformer.SarifLog;
if (response == MessageDialogCommand.Yes)
{
// Prompt for a location to save the transformed log.
outputPath = await PromptForFileSaveLocationAsync(Resources.SaveTransformedV1Log_DialogTitle, filePath).ConfigureAwait(continueOnCapturedContext: false);
if (string.IsNullOrEmpty(outputPath))
{
return;
}
}
logText = JsonConvert.SerializeObject(log);
}
else if (inputVersion != VersionConstants.StableSarifVersion)
{
// It's an older v2 version, so send it through the pre-release compat transformer.
// Ask if they'd like to save the transformed log.
MessageDialogCommand response = promptOnLogConversions ?
await PromptToSaveProcessedLogAsync(string.Format(Resources.TransformPrereleaseV2_DialogMessage, VersionConstants.StableSarifVersion)).ConfigureAwait(continueOnCapturedContext: false) :
MessageDialogCommand.No;
if (response == MessageDialogCommand.Cancel)
{
return;
}
log = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(logText, Formatting.Indented, out logText);
if (response == MessageDialogCommand.Yes)
{
// Prompt for a location to save the transformed log.
outputPath = await PromptForFileSaveLocationAsync(Resources.SaveTransformedPrereleaseV2Log_DialogTitle, filePath).ConfigureAwait(continueOnCapturedContext: false);
if (string.IsNullOrEmpty(outputPath))
{
return;
}
}
}
else
{
// Since we didn't do any pre-processing, we don't need to write to a temp location.
outputPath = filePath;
saveOutputFile = false;
}
}
else
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// The version property wasn't found within the first 100 characters.
// Per the spec, it should appear first in the sarifLog object.
VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider,
Resources.VersionPropertyNotFound_DialogTitle,
null, // title
OLEMSGICON.OLEMSGICON_QUERY,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return;
}
}
else
{
// They're opening a non-SARIF log, so we need to convert it.
// Ask if they'd like to save the converted log.
MessageDialogCommand response = promptOnLogConversions ?
await PromptToSaveProcessedLogAsync(Resources.ConvertNonSarifLog_DialogMessage).ConfigureAwait(continueOnCapturedContext: false) :
MessageDialogCommand.No;
if (response == MessageDialogCommand.Cancel)
{
return;
}
// The converter doesn't have async methods, so spin
// up a task to do this.
await System.Threading.Tasks.Task.Run(() =>
{
var sb = new StringBuilder();
using (FileStream fileStream = File.OpenRead(filePath))
{
using (var outputTextWriter = new StringWriter(sb))
using (var outputJson = new JsonTextWriter(outputTextWriter))
using (var output = new ResultLogJsonWriter(outputJson))
{
var converter = new ToolFormatConverter();
converter.ConvertToStandardFormat(toolFormat, fileStream, output);
}
logText = sb.ToString();
if (response == MessageDialogCommand.Yes)
{
// Prompt for a location to save the converted log.
outputPath = PromptForFileSaveLocationAsync(Resources.SaveConvertedLog_DialogTitle, filePath).Result;
}
}
}).ConfigureAwait(continueOnCapturedContext: false);
}
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.GetTempFileName() + ".sarif";
}
if (saveOutputFile)
{
await SaveLogFileAsync(outputPath, logText).ConfigureAwait(continueOnCapturedContext: false);
}
if (log == null)
{
log = JsonConvert.DeserializeObject<SarifLog>(logText);
}
await ProcessSarifLogAsync(log, outputPath, cleanErrors: cleanErrors, openInEditor: openInEditor).ConfigureAwait(continueOnCapturedContext: false);
}