Skip to content

Commit

Permalink
Refresh cdk init templates
Browse files Browse the repository at this point in the history
- Command now accepts `--type=[lib|app]` and `--language=...`
- Template types are documented using an info.json file (description, ...)
- Language support is enabled juts by adding new files.
- Supports (very) minimal templating:
  + `%name%` gets replaced with the snake_case version of the current directory's basename
  + `%name.CamelCase%` gets replaced with the capitalized CamelCase version of the current directory's basename
  + Replaces happen in files with names ending in `.template.extension` and in filenames

Fixes #23
  • Loading branch information
RomainMuller committed Jun 8, 2018
1 parent 0a7f169 commit e3a7d51
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 80 deletions.
63 changes: 55 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,54 @@
The **AWS Cloud Development Kit (AWS CDK)** is a software development framework
for defining cloud infrastructure in code.

# Development Environment
## Getting Started

### Installation

Make sure you have the required dependencies installed:
* [Node.js 8.11.0](https://nodejs.org/download/release/v8.11.0/)
* The SDK for the language you intend to use (Java, .Net, Ruby, ...)

Download the current release bundle from S3, and install it to `~/.cdk`:
```shell
tmpdir=/tmp/aws-cdk-install
mkdir ${tmpdir}
aws s3 cp s3://<bucket>/<key> ${tmpdir}/aws-cdk.zip
unzip -o ${tmpdir}/aws-cdk.zip -d ~/.cdk
```

Then add the CDK to your `$PATH`:
```shell
# At the end of your ~/.bashrc or ~/.zshrc file
export PATH=${PATH:+${PATH}:}${HOME}/.cdk/bin
```

### Creating a new project

New projects can be initialized using `cdk init`.
```shell
mkdir ${PROJECT_NAME}
cd ${PROJECT_NAME}
cdk init
```

### Useful commands

```shell
# Initialize a new CDK project
cdk init

# Open the documentation in a web browser
cdk docs

# List available commands
cdk help

# Get help on a particular command (e.g: synth)
cdk help synth
```

## Development Environment

This is a monorepo which uses [lerna](https://github.com/lerna/lerna).

Expand All @@ -12,7 +59,7 @@ published to npm. Therefore, the jsii tarballs are checked-in to this repository
under `./local-npm` and the install script will install them in the repo-global
node_modules directory.

## Prerequisites
### Prerequisites

Since this repo produces artifacts for multiple programming languages using
__jsii__, it relies on the following toolchains:
Expand All @@ -37,7 +84,7 @@ docker run --net=host -it -v $PWD:$PWD -w $PWD ${IMAGE}
This will get you into an interactive docker shell. You can then run
./install.sh and ./build.sh as described below.

## Bootstrapping
### Bootstrapping

1. Clone this repository (or run `git clean -fdx` to clean up all build artifacts).
2. Run `./install.sh` - this will install all repo-level dependencies, including
Expand All @@ -46,7 +93,7 @@ This will get you into an interactive docker shell. You can then run
All external dependencies will be installed and internal deps will be
cross-linked.

## Development Iteration
### Development Iteration

After you've bootstrapped the repo, you would probably want to work on individual packages.

Expand Down Expand Up @@ -74,7 +121,7 @@ cd packages/aws-cdk-s3
lr watch
```

## Linking against this repository
### Linking against this repository

The script `./link-all.sh` can be used to generate symlinks to all modules in
this repository under some `node_module` directory. This can be used to develop
Expand All @@ -93,13 +140,13 @@ One can use the `postinstall` script to symlink this repo:
This assumes this repo is a sibling of the target repo and will install the CDK
as a linked dependency during __npm install__.

## Commits and Pull Requests
### Commits and Pull Requests

Commits should follow the
[conventional-changelog-standard](https://github.com/bcoe/conventional-changelog-standard/blob/master/convention.md)
to allow automatic changelog generation.

## Package Linter
### Package Linter

The `pkglint` tool normalizes all packages in the repo. It verifies package.json
is normalized and adheres to the set of rules. To evaluate (and potentially fix)
Expand All @@ -110,7 +157,7 @@ of the repository (after boostrapping):
npm run pkglint
```

## Updating jsii
### Updating jsii

Run `./pack.sh` in the jsii repository and copy the tarballs to `./local-npm`.
Make sure all tarballs are defined in the root `package.json`.
Expand Down
8 changes: 5 additions & 3 deletions packages/aws-cdk-toolkit/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as yargs from 'yargs';
import { bootstrapEnvironment, deployStack, destroyStack, loadToolkitInfo, Mode, SDK } from '../lib';
import * as contextplugins from '../lib/contextplugins';
import { printStackDiff } from '../lib/diff';
import { cliInit } from '../lib/init';
import { availableInitTemplates, cliInit } from '../lib/init';
import { interactive } from '../lib/interactive';
import { data, debug, error, highlight, print, setVerbose, success, warning } from '../lib/logging';
import { PluginHost } from '../lib/plugin';
Expand Down Expand Up @@ -61,7 +61,9 @@ const argv = yargs
.command('diff [STACK]', 'Compares the specified stack with the deployed stack or a local template file', yargs => yargs
.option('template', { type: 'string', desc: 'the path to the CloudFormation template to compare with' }))
.command('metadata [STACK]', 'Returns all metadata associated with this stack')
.command('init [TEMPLATE]', 'Create a new, empty CDK project from a template')
.command('init [TEMPLATE]', 'Create a new, empty CDK project from a template', yargs => yargs
.option('type', { type: 'string', desc: 'the type of package to initialize', choices: availableInitTemplates.map(t => t.name) })
.option('language', { type: 'string', alias: 'lang', desc: 'the language to be used for the new project' }))
.epilogue([
'If your app has a single stack, there is no need to specify the stack name',
'If one of cdk.json or ~/.cdk.json exists, options specified there will be used as defaults. Settings in cdk.json take precedence.'
Expand Down Expand Up @@ -170,7 +172,7 @@ async function main(command: string, args: any): Promise<number | string | {} |
return await cliMetadata(await findStack(args.STACK));

case 'init':
return await cliInit(args.TEMPLATE);
return await cliInit(args.type, args.language);

default:
throw new Error('Unknown command: ' + command);
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-toolkit/lib/init-templates/app/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "Template for a CDK Application",
"aliases": ["application"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js
*.d.ts
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.ts
!*.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node
import { App, Stack, StackProps } from 'aws-cdk';
import { Topic } from 'aws-cdk-sns';
import { Queue } from 'aws-cdk-sqs';

class %name.PascalCased%Stack extends Stack {
constructor(parent: App, name: string, props?: StackProps) {
super(parent, name, props);

const queue = new Queue(this, '%name.PascalCased%Queue', {
visibilityTimeoutSec: 300
});

const topic = new Topic(this, '%name.PascalCased%Topic');

topic.subscribeQueue(queue);
}
}

const app = new App(process.argv);

new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');

process.stdout.write(app.run());
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "%name%",
"version": "%cdk-version%",
"description": "A CDK Application",
"private": "true",
"main": "bin/index.js",
"types": "bin/index.d.ts",
"bin": {
"%name%": "bin/%name%.js"
},
"scripts": {
"prepare": "tslint -p . && tsc && chmod a+x bin/%name%.js",
"watch": "tsc -w",
"lint": "tslint -p . --force"
},
"devDependencies": {
"@types/node": "^8.9.4",
"tslint": "^5.10.0",
"typescript": "^2.8.3"
},
"dependencies": {
"aws-cdk": "^%cdk-version%",
"aws-cdk-resources": "^%cdk-version%",
"aws-cdk-sns": "^%cdk-version%",
"aws-cdk-sqs": "^%cdk-version%"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
}
}

4 changes: 4 additions & 0 deletions packages/aws-cdk-toolkit/lib/init-templates/lib/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "Template for a CDK Construct Library",
"aliases": "library"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js
*.d.ts
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.ts
!*.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Construct } from 'aws-cdk';
import { sqs } from 'aws-cdk-resources';
import { Topic } from 'aws-cdk-sns';
import { Queue } from 'aws-cdk-sqs';

export interface %name.PascalCased%Props {
/**
* The visibility timeout to be configured on the SQS Queue, in seconds.
*
* @default 300
*/
visibilityTimeout?: number;
}

export class %name.PascalCased% extends Construct {
/** @returns the ARN of the SQS queue */
public readonly queueArn: sqs.QueueArn;

constructor(parent: Construct, name: string, props: %name.PascalCased%Props = {}) {
super(parent, name);

const queue = new Queue(this, '%name.PascalCased%Queue', {
visibilityTimeoutSec: props.visibilityTimeout || 300
});

const topic = new Topic(this, '%name.PascalCased%Topic');

topic.subscribeQueue(queue);

this.queueArn = queue.queueArn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "%name%",
"version": "%cdk-version%",
"description": "A CDK Construct Library",
"private": true,
"main": "lib/index.js",
"types": "lib/index.d.ts",
"jsii": {
"outdir": "dist",
"names": {
"java": "com.acme.cdk.%name.camelCased%",
"dotnet": "Acme.Cdk.%name.PascalCased%"
}
},
"scripts": {
"prepare": "jsii && tslint -p .",
"watch": "jsii -w",
"lint": "tsc && tslint -p .",
"test": "nodeunit test/test.*.js"
},
"devDependencies": {
"@types/node": "^8.9.4",
"jsii": "^0.4.0",
"tslint": "^5.10.0",
"typescript": "^2.8.3"
},
"dependencies": {
"aws-cdk": "^%cdk-version%",
"aws-cdk-resources": "^%cdk-version%",
"aws-cdk-sns": "^%cdk-version%",
"aws-cdk-sqs": "^%cdk-version%"
}
}
Loading

0 comments on commit e3a7d51

Please sign in to comment.