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

Support specifying rule type and adding tags #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion PluginGenerator/DataModel/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public XmlCDataSection DescriptionAsCDATA

[XmlElement(ElementName = "status")]
public string Status { get; set; }


[XmlElement(ElementName = "type")]
public string Type { get; set; }

[XmlElement(ElementName = "tag")]
public string[] Tags { get; set; }

Expand Down
24 changes: 19 additions & 5 deletions RoslynPluginGenerator/AnalyzerPluginGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public bool Generate(ProcessedArgs args)
// Initial run with the user-targeted package and arguments
if (analyzersByPackage.ContainsKey(targetPackage))
{
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, args.SqaleFilePath, targetPackage, analyzersByPackage[targetPackage]);
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, args.SqaleFilePath, targetPackage, analyzersByPackage[targetPackage], args.TagsToAdd, args.RuleType);
if (generatedJarPath == null)
{
return false;
Expand All @@ -159,7 +159,7 @@ public bool Generate(ProcessedArgs args)
foreach (IPackage currentPackage in analyzersByPackage.Keys)
{
// No way to specify the SQALE file for any but the user-targeted package at this time
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, null, currentPackage, analyzersByPackage[currentPackage]);
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, null, currentPackage, analyzersByPackage[currentPackage], args.TagsToAdd, args.RuleType);
if (generatedJarPath == null)
{
return false;
Expand All @@ -179,7 +179,7 @@ public bool Generate(ProcessedArgs args)
return true;
}

private string GeneratePluginForPackage(string outputDir, string language, string sqaleFilePath, IPackage package, IEnumerable<DiagnosticAnalyzer> analyzers)
private string GeneratePluginForPackage(string outputDir, string language, string sqaleFilePath, IPackage package, IEnumerable<DiagnosticAnalyzer> analyzers, string[] tagsToAdd, string ruleType)
{
Debug.Assert(analyzers.Any(), "The method must be called with a populated list of DiagnosticAnalyzers.");

Expand All @@ -202,7 +202,7 @@ private string GeneratePluginForPackage(string outputDir, string language, strin
definition.SourceZipFilePath = this.CreateAnalyzerStaticPayloadFile(packageDir, baseDirectory);
definition.StaticResourceName = Path.GetFileName(definition.SourceZipFilePath);

definition.RulesFilePath = GenerateRulesFile(analyzers, baseDirectory);
definition.RulesFilePath = GenerateRulesFile(analyzers, baseDirectory, tagsToAdd, ruleType);

string generatedSqaleFile = null;
bool generate = true;
Expand Down Expand Up @@ -328,7 +328,7 @@ private static bool IncludeFileInZip(string filePath)
/// Generate a rules file for the specified analyzers
/// </summary>
/// <returns>The full path to the generated file</returns>
private string GenerateRulesFile(IEnumerable<DiagnosticAnalyzer> analyzers, string baseDirectory)
private string GenerateRulesFile(IEnumerable<DiagnosticAnalyzer> analyzers, string baseDirectory, string[] tagsToAdd, string ruleType)
{
this.logger.LogInfo(UIResources.APG_GeneratingRules);

Expand All @@ -341,6 +341,20 @@ private string GenerateRulesFile(IEnumerable<DiagnosticAnalyzer> analyzers, stri

if (rules != null)
{
foreach (Rule rule in rules)
{
rule.Type = ruleType;

if (rule.Tags == null)
{
rule.Tags = tagsToAdd;
}
else
{
rule.Tags = rule.Tags.Concat(tagsToAdd).ToArray();
}
}

rules.Save(rulesFilePath, logger);
this.logger.LogDebug(UIResources.APG_RulesGeneratedToFile, rules.Count, rulesFilePath);
}
Expand Down
24 changes: 23 additions & 1 deletion RoslynPluginGenerator/CommandLine/ArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private static class KeywordIds
public const string SqaleXmlFile = "sqale.xml";
public const string AcceptLicenses = "accept.licenses";
public const string RecurseDependencies = "recurse.dependencies";
public const string AddTag = "add.tag";
public const string RuleType = "rule.type";
}

private static IList<ArgumentDescriptor> Descriptors;
Expand All @@ -59,6 +61,10 @@ static ArgumentProcessor()
id: KeywordIds.AcceptLicenses, prefixes: new string[] { "/acceptLicenses" }, required: false, allowMultiple: false, description: CmdLineResources.ArgDescription_AcceptLicenses, isVerb: true));
Descriptors.Add(new ArgumentDescriptor(
id: KeywordIds.RecurseDependencies, prefixes: new string[] { "/recurse" }, required: false, allowMultiple: false, description: CmdLineResources.ArgDescription_RecurseDependencies, isVerb: true));
Descriptors.Add(new ArgumentDescriptor(
id: KeywordIds.AddTag, prefixes: new string[] { "/addTag:" }, required: false, allowMultiple: true, description: CmdLineResources.ArgDescription_AddTag));
Descriptors.Add(new ArgumentDescriptor(
id: KeywordIds.RuleType, prefixes: new string[] { "/ruleType:" }, required: false, allowMultiple: false, description: CmdLineResources.ArgDescription_RuleType));

Debug.Assert(Descriptors.All(d => d.Prefixes != null && d.Prefixes.Any()), "All descriptors must provide at least one prefix");
Debug.Assert(Descriptors.Select(d => d.Id).Distinct().Count() == Descriptors.Count, "All descriptors must have a unique id");
Expand Down Expand Up @@ -121,6 +127,8 @@ public ProcessedArgs Process(string[] commandLineArgs)

bool acceptLicense = GetLicenseAcceptance(arguments);
bool recurseDependencies = GetRecursion(arguments);
string[] tagsToAdd = GetTagsToAdd(arguments);
string ruleType = GetRuleType(arguments);

if (parsedOk)
{
Expand All @@ -132,7 +140,9 @@ public ProcessedArgs Process(string[] commandLineArgs)
sqaleFilePath,
acceptLicense,
recurseDependencies,
System.IO.Directory.GetCurrentDirectory());
System.IO.Directory.GetCurrentDirectory(),
tagsToAdd,
ruleType);
}

