Skip to content

Commit

Permalink
Replace is${LANG}Enabled flags with an enum set
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Apr 14, 2024
1 parent 7677db0 commit 29153b8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> compile(Collection<Path> sourcesToCompile) {
if (outputTargetCount == 0) {
throw new IllegalStateException("No output targets were provided");
Expand All @@ -67,29 +60,22 @@ 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<Path> includePaths) {
for (var includePath : includePaths) {
args.add("--proto_path=" + includePath);
}
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<ResolvedPlugin> plugins, Path outputPath) {
for (var plugin : plugins) {
// protoc always maps a flag `--xxx_out` to a plugin named `protoc-gen-xxx`, so we have
Expand All @@ -101,32 +87,7 @@ public ArgLineBuilder plugins(Collection<ResolvedPlugin> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public interface GenerationRequest {

DependencyResolutionDepth getDependencyResolutionDepth();

Collection<Language> getEnabledLanguages();

Collection<? extends MavenArtifact> getImportDependencies();

Collection<Path> getImportPaths();
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<Language> set;

private LanguageSetBuilder() {
set = new LinkedHashSet<>();
}

public LanguageSetBuilder addIf(boolean condition, Language language) {
if (condition) {
set.add(language);
}

return this;
}

public Set<Language> build() {
return EnumSet.copyOf(set);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -141,16 +135,6 @@ private Path discoverProtocPath(GenerationRequest request) throws ResolutionExce
return protocResolver.resolve(request.getMavenSession(), request.getProtocVersion());
}

private void addOptionalOutput(
GenerationRequest request,
Predicate<GenerationRequest> check,
BiConsumer<Path, Boolean> 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);
Expand Down

0 comments on commit 29153b8

Please sign in to comment.