Skip to content

Commit

Permalink
chore(core): migrate constructs to use "constructs" module (#10655)
Browse files Browse the repository at this point in the history
With all of  CDK construct libraries in the repo migrated[1],[2],[3]
to use "constructs" module, finally move "core" module also to
use the same pattern.

[1]: c179699
[2]: 60c782f
[3]: a76428b


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored Oct 4, 2020
1 parent dd543d1 commit 92080a2
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 38 deletions.
8 changes: 6 additions & 2 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import * as crypto from 'crypto';
import * as os from 'os';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import * as fs from 'fs-extra';
import { AssetHashType, AssetOptions } from './assets';
import { BundlingOptions } from './bundling';
import { Construct } from './construct-compat';
import { FileSystem, FingerprintOptions } from './fs';
import { Stack } from './stack';
import { Stage } from './stage';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';

/**
* Initialization properties for `AssetStaging`.
*/
Expand Down Expand Up @@ -38,7 +42,7 @@ export interface AssetStagingProps extends FingerprintOptions, AssetOptions {
* The file/directory are staged based on their content hash (fingerprint). This
* means that only if content was changed, copy will happen.
*/
export class AssetStaging extends Construct {
export class AssetStaging extends CoreConstruct {
/**
* The directory inside the bundling container into which the asset sources will be mounted.
* @experimental
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { CfnHook } from './cfn-hook';
import { FromCloudFormationOptions } from './cfn-parse';
import { CfnResource } from './cfn-resource';
import { Construct } from './construct-compat';

/**
* The possible types of traffic shifting for the blue-green deployment configuration.
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-condition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { Construct } from './construct-compat';
import { IResolvable, IResolveContext } from './resolvable';

export interface CfnConditionProps {
Expand Down
16 changes: 10 additions & 6 deletions packages/@aws-cdk/core/lib/cfn-element.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Construct } from './construct-compat';
import { Construct, Node } from 'constructs';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';
import { Lazy } from './lazy';
import { Token } from './token';

const CFN_ELEMENT_SYMBOL = Symbol.for('@aws-cdk/core.CfnElement');

/**
* An element of a CloudFormation stack.
*/
export abstract class CfnElement extends Construct {
export abstract class CfnElement extends CoreConstruct {
/**
* Returns `true` if a construct is a stack element (i.e. part of the
* synthesized cloudformation template).
Expand Down Expand Up @@ -58,10 +61,10 @@ export abstract class CfnElement extends Construct {
this.stack = Stack.of(this);

this.logicalId = Lazy.stringValue({ produce: () => this.synthesizeLogicalId() }, {
displayHint: `${notTooLong(this.node.path)}.LogicalID`,
displayHint: `${notTooLong(Node.of(this).path)}.LogicalID`,
});

this.node.addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
}

/**
Expand All @@ -78,7 +81,7 @@ export abstract class CfnElement extends Construct {
* node +internal+ entries filtered.
*/
public get creationStack(): string[] {
const trace = this.node.metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace;
const trace = Node.of(this).metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace;
if (!trace) {
return [];
}
Expand Down Expand Up @@ -161,3 +164,4 @@ function notTooLong(x: string) {

import { CfnReference } from './private/cfn-reference';
import { Stack } from './stack';
import { Token } from './token';
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { Construct } from './construct-compat';
import { ignoreEmpty } from './util';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-include.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { Construct } from './construct-compat';

export interface CfnIncludeProps {
/**
Expand Down
8 changes: 6 additions & 2 deletions packages/@aws-cdk/core/lib/cfn-json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct } from './construct-compat';
import { Construct } from 'constructs';
import { CustomResource } from './custom-resource';
import { CfnUtilsProvider } from './private/cfn-utils-provider';
import { CfnUtilsResourceType } from './private/cfn-utils-provider/consts';
Expand All @@ -7,6 +7,10 @@ import { IResolvable, IResolveContext } from './resolvable';
import { Stack } from './stack';
import { captureStackTrace } from './stack-trace';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';

export interface CfnJsonProps {
/**
* The value to resolve. Can be any JavaScript object, including tokens and
Expand All @@ -29,7 +33,7 @@ export interface CfnJsonProps {
*
* This construct is backed by a custom resource.
*/
export class CfnJson extends Construct implements IResolvable {
export class CfnJson extends CoreConstruct implements IResolvable {
public readonly creationStack: string[] = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from 'constructs';
import { CfnRefElement } from './cfn-element';
import { Fn } from './cfn-fn';
import { Construct } from './construct-compat';
import { Token } from './token';

export interface CfnMappingProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { Construct } from './construct-compat';

export interface CfnOutputProps {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { Construct } from './construct-compat';
import { CfnReference } from './private/cfn-reference';
import { IResolvable, IResolveContext } from './resolvable';
import { Token } from './token';
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/core/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnCondition } from './cfn-condition';
/* eslint-disable import/order */
import { CfnRefElement } from './cfn-element';
import { CfnCreationPolicy, CfnDeletionPolicy, CfnUpdatePolicy } from './cfn-resource-policy';
import { Construct, IConstruct } from './construct-compat';
import { Construct, IConstruct, Node } from 'constructs';
import { addDependency } from './deps';
import { CfnReference } from './private/cfn-reference';
import { Reference } from './reference';
Expand Down Expand Up @@ -92,8 +92,8 @@ export class CfnResource extends CfnRefElement {
// if aws:cdk:enable-path-metadata is set, embed the current construct's
// path in the CloudFormation template, so it will be possible to trace
// back to the actual construct path.
if (this.node.tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) {
this.addMetadata(cxapi.PATH_METADATA_KEY, this.node.path);
if (Node.of(this).tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) {
this.addMetadata(cxapi.PATH_METADATA_KEY, Node.of(this).path);
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ export class CfnResource extends CfnRefElement {
return;
}

addDependency(this, target, `"${this.node.path}" depends on "${target.node.path}"`);
addDependency(this, target, `"${Node.of(this).path}" depends on "${Node.of(target).path}"`);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-rule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from 'constructs';
import { ICfnConditionExpression } from './cfn-condition';
import { CfnRefElement } from './cfn-element';
import { Construct } from './construct-compat';
import { capitalizePropertyNames } from './util';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
import { CfnResource } from '../cfn-resource';
import { Construct } from '../construct-compat';
import { Duration } from '../duration';
import { Size } from '../size';
import { Stack } from '../stack';
Expand All @@ -12,6 +12,10 @@ import { Token } from '../token';
const ENTRYPOINT_FILENAME = '__entrypoint__';
const ENTRYPOINT_NODEJS_SOURCE = path.join(__dirname, 'nodejs-entrypoint.js');

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '../construct-compat';

/**
* Initialization properties for `CustomResourceProvider`.
*
Expand Down Expand Up @@ -75,7 +79,7 @@ export enum CustomResourceProviderRuntime {
*
* @experimental
*/
export class CustomResourceProvider extends Construct {
export class CustomResourceProvider extends CoreConstruct {
/**
* Returns a stack-level singleton ARN (service token) for the custom resource
* provider.
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/custom-resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { CfnResource } from './cfn-resource';
import { Construct } from './construct-compat';
import { RemovalPolicy } from './removal-policy';
import { Resource } from './resource';
import { Token } from './token';
Expand Down
10 changes: 7 additions & 3 deletions packages/@aws-cdk/core/lib/nested-stack.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import * as crypto from 'crypto';
import { Construct, Node } from 'constructs';
import { FileAssetPackaging } from './assets';
import { Fn } from './cfn-fn';
import { Aws } from './cfn-pseudo';
import { CfnResource } from './cfn-resource';
import { CfnStack } from './cloudformation.generated';
import { Construct } from './construct-compat';
import { Duration } from './duration';
import { Lazy } from './lazy';
import { IResolveContext } from './resolvable';
import { Stack } from './stack';
import { NestedStackSynthesizer } from './stack-synthesizers';
import { Token } from './token';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';

const NESTED_STACK_SYMBOL = Symbol.for('@aws-cdk/core.NestedStack');

/**
Expand Down Expand Up @@ -105,7 +109,7 @@ export class NestedStack extends Stack {
this._parentStack = parentStack;

// @deprecate: remove this in v2.0 (redundent)
const parentScope = new Construct(scope, id + '.NestedStack');
const parentScope = new CoreConstruct(scope, id + '.NestedStack');

Object.defineProperty(this, NESTED_STACK_SYMBOL, { value: true });

Expand Down Expand Up @@ -223,7 +227,7 @@ function findParentStack(scope: Construct): Stack {
throw new Error('Nested stacks cannot be defined as a root construct');
}

const parentStack = scope.node.scopes.reverse().find(p => Stack.isStack(p));
const parentStack = Node.of(scope).scopes.reverse().find(p => Stack.isStack(p));
if (!parentStack) {
throw new Error('Nested stacks must be defined within scope of another non-nested stack');
}
Expand Down
10 changes: 7 additions & 3 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { IConstruct, Node } from 'constructs';
import { IConstruct, Construct, Node } from 'constructs';
import { Annotations } from './annotations';
import { App } from './app';
import { Arn, ArnComponents } from './arn';
Expand All @@ -11,7 +11,7 @@ import { CfnElement } from './cfn-element';
import { Fn } from './cfn-fn';
import { Aws, ScopedAws } from './cfn-pseudo';
import { CfnResource, TagType } from './cfn-resource';
import { Construct, ISynthesisSession } from './construct-compat';
import { ISynthesisSession } from './construct-compat';
import { ContextProvider } from './context-provider';
import { Environment } from './environment';
import { FeatureFlags } from './feature-flags';
Expand All @@ -20,6 +20,10 @@ import { LogicalIDs } from './private/logical-id';
import { resolve } from './private/resolve';
import { makeUniqueId } from './private/uniqueid';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';

const STACK_SYMBOL = Symbol.for('@aws-cdk/core.Stack');
const MY_STACK_CACHE = Symbol.for('@aws-cdk/core.Stack.myStack');

Expand Down Expand Up @@ -140,7 +144,7 @@ export interface StackProps {
/**
* A root construct which represents a single CloudFormation stack.
*/
export class Stack extends Construct implements ITaggable {
export class Stack extends CoreConstruct implements ITaggable {
/**
* Return whether the given object is a Stack.
*
Expand Down
9 changes: 6 additions & 3 deletions packages/@aws-cdk/core/lib/stage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as cxapi from '@aws-cdk/cx-api';
import { IConstruct, Node } from 'constructs';
import { Construct } from './construct-compat';
import { IConstruct, Construct, Node } from 'constructs';
import { Environment } from './environment';
import { synthesize } from './private/synthesis';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from './construct-compat';

/**
* Initialization props for a stage.
*/
Expand Down Expand Up @@ -65,7 +68,7 @@ export interface StageProps {
* copies of your application which should be be deployed to different
* environments.
*/
export class Stage extends Construct {
export class Stage extends CoreConstruct {
/**
* Return the stage this construct is contained with, if available. If called
* on a nested stage, returns its parent.
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/tag-aspect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// import * as cxapi from '@aws-cdk/cx-api';
import { Annotations } from './annotations';
import { IAspect, Aspects } from './aspect';
import { Construct, IConstruct } from './construct-compat';
import { ITaggable, TagManager } from './tag-manager';
import { Annotations } from './annotations';

/**
* Properties for a tag
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IConstruct } from './construct-compat';
import { IConstruct } from 'constructs';
import { Intrinsic } from './private/intrinsic';
import { IPostProcessor, IResolveContext } from './resolvable';
import { Stack } from './stack';
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@
"cfn2ts-core-import": ".",
"pre": [
"rm -rf test/fs/fixtures && cd test/fs && tar -xzf fixtures.tar.gz"
]
],
"env": {
"AWSLINT_BASE_CONSTRUCT": "true"
}
},
"nyc": {
"statements": 55,
Expand Down
2 changes: 1 addition & 1 deletion packages/awslint/lib/rules/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ constructLinter.add({
const expectedParams = new Array<MethodSignatureParameterExpectation>();

let baseType;
if (process.env.AWSLINT_BASE_CONSTRUCT && !initializer.parentType.name.startsWith('Cfn')) {
if (process.env.AWSLINT_BASE_CONSTRUCT && !CoreTypes.isCfnResource(e.ctx.classType)) {
baseType = e.ctx.core.baseConstructClass;
} else {
baseType = e.ctx.core.constructClass;
Expand Down

0 comments on commit 92080a2

Please sign in to comment.