return processed;
Expand Down Expand Up @@ -220,5 +230,17 @@ private static bool GetRecursion(IEnumerable<ArgumentInstance> arguments)
return arg != null;
}

private static string[] GetTagsToAdd(IEnumerable<ArgumentInstance> arguments)
{
ArgumentInstance[] args = arguments.Where(a => ArgumentDescriptor.IdComparer.Equals(KeywordIds.AddTag, a.Descriptor.Id)).ToArray();
string[] tags = args.Select(x => x.Value).ToArray();
return tags;
}

private static string GetRuleType(IEnumerable<ArgumentInstance> arguments)
{
ArgumentInstance arg = arguments.SingleOrDefault(a => ArgumentDescriptor.IdComparer.Equals(KeywordIds.RuleType, a.Descriptor.Id));
return arg == null ? null : arg.Value;
}
}
}
20 changes: 19 additions & 1 deletion RoslynPluginGenerator/CommandLine/CmdLineResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions RoslynPluginGenerator/CommandLine/CmdLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,18 @@
<data name="ArgDescription_AcceptLicenses" xml:space="preserve">
<value>/acceptLicenses - indicates that you accept the licenses for any packages that required license acceptance</value>
</data>
<data name="ArgDescription_AddTag" xml:space="preserve">
<value>/addTag:[tag] - add this tag to all rules </value>
</data>
<data name="ArgDescription_AnalzyerRef" xml:space="preserve">
<value>/a:[NuGet package id] or /a:[NuGet package id]:[version]</value>
</data>
<data name="ArgDescription_RecurseDependencies" xml:space="preserve">
<value>/recurse - search for analyzers in target package and any dependencies</value>
</data>
<data name="ArgDescription_RuleType" xml:space="preserve">
<value>/ruleType:["CODE_SMELL" (default) or "BUG" or "VULNERABILITY"] - set all rule's type to Code smell or Bug or Vulnerabilty</value>
</data>
<data name="ArgDescription_SqaleXmlFile" xml:space="preserve">
<value>/s:[path to sqale xml file]</value>
</data>
Expand Down
10 changes: 9 additions & 1 deletion RoslynPluginGenerator/CommandLine/ProcessedArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public class ProcessedArgs
private readonly bool acceptLicenses;
private readonly bool recurseDependencies;
private readonly string outputDirectory;
private readonly string[] tagsToAdd;
private readonly string ruleType;

