Skip to content

Commit 1f50207

Browse files
wing328jimschubert
andauthored
[doc] Update new-generator steps (new.sh) (#7336)
* [doc] Update new-generator steps (new.sh) (#7334) * remove CommonMarkDocumentationCodegen Co-authored-by: Jim Schubert <james.schubert@gmail.com>
1 parent e0ec332 commit 1f50207

2 files changed

Lines changed: 38 additions & 117 deletions

File tree

docs/new-generator.md

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The minimum set of files required to create a new generator are:
2323
- Should include a README explaining usage
2424
- Must include an `api.mustache`
2525
- Exists under `modules/openapi-generator/src/main/resources/` (plus `embeddedTemplate` dir value, see below)
26-
* Sample scripts under `./bin`
26+
* Config file under `./bin/configs`
2727
- Gives users a "real life" example of generated output
2828
- Samples are used by CI to verify generators and test for regressions in some cases
2929

@@ -43,6 +43,8 @@ Usage:
4343
-c Create a client generator
4444
-s Create a server generator
4545
-d Create a documentation generator
46+
-H Create a schema generator
47+
-f Create a config generator
4648
-t When specified, creates test file(s) for the generator.
4749
-h Display help.
4850
@@ -55,8 +57,7 @@ Examples:
5557
modules/openapi-generator/src/main/resources/kotlin-server/README.mustache
5658
modules/openapi-generator/src/main/resources/kotlin-server/model.mustache
5759
modules/openapi-generator/src/main/resources/kotlin-server/api.mustache
58-
bin/windows/kotlin-server-petstore.bat
59-
bin/kotlin-server-petstore.sh
60+
bin/configs/kotlin-server-petstore-new.yaml
6061
6162
Create a generic C# server generator:
6263
./new.sh -n csharp -s -t
@@ -65,8 +66,7 @@ Examples:
6566
modules/openapi-generator/src/main/resources/csharp-server/README.mustache
6667
modules/openapi-generator/src/main/resources/csharp-server/model.mustache
6768
modules/openapi-generator/src/main/resources/csharp-server/api.mustache
68-
bin/windows/csharp-server-petstore.bat
69-
bin/csharp-server-petstore.sh
69+
bin/configs/csharp-server-petstore-new.yaml
7070
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenTest.java
7171
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenModelTest.java
7272
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenOptionsTest.java
@@ -88,8 +88,7 @@ Creating modules/openapi-generator/src/main/java/org/openapitools/codegen/langua
8888
Creating modules/openapi-generator/src/main/resources/common-mark-documentation/README.mustache
8989
Creating modules/openapi-generator/src/main/resources/common-mark-documentation/model.mustache
9090
Creating modules/openapi-generator/src/main/resources/common-mark-documentation/api.mustache
91-
Creating bin/windows/common-mark-documentation-petstore.bat
92-
Creating bin/common-mark-documentation-petstore.sh
91+
Creating bin/configs/common-mark-documentation-petstore-new.yaml
9392
Finished.
9493
```
9594

@@ -151,15 +150,15 @@ The `templateDir` variable refers to the "current" template directory setting, a
151150
Both of these variables exist because the generator will fallback to files under `embeddedTemplateDir` if they are not defined in the user's custom template directory.
152151

153152
```java
154-
apiPackage = File.separator + "Apis";
153+
apiPackage = "Apis";
155154
```
156155

157156
This sets the "package" location for anything considered an API document. You might want to change this setting if, for instance, your language doesn't support uppercase letters in the path. We don't need to worry about that here.
158157

159158
Every templated output from `api.mustache` (registered via `apiTemplateFiles` above) will end up in the directory defined by `apiPackage` here.
160159

161160
```java
162-
modelPackage = File.separator + "Models";
161+
modelPackage = "Models";
163162
```
164163

165164
Similarly, this sets the package for `Models`.
@@ -330,61 +329,29 @@ To compile quickly to test this out, you can run `mvn clean package -DskipTests`
330329
331330
### Compile Sample
332331

333-
The `new.sh` script created `bin/common-mark-documentation-petstore.sh`:
332+
The `new.sh` script created the generation config file `bin/configs/common-mark-documentation-petstore-new.yaml`:
334333

335334
```bash
336-
#!/bin/sh
337-
338-
SCRIPT="$0"
339-
340-
while [ -h "$SCRIPT" ] ; do
341-
ls=$(ls -ld "$SCRIPT")
342-
link=$(expr "$ls" : '.*-> \(.*\)$')
343-
if expr "$link" : '/.*' > /dev/null; then
344-
SCRIPT="$link"
345-
else
346-
SCRIPT=$(dirname "$SCRIPT")/"$link"
347-
fi
348-
done
349-
350-
if [ ! -d "${APP_DIR}" ]; then
351-
APP_DIR=$(dirname "$SCRIPT")/..
352-
APP_DIR=$(cd "${APP_DIR}"; pwd)
353-
fi
354-
355-
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
356-
357-
if [ ! -f "$executable" ]
358-
then
359-
mvn clean package
360-
fi
361-
362-
# if you've executed sbt assembly previously it will use that instead.
363-
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
364-
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g common-mark -o samples/documentation/petstore/common/mark"
365-
366-
java ${JAVA_OPTS} -jar ${executable} ${ags}
335+
generatorName: common-mark
336+
outputDir: samples/documentation/petstore/common/mark
337+
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
338+
templateDir: modules/openapi-generator/src/main/resources/common-mark
339+
additionalProperties:
340+
hideGenerationTimestamp: "true"
367341
```
368342

369-
This script is often used to apply default options for generation. A common option in most of these script is to define the template directory as the generator's directory under `resources`. This allows template maintainers to modify and test out template changes which don't require recompilation of the entire project. You'd still need to recompile the project in full if you add or modify behaviors to the generator (such as adding a `CliOption`).
370-
371-
Add `-t modules/openapi-generator/src/main/resources/common-mark-documentation` to `ags` line to simplify the evaluation of template-only modifications:
343+
This configuration file is passed to the generator's CLI tool during continuous integration builds, and many outputs are compiled and tested as a regression test on every build. Contributors are also asked to run `./bin/utils/ensure-up-to-date` before opening a pull request to regenerate all samples defined under `./bin/configs`. This allows maintainers to quickly verify whether changes impact other generators.
372344

373-
```diff
374-
diff --git a/bin/markdown-documentation-petstore.sh b/bin/markdown-documentation-petstore.sh
375-
index d816771478..94b4ce6d12 100644
376-
--- a/bin/markdown-documentation-petstore.sh
377-
+++ b/bin/markdown-documentation-petstore.sh
378-
@@ -26,6 +26,6 @@ fi
345+
Configuration based examples allow us to test the same samples in each tooling option (CLI, Gradle Plugin, Maven Plugin, etc.).
379346

380-
# if you've executed sbt assembly previously it will use that instead.
381-
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
382-
-ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g common-mark -o samples/documentation/petstore/common-mark"
383-
+ags="$@ generate -t modules/openapi-generator/src/main/resources/common-mark-documentation -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g common-mark -o samples/documentation/petstore/common/markdown"
347+
You can compile your generator by running:
384348

385-
java ${JAVA_OPTS} -jar ${executable} ${ags}
349+
```bash
350+
./bin/generate-samples.sh bin/configs/common-mark-documentation-petstore-new.yaml
386351
```
387352

353+
This configuration file can be used to demonstrate the default options for generation. A common option in most of these configs is to define the template directory as the generator's directory under `resources`. This allows template maintainers to modify and test out template changes which don't require recompilation of the entire project. You'd still need to recompile the project in full if you add or modify behaviors to the generator (such as adding a `CliOption`).
354+
388355
### Verify output
389356

390357
Creating a new generator will be an iterative task. Once you've generated the sample, you'll want to try it out. For compiled client/server outputs, this would mean running the code or creating a small sample project to consume your artifact just to make sure it works.

new.sh

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Stubs out files for new generators
1010
Usage:
1111
$0 [options]
1212
Options:
13-
$(grep "[[:space:]].)\ #" $0 | tr -d "#" | sed -E 's/( \| \*)//' | sed -E 's/([a-z])\)/-\1/')
13+
$(grep "[[:space:]].)\ #" $0 | tr -d "#" | sed -E 's/( \| \*)//' | sed -E 's/([a-zA-Z])\)/-\1/')
1414
1515
Examples:
1616
Create a server generator for ktor:
@@ -21,8 +21,7 @@ Examples:
2121
modules/openapi-generator/src/main/resources/kotlin-server/README.mustache
2222
modules/openapi-generator/src/main/resources/kotlin-server/model.mustache
2323
modules/openapi-generator/src/main/resources/kotlin-server/api.mustache
24-
bin/windows/kotlin-server-petstore.bat
25-
bin/kotlin-server-petstore.sh
24+
bin/configs/kotlin-server-petstore-new.yaml
2625
2726
Create a generic C# server generator:
2827
$0 -n csharp -s -t
@@ -31,8 +30,7 @@ Examples:
3130
modules/openapi-generator/src/main/resources/csharp-server/README.mustache
3231
modules/openapi-generator/src/main/resources/csharp-server/model.mustache
3332
modules/openapi-generator/src/main/resources/csharp-server/api.mustache
34-
bin/windows/csharp-server-petstore.bat
35-
bin/csharp-server-petstore.sh
33+
bin/configs/csharp-server-petstore-new.yaml
3634
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenTest.java
3735
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenModelTest.java
3836
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenOptionsTest.java
@@ -56,7 +54,7 @@ checkPreviousGenType() {
5654
}
5755

5856
[ $# -eq 0 ] && usage
59-
while getopts ":hcsdtn:" arg; do
57+
while getopts ":hcsdtfHn:" arg; do
6058
case ${arg} in
6159
n) # Required. Specify generator name, should be kebab-cased.
6260
gen_name=${OPTARG}
@@ -73,7 +71,7 @@ while getopts ":hcsdtn:" arg; do
7371
checkPreviousGenType
7472
gen_type=documentation
7573
;;
76-
h) # Create a schema generator
74+
H) # Create a schema generator
7775
checkPreviousGenType
7876
gen_type=schema
7977
;;
@@ -181,8 +179,8 @@ public class ${lang_classname} extends DefaultCodegen implements CodegenConfig {
181179
modelTemplateFiles.put("model.mustache", ".zz");
182180
apiTemplateFiles.put("api.mustache", ".zz");
183181
embeddedTemplateDir = templateDir = "${gen_name_camel}-${gen_type}";
184-
apiPackage = File.separator + "Apis";
185-
modelPackage = File.separator + "Models";
182+
apiPackage = "Apis";
183+
modelPackage = "Models";
186184
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
187185
// TODO: Fill this out.
188186
}
@@ -201,61 +199,17 @@ echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${
201199
echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache" && \
202200
touch "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache"
203201

204-
# Step 4: Create bash/batch scripts
205-
206-
## Windows batch file
207-
echo "Creating bin/windows/${gen_name_camel}-${gen_type}-petstore.bat"
208-
cat > "${root}/bin/windows/${gen_name_camel}-${gen_type}-petstore.bat"<<EOF
209-
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
210-
211-
If Not Exist %executable% (
212-
mvn clean package
213-
)
214-
215-
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
216-
set ags=generate --artifact-id "${gen_name_camel}-petstore-${gen_type}" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g ${gen_name_camel} -o samples\\${gen_type}\petstore\\${gen_name_camel_pathwin}
217-
218-
java %JAVA_OPTS% -jar %executable% %ags%
202+
# Step 4: Create generation config scripts
203+
echo "Creating bin/configs/${gen_name_camel}-${gen_type}-petstore-new.yaml"
204+
cat > "${root}/bin/configs/${gen_name_camel}-${gen_type}-petstore-new.yaml"<<EOF
205+
generatorName: ${gen_name_camel}
206+
outputDir: samples/${gen_type}/petstore/${gen_name_camel_path}
207+
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
208+
templateDir: modules/openapi-generator/src/main/resources/${gen_name_camel}
209+
additionalProperties:
210+
hideGenerationTimestamp: "true"
219211
EOF
220212

221-
## Bash file
222-
echo "Creating bin/${gen_name_camel}-${gen_type}-petstore.sh"
223-
cat > "${root}/bin/${gen_name_camel}-${gen_type}-petstore.sh"<<EOF
224-
#!/bin/sh
225-
226-
SCRIPT="\$0"
227-
228-
while [ -h "\$SCRIPT" ] ; do
229-
ls=\$(ls -ld "\$SCRIPT")
230-
link=\$(expr "\$ls" : '.*-> \(.*\)$')
231-
if expr "\$link" : '/.*' > /dev/null; then
232-
SCRIPT="\$link"
233-
else
234-
SCRIPT=\$(dirname "\$SCRIPT")/"\$link"
235-
fi
236-
done
237-
238-
if [ ! -d "\${APP_DIR}" ]; then
239-
APP_DIR=\$(dirname "\$SCRIPT")/..
240-
APP_DIR=\$(cd "\${APP_DIR}"; pwd)
241-
fi
242-
243-
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
244-
245-
if [ ! -f "\$executable" ]
246-
then
247-
mvn clean package
248-
fi
249-
250-
# if you've executed sbt assembly previously it will use that instead.
251-
export JAVA_OPTS="\${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
252-
ags="\$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g ${gen_name_camel} -o samples/${gen_type}/petstore/${gen_name_camel_path}"
253-
254-
java \${JAVA_OPTS} -jar \${executable} \${ags}
255-
EOF
256-
257-
chmod u+x "${root}/bin/${gen_name_camel}-${gen_type}-petstore.sh"
258-
259213
# Step 5: (optional) Create OpenAPI Generator test files
260214
if [ "1" -eq "${tests}" ]; then
261215
mkdir -p "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}"

0 commit comments

Comments
 (0)