Skip to content

Commit

Permalink
Updated naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed May 29, 2024
1 parent f73c83c commit 8c0f498
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.cli.Option;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.WordUtils;
import org.apache.rat.OptionCollection;
import org.apache.rat.utils.CasedString;
Expand All @@ -35,7 +36,10 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -57,6 +61,12 @@ public class AntGenerator {
*/
public static Predicate<Option> ANT_FILTER = option -> !(ANT_FILTER_LIST.contains(option) || option.getLongOpt() == null);

/** A mapping of external name to internal name if not standard */
private static Map<String,String> RENAME_MAP = new HashMap<>();

static {
RENAME_MAP.put("addLicense", "add-license");
}

private AntGenerator() {}

Expand Down Expand Up @@ -144,77 +154,9 @@ private static String getElementClass(AntOption option) throws IOException {
return format(" public class %1$s extends Child { %1$s() {super(%2$s);}}%n%n", WordUtils.capitalize(option.name), option.keyValue());
}

/**
* A class that wraps the CLI option and provides Ant specific values.
*/
private static class AntOption {
final Option option;
/** An uncapitalized name */
final String name;

AntOption(Option option) {
this.option = option;
name = new CasedString(StringCase.KEBAB, option.getLongOpt()).toCase(StringCase.CAMEL);
}

/**
* Returns {@code true} if the option should be an attribute of the &lt;rat:report> element.
* @return {@code true} if the option should be an attribute of the &lt;rat:report> element.
*/
public boolean isAttribute() {
return (!option.hasArgs());
}
/**
* Returns {@code true} if the option should be a child element of the &lt;rat:report> element.
* @return {@code true} if the option should be a child element of the &lt;rat:report> element.
*/
public boolean isElement() {
return !isAttribute() || option.getType() != String.class;
}

/**
* Returns {@code true} if the enclosed option has one or more arguments.
* @return {@code true} if the enclosed option has one or more arguments.
*/
public boolean hasArg() {
return option.hasArg();
}

/**
* Returns The key value for the option. This is the long opt enclosed in quotes and with leading dashes.
* @return The key value for the option.
*/
public String keyValue() {
return format("\"--%s\"", option.getLongOpt());
}

/**
* Get the method comment for this option.
* @param addParam if {@code true} the param annotation is added.
* @return the Comment block for the function.
*/
public String getComment(boolean addParam) {
StringBuilder sb = new StringBuilder()
.append(format(" /**%n * %s%n", option.getDescription().replaceAll("<", "&lt;").replaceAll(">", "&gt;")));
if (option.isDeprecated()) {
sb.append(format(" * %s%n * @deprecated%n",option.getDeprecated()));
}
if (addParam && option.hasArg()) {
sb.append(format(" * @param %s The value to set%n", name));
}
return sb.append(format(" */%n")).toString();
}

/**
* Get the signature of th eattribute function.
* @return the signature of the attribue function.
*/
public String getAttributeFunctionName() {
return "set" +
WordUtils.capitalize(name) +
(option.hasArg() ?"(String " : "(boolean ") +
name +
")";
}
static String createName(Option option) {
String name = option.getLongOpt();
name = StringUtils.defaultIfEmpty(RENAME_MAP.get(name), name).toLowerCase(Locale.ROOT);
return new CasedString(StringCase.KEBAB, name).toCase(StringCase.CAMEL);
}
}
90 changes: 90 additions & 0 deletions apache-rat-tools/src/main/java/org/apache/rat/tools/AntOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.apache.rat.tools;

import org.apache.commons.cli.Option;
import org.apache.commons.text.WordUtils;
import org.apache.rat.utils.CasedString;

import static java.lang.String.format;

/**
* A class that wraps the CLI option and provides Ant specific values.
*/
public class AntOption {
final Option option;
/**
* An uncapitalized name
*/
final String name;

AntOption(Option option) {
this.option = option;
name = AntGenerator.createName(option);
}

/**
* Returns {@code true} if the option should be an attribute of the &lt;rat:report> element.
*
* @return {@code true} if the option should be an attribute of the &lt;rat:report> element.
*/
public boolean isAttribute() {
return (!option.hasArgs());
}

/**
* Returns {@code true} if the option should be a child element of the &lt;rat:report> element.
*
* @return {@code true} if the option should be a child element of the &lt;rat:report> element.
*/
public boolean isElement() {
return !isAttribute() || option.getType() != String.class;
}

/**
* Returns {@code true} if the enclosed option has one or more arguments.
*
* @return {@code true} if the enclosed option has one or more arguments.
*/
public boolean hasArg() {
return option.hasArg();
}

/**
* Returns The key value for the option. This is the long opt enclosed in quotes and with leading dashes.
*
* @return The key value for the option.
*/
public String keyValue() {
return format("\"--%s\"", option.getLongOpt());
}

/**
* Get the method comment for this option.
*
* @param addParam if {@code true} the param annotation is added.
* @return the Comment block for the function.
*/
public String getComment(boolean addParam) {
StringBuilder sb = new StringBuilder()
.append(format(" /**%n * %s%n", option.getDescription().replaceAll("<", "&lt;").replaceAll(">", "&gt;")));
if (option.isDeprecated()) {
sb.append(format(" * %s%n * @deprecated%n", option.getDeprecated()));
}
if (addParam && option.hasArg()) {
sb.append(format(" * @param %s The value to set%n", name));
}
return sb.append(format(" */%n")).toString();
}

/**
* Get the signature of th eattribute function.
*
* @return the signature of the attribue function.
*/
public String getAttributeFunctionName() {
return "set" +
WordUtils.capitalize(name) +
(option.hasArg() ? "(String " : "(boolean ") +
name +
")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -50,7 +51,8 @@ public class MavenGenerator {
private static Map<String,String> RENAME_MAP = new HashMap<>();

static {
RENAME_MAP.put("licenses", "configuration");
RENAME_MAP.put("licenses", "config");
RENAME_MAP.put("addLicense", "add-license");
}
/**
* List of CLI Options that are not supported by Maven.
Expand Down Expand Up @@ -84,7 +86,6 @@ public static void main(String[] args) throws IOException {
String className = args[1];
String destDir = args[2];
List<MavenOption> options = OptionCollection.buildOptions().getOptions().stream().filter(MAVEN_FILTER).map(MavenOption::new).collect(Collectors.toList());
Map<String,String> renameMap = new HashMap<>();
String pkgName = String.join(File.separator, new CasedString(StringCase.DOT, packageName).getSegments());
File file = new File(new File(new File(destDir), pkgName), className + ".java");
System.out.println("Creating " + file);
Expand All @@ -104,7 +105,7 @@ public static void main(String[] args) throws IOException {
}
break;
case "${methods}":
writeMethods(writer, options, renameMap);
writeMethods(writer, options);
break;
case "${package}":
writer.append(format("package %s;%n", packageName));
Expand All @@ -123,22 +124,26 @@ public static void main(String[] args) throws IOException {
}
}

private static void writeMethods(FileWriter writer, List<MavenOption> options, Map<String,String> renameMap) throws IOException {
for (MavenOption option : options) {
String desc = option.getDescription().replace("<", "&lt;").replace(">", "&gt;");
private static String getComment(MavenOption option) {
String desc = option.getDescription().replace("<", "&lt;").replace(">", "&gt;");
StringBuilder sb = new StringBuilder()
.append(format(" /**%n * %s%n * @param %s the argument.%n", desc, option.name));
if (option.isDeprecated()) {
sb.append(format(" * %s%n * @deprecated%n", option.getDeprecated()));
}
return sb.append(format(" */%n")).toString();
}

writer.append(format(" /**%n * %s%n * @param %s the argument.%n", desc, option.name));
if (option.isDeprecated()) {
writer.append(format(" * %s%n * @deprecated", option.getDeprecated()));
}
writer.append(format(" */%n @Parameter(property = \"rat.%2$s\")%n public void set%1$s(%3$s %2$s) {%n",
WordUtils.capitalize(option.name), option.name, option.hasArg() ? "String" : "boolean"))
private static void writeMethods(FileWriter writer, List<MavenOption> options) throws IOException {
for (MavenOption option : options) {
writer.append(getComment(option))
.append(option.getMethodSignature(" ")).append(" {").append(System.lineSeparator())
.append(getBody(option))
.append(format(" }%n"));
.append(" }").append(System.lineSeparator());
}
}

private static String getBody(MavenOption option) throws IOException {
private static String getBody(MavenOption option) {
if (option.hasArg()) {
return format(" %sArg(%s, %s);%n", option.option.hasArgs() ? "add" : "set" , option.keyValue(), option.name);
} else {
Expand All @@ -148,89 +153,10 @@ private static String getBody(MavenOption option) throws IOException {
}
}

// private static String getImports(List<MavenOption> options) {
// Set<Class<?>> includes = new TreeSet<>((o1, o2) -> o1.getName().compareTo(o2.getName()));
// includes.add(java.util.ArrayList.class);
// includes.add(java.util.List.class);
// options.stream().map(o -> ((Class<?>)o.option.getType())).filter(t -> !t.getName().startsWith("java.lang.")).forEach(includes::add);
// StringBuilder sb = new StringBuilder();
// includes.stream().map(t -> format("import %s;%n", t.getName())).forEach(sb::append);
// return sb.toString();
// }

private static class MavenOption {
final Option option;
final String name;

public static String createName(Option option) {
String name = option.getLongOpt();
name = StringUtils.defaultIfEmpty(RENAME_MAP.get(name), name);
return new CasedString(CasedString.StringCase.KEBAB, name).toCase(CasedString.StringCase.CAMEL);
}

/**
* Constructor.
*
* @param option The CLI option
*/
MavenOption(Option option) {
this.option = option;
this.name = createName(option);
}

/**
* Get the description escaped for XML format.
*
* @return the description.
*/
public String getDescription() {
return option.getDescription().replace("<", "&lt;").replace(">", "&gt;");
}

/**
* Returns the value as an POM xml node.
*
* @param value the value
* @return the pom xml node.
*/
public String xmlNode(String value) {
return format("<%1$s>%2$s</%1$s>%n", name, value == null ? "false" : value);//: format("<%s>%n", name);
}

/**
* Gets the simple class name for the data type for this option.
* Normally "String".
*
* @return the simple class name for the type.
*/
public Class<?> getType() {
return option.hasArg() ? ((Class<?>) option.getType()) : boolean.class;
}

public boolean isDeprecated() {
return option.isDeprecated();
}

/**
* Determine if true if the enclosed option expects an argument.
*
* @return {@code true} if the enclosed option expects at least one argument.
*/
public boolean hasArg() {
return option.hasArg();
}

/**
* the key value for the option.
*
* @return the key value for the CLI argument map.
*/
public String keyValue() {
return "\"--" + option.getLongOpt() + "\"";
}

public String getDeprecated() {
return option.getDeprecated().toString();
}
static String createName(Option option) {
String name = option.getLongOpt();
name = StringUtils.defaultIfEmpty(RENAME_MAP.get(name), name).toLowerCase(Locale.ROOT);
return new CasedString(StringCase.KEBAB, name).toCase(StringCase.CAMEL);
}

}
Loading

0 comments on commit 8c0f498

Please sign in to comment.