From f88d482d7c59d037d69d676a4b1b0e80f631a5c4 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Sun, 16 Jun 2019 15:18:54 -0700 Subject: [PATCH] Change bind API in Artifacts. --- .../@aws-cdk/aws-codebuild/lib/artifacts.ts | 70 ++++++++++--------- .../lib/codepipeline-artifacts.ts | 2 +- .../aws-codebuild/lib/no-artifacts.ts | 2 +- .../@aws-cdk/aws-codebuild/lib/project.ts | 23 +++--- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/artifacts.ts b/packages/@aws-cdk/aws-codebuild/lib/artifacts.ts index aaa4cf45b735c..189bff76b0a9b 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/artifacts.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/artifacts.ts @@ -1,6 +1,22 @@ import s3 = require('@aws-cdk/aws-s3'); +import { Construct } from '@aws-cdk/cdk'; import { CfnProject } from './codebuild.generated'; -import { Project } from './project'; +import { IProject } from './project'; + +/** + * The type returned from {@link IArtifacts#bind}. + */ +export interface ArtifactsConfig { + readonly artifactsProperty: CfnProject.ArtifactsProperty; +} + +export interface IArtifacts { + readonly identifier?: string; + + readonly type: string; + + bind(scope: Construct, project: IProject): ArtifactsConfig; +} /** * Properties common to all Artifacts classes. @@ -16,36 +32,24 @@ export interface ArtifactsProps { /** * Artifacts definition for a CodeBuild Project. */ -export abstract class Artifacts { +export abstract class Artifacts implements IArtifacts { public static s3(props: S3ArtifactsProps): S3Artifacts { return new S3Artifacts(props); } public readonly identifier?: string; - protected abstract readonly type: string; + public abstract readonly type: string; - constructor(props: ArtifactsProps) { + protected constructor(props: ArtifactsProps) { this.identifier = props.identifier; } - /** - * @internal - */ - public _bind(_project: Project) { - return; - } - - public toArtifactsJSON(): CfnProject.ArtifactsProperty { - const artifactsProp = this.toArtifactsProperty(); - return { - artifactIdentifier: this.identifier, - type: this.type, - ...artifactsProp, - }; - } - - protected toArtifactsProperty(): any { + public bind(_scope: Construct, _project: IProject): ArtifactsConfig { return { + artifactsProperty: { + artifactIdentifier: this.identifier, + type: this.type, + }, }; } } @@ -95,26 +99,24 @@ export interface S3ArtifactsProps extends ArtifactsProps { * S3 Artifact definition for a CodeBuild Project. */ export class S3Artifacts extends Artifacts { - protected readonly type = 'S3'; + public readonly type = 'S3'; constructor(private readonly props: S3ArtifactsProps) { super(props); } - /** - * @internal - */ - public _bind(project: Project) { + public bind(_scope: Construct, project: IProject): ArtifactsConfig { this.props.bucket.grantReadWrite(project); - } - - protected toArtifactsProperty(): any { + const superConfig = super.bind(_scope, project); return { - location: this.props.bucket.bucketName, - path: this.props.path, - namespaceType: this.props.includeBuildId === false ? 'NONE' : 'BUILD_ID', - name: this.props.name, - packaging: this.props.packageZip === false ? 'NONE' : 'ZIP', + artifactsProperty: { + ...superConfig.artifactsProperty, + location: this.props.bucket.bucketName, + path: this.props.path, + namespaceType: this.props.includeBuildId === false ? 'NONE' : 'BUILD_ID', + name: this.props.name, + packaging: this.props.packageZip === false ? 'NONE' : 'ZIP', + } }; } } diff --git a/packages/@aws-cdk/aws-codebuild/lib/codepipeline-artifacts.ts b/packages/@aws-cdk/aws-codebuild/lib/codepipeline-artifacts.ts index 4d7352edbb5d6..8b8d72e6c2cc7 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/codepipeline-artifacts.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/codepipeline-artifacts.ts @@ -6,7 +6,7 @@ import { Artifacts } from './artifacts'; * and because of that, you're not allowed to specify an identifier for it. */ export class CodePipelineArtifacts extends Artifacts { - protected readonly type = 'CODEPIPELINE'; + public readonly type = 'CODEPIPELINE'; constructor() { super({}); diff --git a/packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts b/packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts index c99aa11cdbd55..72bbbd07b9f71 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts @@ -11,7 +11,7 @@ import { Artifacts } from './artifacts'; * This class is private to the @aws-codebuild package. */ export class NoArtifacts extends Artifacts { - protected readonly type = 'NO_ARTIFACTS'; + public readonly type = 'NO_ARTIFACTS'; constructor() { super({}); diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index c66a356d0c2da..86269e5747ab2 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -7,7 +7,7 @@ import events = require('@aws-cdk/aws-events'); import iam = require('@aws-cdk/aws-iam'); import kms = require('@aws-cdk/aws-kms'); import { Aws, Construct, IResource, Lazy, PhysicalName, Resource, ResourceIdentifiers, Stack } from '@aws-cdk/cdk'; -import { Artifacts } from './artifacts'; +import { IArtifacts } from './artifacts'; import { BuildSpec, mergeBuildSpecs } from './build-spec'; import { Cache } from './cache'; import { CfnProject } from './codebuild.generated'; @@ -545,7 +545,7 @@ export interface ProjectProps extends CommonProjectProps { * * @default NoArtifacts */ - readonly artifacts?: Artifacts; + readonly artifacts?: IArtifacts; /** * The secondary sources for the Project. @@ -563,7 +563,7 @@ export interface ProjectProps extends CommonProjectProps { * @default - No secondary artifacts. * @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-multi-in-out.html */ - readonly secondaryArtifacts?: Artifacts[]; + readonly secondaryArtifacts?: IArtifacts[]; } /** @@ -646,7 +646,7 @@ export class Project extends ProjectBase { private readonly source: ISource; private readonly buildImage: IBuildImage; private readonly _secondarySources: CfnProject.SourceProperty[]; - private readonly _secondaryArtifacts: Artifacts[]; + private readonly _secondaryArtifacts: CfnProject.ArtifactsProperty[]; constructor(scope: Construct, id: string, props: ProjectProps) { super(scope, id, { @@ -678,7 +678,7 @@ export class Project extends ProjectBase { : (this.source.type === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE ? new CodePipelineArtifacts() : new NoArtifacts()); - artifacts._bind(this); + const artifactsConfig = artifacts.bind(this, this); const cache = props.cache || Cache.none(); @@ -718,7 +718,7 @@ export class Project extends ProjectBase { ...sourceConfig.sourceProperty, buildSpec: buildSpec && buildSpec.toBuildSpec() }, - artifacts: artifacts.toArtifactsJSON(), + artifacts: artifactsConfig.artifactsProperty, serviceRole: this.role.roleArn, environment: this.renderEnvironment(props.environment, environmentVariables), encryptionKey: props.encryptionKey && props.encryptionKey.keyArn, @@ -784,12 +784,11 @@ export class Project extends ProjectBase { * @param secondaryArtifact the artifact to add as a secondary artifact * @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-multi-in-out.html */ - public addSecondaryArtifact(secondaryArtifact: Artifacts): any { + public addSecondaryArtifact(secondaryArtifact: IArtifacts): any { if (!secondaryArtifact.identifier) { throw new Error("The identifier attribute is mandatory for secondary artifacts"); } - secondaryArtifact._bind(this); - this._secondaryArtifacts.push(secondaryArtifact); + this._secondaryArtifacts.push(secondaryArtifact.bind(this, this).artifactsProperty); } /** @@ -875,7 +874,7 @@ export class Project extends ProjectBase { private renderSecondaryArtifacts(): CfnProject.ArtifactsProperty[] | undefined { return this._secondaryArtifacts.length === 0 ? undefined - : this._secondaryArtifacts.map((secondaryArtifact) => secondaryArtifact.toArtifactsJSON()); + : this._secondaryArtifacts; } /** @@ -935,9 +934,9 @@ export class Project extends ProjectBase { }; } - private validateCodePipelineSettings(artifacts: Artifacts) { + private validateCodePipelineSettings(artifacts: IArtifacts) { const sourceType = this.source.type; - const artifactsType = artifacts.toArtifactsJSON().type; + const artifactsType = artifacts.type; if ((sourceType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE || artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) &&