Skip to content

Commit 56b9dfe

Browse files
sbt plugin support #232 (#235)
* add sbt plugin * rm log to file * update * update readme * update readme * update readme * update client example * update client example * update log * update client example * add scalaiform * add scalaiform and publish * support schemaFinderConfig and parentInterfacesConfig * update readme license, convert GraphQLCodegen to Def * update readme * Create scala.yml * update scala ci * update scala ci * rm scala ci * rm scala ci * use publishLocal * ci support ivy cache * ci support ivy cache * fix support ivy cache * change example project level * change example project level * fix path for ci * fix workflows * move generate code to src_managed_graphql * change generate code location to src_managed_graphql * add sbt-test for plugin * fix workflows * fix workflows * change generate code location to src_managed_graphql * add watch resource, refactor plugins * add developer for publish * add key apiAsyncReturnType and apiAsyncReturnListType, refactor code, update sbt-test with two options * add resolvers * add resolvers for ci * add resolvers for ci * add resolvers for ci * Remove redundant dependencies * fix support ivy local * Update github.yml * fix support ivy local * update ci * fix compile failed when generate code not found in classpath. test in the standard way * Update github.yml * 1. update default option code 2. update client example 3. update version to 2.3.0 * update version to 2.3.0 * Merge branch 'master' of github.com:jxnu-liguobin/graphql-java-codegen * pre release 2.4.0 * next dev 2.4.1-SNAPSHOT fix example support with v2.4.0 add typename
1 parent 8e7b084 commit 56b9dfe

33 files changed

Lines changed: 1107 additions & 1 deletion

File tree

.github/workflows/github.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ jobs:
3636
path: ~/.m2
3737
key: m2
3838

39+
- name: Loading ivy cache
40+
uses: actions/cache@v1
41+
with:
42+
path: ~/.ivy2/cache
43+
key: ${{ runner.os }}-ivy-${{ hashFiles('**/*.sbt') }}
44+
restore-keys: |
45+
${{ runner.os }}-ivy-
3946
4047
- name: Build library
41-
run: ./gradlew build publishToMavenLocal --warning-mode all
48+
run: ./gradlew build publishToMavenLocal publishAllPublicationsToIvyRepository --warning-mode all
4249

4350
- name: Build gradle plugin
4451
run: ./gradlew -p plugins/gradle/graphql-java-codegen-gradle-plugin build publishToMavenLocal --warning-mode all
@@ -47,7 +54,13 @@ jobs:
4754
working-directory: plugins/maven/graphql-java-codegen-maven-plugin
4855
run: mvn install
4956

57+
- name: Build sbt plugin
58+
working-directory: plugins/sbt/graphql-java-codegen-sbt-plugin
59+
run: sbt compile publishLocal --debug
5060

61+
- name: Build sbt test
62+
working-directory: plugins/sbt/graphql-java-codegen-sbt-plugin
63+
run: sbt scripted
5164

5265
- name: Build gradle example-server --warning-mode all
5366
run: ./gradlew -p plugins/gradle/example-server test --warning-mode all
@@ -63,6 +76,7 @@ jobs:
6376
working-directory: plugins/maven/example-client
6477
run: mvn package
6578

79+
6680
sonar:
6781
needs: build
6882
runs-on: ubuntu-latest
@@ -86,6 +100,14 @@ jobs:
86100
path: ~/.m2
87101
key: m2
88102

103+
- name: Loading ivy cache
104+
uses: actions/cache@v1
105+
with:
106+
path: ~/.ivy2/cache
107+
key: ${{ runner.os }}-ivy-${{ hashFiles('**/*.sbt') }}
108+
restore-keys: |
109+
${{ runner.os }}-ivy-
110+
89111
- name: Generate code coverage report
90112
run: ./gradlew codeCoverageReport --stacktrace
91113

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ publishing {
131131
password sonatypePassword
132132
}
133133
}
134+
ivy {
135+
url "${System.properties['user.home']}/.ivy2/local"
136+
patternLayout {
137+
artifact '[organisation]/[module]/[revision]/[ext]s/[artifact](-[classifier])(.[ext])'
138+
// for ci and local, must publish to ivy2 local, can be used for sbt plugin
139+
ivy '[organisation]/[module]/[revision]/[type]s/[artifact](.[ext])'
140+
}
141+
}
134142
}
135143
}
136144

