Exemple #1
0
        public static int RunConvertFile(ConvertOptions convertOptions)
        {
            try
            {
                LoggingOptions loggingOptions = LoggingOptions.None;

                if (convertOptions.PrettyPrint)
                {
                    loggingOptions |= LoggingOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    loggingOptions |= LoggingOptions.OverwriteExistingOutputFile;
                }
                ;

                if (convertOptions.PersistFileContents)
                {
                    loggingOptions |= LoggingOptions.PersistFileContents;
                }

                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + ".sarif";
                }

                if (convertOptions.ToolFormat.MatchesToolFormat(ToolFormat.PREfast))
                {
                    string sarif = ToolFormatConverter.ConvertPREfastToStandardFormat(convertOptions.InputFilePath);
                    File.WriteAllText(convertOptions.OutputFilePath, sarif);
                }
                else
                {
                    new ToolFormatConverter().ConvertToStandardFormat(
                        convertOptions.ToolFormat,
                        convertOptions.InputFilePath,
                        convertOptions.OutputFilePath,
                        loggingOptions,
                        convertOptions.PluginAssemblyPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(1);
            }

            return(0);
        }
Exemple #2
0
        public static int RunConvertFile(ConvertOptions convertOptions)
        {
            try
            {
                ToolFormatConversionOptions toolFormatConversionOptions = 0;

                if (convertOptions.PrettyPrint)
                {
                    toolFormatConversionOptions |= ToolFormatConversionOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    toolFormatConversionOptions |= ToolFormatConversionOptions.OverwriteExistingOutputFile;
                }
                ;

                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + ".sarif";
                }

                if (convertOptions.ToolFormat == ToolFormat.PREfast)
                {
                    string sarif = ToolFormatConverter.ConvertPREfastToStandardFormat(convertOptions.InputFilePath);
                    File.WriteAllText(convertOptions.OutputFilePath, sarif);
                }
                else
                {
                    new ToolFormatConverter().ConvertToStandardFormat(
                        convertOptions.ToolFormat,
                        convertOptions.InputFilePath,
                        convertOptions.OutputFilePath,
                        toolFormatConversionOptions);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(1);
            }

            return(0);
        }
        public static void ProcessLogFile(string filePath, Solution solution, string toolFormat = ToolFormat.None)
        {
            SarifLog log;

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
            };

            string logText;

            if (toolFormat.MatchesToolFormat(ToolFormat.None))
            {
                logText = File.ReadAllText(filePath);
            }
            else if (toolFormat.MatchesToolFormat(ToolFormat.PREfast))
            {
                logText = ToolFormatConverter.ConvertPREfastToStandardFormat(filePath);
            }
            else
            {
                // We have conversion to do
                var converter = new ToolFormatConverter();
                var sb        = new StringBuilder();

                using (var input = new MemoryStream(File.ReadAllBytes(filePath)))
                {
                    var outputTextWriter = new StringWriter(sb);
                    var outputJson       = new JsonTextWriter(outputTextWriter);
                    var output           = new ResultLogJsonWriter(outputJson);

                    input.Seek(0, SeekOrigin.Begin);
                    converter.ConvertToStandardFormat(toolFormat, input, output);

                    // This is serving as a flush mechanism
                    output.Dispose();

                    logText = sb.ToString();
                }
            }

            log = JsonConvert.DeserializeObject <SarifLog>(logText, settings);
            ProcessSarifLog(log, filePath, solution);
        }
        private void LoadPREfastButton_Click(object sender, RoutedEventArgs e)
        {
            string title  = "Open PREfast XML log file";
            string filter = "PREfast log files (*.xml)|*.xml";

            OpenFileDialog ofd = new OpenFileDialog()
            {
                Title  = title,
                Filter = filter
            };

            if (!ofd.ShowDialog().Value)
            {
                return;
            }

            string sarifText = ToolFormatConverter.ConvertPREfastToStandardFormat(ofd.FileName);

            PopulateControlFromSarifText(sarifText);
        }