-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: publish construct hierarchy with metadata to cloud assembly #4194
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c971d50
feat: publish construct hierarchy with metadata to cloud assembly
12ffcda
Trying to fix environment
4005ea7
Fix breaking tests in @aws-cdk/assets
6254c2b
Option to disable construct tree gen
4e46368
Prepare only once
bf942ad
PR feedback
c8aba4c
Merge branch 'master' into nija-at/app-metadata
nija-at cc6d4a0
Fix broken route53 tests
56e2af3
Better fix for failing Route53 tests
ce6bca2
Merge branch 'master' into nija-at/app-metadata
nija-at 8f7d849
PR feedback
a8c6b09
Merge branch 'master' into nija-at/app-metadata
nija-at 7364581
Merge branch 'master' into nija-at/app-metadata
nija-at File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fs = require('fs'); | ||
import path = require('path'); | ||
|
||
import { ArtifactType } from '@aws-cdk/cx-api'; | ||
import { Construct, IConstruct, ISynthesisSession } from '../construct'; | ||
|
||
const FILE_PATH = 'tree.json'; | ||
|
||
/** | ||
* Construct that is automatically attached to the top-level `App`. | ||
* This generates, as part of synthesis, a file containing metadata of the `Construct`s in the construct tree. | ||
nija-at marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* The output is in a tree format so as to preserve the construct hierarchy. | ||
* | ||
* @experimental | ||
*/ | ||
export class Tree extends Construct { | ||
nija-at marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructor(scope: Construct) { | ||
super(scope, 'Tree'); | ||
} | ||
|
||
protected synthesize(session: ISynthesisSession) { | ||
const lookup: { [path: string]: Node } = { }; | ||
|
||
const visit = (construct: IConstruct): Node => { | ||
const children = construct.node.children.map(visit); | ||
const node: Node = { | ||
id: construct.node.id || 'App', | ||
path: construct.node.path, | ||
children: children.length === 0 ? undefined : children, | ||
}; | ||
|
||
lookup[node.path] = node; | ||
|
||
return node; | ||
}; | ||
|
||
const tree = { | ||
version: 'tree-0.1', | ||
tree: visit(this.node.root), | ||
}; | ||
|
||
const builder = session.assembly; | ||
fs.writeFileSync(path.join(builder.outdir, FILE_PATH), JSON.stringify(tree, undefined, 2), { encoding: 'utf-8' }); | ||
|
||
builder.addArtifact('Tree', { | ||
type: ArtifactType.CDK_METADATA, | ||
properties: { | ||
file: FILE_PATH | ||
} | ||
}); | ||
} | ||
} | ||
|
||
interface Node { | ||
id: string; | ||
path: string; | ||
children?: Node[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs = require('fs'); | ||
import { Test } from 'nodeunit'; | ||
import path = require('path'); | ||
import { App, Construct, Resource, Stack } from '../../lib/index'; | ||
|
||
export = { | ||
'annotations are generated as expected'(test: Test) { | ||
const app = new App(); | ||
|
||
const stack = new Stack(app, 'mystack'); | ||
new Construct(stack, 'group1'); | ||
const group2 = new Construct(stack, 'group2'); | ||
|
||
new MyResource(group2, 'resource3'); | ||
|
||
const assembly = app.synth(); | ||
const annotationsFile = assembly.getMetadata('Tree').file; | ||
|
||
test.deepEqual(readJson(assembly.directory, annotationsFile), { | ||
version: 'tree-0.1', | ||
tree: { | ||
id: 'App', | ||
path: '', | ||
children: [ | ||
{ | ||
id: 'Tree', | ||
path: 'Tree' | ||
}, | ||
{ | ||
id: 'mystack', | ||
path: 'mystack', | ||
children: [ | ||
{ | ||
id: 'group1', | ||
path: 'mystack/group1' | ||
}, | ||
{ | ||
id: 'group2', | ||
path: 'mystack/group2', | ||
children: [ | ||
{ id: 'resource3', path: 'mystack/group2/resource3' } | ||
] | ||
} | ||
] | ||
}, | ||
] | ||
} | ||
}); | ||
test.done(); | ||
}, | ||
}; | ||
|
||
class MyResource extends Resource { | ||
constructor(scope: Construct, id: string) { | ||
super(scope, id); | ||
} | ||
} | ||
|
||
function readJson(outdir: string, file: string) { | ||
return JSON.parse(fs.readFileSync(path.join(outdir, file), 'utf-8')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a "@see" with a link to the RFC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I'll wait until the RFC is published so I can get the a more stable link.