Skip to content

Commit

Permalink
[config] remove Reflections dependency, use ServiceLoader instead. Re…
Browse files Browse the repository at this point in the history
…solves #567
  • Loading branch information
aalmiray committed Dec 4, 2021
1 parent 1e44e68 commit ab54c56
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/jreleaser-config-yaml/jreleaser-config-yaml.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api("com.github.sbaudoin:yamllint:$yamllintVersion") {
exclude group: 'commons-cli', module: 'commons-cli'
exclude group: 'com.google.code.findbugs', module: 'jsr305'
exclude group: 'org.reflections', module: 'reflections'
}

compileOnly "org.kordamp.jipsy:jipsy-annotations:${jipsyVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.sbaudoin.yamllint.YamlLintConfig;
import com.github.sbaudoin.yamllint.YamlLintConfigException;
import org.jreleaser.config.JReleaserConfigParser;
import org.jreleaser.config.yaml.lint.YamlLintConfig2;
import org.jreleaser.model.JReleaserModel;
import org.kordamp.jipsy.annotations.ServiceProviderFor;

Expand Down Expand Up @@ -68,9 +69,9 @@ public boolean supports(String resource) {

@Override
public void validate(Path configFile) throws IOException {
YamlLintConfig config = null;
YamlLintConfig2 config = null;
try {
config = new YamlLintConfig(YAML_LINT_CONFIG);
config = new YamlLintConfig2(YAML_LINT_CONFIG);
} catch (YamlLintConfigException e) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.config.yaml.lint;

import com.github.sbaudoin.yamllint.rules.Rule;

import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
* @author Andres Almiray
* @since 0.9.1
*/
public class RuleFactory {
/**
* The instance that holds this singleton
*/
public static final RuleFactory instance = new RuleFactory();
private final Map<String, Rule> rules = new HashMap<>();

/**
* Hide default constructor
*/
private RuleFactory() {
ServiceLoader.load(Rule.class, getClass().getClassLoader())
.forEach(rule -> rules.put(rule.getId(), rule));
}

/**
* Returns a matching rule by id
*
* @param id the ID of the rule to be returned
* @return the rule corresponding to the passed ID or <code>null</code> if not found
*/
public Rule getRule(String id) {
if (rules.containsKey(id)) {
return rules.get(id);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.config.yaml.lint;

import com.github.sbaudoin.yamllint.YamlLintConfig;
import com.github.sbaudoin.yamllint.YamlLintConfigException;
import com.github.sbaudoin.yamllint.rules.Rule;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author Andres Almiray
* @since 0.9.1
*/
public class YamlLintConfig2 extends YamlLintConfig {
public YamlLintConfig2(CharSequence content) throws YamlLintConfigException {
super(content);
}

public YamlLintConfig2(URL file) throws IOException, YamlLintConfigException {
super(file);
}

public YamlLintConfig2(InputStream in) throws YamlLintConfigException {
super(in);
}

@Override
public List<Rule> getEnabledRules(File file) {
List<Rule> rules = new ArrayList<>();
for (Map.Entry<String, Object> entry : ruleConf.entrySet()) {
Rule rule = RuleFactory.instance.getRule(entry.getKey());
if (rule != null && entry.getValue() != null && (file == null || !rule.ignores(file))) {
rules.add(rule);
}
}
return rules;
}

@Override
protected void validate() throws YamlLintConfigException {
for (Map.Entry<String, Object> entry : ruleConf.entrySet()) {
String id = entry.getKey();
Rule rule = RuleFactory.instance.getRule(id);
if (rule == null) {
throw getInvalidConfigException(String.format("no such rule: \"%s\"", id));
}

Map<String, Object> newConf = validateRuleConf(rule, entry.getValue());
ruleConf.put(id, newConf);
}
}

/**
* Returns a {@code YamlLintConfigException} with the message "invalid config: %passed_message%"
*
* @param message a message that describes the configuration error
* @return a {@code YamlLintConfigException} with the passed message
*/
private static YamlLintConfigException getInvalidConfigException(String message) {
return getInvalidConfigException(null, message, null);
}

/**
* Returns a {@code YamlLintConfigException} with the message "invalid%specifier% config: %passed_message%"
*
* @param specifier a string to be passed after 'invalid'. Pass {@code null} if you do not want any specifier.
* @param message a message that describes the configuration error
* @param e an optional (may be {@code null}) {@code Throwable} to be set as the ancestor of the returned exception
* @return a {@code YamlLintConfigException} with the passed message
*/
private static YamlLintConfigException getInvalidConfigException(String specifier, String message, Throwable e) {
String m = String.format("invalid%s config: %s", (specifier == null) ? "" : (" " + specifier), message);
if (e == null) {
return new YamlLintConfigException(m);
} else {
return new YamlLintConfigException(m, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
com.github.sbaudoin.yamllint.rules.Braces
com.github.sbaudoin.yamllint.rules.Brackets
com.github.sbaudoin.yamllint.rules.Colons
com.github.sbaudoin.yamllint.rules.Commas
com.github.sbaudoin.yamllint.rules.Comments
com.github.sbaudoin.yamllint.rules.CommentsIndentation
com.github.sbaudoin.yamllint.rules.DocumentEnd
com.github.sbaudoin.yamllint.rules.DocumentStart
com.github.sbaudoin.yamllint.rules.EmptyLines
com.github.sbaudoin.yamllint.rules.EmptyValues
com.github.sbaudoin.yamllint.rules.Hyphens
com.github.sbaudoin.yamllint.rules.Indentation
com.github.sbaudoin.yamllint.rules.KeyDuplicates
com.github.sbaudoin.yamllint.rules.KeyOrdering
com.github.sbaudoin.yamllint.rules.LineLength
com.github.sbaudoin.yamllint.rules.NewLineAtEndOfFile
com.github.sbaudoin.yamllint.rules.NewLines
com.github.sbaudoin.yamllint.rules.OctalValues
com.github.sbaudoin.yamllint.rules.QuotedStrings
com.github.sbaudoin.yamllint.rules.TrailingSpaces
com.github.sbaudoin.yamllint.rules.Truthy
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ opentest4jVersion = 1.2.0
picocliVersion = 4.6.2
plexusVersion = 3.4.1
plexusArchiverVersion = 4.2.6
reflectionsVersion = 0.10.2
tikaVersion = 2.1.0
wiremockVersion = 2.31.0
slf4jVersion = 1.7.32
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ enforce {
"junit:junit:$junitVersion",
"org.hamcrest:hamcrest:$hamcrestVersion",
"org.apache.httpcomponents:httpclient:$httpclientVersion",
"com.sun.mail:jakarta.mail:$mailApiVersion",
"org.reflections:reflections:$reflectionsVersion"
"com.sun.mail:jakarta.mail:$mailApiVersion"
}
}

0 comments on commit ab54c56

Please sign in to comment.