Skip to content

Commit e0ec332

Browse files
authored
Revert "[doc] Update new-generator steps (new.sh) (#7334)" (#7335)
This reverts commit 151752a.
1 parent 151752a commit e0ec332

3 files changed

Lines changed: 117 additions & 40 deletions

File tree

docs/new-generator.md

Lines changed: 55 additions & 22 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-
* Config file under `./bin/configs`
26+
* Sample scripts under `./bin`
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,8 +43,6 @@ 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
4846
-t When specified, creates test file(s) for the generator.
4947
-h Display help.
5048
@@ -57,7 +55,8 @@ Examples:
5755
modules/openapi-generator/src/main/resources/kotlin-server/README.mustache
5856
modules/openapi-generator/src/main/resources/kotlin-server/model.mustache
5957
modules/openapi-generator/src/main/resources/kotlin-server/api.mustache
60-
bin/configs/kotlin-server-petstore-new.yaml
58+
bin/windows/kotlin-server-petstore.bat
59+
bin/kotlin-server-petstore.sh
6160
6261
Create a generic C# server generator:
6362
./new.sh -n csharp -s -t
@@ -66,7 +65,8 @@ Examples:
6665
modules/openapi-generator/src/main/resources/csharp-server/README.mustache
6766
modules/openapi-generator/src/main/resources/csharp-server/model.mustache
6867
modules/openapi-generator/src/main/resources/csharp-server/api.mustache
69-
bin/configs/csharp-server-petstore-new.yaml
68+
bin/windows/csharp-server-petstore.bat
69+
bin/csharp-server-petstore.sh
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,7 +88,8 @@ 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/configs/common-mark-documentation-petstore-new.yaml
91+
Creating bin/windows/common-mark-documentation-petstore.bat
92+
Creating bin/common-mark-documentation-petstore.sh
9293
Finished.
9394
```
9495

@@ -150,15 +151,15 @@ The `templateDir` variable refers to the "current" template directory setting, a
150151
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.
151152

152153
```java
153-
apiPackage = "Apis";
154+
apiPackage = File.separator + "Apis";
154155
```
155156

156157
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.
157158

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

160161
```java
161-
modelPackage = "Models";
162+
modelPackage = File.separator + "Models";
162163
```
163164

164165
Similarly, this sets the package for `Models`.
@@ -329,28 +330,60 @@ To compile quickly to test this out, you can run `mvn clean package -DskipTests`
329330
330331
### Compile Sample
331332

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

334335
```bash
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"
341-
```
336+
#!/bin/sh
342337

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.
338+
SCRIPT="$0"
344339

345-
Configuration based examples allow us to test the same samples in each tooling option (CLI, Gradle Plugin, Maven Plugin, etc.).
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
346349

347-
You can compile your generator by running:
350+
if [ ! -d "${APP_DIR}" ]; then
351+
APP_DIR=$(dirname "$SCRIPT")/..
352+
APP_DIR=$(cd "${APP_DIR}"; pwd)
353+
fi
348354

349-
```bash
350-
./bin/generate-samples.sh bin/configs/common-mark-documentation-petstore-new.yaml
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}
351367
```
352368

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`).
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:
372+
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
379+
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"
384+
385+
java ${JAVA_OPTS} -jar ${executable} ${ags}
386+
```
354387

355388
### Verify output
356389

modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,3 @@ org.openapitools.codegen.languages.TypeScriptJqueryClientCodegen
127127
org.openapitools.codegen.languages.TypeScriptNodeClientCodegen
128128
org.openapitools.codegen.languages.TypeScriptReduxQueryClientCodegen
129129
org.openapitools.codegen.languages.TypeScriptRxjsClientCodegen
130-
131-
org.openapitools.codegen.languages.CommonMarkDocumentationCodegen

new.sh

Lines changed: 62 additions & 16 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-zA-Z])\)/-\1/')
13+
$(grep "[[:space:]].)\ #" $0 | tr -d "#" | sed -E 's/( \| \*)//' | sed -E 's/([a-z])\)/-\1/')
1414
1515
Examples:
1616
Create a server generator for ktor:
@@ -21,7 +21,8 @@ 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/configs/kotlin-server-petstore-new.yaml
24+
bin/windows/kotlin-server-petstore.bat
25+
bin/kotlin-server-petstore.sh
2526
2627
Create a generic C# server generator:
2728
$0 -n csharp -s -t
@@ -30,7 +31,8 @@ Examples:
3031
modules/openapi-generator/src/main/resources/csharp-server/README.mustache
3132
modules/openapi-generator/src/main/resources/csharp-server/model.mustache
3233
modules/openapi-generator/src/main/resources/csharp-server/api.mustache
33-
bin/configs/csharp-server-petstore-new.yaml
34+
bin/windows/csharp-server-petstore.bat
35+
bin/csharp-server-petstore.sh
3436
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenTest.java
3537
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenModelTest.java
3638
modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CsharpServerCodegenOptionsTest.java
@@ -54,7 +56,7 @@ checkPreviousGenType() {
5456
}
5557

5658
[ $# -eq 0 ] && usage
57-
while getopts ":hcsdtfHn:" arg; do
59+
while getopts ":hcsdtn:" arg; do
5860
case ${arg} in
5961
n) # Required. Specify generator name, should be kebab-cased.
6062
gen_name=${OPTARG}
@@ -71,7 +73,7 @@ while getopts ":hcsdtfHn:" arg; do
7173
checkPreviousGenType
7274
gen_type=documentation
7375
;;
74-
H) # Create a schema generator
76+
h) # Create a schema generator
7577
checkPreviousGenType
7678
gen_type=schema
7779
;;
@@ -179,8 +181,8 @@ public class ${lang_classname} extends DefaultCodegen implements CodegenConfig {
179181
modelTemplateFiles.put("model.mustache", ".zz");
180182
apiTemplateFiles.put("api.mustache", ".zz");
181183
embeddedTemplateDir = templateDir = "${gen_name_camel}-${gen_type}";
182-
apiPackage = "Apis";
183-
modelPackage = "Models";
184+
apiPackage = File.separator + "Apis";
185+
modelPackage = File.separator + "Models";
184186
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
185187
// TODO: Fill this out.
186188
}
@@ -199,17 +201,61 @@ echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${
199201
echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache" && \
200202
touch "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache"
201203

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"
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%
211219
EOF
212220

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+
213259
# Step 5: (optional) Create OpenAPI Generator test files
214260
if [ "1" -eq "${tests}" ]; then
215261
mkdir -p "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}"

0 commit comments

Comments
 (0)