-
Notifications
You must be signed in to change notification settings - Fork 6
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
[Renew] custom rules support sonar 9.9 #23
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,21 +10,20 @@ | |
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<sonar.version>8.6.1.40680</sonar.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.sonarsource.sonarqube</groupId> | ||
<artifactId>sonar-plugin-api</artifactId> | ||
<version>${sonar.version}</version> | ||
<version>7.9</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId> | ||
<artifactId>sonar-cxx-plugin</artifactId> | ||
<type>sonar-plugin</type> | ||
<version>0.9.7-SNAPSHOT</version> | ||
<version>2.1.1-SNAPSHOT</version> | ||
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. Think this must be the version of the plugin. The "-SNAPSHOT" is replaced with the build number. |
||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
|
@@ -60,8 +59,8 @@ | |
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
<source>10</source> | ||
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. This is
with see https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/pom.xml |
||
<target>10</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,73 @@ | ||
package org.sonar.cxx; | ||
|
||
import org.sonar.cxx.checks.UsingNamespaceCheck; | ||
import org.sonar.plugins.cxx.api.CustomCxxRulesDefinition; | ||
import org.sonar.api.config.internal.MapSettings; | ||
import org.sonar.api.resources.Language; | ||
import org.sonar.api.scanner.ScannerSide; | ||
import org.sonar.api.server.rule.RulesDefinition; | ||
import org.sonar.cxx.checks.*; | ||
import org.sonar.cxx.squidbridge.annotations.AnnotationBasedRulesDefinition; | ||
import org.sonar.plugins.cxx.CustomCxxRulesDefinition; | ||
import org.sonar.plugins.cxx.CxxLanguage; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
@ScannerSide | ||
public class MyCustomRulesDefinition extends CustomCxxRulesDefinition { | ||
private static final Language LANGUAGE = new CxxLanguage(new MapSettings().asConfig()); | ||
|
||
@Override | ||
public Language getLanguage() { | ||
return LANGUAGE; | ||
} | ||
|
||
@Override | ||
public String repositoryName() { | ||
return "Repository"; | ||
return "Custom Repository"; | ||
} | ||
|
||
@Override | ||
public String repositoryKey() { | ||
return "repo"; | ||
return "customrepo"; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public Class[] checkClasses() { | ||
return new Class[] { UsingNamespaceCheck.class }; | ||
} | ||
@Override | ||
public void define(RulesDefinition.Context context) { | ||
var repo = context.createRepository(repositoryKey(), getLanguage().getKey()) | ||
.setName(repositoryName()); | ||
|
||
// Load metadata from check classes' annotations | ||
new AnnotationBasedRulesDefinition(repo, getLanguage().getKey()).addRuleClasses(false, | ||
Arrays.asList(checkClasses())); | ||
|
||
// Optionally override html description from annotation with content from html files | ||
repo.rules().forEach(rule -> rule.setHtmlDescription(loadResource("/org/sonar/l10n/cxx/rules/cxx/" + rule.key() + ".html"))); | ||
repo.done(); | ||
} | ||
|
||
private String loadResource(String path) { | ||
URL resource = getClass().getResource(path); | ||
if (resource == null) { | ||
throw new IllegalStateException("Resource not found: " + path); | ||
} | ||
ByteArrayOutputStream result = new ByteArrayOutputStream(); | ||
try (InputStream in = resource.openStream()) { | ||
byte[] buffer = new byte[1024]; | ||
for (int len = in.read(buffer); len != -1; len = in.read(buffer)) { | ||
result.write(buffer, 0, len); | ||
} | ||
return new String(result.toByteArray(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Failed to read resource: " + path, e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
package org.sonar.cxx; | ||
import org.sonar.api.Plugin; | ||
|
||
import java.util.List; | ||
public class MyCustomRulesPlugin implements Plugin { | ||
|
||
import org.sonar.api.SonarPlugin; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class MyCustomRulesPlugin extends SonarPlugin { | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public List getExtensions() { | ||
return ImmutableList.of(MyCustomRulesDefinition.class); | ||
public void define(Context context) { | ||
context.addExtension( | ||
MyCustomRulesDefinition.class | ||
); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<p> | ||
Sharing some naming conventions is a key point to make it possible for a team to efficiently collaborate. | ||
This rule allows to check that all class names match a provided regular expression. | ||
</p> | ||
|
||
<h2>Non-compliant Code Example</h2> | ||
<p>To Do</p> | ||
|
||
<pre> | ||
class myClass { | ||
... | ||
} | ||
</pre> | ||
|
||
<h2>Compliant Solution</h2> | ||
|
||
<pre> | ||
class MyClass { | ||
... | ||
} | ||
</pre> |
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.
Would keep this:
with
<sonar.version>9.9.0.65466</sonar.version>
<sonar.plugin.api.version>9.14.0.375</sonar.plugin.api.version>