-
Notifications
You must be signed in to change notification settings - Fork 91
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
#2227 Move Transform functionality into Rewrite #2252
Changes from 13 commits
47e9796
ac7ea6e
ff37584
5050c7f
9bea969
cc56480
1763489
8067797
e06c072
17af1e5
8b700de
188db02
79ca0bc
d537617
405c18d
74c7c74
263e636
2d348c3
8dc8f9b
4066d45
f6efc3e
a06f8e4
01d26b9
8bf66a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,15 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
using Microsoft.CodeAnalysis.Sarif.Readers; | ||
using Microsoft.CodeAnalysis.Sarif.VersionOne; | ||
using Microsoft.CodeAnalysis.Sarif.Visitors; | ||
using Microsoft.CodeAnalysis.Sarif.Writers; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
|
@@ -27,9 +33,29 @@ public int Run(RewriteOptions options) | |
Stopwatch w = Stopwatch.StartNew(); | ||
|
||
bool valid = ValidateOptions(options); | ||
if (!valid) { return FAILURE; } | ||
|
||
SarifLog actualLog = ReadSarifFile<SarifLog>(_fileSystem, options.InputFilePath); | ||
if (!valid) | ||
{ | ||
return FAILURE; | ||
} | ||
|
||
string actualOutputPath = CommandUtilities.GetTransformedOutputFileName(options); | ||
|
||
SarifLog actualLog = null; | ||
|
||
string inputVersion = SniffVersion(options.InputFilePath); | ||
if (!inputVersion.Equals(SarifUtilities.StableSarifVersion)) | ||
{ | ||
// Transform file and write to output path. Then continue rewriting as before. | ||
// TODO: Consolidate this so only a single write is necessary. | ||
TransformFileToVersionTwo(options, inputVersion, actualOutputPath); | ||
// We wrote to the output path, so read from it again. | ||
actualLog = ReadSarifFile<SarifLog>(_fileSystem, actualOutputPath); | ||
} | ||
else | ||
{ | ||
// Read from the original input path. | ||
actualLog = ReadSarifFile<SarifLog>(_fileSystem, options.InputFilePath); | ||
} | ||
|
||
OptionallyEmittedData dataToInsert = options.DataToInsert.ToFlags(); | ||
OptionallyEmittedData dataToRemove = options.DataToRemove.ToFlags(); | ||
|
@@ -38,9 +64,17 @@ public int Run(RewriteOptions options) | |
SarifLog reformattedLog = new RemoveOptionalDataVisitor(dataToRemove).VisitSarifLog(actualLog); | ||
reformattedLog = new InsertOptionalDataVisitor(dataToInsert, originalUriBaseIds).VisitSarifLog(reformattedLog); | ||
|
||
string fileName = CommandUtilities.GetTransformedOutputFileName(options); | ||
if (options.SarifOutputVersion == SarifVersion.OneZeroZero) | ||
{ | ||
var visitor = new SarifCurrentToVersionOneVisitor(); | ||
visitor.VisitSarifLog(reformattedLog); | ||
|
||
WriteSarifFile(_fileSystem, reformattedLog, fileName, options.Minify); | ||
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}."); | ||
|
@@ -56,13 +90,59 @@ public int Run(RewriteOptions options) | |
|
||
private bool ValidateOptions(RewriteOptions rewriteOptions) | ||
{ | ||
bool valid = true; | ||
if (!rewriteOptions.Validate()) | ||
{ | ||
return false; | ||
} | ||
|
||
valid &= rewriteOptions.Validate(); | ||
// While this is returning true for inline cases, I think it's doing so for the wrong reasons. | ||
// TODO: validate whether "actualOutputPath" can be created. | ||
if (!DriverUtilities.ReportWhetherOutputFileCanBeCreated(rewriteOptions.OutputFilePath, rewriteOptions.Force, _fileSystem)) | ||
{ | ||
return false; | ||
} | ||
|
||
valid &= DriverUtilities.ReportWhetherOutputFileCanBeCreated(rewriteOptions.OutputFilePath, rewriteOptions.Force, _fileSystem); | ||
return true; | ||
} | ||
|
||
return valid; | ||
// TODO Move this into a separate class for better unit testing | ||
private string SniffVersion(string sarifPath) | ||
{ | ||
using (JsonTextReader reader = new JsonTextReader(new StreamReader(_fileSystem.FileOpenRead(sarifPath)))) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonToken.PropertyName && ((string)reader.Value).Equals("version")) | ||
{ | ||
reader.Read(); | ||
return (string)reader.Value; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void TransformFileToVersionTwo(SingleFileOptionsBase options, string inputVersion, string outputFilePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can you return SarifLog instead of void? remove the else and return JsonConvert.... to SarifLog. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would change from options to the sarif string to facilitate, since we won't need to use the options In reply to: 564740510 [](ancestors = 564740510) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
if (inputVersion == "1.0.0") | ||
{ | ||
// Converting version 1 to version 2 | ||
SarifLogVersionOne actualLog = ReadSarifFile<SarifLogVersionOne>(_fileSystem, options.InputFilePath, SarifContractResolverVersionOne.Instance); | ||
var visitor = new SarifVersionOneToCurrentVisitor(); | ||
visitor.VisitSarifLogVersionOne(actualLog); | ||
WriteSarifFile(_fileSystem, visitor.SarifLog, outputFilePath, options.Minify); | ||
} | ||
else | ||
{ | ||
// Converting prerelease version 2 to version 2 | ||
PrereleaseCompatibilityTransformer.UpdateToCurrentVersion( | ||
_fileSystem.FileReadAllText(options.InputFilePath), | ||
formatting: options.Formatting, | ||
out string sarifText); | ||
|
||
_fileSystem.FileWriteAllText(outputFilePath, sarifText); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,13 +46,12 @@ public static int Main(string[] args) | |
(ExportValidationConfigurationOptions options) => new ExportValidationConfigurationCommand().Run(options), | ||
(ExportValidationRulesMetadataOptions options) => new ExportValidationRulesMetadataCommand().Run(options), | ||
(FileWorkItemsOptions fileWorkItemsOptions) => new FileWorkItemsCommand().Run(fileWorkItemsOptions), | ||
(ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions), | ||
(MergeOptions mergeOptions) => new MergeCommand().Run(mergeOptions), | ||
(PageOptions pageOptions) => new PageCommand().Run(pageOptions), | ||
(QueryOptions queryOptions) => new QueryCommand().Run(queryOptions), | ||
(RebaseUriOptions rebaseOptions) => new RebaseUriCommand().Run(rebaseOptions), | ||
(ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I didn't set in the alphabetical order because the resultmatching is equal match-results-foward command. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worthwhile to rename the file/class to something like "MatchResultOptions" for consistency? In reply to: 561433654 [](ancestors = 561433654) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's do it in another pr, separating what we are doing right now :) In reply to: 561480791 [](ancestors = 561480791,561433654) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
(RewriteOptions rewriteOptions) => new RewriteCommand().Run(rewriteOptions), | ||
(TransformOptions transformOptions) => new TransformCommand().Run(transformOptions), | ||
(ValidateOptions validateOptions) => new ValidateCommand().Run(validateOptions), | ||
_ => HandleParseError(args)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ namespace Microsoft.CodeAnalysis.Sarif.Writers | |
{ | ||
public class ConsoleLogger : BaseLogger, IAnalysisLogger | ||
{ | ||
// TODO: We directly instantiate this logger in two classes, creating | ||
// unamanged dependencies. Fix this pattern with dependency injection or a factory. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we have to worry in this. Since the lifetime of the use is a command, the unmanaged resource will go away when it finishes. #ByDesign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
public ConsoleLogger(bool quietConsole, string toolName, IEnumerable<FailureLevel> levels = null, IEnumerable<ResultKind> kinds = null) : base(levels, kinds) | ||
{ | ||
_quietConsole = quietConsole; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't remove this. For now, this is the way we initialize the FileSytem. If you check, that is used in MultifileCommandBase, calling the FileSystem directly. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
In reply to: 564738564 [](ancestors = 564738564)