-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[config] remove Reflections dependency, use ServiceLoader instead. Re…
…solves #567
- Loading branch information
Showing
7 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
core/jreleaser-config-yaml/src/main/java/org/jreleaser/config/yaml/lint/RuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
core/jreleaser-config-yaml/src/main/java/org/jreleaser/config/yaml/lint/YamlLintConfig2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-config-yaml/src/main/resources/META-INF/services/com.github.sbaudoin.yamllint.rules.Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters