lombok.core.configuration.ConfigurationApp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datacap-shaded-pinot Show documentation
Show all versions of datacap-shaded-pinot Show documentation
DataCap - Shaded for Apache Pinot
/*
* Copyright (C) 2014-2021 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.core.configuration;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.zwitserloot.cmdreader.CmdReader;
import com.zwitserloot.cmdreader.Description;
import com.zwitserloot.cmdreader.Excludes;
import com.zwitserloot.cmdreader.FullName;
import com.zwitserloot.cmdreader.InvalidCommandLineException;
import com.zwitserloot.cmdreader.Mandatory;
import com.zwitserloot.cmdreader.Requires;
import com.zwitserloot.cmdreader.Sequential;
import com.zwitserloot.cmdreader.Shorthand;
import lombok.ConfigurationKeys;
import lombok.core.LombokApp;
import lombok.core.configuration.ConfigurationParser.Collector;
import lombok.spi.Provides;
@Provides
public class ConfigurationApp extends LombokApp {
private static final URI NO_CONFIG = URI.create("");
private PrintStream out = System.out;
private PrintStream err = System.err;
@Override public String getAppName() {
return "config";
}
@Override public String getAppDescription() {
return "Prints the configurations for the provided paths to standard out.";
}
@Override public List getAppAliases() {
return Arrays.asList("configuration", "config", "conf", "settings");
}
public static class CmdArgs {
@Sequential
@Mandatory(onlyIfNot={"help", "generate"})
@Description("Paths to java files or directories the configuration is to be printed for.")
private List paths = new ArrayList();
@Shorthand("g")
@Excludes("paths")
@Description("Generates a list containing all the available configuration parameters. Add --verbose to print more information.")
boolean generate = false;
@Shorthand("v")
@Description("Displays more information.")
boolean verbose = false;
@Shorthand("n")
@FullName("not-mentioned")
@Requires("verbose")
@Description("Also display files that don't mention the key.")
boolean notMentioned = false;
@Shorthand("k")
@Description("Limit the result to these keys.")
private List key = new ArrayList();
@Shorthand({"h", "?"})
@Description("Shows this help text.")
boolean help = false;
}
@Override public int runApp(List raw) throws Exception {
CmdReader reader = CmdReader.of(CmdArgs.class);
CmdArgs args;
try {
args = reader.make(raw.toArray(new String[0]));
if (args.help) {
out.println(reader.generateCommandLineHelp("java -jar lombok.jar configuration"));
return 0;
}
} catch (InvalidCommandLineException e) {
err.println(e.getMessage());
err.println(reader.generateCommandLineHelp("java -jar lombok.jar configuration"));
return 1;
}
ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys();
Collection> keys = checkKeys(args.key);
if (keys == null) return 1;
boolean verbose = args.verbose;
if (args.generate) {
return generate(keys, verbose, !args.key.isEmpty());
}
return display(keys, verbose, args.paths, !args.key.isEmpty(), args.notMentioned);
}
public ConfigurationApp redirectOutput(PrintStream out, PrintStream err) {
if (out != null) this.out = out;
if (err != null) this.err = err;
return this;
}
public int generate(Collection> keys, boolean verbose, boolean explicit) {
for (ConfigurationKey key : keys) {
if (!explicit && key.isHidden()) continue;
String keyName = key.getKeyName();
ConfigurationDataType type = key.getType();
String description = key.getDescription();
boolean hasDescription = description != null && !description.isEmpty();
if (!verbose) {
out.println(keyName);
if (hasDescription) {
out.print(" ");
out.println(description);
}
out.println();
continue;
}
out.printf("##%n## Key : %s%n## Type: %s%n", keyName, type);
if (hasDescription) {
out.printf("##%n## %s%n", description);
}
out.printf("##%n## Examples:%n#%n");
out.printf("# clear %s%n", keyName);
String exampleValue = type.getParser().exampleValue();
if (type.isList()) {
out.printf("# %s += %s%n", keyName, exampleValue);
out.printf("# %s -= %s%n", keyName, exampleValue);
} else {
out.printf("# %s = %s%n", keyName, exampleValue);
}
out.printf("#%n%n");
}
if (!verbose) {
out.println("Use --verbose for more information.");
}
return 0;
}
public int display(Collection> keys, boolean verbose, Collection argsPaths, boolean explicitKeys, boolean notMentioned) throws Exception {
TreeMap> sharedDirectories = findSharedDirectories(argsPaths);
if (sharedDirectories == null) return 1;
Set none = sharedDirectories.remove(NO_CONFIG);
if (none != null) {
if (none.size() == 1) {
out.printf("No 'lombok.config' found for '%s'.%n", none.iterator().next());
} else {
out.println("No 'lombok.config' found for: ");
for (String path : none) out.printf("- %s%n", path);
}
}
final List problems = new ArrayList();
ConfigurationProblemReporter reporter = new ConfigurationProblemReporter() {
@Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) {
problems.add(String.format("%s: %s (%s:%d)", problem, line, sourceDescription, lineNumber));
}
};
FileSystemSourceCache cache = new FileSystemSourceCache();
ConfigurationParser parser = new ConfigurationParser(reporter);
boolean first = true;
for (Entry> entry : sharedDirectories.entrySet()) {
if (!first) {
out.printf("%n%n");
}
Set paths = entry.getValue();
if (paths.size() == 1) {
if (!(argsPaths.size() == 1)) out.printf("Configuration for '%s'.%n%n", paths.iterator().next());
} else {
out.printf("Configuration for:%n");
for (String path : paths) out.printf("- %s%n", path);
out.println();
}
URI directory = entry.getKey();
ConfigurationResolver resolver = new BubblingConfigurationResolver(cache.forUri(directory), cache.fileToSource(parser));
Map, ? extends Collection> traces = trace(keys, directory, notMentioned);
boolean printed = false;
for (ConfigurationKey key : keys) {
Object value = resolver.resolve(key);
Collection modifications = traces.get(key);
if (!modifications.isEmpty() || explicitKeys) {
if (printed && verbose) out.println();
printValue(key, value, verbose, modifications);
printed = true;
}
}
if (!printed) out.println("");
first = false;
}
if (!problems.isEmpty()) {
err.printf("Problems in the configuration files:%n");
for (String problem : problems) err.printf("- %s%n", problem);
return 2;
}
return 0;
}
private void printValue(ConfigurationKey key, Object value, boolean verbose, Collection history) {
if (verbose) out.printf("# %s%n", key.getDescription());
if (value == null) {
out.printf("clear %s%n", key.getKeyName());
} else if (value instanceof List) {
List list = (List)value;
if (list.isEmpty()) out.printf("clear %s%n", key.getKeyName());
for (Object element : list) out.printf("%s += %s%n", key.getKeyName(), element);
} else {
out.printf("%s = %s%n", key.getKeyName(), value);
}
if (!verbose) return;
for (String modification : history) out.printf("# %s%n", modification);
}
private static final ConfigurationProblemReporter VOID = new ConfigurationProblemReporter() {
@Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) {}
};
private Map, ? extends Collection> trace(Collection> keys, URI directory, boolean notMentioned) throws Exception {
Map, List> result = new HashMap, List>();
for (ConfigurationKey key : keys) result.put(key, new ArrayList());
Set> used = new HashSet>();
boolean stopBubbling = false;
Collection visited = new HashSet();
for (ConfigurationFile context = ConfigurationFile.forDirectory(new File(directory)); context != null && !stopBubbling; context = context.parent()) {
if (!context.exists()) continue;
Deque