plugins/sbt/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# GraphQL Codegen SBT plugin #
2+
3+
This is a sbt plugin for https://github.com/kobylynskyi/graphql-java-codegen
4+
5+
Server example at https://github.com/jxnu-liguobin/springboot-examples/tree/master/graphql-complete (do not use plugin, only a normal graphql server )
6+
7+
8+
![Build](https://github.com/kobylynskyi/graphql-java-codegen/workflows/Build/badge.svg)
9+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.jxnu-liguobin/graphql-codegen-sbt-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.jxnu-liguobin/graphql-codegen-sbt-plugin)
10+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11+
12+
13+
### Plugin Setup
14+
15+
16+
```scala
17+
// plugins.sbt
18+
addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "<version>")
19+
20+
//since graphql-java-codegen V2.2.1
21+
```
22+
23+
### Config
24+
25+
26+
```scala
27+
// build.sbt
28+
29+
30+
enablePlugins(GraphQLCodegenPlugin)
31+
32+
33+
GraphQLCodegenPluginDependencies
34+
35+
//default graphqlJavaCodegen is release
36+
graphqlJavaCodegenVersion := Some("2.2.2-SNAPSHOT")
37+
graphqlSchemaPaths := List("src/main/resources/schema.graphqls")
38+
modelPackageName := Some("io.github.dreamylost.model")
39+
apiPackageName := Some("io.github.dreamylost.api")
40+
generateClient := false
41+
generateApis := true
42+
// Scala collection cannot be used. The latter one uses the put method, which is not supported by Scala collection.
43+
// in FB, collection is immutable
44+
customTypesMapping := {
45+
val mapping = new util.HashMap[String, String]
46+
mapping.put("Email", "io.github.dreamylost.scalar.EmailScalar")
47+
//Character will conflict with java.lang.Character. maybe because Scala imports it automatically java.lang *.
48+
//So we use Full class name
49+
mapping
50+
}
51+
52+
//Of course, you can also add a suffix to be different from it
53+
modelNameSuffix := Some("DO")
54+
55+
customAnnotationsMapping := {
56+
val mapping = new util.HashMap[String, String]
57+
//must add this annotation
58+
//property is __typename and you must with __typename while invoke, like new CharacterResponseProjection().id().name().typename()
59+
//and in @JsonSubTypes.Type, name is __typename's value
60+
mapping.put("Character",
61+
s"""com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = "__typename")${System.lineSeparator()}@com.fasterxml.jackson.annotation.JsonSubTypes(value = {
62+
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = HumanDO.class, name = "Human"),
63+
| @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidDO.class, name = "Droid")})
64+
|""".stripMargin)
65+
mapping
66+
}
67+
```
68+
69+
### Codegen Options
70+
71+
72+
SBT task
73+
74+
1. graphqlSchemaValidate
75+
- use validate at terminal by user, can get args from terminal, such as `graphqlSchemaValidate src/main/resources/schema.graphqls`, args split with space
76+
2. graphqlCodegen
77+
- generate java code from graphql schema
78+
3. graphqlCodegenValidate
79+
- use validate schemas that config in build.sbt key: `graphqlSchemaPaths`
80+
81+
82+
### Plugin Options
83+
84+
85+
Please refer to [Codegen Options](../../docs/codegen-options.md)
86+
87+
> in sbt plugin option `packageName` was rename to `generatePackageName`
88+
89+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
autoformat = true
2+
withBaseDirectory = true
3+
4+
// Preferences
5+
alignParameters = true
6+
newlineAtEndOfFile = true
7+
rewriteArrowSymbols = false
8+
allowParamGroupsOnNewlines = true
9+
danglingCloseParenthesis = Preserve
10+
alignSingleLineCaseStatements = true
11+
doubleIndentConstructorArguments = true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Dependencies._
2+
3+
name := "graphql-codegen-sbt-plugin"
4+
// must be equals to oss Group Id
5+
organization := "io.github.jxnu-liguobin"
6+
7+
// publish only root project
8+
//publish / skip := true
9+
10+
//keep version is equals with parent project `graphql-java-codegen`
11+
lazy val `graphql-codegen-sbt-plugin` = Project(id = "graphql-codegen-sbt-plugin", base = file(".")).
12+
enablePlugins(SbtPlugin).
13+
settings(Publishing.publishSettings).
14+
settings(
15+
sbtPlugin := true,
16+
scalaVersion := Versions.scala212,
17+
crossScalaVersions := List(Versions.scala212, Versions.scala211),
18+
scalacOptions += "-target:jvm-1.8").
19+
settings(Compiles.selfDependencies)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sbt.Keys.libraryDependencies
2+
import sbt._
3+
4+
/**
5+
* The dependence of the plugin itself
6+
*
7+
* @author 梦境迷离 dreamylost
8+
* @since 2020-07-19
9+
* @version v1.0
10+
*/
11+
object Dependencies {
12+
13+
object Versions {
14+
lazy val scala212 = "2.12.12"
15+
lazy val scala211 = "2.11.12"
16+
val codegen = "2.4.0"
17+
}
18+
19+
import Versions._
20+
21+
object Compiles {
22+
val selfDependencies = libraryDependencies ++= Seq(
23+
"io.github.kobylynskyi" % "graphql-java-codegen" % codegen
24+
)
25+
}
26+
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sbt._
2+
import sbt.Keys._
3+
import xerial.sbt.Sonatype.autoImport.sonatypeProfileName
4+
5+
/**
6+
* sbt publish setting
7+
*
8+
* @author 梦境迷离 dreamylost
9+
* @since 2020-07-19
10+
* @version v1.0
11+
*/
12+
object Publishing {
13+
14+
//publish by sbt publishSigned
15+
lazy val publishSettings = Seq(
16+
credentials += Credentials(Path.userHome / ".ivy2" / ".sonatype_credentials"),
17+
publishTo := {
18+
val nexus = "https://oss.sonatype.org/"
19+
if (isSnapshot.value)
20+
Some("snapshots" at nexus + "content/repositories/snapshots")
21+
else
22+
Some("releases" at nexus + "service/local/staging/deploy/maven2")
23+
},
24+
licenses := Seq("MIT" -> url("https://opensource.org/licenses/MIT")),
25+
publishMavenStyle := true,
26+
publishArtifact in Test := false,
27+
pomIncludeRepository := { _ => false },
28+
developers := List(
29+
Developer(
30+
id = "dreamylost",
31+
name = "梦境迷离",
32+
email = "[email protected]",
33+
url = url("https://dreamylost.cn")
34+
)),
35+
sonatypeProfileName := organization.value,
36+
isSnapshot := version.value endsWith "SNAPSHOT",
37+
homepage := Some(url("https://github.com/jxnu-liguobin")),
38+
scmInfo := Some(
39+
ScmInfo(
40+
//it is fork from https://github.com/kobylynskyi/graphql-java-codegen
41+
url("https://github.com/jxnu-liguobin/graphql-java-codegen"),
42+
"scm:[email protected]:jxnu-liguobin/graphql-java-codegen.git"
43+
))
44+
)
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.3.1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.3")
2+
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
3+
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.dreamylost.graphql.codegen
2+
3+
import sbt.Keys.{ sourceDirectory, watchSources }
4+
import sbt.Watched.WatchSource
5+
import sbt.internal.io.Source
6+
import sbt.io.{ AllPassFilter, SuffixFilter }
7+
import sbt.{ Def, Task }
8+
9+
/**
10+
*
11+
* @author 梦境迷离 dreamylost
12+
* @since 2020-07-18
13+
* @version v1.0
14+
*/
15+
trait Compat {
16+
self: GraphQLCodegenPlugin =>
17+
18+
import self.GlobalImport._
19+
20+
val watchSourcesSetting: Def.Setting[Task[Seq[WatchSource]]] = {
21+
watchSources += new Source(
22+
(sourceDirectory in graphqlCodegen).value,
23+
new SuffixFilter(".graphql") | new SuffixFilter(".graphqls"),
24+
AllPassFilter)
25+
}
26+
27+
}

0 commit comments

Comments
 (0)