org.sonar.plugins.python.PythonRuleRepository Maven / Gradle / Ivy
The newest version!
/*
* SonarQube Python Plugin
* Copyright (C) 2011-2024 SonarSource SA
* mailto:info AT sonarsource DOT 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.python;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.python.checks.CheckList;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;
public class PythonRuleRepository implements RulesDefinition {
private static final String REPOSITORY_NAME = "SonarAnalyzer";
static final String RESOURCE_FOLDER = "org/sonar/l10n/py/rules/python";
private static final Set TEMPLATE_RULE_KEYS = Collections.singleton("CommentRegularExpression");
private final SonarRuntime runtime;
public PythonRuleRepository(SonarRuntime runtime) {
this.runtime = runtime;
}
@Override
public void define(Context context) {
NewRepository repository = context
.createRepository(CheckList.REPOSITORY_KEY, Python.KEY)
.setName(REPOSITORY_NAME);
RuleMetadataLoader loader = new RuleMetadataLoader(RESOURCE_FOLDER, PythonProfile.PROFILE_LOCATION, runtime);
loader.addRulesByAnnotatedClass(repository, getCheckClasses());
repository.rules().stream()
.filter(rule -> TEMPLATE_RULE_KEYS.contains(rule.key()))
.forEach(rule -> rule.setTemplate(true));
repository.done();
}
private static List> getCheckClasses() {
return StreamSupport.stream(CheckList.getChecks().spliterator(), false)
.map(check -> (Class>) check)
.collect(Collectors.toList());
}
}