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

Refactor sbt plugin, fix bug (#277)

* add use case with proxy

* add comment

* add Support code

* update README

* add scope on conf key

* add release plugin

* add release plugin

* Setting version to 2.4.2

* Setting version to 2.4.3-SNAPSHOT

* add release plugin (not release)

* upgrade sbt version, update example Deprecated api

* refactor sbt plugin

* refactor sbt plugin
上级 d71fb3b9
......@@ -85,5 +85,6 @@ SBT task
Please refer to [Codegen Options](../../docs/codegen-options.md)
> in sbt plugin option `packageName` was rename to `generatePackageName`
> in sbt plugin option `generateCodegenTargetPath` can be used for setting codegen path where code have be created by task `graphqlCodegen`
import Dependencies._
import sbtrelease.ReleaseStateTransformations._
name := "graphql-codegen-sbt-plugin"
// must be equals to oss Group Id
......@@ -15,5 +16,21 @@ lazy val `graphql-codegen-sbt-plugin` = Project(id = "graphql-codegen-sbt-plugin
sbtPlugin := true,
scalaVersion := Versions.scala212,
crossScalaVersions := List(Versions.scala212, Versions.scala211),
scalacOptions += "-target:jvm-1.8").
scriptedBufferLog := false,
scriptedLaunchOpts += s"-Dplugin.version=${version.value}",
scalacOptions += "-target:jvm-1.8",
releaseIgnoreUntrackedFiles := true,
releaseProcess := Seq[ReleaseStep](
checkSnapshotDependencies,
inquireVersions,
runClean,
releaseStepCommandAndRemaining("^ scripted"),
setReleaseVersion,
commitReleaseVersion,
tagRelease,
releaseStepCommandAndRemaining("^ publishSigned"),
setNextVersion,
commitNextVersion,
pushChanges
)).
settings(Compiles.selfDependencies)
\ No newline at end of file
sbt.version = 1.3.1
\ No newline at end of file
sbt.version = 1.3.12
\ No newline at end of file
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.3")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1")
\ No newline at end of file
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13")
\ No newline at end of file
......@@ -5,6 +5,7 @@ import sbt.Watched.WatchSource
import sbt.internal.io.Source
import sbt.io.{ AllPassFilter, SuffixFilter }
import sbt.{ Def, Task }
import sbt.Configuration
/**
*
......@@ -24,4 +25,6 @@ trait Compat {
AllPassFilter)
}
protected[this] lazy val GraphQLCodegenConfig = Configuration.of("GraphQLCodegen", "graphqlCodegen" + configurationPostfix)
}
......@@ -105,4 +105,6 @@ trait GraphQLCodegenKeys {
val javaxValidationApiVersion = settingKey[Option[String]]("javax-validation-api version")
val graphqlJavaCodegenVersion = settingKey[Option[String]]("graphql java codegen version")
val generateCodegenTargetPath = settingKey[File]("The path for graphqlCodegen to save java code which generate by plugin")
}
......@@ -17,12 +17,13 @@ import scala.collection.JavaConverters._
* @author liguobin@growingio.com
* @version 1.0,2020/7/15
*/
object GraphQLCodegenPlugin extends GraphQLCodegenPlugin(Compile) {
object GraphQLCodegenPlugin extends GraphQLCodegenPlugin(Compile, configurationPostfix = "-main") {
//for auto import
val autoImport = GlobalImport
}
class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with Compat {
class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val configurationPostfix: String = "") extends AutoPlugin with Compat {
self =>
//TODO if impl GraphQLCodegenConfiguration, can not use settingKey in override method
......@@ -40,6 +41,8 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
lazy val schemaFinderConfig: SchemaFinderConfig = SchemaFinderConfig(null)
lazy val parentInterfacesConfig: ParentInterfacesConfig = ParentInterfacesConfig()
lazy val GraphQLCodegenConfig = self.GraphQLCodegenConfig
}
//no Auto trigger
......@@ -48,16 +51,18 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
override def requires = sbt.plugins.JvmPlugin
override def projectConfigurations: Seq[Configuration] = GraphQLCodegenConfig :: Nil
import GlobalImport._
//With the implementation of some other plugins, initialization is not necessary,
//but maybe should be related to the dependency of key. For convenience, this is a conservative operation
override def globalSettings: Seq[Def.Setting[_]] = Seq(
override lazy val globalSettings: Seq[Def.Setting[_]] = Seq(
graphqlSchemas := schemaFinderConfig,
jsonConfigurationFile := None,
graphqlSchemaPaths := Seq.empty,
graphqlSchemaValidate := Seq.empty,
customTypesMapping := new util.HashMap[String, String](),
customTypesMapping := new util.HashMap[String, String](), //TODO use scala Map, convert to java Map
customAnnotationsMapping := new util.HashMap[String, String](),
directiveAnnotationsMapping := new util.HashMap[String, String](),
javaxValidationApiVersion := None,
......@@ -102,92 +107,89 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
parentInterfaces := parentInterfacesConfig
)
//setting key must use in Def、:=
private def getMappingConfig(): Def.Initialize[MappingConfig] = Def.setting[MappingConfig] {
//TODO use builder
val mappingConfig = new MappingConfig
mappingConfig.setPackageName(generatePackageName.value.orNull)
mappingConfig.setCustomTypesMapping(customTypesMapping.value)
mappingConfig.setApiNameSuffix(apiNameSuffix.value)
mappingConfig.setApiNamePrefix(apiNamePrefix.value.orNull)
mappingConfig.setApiRootInterfaceStrategy(apiRootInterfaceStrategy.value)
mappingConfig.setApiNamePrefixStrategy(apiNamePrefixStrategy.value)
mappingConfig.setModelNamePrefix(modelNamePrefix.value.orNull)
mappingConfig.setModelNameSuffix(modelNameSuffix.value.orNull)
mappingConfig.setApiPackageName(apiPackageName.value.orNull)
mappingConfig.setModelPackageName(modelPackageName.value.orNull)
mappingConfig.setGenerateBuilder(generateBuilder.value)
mappingConfig.setGenerateApis(generateApis.value)
mappingConfig.setTypeResolverSuffix(typeResolverSuffix.value)
mappingConfig.setTypeResolverPrefix(typeResolverPrefix.value.orNull)
mappingConfig.setModelValidationAnnotation(modelValidationAnnotation.value)
mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping.value)
mappingConfig.setGenerateEqualsAndHashCode(generateEqualsAndHashCode.value)
mappingConfig.setGenerateImmutableModels(generateImmutableModels.value)
mappingConfig.setGenerateToString(generateToString.value)
mappingConfig.setSubscriptionReturnType(subscriptionReturnType.value.orNull)
mappingConfig.setGenerateAsyncApi(generateAsyncApi.value)
mappingConfig.setGenerateParameterizedFieldsResolvers(generateParameterizedFieldsResolvers.value)
mappingConfig.setGenerateDataFetchingEnvironmentArgumentInApis(generateDataFetchingEnvironmentArgumentInApis.value)
mappingConfig.setGenerateExtensionFieldsResolvers(generateExtensionFieldsResolvers.value)
mappingConfig.setGenerateModelsForRootTypes(generateModelsForRootTypes.value)
mappingConfig.setFieldsWithResolvers(fieldsWithResolvers.value)
mappingConfig.setFieldsWithoutResolvers(fieldsWithoutResolvers.value)
mappingConfig.setGenerateClient(generateClient.value)
mappingConfig.setRequestSuffix(requestSuffix.value)
mappingConfig.setResponseSuffix(responseSuffix.value)
mappingConfig.setResponseProjectionSuffix(responseProjectionSuffix.value)
mappingConfig.setParametrizedInputSuffix(parametrizedInputSuffix.value)
mappingConfig.setResolverParentInterface(parentInterfaces.value.resolver)
mappingConfig.setQueryResolverParentInterface(parentInterfaces.value.queryResolver)
mappingConfig.setMutationResolverParentInterface(parentInterfaces.value.mutationResolver)
mappingConfig.setSubscriptionResolverParentInterface(parentInterfaces.value.subscriptionResolver)
mappingConfig.setApiAsyncReturnType(apiAsyncReturnType.value)
mappingConfig.setApiAsyncReturnListType(apiAsyncReturnListType.value.orNull)
mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping.value)
mappingConfig.setPackageName((generatePackageName in GraphQLCodegenConfig).value.orNull)
mappingConfig.setCustomTypesMapping((customTypesMapping in GraphQLCodegenConfig).value)
mappingConfig.setApiNameSuffix((apiNameSuffix in GraphQLCodegenConfig).value)
mappingConfig.setApiNamePrefix((apiNamePrefix in GraphQLCodegenConfig).value.orNull)
mappingConfig.setApiRootInterfaceStrategy((apiRootInterfaceStrategy in GraphQLCodegenConfig).value)
mappingConfig.setApiNamePrefixStrategy((apiNamePrefixStrategy in GraphQLCodegenConfig).value)
mappingConfig.setModelNamePrefix((modelNamePrefix in GraphQLCodegenConfig).value.orNull)
mappingConfig.setModelNameSuffix((modelNameSuffix in GraphQLCodegenConfig).value.orNull)
mappingConfig.setApiPackageName((apiPackageName in GraphQLCodegenConfig).value.orNull)
mappingConfig.setModelPackageName((modelPackageName in GraphQLCodegenConfig).value.orNull)
mappingConfig.setGenerateBuilder((generateBuilder in GraphQLCodegenConfig).value)
mappingConfig.setGenerateApis((generateApis in GraphQLCodegenConfig).value)
mappingConfig.setTypeResolverSuffix((typeResolverSuffix in GraphQLCodegenConfig).value)
mappingConfig.setTypeResolverPrefix((typeResolverPrefix in GraphQLCodegenConfig).value.orNull)
mappingConfig.setModelValidationAnnotation((modelValidationAnnotation in GraphQLCodegenConfig).value)
mappingConfig.setCustomAnnotationsMapping((customAnnotationsMapping in GraphQLCodegenConfig).value)
mappingConfig.setGenerateEqualsAndHashCode((generateEqualsAndHashCode in GraphQLCodegenConfig).value)
mappingConfig.setGenerateImmutableModels((generateImmutableModels in GraphQLCodegenConfig).value)
mappingConfig.setGenerateToString((generateToString in GraphQLCodegenConfig).value)
mappingConfig.setSubscriptionReturnType((subscriptionReturnType in GraphQLCodegenConfig).value.orNull)
mappingConfig.setGenerateAsyncApi((generateAsyncApi in GraphQLCodegenConfig).value)
mappingConfig.setGenerateParameterizedFieldsResolvers((generateParameterizedFieldsResolvers in GraphQLCodegenConfig).value)
mappingConfig.setGenerateDataFetchingEnvironmentArgumentInApis((generateDataFetchingEnvironmentArgumentInApis in GraphQLCodegenConfig).value)
mappingConfig.setGenerateExtensionFieldsResolvers((generateExtensionFieldsResolvers in GraphQLCodegenConfig).value)
mappingConfig.setGenerateModelsForRootTypes((generateModelsForRootTypes in GraphQLCodegenConfig).value)
mappingConfig.setFieldsWithResolvers((fieldsWithResolvers in GraphQLCodegenConfig).value)
mappingConfig.setFieldsWithoutResolvers((fieldsWithoutResolvers in GraphQLCodegenConfig).value)
mappingConfig.setGenerateClient((generateClient in GraphQLCodegenConfig).value)
mappingConfig.setRequestSuffix((requestSuffix in GraphQLCodegenConfig).value)
mappingConfig.setResponseSuffix((responseSuffix in GraphQLCodegenConfig).value)
mappingConfig.setResponseProjectionSuffix((responseProjectionSuffix in GraphQLCodegenConfig).value)
mappingConfig.setParametrizedInputSuffix((parametrizedInputSuffix in GraphQLCodegenConfig).value)
mappingConfig.setResolverParentInterface((parentInterfaces in GraphQLCodegenConfig).value.resolver)
mappingConfig.setQueryResolverParentInterface((parentInterfaces in GraphQLCodegenConfig).value.queryResolver)
mappingConfig.setMutationResolverParentInterface((parentInterfaces in GraphQLCodegenConfig).value.mutationResolver)
mappingConfig.setSubscriptionResolverParentInterface((parentInterfaces in GraphQLCodegenConfig).value.subscriptionResolver)
mappingConfig.setApiAsyncReturnType((apiAsyncReturnType in GraphQLCodegenConfig).value)
mappingConfig.setApiAsyncReturnListType((apiAsyncReturnListType in GraphQLCodegenConfig).value.orNull)
mappingConfig.setDirectiveAnnotationsMapping((directiveAnnotationsMapping in GraphQLCodegenConfig).value)
sLog.value.debug(s"Current mapping config is <$mappingConfig>")
mappingConfig
}
//skip test
override lazy val projectSettings: Seq[Def.Setting[_]] = inConfig(configuration) {
override lazy val projectSettings: Seq[Def.Setting[_]] = inConfig(GraphQLCodegenConfig) {
Seq(
//must use sourceManaged in projectSettings
generateCodegenTargetPath := crossTarget.value / "src_managed_graphql",
sourceManaged := (generateCodegenTargetPath in GraphQLCodegenConfig).value,
javaSource in configuration := (sourceManaged in GraphQLCodegenConfig).value,
managedSourceDirectories in configuration ++= Seq((sourceManaged in GraphQLCodegenConfig).value),
managedClasspath := {
Classpaths.managedJars(GraphQLCodegenConfig, (classpathTypes in GraphQLCodegenConfig).value, (update in GraphQLCodegenConfig).value)
},
outputDir := {
val file = (sourceManaged in graphqlCodegen).value
val file = (javaSource in configuration).value
if (!file.exists()) {
file.mkdirs()
}
sLog.value.info(s"Default outputDir is <${file.getAbsolutePath}>")
file
} //use validate that config in build.sbt
, graphqlCodegenValidate := {
val schemas = if (graphqlSchemaPaths.value.isEmpty) {
Seq((sourceDirectory.value / "resources/schema.graphql").getCanonicalPath).asJava
}, //use validate that config in build.sbt
graphqlCodegenValidate := {
val schemas = if ((graphqlSchemaPaths in GraphQLCodegenConfig).value.isEmpty) {
Seq(((resourceDirectory in configuration).value / "schema.graphql").getCanonicalPath).asJava
} else {
graphqlSchemaPaths.value.asJava
(graphqlSchemaPaths in GraphQLCodegenConfig).value.asJava
}
new GraphQLCodegenValidate(schemas).validate() //use validate at terminal by user
} // use a new src_managed for graphql, and must append to managedSourceDirectories
, sourceManaged in graphqlCodegen := crossTarget.value / "src_managed_graphql" //if generate code successfully but compile failed, reimport project, because ivy cache
, managedSourceDirectories in configuration := {
managedSourceDirectories.value ++ Seq((sourceManaged in graphqlCodegen).value)
}, javaSource := {
(sourceManaged in graphqlCodegen).value
}, managedClasspath := {
Classpaths.managedJars(configuration, classpathTypes.value, update.value)
}, graphqlSchemaValidate := {
},
graphqlSchemaValidate := {
//use by user
val args: Seq[String] = spaceDelimited("<arg>").parsed
new GraphQLCodegenValidate(args.asJava).validate()
args.foreach(a sLog.value.info(s"Obtain args <$a>"))
args
}, graphqlCodegen := {
val mappingConfigSupplier: JsonMappingConfigSupplier = buildJsonSupplier(jsonConfigurationFile.value.orNull)
val mappingConfigSupplier: JsonMappingConfigSupplier = buildJsonSupplier((jsonConfigurationFile in GraphQLCodegenConfig).value.orNull)
var result: Seq[File] = Seq.empty
try {
result = new GraphQLCodegen(getSchemas, outputDir.value, getMappingConfig().value, mappingConfigSupplier).generate.asScala
result = new GraphQLCodegen(getSchemas, (outputDir in GraphQLCodegenConfig).value, getMappingConfig().value, mappingConfigSupplier).generate.asScala
for (file result) {
sLog.value.success(s"${file.getName}")
}
......@@ -198,17 +200,18 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
}
def getSchemas: util.List[String] = {
if (graphqlSchemaPaths != null && graphqlSchemaPaths.value.nonEmpty) return graphqlSchemaPaths.value.asJava
if ((graphqlSchemaPaths in GraphQLCodegenConfig).value != null && (graphqlSchemaPaths in GraphQLCodegenConfig).value.nonEmpty)
return (graphqlSchemaPaths in GraphQLCodegenConfig).value.asJava
val schemasRootDir: Path = getSchemasRootDir
val finder: SchemaFinder = new SchemaFinder(schemasRootDir)
finder.setRecursive(graphqlSchemas.value.recursive)
finder.setIncludePattern(graphqlSchemas.value.includePattern)
finder.setExcludedFiles(graphqlSchemas.value.excludedFiles.asJava)
finder.setRecursive((graphqlSchemas in GraphQLCodegenConfig).value.recursive)
finder.setIncludePattern((graphqlSchemas in GraphQLCodegenConfig).value.includePattern)
finder.setExcludedFiles((graphqlSchemas in GraphQLCodegenConfig).value.excludedFiles.asJava)
finder.findSchemas
}
def getSchemasRootDir: Path = {
val rootDir = graphqlSchemas.value.rootDir
val rootDir = (graphqlSchemas in GraphQLCodegenConfig).value.rootDir
if (rootDir == null) {
val default = getDefaultResourcesDirectory
if (default == null) throw new IllegalStateException("Default resource folder not found, please provide <rootDir> in <graphqlSchemas>")
......@@ -218,7 +221,7 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
}
def getDefaultResourcesDirectory: Path = {
val file = sourceDirectory.value / "resources"
val file = (resourceDirectory in configuration).value
if (!file.exists()) {
file.mkdirs()
}
......@@ -230,7 +233,7 @@ class GraphQLCodegenPlugin(configuration: Configuration) extends AutoPlugin with
result
}
//watch graphql schema source
) ++ watchSourcesSetting
) ++ watchSourcesSetting ++ Seq(cleanFiles += (generateCodegenTargetPath in GraphQLCodegenConfig).value)
}
private def buildJsonSupplier(jsonConfigurationFile: String): JsonMappingConfigSupplier = {
......
......@@ -50,4 +50,6 @@ customAnnotationsMapping := {
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")})""".stripMargin)
mapping
}
\ No newline at end of file
}
generateCodegenTargetPath in GraphQLCodegenConfig := crossTarget.value / "src_managed_graphql_my_folder"
\ No newline at end of file
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "2.4.2-SNAPSHOT")
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "3.0.0-SNAPSHOT")
......@@ -15,22 +15,23 @@ import java.util.regex.Pattern;
*/
public class EmailScalar {
public static final GraphQLScalarType Email = new GraphQLScalarType("Email", "A custom scalar that handles emails", new Coercing() {
@Override
public Object serialize(Object dataFetcherResult) {
return serializeEmail(dataFetcherResult);
}
public static final GraphQLScalarType Email = GraphQLScalarType.newScalar().name("Email").description("A custom scalar that handles emails").coercing(
new Coercing() {
@Override
public Object serialize(Object dataFetcherResult) {
return serializeEmail(dataFetcherResult);
}
@Override
public Object parseValue(Object input) {
return parseEmailFromVariable(input);
}
@Override
public Object parseValue(Object input) {
return parseEmailFromVariable(input);
}
@Override
public Object parseLiteral(Object input) {
return parseEmailFromAstLiteral(input);
}
});
@Override
public Object parseLiteral(Object input) {
return parseEmailFromAstLiteral(input);
}
}).build();
private static boolean looksLikeAnEmailAddress(String possibleEmailValue) {
......
......@@ -3,4 +3,4 @@
> graphqlSchemaValidate src/main/resources/schema.graphqls
> graphqlCodegenValidate
> compile
$ exists target/scala-2.12/src_managed_graphql/io/github/dreamylost/model/CharacterDO.java
\ No newline at end of file
$ exists target/scala-2.12/src_managed_graphql_my_folder/io/github/dreamylost/model/CharacterDO.java
\ No newline at end of file
......@@ -10,3 +10,6 @@ lazy val root = (project in file("."))
generateAsyncApi := true
//use full class name is good
).enablePlugins(GraphQLCodegenPlugin).settings(GraphQLCodegenPluginDependencies)
//javaSource in Compile := crossTarget.value / "src_managed_graphql"
sys.props.get("plugin.version").orElse(Some("2.4.2-SNAPSHOT")) match {
sys.props.get("plugin.version").orElse(Some("3.0.0-SNAPSHOT")) match {
case Some(x) => addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
......
version in ThisBuild := "2.4.2-SNAPSHOT"
version in ThisBuild := "3.0.0-SNAPSHOT"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册