Skip to content

Commit

Permalink
feat(cli): support generating provider bindings in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Sep 13, 2022
1 parent 4616bb1 commit 1de92f3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## 0.13.0 (unreleased)

**Breaking Changes**

A very minor change in the UX of the `cdktf get` command now generates the provider bindings for all languages other than Typescript in parallel. This speeds up the process in general, but on devices with limited memory it could lead to Out Of Memory errors. If this happens you can limit the parallelism by providing the parallelism flag: `cdktf get --parallelism=1`.

## 0.12.2

**Breaking Changes**
Expand Down
22 changes: 19 additions & 3 deletions packages/@cdktf/provider-generator/lib/get/constructs-maker.ts
Expand Up @@ -52,6 +52,7 @@ export async function generateJsiiLanguage(
export interface GetOptions {
readonly targetLanguage: Language;
readonly codeMakerOutput: string;
readonly jsiiParallelism?: number;
/**
* Path to copy the output .jsii file.
* @default - jsii file is not emitted
Expand Down Expand Up @@ -365,9 +366,24 @@ a NODE_OPTIONS variable, we won't override it. Hence, the provider generation mi
}

if (!this.isJavascriptTarget || this.options.outputJsii) {
for (const target of this.targets) {
await this.generateJsiiLanguage(target);
}
const numberOfWorkers =
this.options.jsiiParallelism === -1
? this.targets.length
: this.options.jsiiParallelism;

const work = [...this.targets];
const workers = new Array(numberOfWorkers).fill(async () => {
let target: ConstructsMakerTarget | undefined;
while ((target = work.pop())) {
const endJsiiTarget = logTimespan(
`Generating JSII bindings for ${target.name}`
);
await this.generateJsiiLanguage(target);
endJsiiTarget();
}
});

await Promise.all(workers.map((fn) => fn()));
}

for (const target of this.targets) {
Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/bin/cmds/get.ts
Expand Up @@ -28,6 +28,12 @@ class Command extends BaseCommand {
desc: "Output programming language",
alias: "l",
choices: LANGUAGES,
})
.option("parallelism", {
type: "number",
required: false,
desc: "Number of concurrently generated provider / module bindings. Only applies for languages that are not Typescript (translated by JSII). Defaults to infinity, denoted by -1",
default: -1,
});

public async handleCommand(argv: any) {
Expand Down
15 changes: 12 additions & 3 deletions packages/cdktf-cli/bin/cmds/handlers.ts
Expand Up @@ -184,7 +184,11 @@ export async function diff(argv: any) {
);
}

export async function get(argv: { output: string; language: Language }) {
export async function get(argv: {
output: string;
language: Language;
parallelism: number;
}) {
throwIfNotProjectDirectory();
await displayVersionMessage();
await initializErrorReporting(true);
Expand All @@ -193,7 +197,7 @@ export async function get(argv: { output: string; language: Language }) {
const config = cfg.readConfigSync(); // read config again to be up-to-date (if called via 'add' command)
const providers = config.terraformProviders ?? [];
const modules = config.terraformModules ?? [];
const { output, language } = argv;
const { output, language, parallelism } = argv;

const constraints: cfg.TerraformDependencyConstraint[] = [
...providers,
Expand All @@ -212,6 +216,7 @@ export async function get(argv: { output: string; language: Language }) {
codeMakerOutput: output,
language: language,
constraints,
parallelism,
})
);
}
Expand Down Expand Up @@ -418,6 +423,10 @@ export async function providerAdd(argv: any) {
console.log(
"Local providers have been updated. Running cdktf get to update..."
);
await get({ language: language, output: config.codeMakerOutput });
await get({
language: language,
output: config.codeMakerOutput,
parallelism: -1,
});
}
}
3 changes: 3 additions & 0 deletions packages/cdktf-cli/bin/cmds/ui/get.tsx
Expand Up @@ -10,12 +10,14 @@ interface GetConfig {
codeMakerOutput: string;
language: Language;
constraints: config.TerraformDependencyConstraint[];
parallelism: number;
}

export const Get = ({
codeMakerOutput,
language,
constraints,
parallelism,
}: GetConfig): React.ReactElement => {
const [currentStatus, setCurrentStatus] = React.useState<Status>(
Status.STARTING
Expand All @@ -25,6 +27,7 @@ export const Get = ({
const constructsOptions: GetOptions = {
codeMakerOutput: codeMakerOutput,
targetLanguage: language,
jsiiParallelism: parallelism,
};

React.useEffect(() => {
Expand Down
20 changes: 15 additions & 5 deletions website/docs/cdktf/cli-reference/commands.mdx
Expand Up @@ -301,11 +301,15 @@ cdktf get [OPTIONS]
Generate CDK Constructs for Terraform providers and modules.
Options:
--version Show version number [boolean]
--log-level Which log level should be written. Only supported via setting the env CDKTF_LOG_LEVEL [string]
--output, -o Output directory for generated Constructs [string] [default: ".gen"]
--language, -l Output programming language [string] [required] [choices: "typescript", "python", "java", "csharp", "go"]
-h, --help Show help [boolean]
--version Show version number [boolean]
--disable-plugin-cache-env Dont set TF_PLUGIN_CACHE_DIR automatically. This is useful when the plugin cache is configured differently. Supported using the env CDKTF_DISABLE_PLUGIN_CACHE_ENV.
[boolean] [default: false]
--log-level Which log level should be written. Only supported via setting the env CDKTF_LOG_LEVEL [string]
-o, --output Output directory for generated Constructs [string] [default: "imports"]
-l, --language Output programming language [string] [required] [choices: "typescript", "python", "java", "csharp", "go"] [default: "python"]
--parallelism Number of concurrently generated provider / module bindings. Only applies for languages that are not Typescript (translated by JSII). Defaults to infinity, denoted by -1
[number] [default: -1]
-h, --help Show help [boolean]
```

**Examples**
Expand All @@ -325,6 +329,12 @@ $ cat cdktf.json
$ cdktf get
```

If you run into issues generating a lot of bindings you can use the `--parallelism` option to limit the number of bindings generated in parallel.

```bash
$ cdktf get --parallelism 1
```

## init

This command creates a new CDK for Terraform project using a template.
Expand Down

0 comments on commit 1de92f3

Please sign in to comment.