Skip to content

Commit

Permalink
Refactor rules/profiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthepsy committed Oct 3, 2021
1 parent e0a866a commit e620b46
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class CheckList {

public static final String REPOSITORY_KEY = "erlang";

public static final String REPOSITORY_NAME = "Sonar";
public static final String REPOSITORY_NAME = "SonarQube";

private CheckList() {
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

<properties>
<_rootdir>${project.basedir}</_rootdir>
<sonar.version>8.9.0.43852</sonar.version>
<sonar.version>9.0.0.45539</sonar.version>
<sslr.version>1.21</sslr.version>
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
<sonar.projectKey>evolution-gaming_sonar-erlang</sonar.projectKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import org.sonar.api.Plugin;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.plugins.erlang.checks.ErlangChecksRuleDefinition;
import org.sonar.plugins.erlang.rules.ErlangChecksRuleDefinition;
import org.sonar.plugins.erlang.cover.CoverCoverageSensor;
import org.sonar.plugins.erlang.dialyzer.DialyzerRuleDefinition;
import org.sonar.plugins.erlang.dialyzer.DialyzerSensor;
import org.sonar.plugins.erlang.eunit.EunitXmlSensor;
import org.sonar.plugins.erlang.languages.ErlangLanguage;
import org.sonar.plugins.erlang.languages.ErlangQualityProfile;
import org.sonar.plugins.erlang.settings.ErlangLanguageProperties;
import org.sonar.plugins.erlang.xref.XrefRuleDefinition;
import org.sonar.plugins.erlang.xref.XrefSensor;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void define(Context context) {
ErlangChecksRuleDefinition.class,
DialyzerRuleDefinition.class,
XrefRuleDefinition.class,
ErlangProfile.class,
ErlangQualityProfile.class,

EunitXmlSensor.class,

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;
import org.sonar.plugins.erlang.languages.ErlangLanguage;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DialyzerRuleDefinition implements RulesDefinition {
Expand All @@ -32,16 +33,14 @@ public class DialyzerRuleDefinition implements RulesDefinition {
public static final String REPOSITORY_KEY = "dialyzer";
public static final String DIALYZER_PATH = "/org/sonar/plugins/erlang/dialyzer/rules.xml";

private final RulesDefinitionXmlLoader xmlLoader;

public DialyzerRuleDefinition(RulesDefinitionXmlLoader xmlLoader) {
this.xmlLoader = xmlLoader;
}

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, ErlangLanguage.KEY).setName(REPOSITORY_NAME);
xmlLoader.load(repository, getClass().getResourceAsStream(DIALYZER_PATH), StandardCharsets.UTF_8);
InputStream rulesXml = this.getClass().getResourceAsStream(DIALYZER_PATH);
if (rulesXml != null) {
RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();
rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8);
}
repository.done();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* SonarQube Erlang Plugin
* Copyright © 2012-2018 Tamas Kende <kende.tamas@gmail.com>
* Copyright © 2018 Denes Hegedus (Cursor Insight Ltd.) <hegedenes@cursorinsight.com>
* Copyright © 2020 Andris Raugulis <moo@arthepsy.eu>
* Copyright © 2021 Daniils Petrovs <dpetrovs@evolution.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.erlang.languages;

import org.apache.commons.lang.StringUtils;
import org.sonar.api.rules.RuleAnnotationUtils;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.BelongsToProfile;
import org.sonar.erlang.checks.CheckList;
import org.sonar.plugins.erlang.dialyzer.DialyzerRuleDefinition;
import org.sonar.plugins.erlang.xref.XrefRuleDefinition;

public class ErlangQualityProfile implements BuiltInQualityProfilesDefinition {

public static final String PROFILE_NAME = "Sonar way";

@Override
public void define(Context context) {
NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(PROFILE_NAME, ErlangLanguage.KEY);
profile.setDefault(true);

// add Erlang Checks rules
for (Class<?> clazz :CheckList.getChecks()) {
BelongsToProfile belongsToProfile = clazz.getAnnotation(BelongsToProfile.class);
if ((belongsToProfile != null) && StringUtils.equals(belongsToProfile.title(), CheckList.REPOSITORY_NAME)) {
String ruleKey = RuleAnnotationUtils.getRuleKey(clazz);
profile.activateRule(CheckList.REPOSITORY_KEY, ruleKey);
}
}

RulesDefinition.Context rulesContext;
RulesDefinition.Repository rulesRepository;

// add Dialyzer rules
rulesContext = new RulesDefinition.Context();
new DialyzerRuleDefinition().define(rulesContext);
rulesRepository = rulesContext.repository(DialyzerRuleDefinition.REPOSITORY_KEY);
if (rulesRepository != null && rulesRepository.rules() != null) {
for (RulesDefinition.Rule rule : rulesRepository.rules()) {
profile.activateRule(rulesRepository.name(), rule.key());
}
}

// add Xref rules
rulesContext = new RulesDefinition.Context();
new XrefRuleDefinition().define(rulesContext);
rulesRepository = rulesContext.repository(XrefRuleDefinition.REPOSITORY_KEY);
if (rulesRepository != null && rulesRepository.rules() != null) {
for (RulesDefinition.Rule rule : rulesRepository.rules()) {
profile.activateRule(rulesRepository.name(), rule.key());
}
}

profile.done();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.erlang.checks;
package org.sonar.plugins.erlang.rules;

import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.erlang.checks.CheckList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ParametersAreNonnullByDefault
package org.sonar.plugins.erlang.checks;
package org.sonar.plugins.erlang.rules;

import javax.annotation.ParametersAreNonnullByDefault;

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;
import org.sonar.plugins.erlang.languages.ErlangLanguage;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class XrefRuleDefinition implements RulesDefinition {
Expand All @@ -32,16 +33,14 @@ public class XrefRuleDefinition implements RulesDefinition {
public static final String REPOSITORY_KEY = "xref";
public static final String XREF_PATH = "/org/sonar/plugins/erlang/xref/rules.xml";

private final RulesDefinitionXmlLoader xmlLoader;

public XrefRuleDefinition(RulesDefinitionXmlLoader xmlLoader) {
this.xmlLoader = xmlLoader;
}

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, ErlangLanguage.KEY).setName(REPOSITORY_NAME);
xmlLoader.load(repository, getClass().getResourceAsStream(XREF_PATH), StandardCharsets.UTF_8);
InputStream rulesXml = this.getClass().getResourceAsStream(XREF_PATH);
if (rulesXml != null) {
RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();
rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8);
}
repository.done();
}

Expand Down
Loading

0 comments on commit e620b46

Please sign in to comment.