Skip to content

Commit fc026b9

Browse files
authored
Support Kotlin source code generation (#12)
* Support Kotlin source code generation * Test Kotlin source code generation on CI * Format * Add descriptions about Kotlin
1 parent 8a4b62e commit fc026b9

File tree

71 files changed

+3109
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3109
-143
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ jobs:
3131
working-directory: ./codegen
3232
run: ./gradlew build
3333

34-
- name: Test plugin
34+
- name: Test plugin (generate Java code)
3535
working-directory: ./codegen-test
36-
run: ./gradlew domaCodeGenDevAll build
36+
run: ./gradlew domaCodeGenJavaAll build
37+
38+
- name: Test plugin (generate Kotlin code)
39+
working-directory: ./codegen-test
40+
run: ./gradlew domaCodeGenKotlinAll build
3741

3842
- name: Set version
3943
id: set-version

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Doma CodeGen Plugin
22
===================
33

44
Doma CodeGen Plugin is a gradle plugin.
5-
It generates Java source files and SQL files from Database.
5+
It generates Java, Kotlin, and SQL files from Database.
66

77
[![Java CI with Gradle](https://github.com/domaframework/doma-codegen-plugin/workflows/Java%20CI%20with%20Gradle/badge.svg)](https://github.com/domaframework/doma-codegen-plugin/actions?query=workflow%3A%22Java+CI+with+Gradle%22)
88
[![Google Group : doma-user](https://img.shields.io/badge/Google%20Group-doma--user-orange.svg)](https://groups.google.com/g/doma-user)
@@ -119,11 +119,13 @@ domaCodeGen {
119119
| ignoredTableNamePattern | database ignored table pattern (Regex) | | `.*\$.*` |
120120
| tableTypes | database table type | such as TABLE, VIEW and so on | `TABLE` |
121121
| versionColumnNamePattern | database version column pattern (Regex) | | `VERSION([_]?NO)?` |
122+
| languageType | language of generation code | `org.seasar.doma.gradle.codegen.desc.LanguageType.JAVA`, `org.seasar.doma.gradle.codegen.desc.LanguageType.KOTLIN` | `org.seasar.doma.gradle.codegen.desc.LanguageType.JAVA` |
123+
| languageClassResolver | class resolver for language dedicated classes | | depends on `languageType` |
122124
| templateEncoding | encoding for freeMarker template files | | `UTF-8` |
123125
| templateDir | directory for user customized template files | | |
124126
| encoding | encoding for generated Java source files | | `UTF-8` |
125-
| sourceDir | directory for generated Java source files | | `src/main/java` |
126-
| testSourceDir | directory for generated Java test source files | | `src/test/java` |
127+
| sourceDir | directory for generated Java source files | | depends on `languageType` |
128+
| testSourceDir | directory for generated Java test source files | | depends on `languageType` |
127129
| resourceDir | directory for generated SQL files | | src/main/resources |
128130
| globalFactory | entry point to customize plugin behavior | | `new org.seasar.doma.gradle.codegen.GlobalFactory()` |
129131

@@ -209,6 +211,32 @@ domaCodeGen {
209211
Customization
210212
-------------
211213

214+
### Generating Kotlin code
215+
216+
To generate Kotlin code, specify `LanguageType.KOTLIN` to the languageType option as follows:
217+
218+
```groovy
219+
import org.seasar.doma.gradle.codegen.desc.LanguageType
220+
221+
...
222+
223+
domaCodeGen {
224+
dev {
225+
url = '...'
226+
user = '...'
227+
password = '...'
228+
languageType = LanguageType.KOTLIN
229+
entity {
230+
packageName = 'org.example.entity'
231+
}
232+
dao {
233+
packageName = 'org.example.dao'
234+
}
235+
}
236+
}
237+
```
238+
239+
212240
### Using custom template files
213241

214242
Default template files are located in the source code repository of the Doma CodeGen Plugin.

codegen-test/build.gradle

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.h2.Driver
2+
import org.seasar.doma.gradle.codegen.desc.LanguageType
23
import org.seasar.doma.gradle.codegen.jdbc.SimpleDataSource
34

45
buildscript {
@@ -17,6 +18,7 @@ buildscript {
1718

1819
plugins {
1920
id 'java'
21+
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
2022
id 'org.seasar.doma.compile' version '1.1.0'
2123
}
2224

@@ -49,7 +51,7 @@ def _user = ''
4951
def _password = ''
5052

5153
domaCodeGen {
52-
dev {
54+
java {
5355
def basePackage = 'codegen'
5456
url = _url.toString()
5557
user = _user
@@ -61,6 +63,19 @@ domaCodeGen {
6163
packageName = "${basePackage}.dao"
6264
}
6365
}
66+
kotlin {
67+
def basePackage = 'codegen'
68+
url = _url.toString()
69+
user = _user
70+
password = _password
71+
languageType = LanguageType.KOTLIN
72+
entity {
73+
packageName = "${basePackage}.entity"
74+
}
75+
dao {
76+
packageName = "${basePackage}.dao"
77+
}
78+
}
6479
}
6580

6681
task deleteSrc {
@@ -100,4 +115,5 @@ task createDb {
100115
}
101116
}
102117

103-
tasks.getByName('domaCodeGenDevDbMeta').dependsOn(deleteSrc, createDb)
118+
tasks.getByName('domaCodeGenJavaDbMeta').dependsOn(deleteSrc, createDb)
119+
tasks.getByName('domaCodeGenKotlinDbMeta').dependsOn(deleteSrc, createDb)
-3.42 KB
Binary file not shown.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Sat Apr 04 07:31:40 JST 2020
2-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
31
distributionBase=GRADLE_USER_HOME
42
distributionPath=wrapper/dists
5-
zipStorePath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-bin.zip
64
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

codegen-test/gradlew

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
#!/usr/bin/env sh
22

3-
#
4-
# Copyright 2015 the original author or authors.
5-
#
6-
# Licensed under the Apache License, Version 2.0 (the "License");
7-
# you may not use this file except in compliance with the License.
8-
# You may obtain a copy of the License at
9-
#
10-
# https://www.apache.org/licenses/LICENSE-2.0
11-
#
12-
# Unless required by applicable law or agreed to in writing, software
13-
# distributed under the License is distributed on an "AS IS" BASIS,
14-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
# See the License for the specific language governing permissions and
16-
# limitations under the License.
17-
#
18-
193
##############################################################################
204
##
215
## Gradle start up script for UN*X
@@ -44,7 +28,7 @@ APP_NAME="Gradle"
4428
APP_BASE_NAME=`basename "$0"`
4529

4630
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
31+
DEFAULT_JVM_OPTS='"-Xmx64m"'
4832

4933
# Use the maximum available, or set MAX_FD != -1 to use that value.
5034
MAX_FD="maximum"
@@ -125,8 +109,8 @@ if $darwin; then
125109
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
126110
fi
127111

128-
# For Cygwin or MSYS, switch paths to Windows format before running java
129-
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
112+
# For Cygwin, switch paths to Windows format before running java
113+
if $cygwin ; then
130114
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
131115
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
132116
JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -154,19 +138,19 @@ if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
154138
else
155139
eval `echo args$i`="\"$arg\""
156140
fi
157-
i=`expr $i + 1`
141+
i=$((i+1))
158142
done
159143
case $i in
160-
0) set -- ;;
161-
1) set -- "$args0" ;;
162-
2) set -- "$args0" "$args1" ;;
163-
3) set -- "$args0" "$args1" "$args2" ;;
164-
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165-
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166-
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167-
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168-
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169-
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
144+
(0) set -- ;;
145+
(1) set -- "$args0" ;;
146+
(2) set -- "$args0" "$args1" ;;
147+
(3) set -- "$args0" "$args1" "$args2" ;;
148+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
170154
esac
171155
fi
172156

@@ -175,9 +159,14 @@ save () {
175159
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
176160
echo " "
177161
}
178-
APP_ARGS=`save "$@"`
162+
APP_ARGS=$(save "$@")
179163

180164
# Collect all arguments for the java command, following the shell quoting and substitution rules
181165
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
182166

167+
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168+
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169+
cd "$(dirname "$0")"
170+
fi
171+
183172
exec "$JAVACMD" "$@"

codegen-test/gradlew.bat

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
@rem
2-
@rem Copyright 2015 the original author or authors.
3-
@rem
4-
@rem Licensed under the Apache License, Version 2.0 (the "License");
5-
@rem you may not use this file except in compliance with the License.
6-
@rem You may obtain a copy of the License at
7-
@rem
8-
@rem https://www.apache.org/licenses/LICENSE-2.0
9-
@rem
10-
@rem Unless required by applicable law or agreed to in writing, software
11-
@rem distributed under the License is distributed on an "AS IS" BASIS,
12-
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
@rem See the License for the specific language governing permissions and
14-
@rem limitations under the License.
15-
@rem
16-
171
@if "%DEBUG%" == "" @echo off
182
@rem ##########################################################################
193
@rem
@@ -29,11 +13,8 @@ if "%DIRNAME%" == "" set DIRNAME=.
2913
set APP_BASE_NAME=%~n0
3014
set APP_HOME=%DIRNAME%
3115

32-
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
33-
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34-
3516
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36-
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
17+
set DEFAULT_JVM_OPTS="-Xmx64m"
3718

3819
@rem Find java.exe
3920
if defined JAVA_HOME goto findJavaFromJavaHome

codegen/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ gradlePlugin {
1010
compilePlugin {
1111
id = 'org.seasar.doma.codegen'
1212
displayName = 'Doma Codegen Plugin'
13-
description = 'Generates Java source files and SQL files from Database'
13+
description = 'Generates Java, Kotlin, and SQL files from Database'
1414
implementationClass = 'org.seasar.doma.gradle.codegen.CodeGenPlugin'
1515
}
1616
}

codegen/src/main/groovy/org/seasar/doma/gradle/codegen/desc/EntityPropertyDescMerger.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ class EntityPropertyDescMerger {
9292
case SEQUENCE:
9393
dest.setGenerationType(GenerationType.SEQUENCE);
9494
if (src.sequenceGenerator != null) {
95-
dest.setInitialValue(src.sequenceGenerator.initialValue());
95+
dest.setInitialValue(src.sequenceGenerator.getDefaultValue());
9696
dest.setAllocationSize(src.sequenceGenerator.allocationSize());
9797
dest.setAllocationSize(src.sequenceGenerator.allocationSize());
9898
}
9999
break;
100100
case TABLE:
101101
dest.setGenerationType(GenerationType.TABLE);
102102
if (src.tableGenerator != null) {
103-
dest.setInitialValue(src.tableGenerator.initialValue());
103+
dest.setInitialValue(src.tableGenerator.getDefaultValue());
104104
dest.setAllocationSize(src.tableGenerator.allocationSize());
105105
dest.setAllocationSize(src.tableGenerator.allocationSize());
106106
}

codegen/src/main/java/org/seasar/doma/gradle/codegen/CodeGenPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ private void connectProperties(CodeGenDtoTask task, CodeGenConfig extension) {
147147
task.getTableNamePattern().set(extension.getTableNamePattern());
148148
task.getIgnoredTableNamePattern().set(extension.getIgnoredTableNamePattern());
149149
task.getVersionColumnNamePattern().set(extension.getVersionColumnNamePattern());
150+
task.getLanguageType().set(extension.getLanguageType());
151+
task.getLanguageClassResolver().set(extension.getLanguageClassResolver());
150152
task.getSourceDir().set(extension.getSourceDir());
151153
task.getEncoding().set(extension.getEncoding());
152154
task.setEntityConfig(extension.getEntityConfig());
@@ -161,12 +163,14 @@ private void connectProperties(CodeGenEntityDescTask task, CodeGenConfig extensi
161163
task.getTableNamePattern().set(extension.getTableNamePattern());
162164
task.getIgnoredTableNamePattern().set(extension.getIgnoredTableNamePattern());
163165
task.getVersionColumnNamePattern().set(extension.getVersionColumnNamePattern());
166+
task.getLanguageClassResolver().set(extension.getLanguageClassResolver());
164167
task.setEntityConfig(extension.getEntityConfig());
165168
}
166169

167170
private void connectProperties(CodeGenEntityTask task, CodeGenConfig extension) {
168171
task.getGlobalFactory().set(extension.getGlobalFactory());
169172
task.getGenerator().set(extension.getGenerator());
173+
task.getLanguageType().set(extension.getLanguageType());
170174
task.getSourceDir().set(extension.getSourceDir());
171175
task.getEncoding().set(extension.getEncoding());
172176
task.setEntityConfig(extension.getEntityConfig());
@@ -179,6 +183,7 @@ private void connectProperties(CodeGenDaoDescTask task, CodeGenConfig extension)
179183

180184
private void connectProperties(CodeGenDaoTask task, CodeGenConfig extension) {
181185
task.getGenerator().set(extension.getGenerator());
186+
task.getLanguageType().set(extension.getLanguageType());
182187
task.getSourceDir().set(extension.getSourceDir());
183188
task.getEncoding().set(extension.getEncoding());
184189
task.setDaoConfig(extension.getDaoConfig());
@@ -200,6 +205,7 @@ private void connectProperties(CodeGenSqlTestTask task, CodeGenConfig extension)
200205
task.getPassword().set(extension.getPassword());
201206
task.getUrl().set(extension.getUrl());
202207
task.getGenerator().set(extension.getGenerator());
208+
task.getLanguageType().set(extension.getLanguageType());
203209
task.getTestSourceDir().set(extension.getTestSourceDir());
204210
task.getEncoding().set(extension.getEncoding());
205211
task.setSqlTestConfig(extension.getSqlTestConfig());

0 commit comments

Comments
 (0)