From 29153b81e5b6ce9761b9051630c7e2a293ccffde Mon Sep 17 00:00:00 2001 From: Ashley Scopes <73482956+ascopes@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:59:56 +0100 Subject: [PATCH] Replace is${LANG}Enabled flags with an enum set --- .../AbstractGenerateMojo.java | 25 +++--- .../execute/ArgLineBuilder.java | 59 +++----------- .../generate/GenerationRequest.java | 22 +---- .../generate/Language.java | 80 +++++++++++++++++++ .../generate/SourceCodeGenerator.java | 28 ++----- 5 files changed, 113 insertions(+), 101 deletions(-) create mode 100644 src/main/java/io/github/ascopes/protobufmavenplugin/generate/Language.java diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java b/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java index 5a6ac4ad..e388de0a 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java @@ -21,6 +21,7 @@ import io.github.ascopes.protobufmavenplugin.dependency.ResolutionException; import io.github.ascopes.protobufmavenplugin.generate.ImmutableGenerationRequest; +import io.github.ascopes.protobufmavenplugin.generate.Language; import io.github.ascopes.protobufmavenplugin.generate.SourceCodeGenerator; import io.github.ascopes.protobufmavenplugin.generate.SourceRootRegistrar; import io.github.ascopes.protobufmavenplugin.utils.FileUtils; @@ -481,32 +482,36 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoExecutionException(ex.getMessage(), ex); } + var enabledLanguages = Language.setBuilder() + .addIf(cppEnabled, Language.CPP) + .addIf(csharpEnabled, Language.C_SHARP) + .addIf(javaEnabled, Language.JAVA) + .addIf(kotlinEnabled, Language.KOTLIN) + .addIf(objcEnabled, Language.OBJECTIVE_C) + .addIf(phpEnabled, Language.PHP) + .addIf(pythonEnabled, Language.PYTHON) + .addIf(pythonStubsEnabled, Language.PYI) + .addIf(rubyEnabled, Language.RUBY) + .addIf(rustEnabled, Language.RUST) + .build(); + var request = ImmutableGenerationRequest.builder() .binaryMavenPlugins(nonNullList(binaryMavenPlugins)) .binaryPathPlugins(nonNullList(binaryPathPlugins)) .binaryUrlPlugins(nonNullList(binaryUrlPlugins)) .dependencyResolutionDepth(dependencyResolutionDepth) + .enabledLanguages(enabledLanguages) .jvmMavenPlugins(nonNullList(jvmMavenPlugins)) .importDependencies(nonNullList(importDependencies)) .importPaths(nonNullList(importPaths) .stream() .map(File::toPath) .collect(Collectors.toList())) - .isCppEnabled(cppEnabled) - .isCsharpEnabled(csharpEnabled) .isFailOnMissingSources(failOnMissingSources) .isFatalWarnings(fatalWarnings) .isIgnoreProjectDependencies(ignoreProjectDependencies) - .isJavaEnabled(javaEnabled) - .isKotlinEnabled(kotlinEnabled) .isLiteEnabled(liteOnly) - .isObjcEnabled(objcEnabled) - .isPhpEnabled(phpEnabled) - .isPythonEnabled(pythonEnabled) - .isPythonStubsEnabled(pythonStubsEnabled) .isRegisterAsCompilationRoot(registerAsCompilationRoot) - .isRubyEnabled(rubyEnabled) - .isRustEnabled(rustEnabled) .mavenSession(session) .outputDirectory(outputDirectory()) .protocVersion(protocVersion()) diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java index 4ddd6c8d..ddde065e 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java @@ -16,6 +16,7 @@ package io.github.ascopes.protobufmavenplugin.execute; +import io.github.ascopes.protobufmavenplugin.generate.Language; import io.github.ascopes.protobufmavenplugin.plugin.ResolvedPlugin; import java.nio.file.Path; import java.util.ArrayList; @@ -38,14 +39,6 @@ public ArgLineBuilder(Path protocPath) { outputTargetCount = 0; } - public ArgLineBuilder cppOut(Path outputPath, boolean lite) { - return langOut("cpp", outputPath, lite); - } - - public ArgLineBuilder csharpOut(Path outputPath, boolean lite) { - return langOut("csharp", outputPath, lite); - } - public List compile(Collection sourcesToCompile) { if (outputTargetCount == 0) { throw new IllegalStateException("No output targets were provided"); @@ -67,6 +60,15 @@ public ArgLineBuilder fatalWarnings(boolean fatalWarnings) { return this; } + public ArgLineBuilder generateCodeFor(Language language, Path outputPath, boolean lite) { + ++outputTargetCount; + var flag = lite + ? "--" + language.getFlagName() + "_out=lite:" + : "--" + language.getFlagName() + "_out="; + args.add(flag + outputPath); + return this; + } + public ArgLineBuilder importPaths(Collection includePaths) { for (var includePath : includePaths) { args.add("--proto_path=" + includePath); @@ -74,22 +76,6 @@ public ArgLineBuilder importPaths(Collection includePaths) { return this; } - public ArgLineBuilder javaOut(Path outputPath, boolean lite) { - return langOut("java", outputPath, lite); - } - - public ArgLineBuilder kotlinOut(Path outputPath, boolean lite) { - return langOut("kotlin", outputPath, lite); - } - - public ArgLineBuilder objcOut(Path outputPath, boolean lite) { - return langOut("objc", outputPath, lite); - } - - public ArgLineBuilder phpOut(Path outputPath, boolean lite) { - return langOut("php", outputPath, lite); - } - public ArgLineBuilder plugins(Collection plugins, Path outputPath) { for (var plugin : plugins) { // protoc always maps a flag `--xxx_out` to a plugin named `protoc-gen-xxx`, so we have @@ -101,32 +87,7 @@ public ArgLineBuilder plugins(Collection plugins, Path outputPat return this; } - public ArgLineBuilder pyiOut(Path outputPath, boolean lite) { - return langOut("pyi", outputPath, lite); - } - - public ArgLineBuilder pythonOut(Path outputPath, boolean lite) { - return langOut("python", outputPath, lite); - } - - public ArgLineBuilder rubyOut(Path outputPath, boolean lite) { - return langOut("ruby", outputPath, lite); - } - - public ArgLineBuilder rustOut(Path outputPath, boolean lite) { - return langOut("rust", outputPath, lite); - } - public List version() { return List.of(args.get(0), "--version"); } - - private ArgLineBuilder langOut(String type, Path outputPath, boolean lite) { - ++outputTargetCount; - var flag = lite - ? "--" + type + "_out=lite:" - : "--" + type + "_out="; - args.add(flag + outputPath); - return this; - } } diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/GenerationRequest.java b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/GenerationRequest.java index 964ee1a9..d53da3b1 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/GenerationRequest.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/GenerationRequest.java @@ -40,6 +40,8 @@ public interface GenerationRequest { DependencyResolutionDepth getDependencyResolutionDepth(); + Collection getEnabledLanguages(); + Collection getImportDependencies(); Collection getImportPaths(); @@ -58,33 +60,13 @@ public interface GenerationRequest { SourceRootRegistrar getSourceRootRegistrar(); - boolean isCppEnabled(); - - boolean isCsharpEnabled(); - boolean isFailOnMissingSources(); boolean isFatalWarnings(); boolean isIgnoreProjectDependencies(); - boolean isJavaEnabled(); - - boolean isKotlinEnabled(); - boolean isLiteEnabled(); - boolean isObjcEnabled(); - - boolean isPhpEnabled(); - - boolean isPythonEnabled(); - - boolean isPythonStubsEnabled(); - boolean isRegisterAsCompilationRoot(); - - boolean isRubyEnabled(); - - boolean isRustEnabled(); } diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/Language.java b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/Language.java new file mode 100644 index 00000000..bc140972 --- /dev/null +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/Language.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 - 2024, Ashley Scopes. + * + * 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 + * + * http://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 io.github.ascopes.protobufmavenplugin.generate; + +import java.util.EnumSet; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Supported generated source languages. + * + * @author Ashley Scopes + * @since 1.2.0 + */ +public enum Language { + CPP("cpp"), + C_SHARP("csharp"), + JAVA("java"), + KOTLIN("kotlin"), + OBJECTIVE_C("objc"), + PHP("php"), + PYTHON("python"), + PYI("pyi"), + RUBY("ruby"), + RUST("rust"); + + private final String flagName; + + Language(String flagName) { + this.flagName = flagName; + } + + public String getFlagName() { + return flagName; + } + + public static LanguageSetBuilder setBuilder() { + return new LanguageSetBuilder(); + } + + /** + * Builder for a set of enabled languages. + * + * @author Ashley Scopes + * @since 1.2.0 + */ + public static final class LanguageSetBuilder { + private final Set set; + + private LanguageSetBuilder() { + set = new LinkedHashSet<>(); + } + + public LanguageSetBuilder addIf(boolean condition, Language language) { + if (condition) { + set.add(language); + } + + return this; + } + + public Set build() { + return EnumSet.copyOf(set); + } + } +} diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java index 822cc327..b6075f58 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java @@ -34,8 +34,6 @@ import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; -import java.util.function.BiConsumer; -import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.inject.Inject; @@ -112,16 +110,12 @@ public boolean generate(GenerationRequest request) throws ResolutionException, I .importPaths(request.getSourceRoots()) .plugins(plugins, request.getOutputDirectory()); - addOptionalOutput(request, GenerationRequest::isCppEnabled, argLineBuilder::cppOut); - addOptionalOutput(request, GenerationRequest::isCsharpEnabled, argLineBuilder::csharpOut); - addOptionalOutput(request, GenerationRequest::isKotlinEnabled, argLineBuilder::kotlinOut); - addOptionalOutput(request, GenerationRequest::isJavaEnabled, argLineBuilder::javaOut); - addOptionalOutput(request, GenerationRequest::isObjcEnabled, argLineBuilder::objcOut); - addOptionalOutput(request, GenerationRequest::isPhpEnabled, argLineBuilder::phpOut); - addOptionalOutput(request, GenerationRequest::isPythonStubsEnabled, argLineBuilder::pyiOut); - addOptionalOutput(request, GenerationRequest::isPythonEnabled, argLineBuilder::pythonOut); - addOptionalOutput(request, GenerationRequest::isRubyEnabled, argLineBuilder::rubyOut); - addOptionalOutput(request, GenerationRequest::isRustEnabled, argLineBuilder::rustOut); + request.getEnabledLanguages() + .forEach(language -> argLineBuilder.generateCodeFor( + language, + request.getOutputDirectory(), + request.isLiteEnabled() + )); var sourceFiles = sourcePaths .stream() @@ -141,16 +135,6 @@ private Path discoverProtocPath(GenerationRequest request) throws ResolutionExce return protocResolver.resolve(request.getMavenSession(), request.getProtocVersion()); } - private void addOptionalOutput( - GenerationRequest request, - Predicate check, - BiConsumer consumer - ) { - if (check.test(request)) { - consumer.accept(request.getOutputDirectory(), request.isLiteEnabled()); - } - } - private boolean logProtocVersion(Path protocPath) throws IOException { var args = new ArgLineBuilder(protocPath).version(); return commandLineExecutor.execute(args);