Skip to content

Commit

Permalink
Change bind API in Artifacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jun 16, 2019
1 parent a06fc99 commit f88d482
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 48 deletions.
70 changes: 36 additions & 34 deletions packages/@aws-cdk/aws-codebuild/lib/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
},
};
}
}
Expand Down Expand Up @@ -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',
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
23 changes: 11 additions & 12 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -545,7 +545,7 @@ export interface ProjectProps extends CommonProjectProps {
*
* @default NoArtifacts
*/
readonly artifacts?: Artifacts;
readonly artifacts?: IArtifacts;

/**
* The secondary sources for the Project.
Expand All @@ -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[];
}

/**
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) &&
Expand Down

0 comments on commit f88d482

Please sign in to comment.