public override void Dispose() { base.Dispose(); JsonSerializerSettings v2Settings = new JsonSerializerSettings() { ContractResolver = SarifContractResolver.Instance, Formatting = PrettyPrint ? Formatting.Indented : Formatting.None }; string logText = File.ReadAllText(_outputFilePath); var v2Log = JsonConvert.DeserializeObject <SarifLog>(logText, v2Settings); var transformer = new SarifCurrentToVersionOneVisitor(); transformer.VisitSarifLog(v2Log); JsonSerializerSettings v1Settings = new JsonSerializerSettings() { ContractResolver = SarifContractResolverVersionOne.Instance, Formatting = PrettyPrint ? Formatting.Indented : Formatting.None }; File.WriteAllText(_outputFilePath, JsonConvert.SerializeObject(transformer.SarifLogVersionOne, v1Settings)); }
public int Run(RewriteOptions options) { try { Console.WriteLine($"Rewriting '{options.InputFilePath}' => '{options.OutputFilePath}'..."); Stopwatch w = Stopwatch.StartNew(); bool valid = ValidateOptions(options); if (!valid) { return(FAILURE); } string actualOutputPath = CommandUtilities.GetTransformedOutputFileName(options); SarifLog actualLog = null; string inputVersion = SniffVersion(options.InputFilePath); if (!inputVersion.Equals(SarifUtilities.StableSarifVersion)) { actualLog = TransformFileToVersionTwo(options.InputFilePath, inputVersion); } else { actualLog = ReadSarifFile <SarifLog>(_fileSystem, options.InputFilePath); } OptionallyEmittedData dataToInsert = options.DataToInsert.ToFlags(); OptionallyEmittedData dataToRemove = options.DataToRemove.ToFlags(); IDictionary <string, ArtifactLocation> originalUriBaseIds = options.ConstructUriBaseIdsDictionary(); SarifLog reformattedLog = new RemoveOptionalDataVisitor(dataToRemove).VisitSarifLog(actualLog); reformattedLog = new InsertOptionalDataVisitor(dataToInsert, originalUriBaseIds).VisitSarifLog(reformattedLog); if (options.SarifOutputVersion == SarifVersion.OneZeroZero) { var visitor = new SarifCurrentToVersionOneVisitor(); visitor.VisitSarifLog(reformattedLog); WriteSarifFile(_fileSystem, visitor.SarifLogVersionOne, actualOutputPath, options.Minify, SarifContractResolverVersionOne.Instance); } else { WriteSarifFile(_fileSystem, reformattedLog, actualOutputPath, options.Minify); } w.Stop(); Console.WriteLine($"Rewrite completed in {w.Elapsed}."); } catch (Exception ex) { Console.WriteLine(ex); return(FAILURE); } return(SUCCESS); }
public static int Run(TransformOptions transformOptions) { try { if (transformOptions.Version < 1 || transformOptions.Version > 2) { Console.WriteLine(MultitoolResources.ErrorInvalidTransformTargetVersion); return(1); } OptionallyEmittedData dataToInsert = OptionallyEmittedData.None; Array.ForEach(transformOptions.DataToInsert, data => dataToInsert |= data); // NOTE: we don't actually utilize the dataToInsert command-line data yet... string fileName = CommandUtilities.GetTransformedOutputFileName(transformOptions); var formatting = transformOptions.PrettyPrint ? Formatting.Indented : Formatting.None; // Assume the input log is the "other" version if (transformOptions.Version == 2) { SarifLogVersionOne actualLog = MultitoolFileHelpers.ReadSarifFile <SarifLogVersionOne>(transformOptions.InputFilePath, SarifContractResolverVersionOne.Instance); var visitor = new SarifVersionOneToCurrentVisitor(); visitor.VisitSarifLogVersionOne(actualLog); MultitoolFileHelpers.WriteSarifFile(visitor.SarifLog, fileName, formatting); } else { SarifLog actualLog = MultitoolFileHelpers.ReadSarifFile <SarifLog>(transformOptions.InputFilePath); var visitor = new SarifCurrentToVersionOneVisitor(); visitor.VisitSarifLog(actualLog); MultitoolFileHelpers.WriteSarifFile(visitor.SarifLogVersionOne, fileName, formatting); } } catch (Exception ex) { Console.WriteLine(ex); return(1); } return(0); }
protected override string ConstructTestOutputFromInputResource(string inputResource) { string v2LogText = GetResourceText(inputResource); PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(v2LogText, formatting: Formatting.Indented, out v2LogText); SarifLog v2Log = JsonConvert.DeserializeObject <SarifLog>(v2LogText); var transformer = new SarifCurrentToVersionOneVisitor { EmbedVersionTwoContentInPropertyBag = false }; transformer.VisitSarifLog(v2Log); SarifLogVersionOne v1Log = transformer.SarifLogVersionOne; return(JsonConvert.SerializeObject(v1Log, SarifTransformerUtilities.JsonSettingsV1Indented)); }
public int Run(TransformOptions transformOptions) { try { if (transformOptions.TargetVersion != SarifVersion.OneZeroZero && transformOptions.TargetVersion != SarifVersion.Current) { Console.WriteLine(MultitoolResources.ErrorInvalidTransformTargetVersion); return(1); } OptionallyEmittedData dataToInsert = transformOptions.DataToInsert.ToFlags(); // NOTE: we don't actually utilize the dataToInsert command-line data yet... string fileName = CommandUtilities.GetTransformedOutputFileName(transformOptions); var formatting = transformOptions.PrettyPrint ? Formatting.Indented : Formatting.None; string inputFilePath = transformOptions.InputFilePath; string inputVersion = SniffVersion(inputFilePath); // If the user wants to transform to current v2, we check to see whether the input // file is v2 or pre-release v2. We upgrade both formats to current v2. // // Correspondingly, if the input file is v2 of any kind, we first ensure that it is // current v2, then drop it down to v1. // // We do not support transforming to any obsoleted pre-release v2 formats. if (transformOptions.TargetVersion == SarifVersion.Current) { if (inputVersion == "1.0.0") { SarifLogVersionOne actualLog = FileHelpers.ReadSarifFile <SarifLogVersionOne>(_fileSystem, transformOptions.InputFilePath, SarifContractResolverVersionOne.Instance); var visitor = new SarifVersionOneToCurrentVisitor(); visitor.VisitSarifLogVersionOne(actualLog); FileHelpers.WriteSarifFile(_fileSystem, visitor.SarifLog, fileName, formatting); } else { // We have a pre-release v2 file that we should upgrade to current. PrereleaseCompatibilityTransformer.UpdateToCurrentVersion( _fileSystem.ReadAllText(inputFilePath), formatting: formatting, out string sarifText); _fileSystem.WriteAllText(fileName, sarifText); } } else { if (inputVersion == "1.0.0") { _fileSystem.WriteAllText(fileName, _fileSystem.ReadAllText(inputFilePath)); } else { string currentSarifVersion = SarifUtilities.SemanticVersion; string sarifText = _fileSystem.ReadAllText(inputFilePath); SarifLog actualLog = null; if (inputVersion != currentSarifVersion) { // Note that we don't provide formatting here. It is not required to indent the v2 SARIF - it // will be transformed to v1 later, where we should apply the indentation settings. actualLog = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion( sarifText, formatting: Formatting.None, out sarifText); } else { actualLog = JsonConvert.DeserializeObject <SarifLog>(sarifText); } var visitor = new SarifCurrentToVersionOneVisitor(); visitor.VisitSarifLog(actualLog); FileHelpers.WriteSarifFile(_fileSystem, visitor.SarifLogVersionOne, fileName, formatting, SarifContractResolverVersionOne.Instance); } } } catch (Exception ex) { Console.WriteLine(ex); return(1); } return(0); }
public int Run(TransformOptions transformOptions) { try { // Only set --output-file if --inline isn't specified. ValidateOptions will check // to make sure that exactly one of those two options is set. if (!transformOptions.Inline) { transformOptions.OutputFilePath = CommandUtilities.GetTransformedOutputFileName(transformOptions); } bool valid = ValidateOptions(transformOptions); if (!valid) { return(FAILURE); } // NOTE: we don't actually utilize the dataToInsert command-line data yet... OptionallyEmittedData dataToInsert = transformOptions.DataToInsert.ToFlags(); string inputFilePath = transformOptions.InputFilePath; string inputVersion = SniffVersion(inputFilePath); // If the user wants to transform to current v2, we check to see whether the input // file is v2 or pre-release v2. We upgrade both formats to current v2. // // Correspondingly, if the input file is v2 of any kind, we first ensure that it is // current v2, then drop it down to v1. // // We do not support transforming to any obsoleted pre-release v2 formats. if (transformOptions.SarifOutputVersion == SarifVersion.Current) { if (inputVersion == "1.0.0") { SarifLogVersionOne actualLog = ReadSarifFile <SarifLogVersionOne>(_fileSystem, transformOptions.InputFilePath, SarifContractResolverVersionOne.Instance); var visitor = new SarifVersionOneToCurrentVisitor(); visitor.VisitSarifLogVersionOne(actualLog); WriteSarifFile(_fileSystem, visitor.SarifLog, transformOptions.OutputFilePath, transformOptions.Formatting); } else { // We have a pre-release v2 file that we should upgrade to current. PrereleaseCompatibilityTransformer.UpdateToCurrentVersion( _fileSystem.ReadAllText(inputFilePath), formatting: transformOptions.Formatting, out string sarifText); _fileSystem.WriteAllText(transformOptions.OutputFilePath, sarifText); } } else { if (inputVersion == "1.0.0") { SarifLogVersionOne logV1 = ReadSarifFile <SarifLogVersionOne>(_fileSystem, transformOptions.InputFilePath, SarifContractResolverVersionOne.Instance); logV1.SchemaUri = SarifVersion.OneZeroZero.ConvertToSchemaUri(); WriteSarifFile(_fileSystem, logV1, transformOptions.OutputFilePath, transformOptions.Formatting, SarifContractResolverVersionOne.Instance); } else { string currentSarifVersion = SarifUtilities.StableSarifVersion; string sarifText = _fileSystem.ReadAllText(inputFilePath); SarifLog actualLog = null; if (inputVersion != currentSarifVersion) { // Note that we don't provide formatting here. It is not required to indent the v2 SARIF - it // will be transformed to v1 later, where we should apply the indentation settings. actualLog = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion( sarifText, formatting: Formatting.None, out sarifText); } else { actualLog = JsonConvert.DeserializeObject <SarifLog>(sarifText); } var visitor = new SarifCurrentToVersionOneVisitor(); visitor.VisitSarifLog(actualLog); WriteSarifFile(_fileSystem, visitor.SarifLogVersionOne, transformOptions.OutputFilePath, transformOptions.Formatting, SarifContractResolverVersionOne.Instance); } } } catch (Exception ex) { Console.WriteLine(ex); return(FAILURE); } return(SUCCESS); }