Skip to content

Commit

Permalink
optimize mojo configs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Feb 18, 2021
1 parent ab11889 commit cef78f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private List<Map.Entry<Path, String>> generate(final Path base, final Annotation
.collect(toList());
}

// local commands
// see io.yupiik.bundlebee.maven.build.MojoGenerator.skipSharedConfig
private boolean skipSharedConfig(final String name) {
return "add-alveolus".equals(name) ||
"build".equals(name) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ public static void main(final String... args) {
final var className = toClassName(name) + "Mojo";
final var mojo = output.resolve(pck.replace('.', '/') + '/' + className + ".java");
Files.createDirectories(mojo.getParent());

final var skipSharedConfig = skipSharedConfig(name);

final var sharedConfigParameters = skipSharedConfig ?
"" :
sharedParameters.keySet().stream()
.flatMap(k -> Stream.of("\"--" + k + "\"", "String.valueOf(" + fromParameterToFieldName(k) + ")"))
.collect(joining(",\n ", " ", ""));

final var specificParameters = parameterDeclarationPerName.keySet().stream()
.flatMap(k -> {
final var simpleName = prefix.matcher(k).replaceAll("");
return Stream.of("\"--" + simpleName + "\"", "String.valueOf(" + simpleName + ")");
})
.collect(joining(",\n ", " ", ""));

final var launchArgs = Stream.of(
" \"" + name + "\"",
sharedConfigParameters,
specificParameters)
.filter(it -> !it.isBlank())
.collect(joining(",\n"));

Files.writeString(
mojo,
"/*\n" +
Expand Down Expand Up @@ -141,24 +164,14 @@ public static void main(final String... args) {
" */\n" +
"@Mojo(name = \"" + name + "\", threadSafe = true /* not strictly true but avoids warning inaccurate for builds */)\n" +
"public class " + className + " extends BaseMojo {\n" +
sharedParameters.values().stream()
.collect(joining("\n\n", "", "\n\n")) +
(skipSharedConfig ? "" : sharedParameters.values().stream()
.collect(joining("\n\n", "", "\n\n"))) +
parameterDeclarationPerName.values().stream()
.collect(joining("\n\n", "", "\n\n")) +
" @Override\n" +
" public void doExecute() {\n" +
" new BundleBee().launch(\n" +
" \"" + name + "\",\n" +
sharedParameters.keySet().stream()
.flatMap(k -> Stream.of("\"--" + k + "\"", "String.valueOf(" + fromParameterToFieldName(k) + ")"))
.collect(joining(",\n ", " ", "")) +
(!parameterDeclarationPerName.isEmpty() ? ",\n" : "") +
parameterDeclarationPerName.keySet().stream()
.flatMap(k -> {
final var simpleName = prefix.matcher(k).replaceAll("");
return Stream.of("\"--" + simpleName + "\"", "String.valueOf(" + simpleName + ")");
})
.collect(joining(",\n ", " ", "")) + ");\n" +
launchArgs + ");\n" +
" }\n" +
"}" +
"\n",
Expand Down Expand Up @@ -213,4 +226,14 @@ private static String toCamelCase(final String name, final char firstChar) {
}
return out.toString();
}

// see io.yupiik.bundlebee.documentation.CommandConfigurationGenerator.skipSharedConfig
private static boolean skipSharedConfig(final String name) {
return "add-alveolus".equals(name) ||
"build".equals(name) ||
"cipher-password".equals(name) ||
"create-master-password".equals(name) ||
"new".equals(name) ||
"version".equals(name);
}
}

0 comments on commit cef78f5

Please # to comment.