Skip to content

Commit

Permalink
Fixed #87: dropped support for SQALE (#91)
Browse files Browse the repository at this point in the history
* Fixed #87: dropped support for SQALE

* Updated template jar. Jar built using SonarSource/sonarqube-roslyn-sdk-template-plugin@82c5a94
  • Loading branch information
duncanp-sonar authored Aug 6, 2018
1 parent d34380f commit 31518d3
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 592 deletions.
96 changes: 6 additions & 90 deletions RoslynPluginGenerator/AnalyzerPluginGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using NuGet;
using SonarLint.XmlDescriptor;
using SonarQube.Plugins.Common;
using SonarQube.Plugins.Roslyn.CommandLine;

Expand All @@ -50,13 +49,6 @@ public class AnalyzerPluginGenerator
/// </summary>
public const string RulesTemplateFileNameFormat = "{0}.{1}.rules.template.xml";

/// <summary>
/// Specifies the format for the name of the placeholder SQALE file
/// </summary>
public const string SqaleTemplateFileNameFormat = "{0}.{1}.sqale.template.xml";

private const string DefaultRemediationCost = "5min";

private readonly INuGetPackageHandler packageHandler;
private readonly SonarQube.Plugins.Common.ILogger logger;

Expand Down Expand Up @@ -139,7 +131,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, args.RuleFilePath, targetPackage, analyzersByPackage[targetPackage]);
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, args.RuleFilePath, targetPackage, analyzersByPackage[targetPackage]);
if (generatedJarPath == null)
{
return false;
Expand All @@ -152,12 +144,12 @@ public bool Generate(ProcessedArgs args)
// Dependent package generation changes the arguments
if (args.RecurseDependencies)
{
logger.LogWarning(UIResources.APG_RecurseEnabled_SQALEandRuleCustomisationNotEnabled);
logger.LogWarning(UIResources.APG_RecurseEnabled_RuleCustomizationNotEnabled);

foreach (IPackage currentPackage in analyzersByPackage.Keys)
{
// No way to specify the SQALE or rules xml files for any but the user-targeted package at this time
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, null, null, currentPackage, analyzersByPackage[currentPackage]);
// No way to specify the rules xml files for any but the user-targeted package at this time
string generatedJarPath = GeneratePluginForPackage(args.OutputDirectory, args.Language, null, currentPackage, analyzersByPackage[currentPackage]);
if (generatedJarPath == null)
{
return false;
Expand All @@ -177,7 +169,7 @@ public bool Generate(ProcessedArgs args)
return true;
}

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

Expand All @@ -191,7 +183,6 @@ private string GeneratePluginForPackage(string outputDir, string language, strin
RoslynPluginDefinition definition = new RoslynPluginDefinition
{
Language = language,
SqaleFilePath = sqaleFilePath,
RulesFilePath = rulesFilePath,
PackageId = package.Id,
PackageVersion = package.Version.ToString(),
Expand All @@ -218,27 +209,12 @@ private string GeneratePluginForPackage(string outputDir, string language, strin
generate = IsValidRulesFile(definition.RulesFilePath);
}

string generatedSqaleFile = null;

if (definition.SqaleFilePath == null)
{
generatedSqaleFile = CalculateSqaleFileName(package, outputDir);
GenerateFixedSqaleFile(analyzers, generatedSqaleFile);
Debug.Assert(File.Exists(generatedSqaleFile));
}
else
{
this.logger.LogInfo(UIResources.APG_UsingSuppliedSqaleFile, definition.SqaleFilePath);
generate = IsValidSqaleFile(definition.SqaleFilePath);
}

if (generate)
{
createdJarFilePath = BuildPlugin(definition, outputDir);
}

LogMessageForGeneratedRules(generatedRulesTemplateFile);
LogMessageForGeneratedSqale(generatedSqaleFile);

return createdJarFilePath;
}
Expand All @@ -252,15 +228,6 @@ private void LogMessageForGeneratedRules(string generatedFile)
}
}

private void LogMessageForGeneratedSqale(string generatedSqaleFile)
{
if (generatedSqaleFile != null)
{
// Log a message about the generated SQALE file for every plugin generated
logger.LogInfo(UIResources.APG_TemplateSqaleFileGenerated, generatedSqaleFile);
}
}

private void LogAcceptedPackageLicenses(IEnumerable<IPackage> licenseAcceptancePackages)
{
if (licenseAcceptancePackages.Any())
Expand Down Expand Up @@ -386,30 +353,6 @@ private static string CalculateRulesTemplateFileName(IPackage package, string di
return filePath;
}

private static string CalculateSqaleFileName(IPackage package, string directory)
{
string filePath = string.Format(System.Globalization.CultureInfo.CurrentCulture,
SqaleTemplateFileNameFormat, package.Id, package.Version);

filePath = Path.Combine(directory, filePath);
return filePath;
}

/// <summary>
/// Generates a SQALE file with fixed remediation costs for the specified analyzers
/// </summary>
private void GenerateFixedSqaleFile(IEnumerable<DiagnosticAnalyzer> analyzers, string outputFilePath)
{
logger.LogInfo(UIResources.APG_GeneratingConstantSqaleFile);

HardcodedConstantSqaleGenerator generator = new HardcodedConstantSqaleGenerator();

SqaleModel sqale = generator.GenerateSqaleData(analyzers, DefaultRemediationCost);

Serializer.SaveModel(sqale, outputFilePath);
logger.LogDebug(UIResources.APG_SqaleGeneratedToFile, outputFilePath);
}

/// <summary>
/// Checks that the supplied rule file has valid content
/// </summary>
Expand All @@ -432,29 +375,7 @@ private bool IsValidRulesFile(string filePath)
}
return true;
}

