[cli][doc] Document batch command (#4572)

* [cli][batch] Better glob support

If invoked as `openapi-generator batch *.yaml`, the command might
previously fail in some shells where `*.yaml` expands to file name only.

This supports globs without path-part in the filename to prevent
possible NPE.

* [cli] Document batch command
This commit is contained in:
Jim Schubert 2019-11-21 22:09:08 -05:00 committed by William Cheng
parent 80064d8411
commit c89d21a903
2 changed files with 100 additions and 11 deletions

View File

@ -557,3 +557,85 @@ The name of the file should be `config.yml` or `config.yaml` (in our example it
openapi-generator generate -i petstore.yaml -g typescript-fetch -o out \ openapi-generator generate -i petstore.yaml -g typescript-fetch -o out \
-c config.yaml -c config.yaml
``` ```
## batch
The `batch` command allows you to move all CLI arguments supported by the `generate` command into a YAML or JSON file.
*NOTE*: This command supports an additional `!include` property which may point to another "shared" file, the base path to which can be
modified by `--includes-base-dir`.
```bash
openapi-generator help batch
NAME
openapi-generator-cli batch - Generate code in batch via external
configs.
SYNOPSIS
openapi-generator-cli batch [--fail-fast]
[--includes-base-dir <includes>] [(-r <threads> | --threads <threads>)]
[--root-dir <root>] [--timeout <timeout>] [(-v | --verbose)] [--]
<configs>...
OPTIONS
--fail-fast
fail fast on any errors
--includes-base-dir <includes>
base directory used for includes
-r <threads>, --threads <threads>
thread count
--root-dir <root>
root directory used output/includes (includes can be overridden)
--timeout <timeout>
execution timeout (minutes)
-v, --verbose
verbose mode
--
This option can be used to separate command-line options from the
list of argument, (useful when arguments might be mistaken for
command-line options
<configs>
Generator configuration files.
```
Example:
```bash
# create "shared" config
mkdir shared && cat > shared/common.yaml <<EOF
inputSpec: https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml
additionalProperties:
x-ext-name: "Your Name"
EOF
# create "standard" configs
cat > kotlin.yaml <<EOF
'!include': 'shared/common.yaml'
outputDir: out/kotlin
generatorName: kotlin
artifactId: kotlin-petstore-string
additionalProperties:
dateLibrary: string
serializableModel: "true"
EOF
cat > csharp.yaml <<EOF
'!include': 'shared/common.yaml'
outputDir: out/csharp-netcore
generatorName: csharp-netcore
additionalProperties:
packageGuid: "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}"
useCompareNetObjects: "true"
EOF
# Generate them
openapi-generator batch *.yaml
```

View File

@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
@ -100,16 +101,6 @@ public class GenerateBatch implements Runnable {
numThreads = threads; numThreads = threads;
} }
// This allows us to put meta-configs in a different file from referenced configs.
// If not specified, we'll assume it's the parent directory of the first file.
File includesDir;
if (includes != null) {
includesDir = new File(includes);
} else {
Path first = Paths.get(configs.get(0));
includesDir = first.getParent().toFile();
}
Path rootDir; Path rootDir;
if (root != null) { if (root != null) {
rootDir = Paths.get(root); rootDir = Paths.get(root);
@ -117,7 +108,23 @@ public class GenerateBatch implements Runnable {
rootDir = Paths.get(System.getProperty("user.dir")); rootDir = Paths.get(System.getProperty("user.dir"));
} }
LOGGER.info(String.format(Locale.ROOT, "Batch generation using %d threads.\nIncludes: %s\nRoot: %s", numThreads, includesDir.getAbsolutePath(), rootDir.toAbsolutePath().toString())); // This allows us to put meta-configs in a different file from referenced configs.
// If not specified, we'll assume it's the parent directory of the first file.
File includesDir;
if (includes != null) {
includesDir = new File(includes);
} else {
Path first = Paths.get(configs.get(0));
if (Files.isRegularFile(first) && !Files.isSymbolicLink(first)) {
includesDir = first.toAbsolutePath().getParent().toFile();
} else {
// Not traversing symbolic links for includes. Falling back to rooted working directory.
includesDir = rootDir.toFile();
}
}
LOGGER.info(String.format(Locale.ROOT, "Batch generation using up to %d threads.\nIncludes: %s\nRoot: %s", numThreads, includesDir.getAbsolutePath(), rootDir.toAbsolutePath().toString()));
// Create a module which loads our config files, but supports a special "!include" key which can point to an existing config file. // Create a module which loads our config files, but supports a special "!include" key which can point to an existing config file.
// This allows us to create a sort of meta-config which holds configs which are otherwise required at CLI time (via generate task). // This allows us to create a sort of meta-config which holds configs which are otherwise required at CLI time (via generate task).