未验证 提交 de7335ad 编写于 作者: 梦境迷离's avatar 梦境迷离 提交者: GitHub

Fix generatedLanguage switch in all plugins #572 (#577)

* https://github.com/kobylynskyi/graphql-java-codegen/issues/572

* add default

* optimize code
上级 8cdb0239
...@@ -3,13 +3,7 @@ package io.github.kobylynskyi.graphql.codegen.gradle; ...@@ -3,13 +3,7 @@ package io.github.kobylynskyi.graphql.codegen.gradle;
import com.kobylynskyi.graphql.codegen.GraphQLCodegen; import com.kobylynskyi.graphql.codegen.GraphQLCodegen;
import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen; import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.model.ApiInterfaceStrategy; import com.kobylynskyi.graphql.codegen.model.*;
import com.kobylynskyi.graphql.codegen.model.ApiNamePrefixStrategy;
import com.kobylynskyi.graphql.codegen.model.ApiRootInterfaceStrategy;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.GraphQLCodegenConfiguration;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants;
import com.kobylynskyi.graphql.codegen.model.exception.LanguageNotSupportedException; import com.kobylynskyi.graphql.codegen.model.exception.LanguageNotSupportedException;
import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.supplier.JsonMappingConfigSupplier; import com.kobylynskyi.graphql.codegen.supplier.JsonMappingConfigSupplier;
...@@ -18,26 +12,15 @@ import com.kobylynskyi.graphql.codegen.supplier.SchemaFinder; ...@@ -18,26 +12,15 @@ import com.kobylynskyi.graphql.codegen.supplier.SchemaFinder;
import org.gradle.api.Action; import org.gradle.api.Action;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.*;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collections; import java.util.*;
import java.util.HashMap; import java.util.function.Supplier;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Gradle task for GraphQL code generation * Gradle task for GraphQL code generation
...@@ -163,15 +146,17 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode ...@@ -163,15 +146,17 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
} }
private GraphQLCodegen instantiateCodegen(MappingConfig mappingConfig) throws IOException { private GraphQLCodegen instantiateCodegen(MappingConfig mappingConfig) throws IOException {
switch (generatedLanguage) { java.util.Optional<MappingConfigSupplier> mappingConfigSupplier = buildJsonSupplier();
GeneratedLanguage language = mappingConfigSupplier.map(Supplier::get).map(MappingConfig::getGeneratedLanguage).orElse(generatedLanguage);
switch (language) {
case JAVA: case JAVA:
return new JavaGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier()); return new JavaGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
case SCALA: case SCALA:
return new ScalaGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier()); return new ScalaGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
case KOTLIN: case KOTLIN:
return new KotlinGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier()); return new KotlinGraphQLCodegen(getActualSchemaPaths(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
default: default:
throw new LanguageNotSupportedException(generatedLanguage); throw new LanguageNotSupportedException(language);
} }
} }
...@@ -216,11 +201,11 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode ...@@ -216,11 +201,11 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
.map(File::toPath); .map(File::toPath);
} }
private MappingConfigSupplier buildJsonSupplier() { private java.util.Optional<MappingConfigSupplier> buildJsonSupplier() {
if (jsonConfigurationFile != null && !jsonConfigurationFile.isEmpty()) { if (jsonConfigurationFile != null && !jsonConfigurationFile.isEmpty()) {
return new JsonMappingConfigSupplier(jsonConfigurationFile); return java.util.Optional.of(new JsonMappingConfigSupplier(jsonConfigurationFile));
} }
return null; return java.util.Optional.empty();
} }
@InputFiles @InputFiles
......
...@@ -3,14 +3,7 @@ package io.github.kobylynskyi.graphql.codegen; ...@@ -3,14 +3,7 @@ package io.github.kobylynskyi.graphql.codegen;
import com.kobylynskyi.graphql.codegen.GraphQLCodegen; import com.kobylynskyi.graphql.codegen.GraphQLCodegen;
import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen; import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.model.ApiInterfaceStrategy; import com.kobylynskyi.graphql.codegen.model.*;
import com.kobylynskyi.graphql.codegen.model.ApiNamePrefixStrategy;
import com.kobylynskyi.graphql.codegen.model.ApiRootInterfaceStrategy;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.GraphQLCodegenConfiguration;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants;
import com.kobylynskyi.graphql.codegen.model.RelayConfig;
import com.kobylynskyi.graphql.codegen.model.exception.LanguageNotSupportedException; import com.kobylynskyi.graphql.codegen.model.exception.LanguageNotSupportedException;
import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.supplier.JsonMappingConfigSupplier; import com.kobylynskyi.graphql.codegen.supplier.JsonMappingConfigSupplier;
...@@ -28,17 +21,8 @@ import java.io.File; ...@@ -28,17 +21,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays; import java.util.function.Supplier;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenConfiguration { public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenConfiguration {
...@@ -261,15 +245,17 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo ...@@ -261,15 +245,17 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
} }
private GraphQLCodegen instantiateCodegen(MappingConfig mappingConfig) throws IOException { private GraphQLCodegen instantiateCodegen(MappingConfig mappingConfig) throws IOException {
switch (generatedLanguage) { java.util.Optional<MappingConfigSupplier> mappingConfigSupplier = buildJsonSupplier(jsonConfigurationFile);
GeneratedLanguage language = mappingConfigSupplier.map(Supplier::get).map(MappingConfig::getGeneratedLanguage).orElse(generatedLanguage);
switch (language) {
case JAVA: case JAVA:
return new JavaGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier(jsonConfigurationFile)); return new JavaGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
case SCALA: case SCALA:
return new ScalaGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier(jsonConfigurationFile)); return new ScalaGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
case KOTLIN: case KOTLIN:
return new KotlinGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, buildJsonSupplier(jsonConfigurationFile)); return new KotlinGraphQLCodegen(getSchemas(), graphqlQueryIntrospectionResultPath, outputDir, mappingConfig, mappingConfigSupplier.orElse(null));
default: default:
throw new LanguageNotSupportedException(generatedLanguage); throw new LanguageNotSupportedException(language);
} }
} }
...@@ -301,11 +287,11 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo ...@@ -301,11 +287,11 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
return project.getResources().stream().findFirst().map(Resource::getDirectory).map(Paths::get); return project.getResources().stream().findFirst().map(Resource::getDirectory).map(Paths::get);
} }
private MappingConfigSupplier buildJsonSupplier(String jsonConfigurationFile) { private java.util.Optional<MappingConfigSupplier> buildJsonSupplier(String jsonConfigurationFile) {
if (jsonConfigurationFile != null && !jsonConfigurationFile.isEmpty()) { if (jsonConfigurationFile != null && !jsonConfigurationFile.isEmpty()) {
return new JsonMappingConfigSupplier(jsonConfigurationFile); return java.util.Optional.of(new JsonMappingConfigSupplier(jsonConfigurationFile));
} }
return null; return java.util.Optional.empty();
} }
private void addCompileSourceRootIfConfigured() { private void addCompileSourceRootIfConfigured() {
......
...@@ -202,18 +202,19 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co ...@@ -202,18 +202,19 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
}, graphqlCodegen := { }, graphqlCodegen := {
sLog.value.info(s"Generating files: ${BuildInfo.toString}") sLog.value.info(s"Generating files: ${BuildInfo.toString}")
val mappingConfigSupplier = buildJsonSupplier(jsonConfigurationFile.value.orNull) val mappingConfigSupplier = buildJsonSupplier(jsonConfigurationFile.value.orNull)
val language = mappingConfigSupplier.map(_.get()).map(_.getGeneratedLanguage).getOrElse(generatedLanguage.value)
var result = Seq.empty[File] var result = Seq.empty[File]
try { try {
val _outputDir = outputDir.value val _outputDir = outputDir.value
val _introspectionResult = graphqlQueryIntrospectionResultPath.value.orNull val _introspectionResult = graphqlQueryIntrospectionResultPath.value.orNull
lazy val instantiateCodegen = (mappingConfig: MappingConfig) => { lazy val instantiateCodegen = (mappingConfig: MappingConfig) => {
generatedLanguage.value match { language match {
case JAVA => case JAVA =>
new JavaGraphQLCodegen(getSchemas(), _introspectionResult, _outputDir, mappingConfig, mappingConfigSupplier) new JavaGraphQLCodegen(getSchemas(), _introspectionResult, _outputDir, mappingConfig, mappingConfigSupplier.orNull)
case SCALA => case SCALA =>
new ScalaGraphQLCodegen(getSchemas(), _introspectionResult, _outputDir, mappingConfig, mappingConfigSupplier) new ScalaGraphQLCodegen(getSchemas(), _introspectionResult, _outputDir, mappingConfig, mappingConfigSupplier.orNull)
case _ => case _ =>
throw new LanguageNotSupportedException(generatedLanguage.value) throw new LanguageNotSupportedException(language)
} }
} }
result = instantiateCodegen(getMappingConfig().value).generate.asScala result = instantiateCodegen(getMappingConfig().value).generate.asScala
...@@ -274,9 +275,9 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co ...@@ -274,9 +275,9 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
) ++ watchSourcesSetting ++ Seq(cleanFiles += generateCodegenTargetPath.value) ) ++ watchSourcesSetting ++ Seq(cleanFiles += generateCodegenTargetPath.value)
} }
protected def buildJsonSupplier(jsonConfigurationFile: String): JsonMappingConfigSupplier = { protected def buildJsonSupplier(jsonConfigurationFile: String): Option[JsonMappingConfigSupplier] = {
if (jsonConfigurationFile != null && jsonConfigurationFile.nonEmpty) if (jsonConfigurationFile != null && jsonConfigurationFile.nonEmpty)
new JsonMappingConfigSupplier(jsonConfigurationFile) else null Some(new JsonMappingConfigSupplier(jsonConfigurationFile)) else None
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册