public ProcessedArgs(string packageId, SemanticVersion packageVersion, string language, string sqaleFilePath, bool acceptLicenses, bool recurseDependencies, string outputDirectory)
public ProcessedArgs(string packageId, SemanticVersion packageVersion, string language, string sqaleFilePath, bool acceptLicenses, bool recurseDependencies, string outputDirectory, string[] tagsToAdd, string ruleType)
{
if (string.IsNullOrWhiteSpace(packageId))
{
Expand All @@ -53,6 +55,8 @@ public ProcessedArgs(string packageId, SemanticVersion packageVersion, string la
this.acceptLicenses = acceptLicenses;
this.recurseDependencies = recurseDependencies;
this.outputDirectory = outputDirectory;
this.tagsToAdd = tagsToAdd;
this.ruleType = ruleType;
}

public string PackageId { get { return this.packageId; } }
Expand All @@ -68,5 +72,9 @@ public ProcessedArgs(string packageId, SemanticVersion packageVersion, string la
public bool RecurseDependencies { get { return this.recurseDependencies; } }

public string OutputDirectory { get { return this.outputDirectory; } }

public string[] TagsToAdd { get { return this.tagsToAdd; } }

public string RuleType { get { return this.ruleType; } }
}
}
6 changes: 3 additions & 3 deletions Tests/IntegrationTests/Roslyn/RoslynGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void RoslynPlugin_GenerateForValidAnalyzer_Succeeds()
// Act
NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(fakeRemotePkgMgr.LocalRepository, localPackageDestination, logger);
AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);
ProcessedArgs args = new ProcessedArgs(packageId, new SemanticVersion("1.0.2"), "cs", null, false, false, outputDir);
ProcessedArgs args = new ProcessedArgs(packageId, new SemanticVersion("1.0.2"), "cs", null, false, false, outputDir, new string[0], null);
bool result = apg.Generate(args);

// Assert
Expand Down Expand Up @@ -92,7 +92,7 @@ public void RoslynPlugin_GenerateForDependencyAnalyzers_Succeeds()
NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(fakeRemotePkgMgr.LocalRepository, localPackageDestination, logger);
AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);
ProcessedArgs args = new ProcessedArgs(targetPkg.Id, targetPkg.Version, "cs", null, false,
true /* generate plugins for dependencies with analyzers*/, outputDir);
true /* generate plugins for dependencies with analyzers*/, outputDir, new string[0], null);
bool result = apg.Generate(args);

// Assert
Expand Down Expand Up @@ -127,7 +127,7 @@ public void RoslynPlugin_GenerateForMultiLevelAnalyzers_Succeeds()
NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(fakeRemotePkgMgr.LocalRepository, localPackageDestination, logger);
AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);
ProcessedArgs args = new ProcessedArgs(targetPkg.Id, targetPkg.Version, "cs", null, false,
true /* generate plugins for dependencies with analyzers*/, outputDir);
true /* generate plugins for dependencies with analyzers*/, outputDir, new string[0], null);
bool result = apg.Generate(args);

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,9 @@ private static ProcessedArgs CreateArgs(string packageId, string packageVersion,
sqaleFilePath,
acceptLicenses,
recurseDependencies,
outputDirectory);
outputDirectory,
new string[0],
null);
return args;
}

Expand Down