/// <summary>
/// Checks that the supplied sqale file has valid content
/// </summary>
private bool IsValidSqaleFile(string sqaleFilePath)
{
Debug.Assert(!string.IsNullOrWhiteSpace(sqaleFilePath));
// Existence is checked when parsing the arguments
Debug.Assert(File.Exists(sqaleFilePath), "Expecting the sqale file to exist: " + sqaleFilePath);

try
{
// TODO: consider adding further checks
Serializer.LoadModel<SqaleModel>(sqaleFilePath);
}
catch(InvalidOperationException) // will be thrown for invalid xml
{
logger.LogError(UIResources.APG_InvalidSqaleFile, sqaleFilePath);
return false;
}
return true;
}


public /* for test */ static PluginManifest CreatePluginManifest(IPackage package)
{
// The manifest properties supported by SonarQube are documented at
Expand Down Expand Up @@ -562,11 +483,6 @@ private string BuildPlugin(RoslynPluginDefinition definition, string outputDirec
.SetPluginManifestProperties(definition.Manifest)
.SetJarFilePath(fullJarPath);

if (!string.IsNullOrWhiteSpace(definition.SqaleFilePath))
{
builder.SetSqaleFilePath(definition.SqaleFilePath);
}

AddRoslynMetadata(builder, definition, repositoryId);

string relativeStaticFilePath = "static/" + Path.GetFileName(definition.StaticResourceName);
Expand Down
20 changes: 4 additions & 16 deletions RoslynPluginGenerator/CommandLine/ArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ProcessedArgs Process(string[] commandLineArgs)

parsedOk &= TryParseAnalyzerRef(arguments, out NuGetReference analyzerRef);

parsedOk &= TryParseSqaleFile(arguments, out string sqaleFilePath);
parsedOk &= TryParseSqaleArgument(arguments);

parsedOk &= TryParseRuleFile(arguments, out string ruleFilePath);

Expand All @@ -128,7 +128,6 @@ public ProcessedArgs Process(string[] commandLineArgs)
analyzerRef.PackageId,
analyzerRef.Version,
SupportedLanguages.CSharp, /* TODO: support multiple languages */
sqaleFilePath,
ruleFilePath,
acceptLicense,
recurseDependencies,
Expand Down Expand Up @@ -186,28 +185,17 @@ private NuGetReference TryParseNuGetReference(string argumentValue)
return new NuGetReference(packageId, packageVersion);
}

private bool TryParseSqaleFile(IEnumerable<ArgumentInstance> arguments, out string sqaleFilePath)
private bool TryParseSqaleArgument(IEnumerable<ArgumentInstance> arguments)
{
sqaleFilePath = null;
ArgumentInstance arg = arguments.SingleOrDefault(a => ArgumentDescriptor.IdComparer.Equals(KeywordIds.SqaleXmlFile, a.Descriptor.Id));

if (arg == null)
{
return true;
}

bool success = true;
if (File.Exists(arg.Value))
{
sqaleFilePath = arg.Value;
logger.LogDebug(CmdLineResources.DEBUG_UsingSqaleFile, sqaleFilePath);
}
else
{
success = false;
logger.LogError(CmdLineResources.ERROR_SqaleFileNotFound, arg.Value);
}
return success;
logger.LogError(CmdLineResources.ERROR_SqaleParameterIsNotSupported);
return false;
}

private bool TryParseRuleFile(IEnumerable<ArgumentInstance> arguments, out string ruleFilePath)
Expand Down
19 changes: 6 additions & 13 deletions RoslynPluginGenerator/CommandLine/CmdLineResources.Designer.cs

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

11 changes: 5 additions & 6 deletions RoslynPluginGenerator/CommandLine/CmdLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,14 @@
<value>/rules:[path to rules xml file]</value>
</data>
<data name="ArgDescription_SqaleXmlFile" xml:space="preserve">
<value>/sqale:[path to sqale xml file]</value>
<value>/sqale:[path to sqale xml file] - DEPRECATED. Use the /rules parameter instead.</value>
</data>
<data name="DEBUG_ParsedReference" xml:space="preserve">
<value>Parsed NuGet reference. Id: {0}, version: {1}</value>
</data>
<data name="DEBUG_UsingRuleFile" xml:space="preserve">
<value>Using rules xml file '{0}'</value>
</data>
<data name="DEBUG_UsingSqaleFile" xml:space="preserve">
<value>Using SQALE file '{0}'</value>
</data>
<data name="ERROR_InvalidVersion" xml:space="preserve">
<value>Invalid NuGet package version: {0}</value>
</data>
Expand All @@ -150,7 +147,9 @@
<data name="ERROR_RuleFileNotFound" xml:space="preserve">
<value>The specified rules xml file could not found: {0}</value>
</data>
<data name="ERROR_SqaleFileNotFound" xml:space="preserve">
<value>The specified SQALE file could not found: {0}</value>
<data name="ERROR_SqaleParameterIsNotSupported" xml:space="preserve">
<value>The /sqale parameter is no longer supported.
If you are targetting SonarQube v6.7 or higher, use the /rules parameter and add the debt remediation information to the rules.xml file instead. If you are targetting an older version of SonarQube, use v1.0 of this SDK.
</value>
</data>
</root>
5 changes: 1 addition & 4 deletions RoslynPluginGenerator/CommandLine/ProcessedArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SonarQube.Plugins.Roslyn.CommandLine
{
public class ProcessedArgs
{
public ProcessedArgs(string packageId, SemanticVersion packageVersion, string language, string sqaleFilePath, string ruleFilePath,
public ProcessedArgs(string packageId, SemanticVersion packageVersion, string language, string ruleFilePath,
bool acceptLicenses, bool recurseDependencies, string outputDirectory)
{
if (string.IsNullOrWhiteSpace(packageId))
Expand All @@ -41,7 +41,6 @@ public ProcessedArgs(string packageId, SemanticVersion packageVersion, string la

PackageId = packageId;
PackageVersion = packageVersion;
SqaleFilePath = sqaleFilePath; // can be null
RuleFilePath = ruleFilePath;
Language = language;
AcceptLicenses = acceptLicenses;
Expand All @@ -53,8 +52,6 @@ public ProcessedArgs(string packageId, SemanticVersion packageVersion, string la

public SemanticVersion PackageVersion { get; }

public string SqaleFilePath { get; }

public string RuleFilePath { get; }

public string Language { get; }
Expand Down
2 changes: 0 additions & 2 deletions RoslynPluginGenerator/DataModel/RoslynSdkConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public RoslynSdkConfiguration()

public string RulesXmlResourcePath { get; set; }

public string SqaleXmlResourcePath { get; set; }

[XmlElement("PluginProperties")]
public PluginProperties Properties { get; set; }

Expand Down
Loading

0 comments on commit 31518d3

Please sign in to comment.