-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): move all types from "assets" to "core" (#7708)
Fold the "assets" module, which includes the `Staging` construct that takes care of staging asset files into the cloud assembly during synthesis into "core". This is in order to allow implementing custom resources that leverage assets throughout the framework. NOTE: since interfaces in @aws-cdk/assets are used as based types for stable APIs, we are unable to fully deprecate this module within the codebase, so these types are left in tact and a compatibility layer was added. A subsequent commit will add a mini-framework for custom resources that leverages this capability.
- Loading branch information
Elad Ben-Israel
committed
May 1, 2020
1 parent
5861d18
commit 4a84c96
Showing
26 changed files
with
416 additions
and
174 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SymlinkFollowMode } from '@aws-cdk/core'; | ||
import { FollowMode } from './fs/follow-mode'; | ||
|
||
export function toSymlinkFollow(follow?: FollowMode): SymlinkFollowMode | undefined { | ||
if (!follow) { | ||
return undefined; | ||
} | ||
|
||
switch (follow) { | ||
case FollowMode.NEVER: return SymlinkFollowMode.NEVER; | ||
case FollowMode.ALWAYS: return SymlinkFollowMode.ALWAYS; | ||
case FollowMode.BLOCK_EXTERNAL: return SymlinkFollowMode.BLOCK_EXTERNAL; | ||
case FollowMode.EXTERNAL: return SymlinkFollowMode.EXTERNAL; | ||
default: | ||
throw new Error(`unknown follow mode: ${follow}`); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './api'; | ||
export * from './fs/follow-mode'; | ||
export * from './fs/options'; | ||
export * from './staging'; | ||
export * from './staging'; |
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 |
---|---|---|
@@ -1,92 +1,29 @@ | ||
import { Construct, ISynthesisSession } from '@aws-cdk/core'; | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { copyDirectory, fingerprint, FingerprintOptions } from './fs'; | ||
import { AssetStaging, Construct } from '@aws-cdk/core'; | ||
import { toSymlinkFollow } from './compat'; | ||
import { FingerprintOptions } from './fs/options'; | ||
|
||
/** | ||
* Deprecated | ||
* @deprecated use `core.AssetStagingProps` | ||
*/ | ||
export interface StagingProps extends FingerprintOptions { | ||
/** | ||
* Local file or directory to stage. | ||
*/ | ||
readonly sourcePath: string; | ||
} | ||
|
||
/** | ||
* Stages a file or directory from a location on the file system into a staging | ||
* directory. | ||
* | ||
* This is controlled by the context key 'aws:cdk:asset-staging' and enabled | ||
* by the CLI by default in order to ensure that when the CDK app exists, all | ||
* assets are available for deployment. Otherwise, if an app references assets | ||
* in temporary locations, those will not be available when it exists (see | ||
* https://github.com/aws/aws-cdk/issues/1716). | ||
* | ||
* The `stagedPath` property is a stringified token that represents the location | ||
* of the file or directory after staging. It will be resolved only during the | ||
* "prepare" stage and may be either the original path or the staged path | ||
* depending on the context setting. | ||
* | ||
* The file/directory are staged based on their content hash (fingerprint). This | ||
* means that only if content was changed, copy will happen. | ||
* Deprecated | ||
* @deprecated use `core.AssetStaging` | ||
*/ | ||
export class Staging extends Construct { | ||
|
||
/** | ||
* The path to the asset (stringinfied token). | ||
* | ||
* If asset staging is disabled, this will just be the original path. | ||
* If asset staging is enabled it will be the staged path. | ||
*/ | ||
public readonly stagedPath: string; | ||
|
||
/** | ||
* The path of the asset as it was referenced by the user. | ||
*/ | ||
public readonly sourcePath: string; | ||
|
||
/** | ||
* A cryptographic hash of the source document(s). | ||
*/ | ||
public readonly sourceHash: string; | ||
|
||
private readonly fingerprintOptions: FingerprintOptions; | ||
|
||
private readonly relativePath?: string; | ||
|
||
export class Staging extends AssetStaging { | ||
constructor(scope: Construct, id: string, props: StagingProps) { | ||
super(scope, id); | ||
|
||
this.sourcePath = props.sourcePath; | ||
this.fingerprintOptions = props; | ||
this.sourceHash = fingerprint(this.sourcePath, props); | ||
|
||
const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT); | ||
if (stagingDisabled) { | ||
this.stagedPath = this.sourcePath; | ||
} else { | ||
this.relativePath = 'asset.' + this.sourceHash + path.extname(this.sourcePath); | ||
this.stagedPath = this.relativePath; // always relative to outdir | ||
} | ||
} | ||
|
||
protected synthesize(session: ISynthesisSession) { | ||
if (!this.relativePath) { | ||
return; | ||
} | ||
|
||
const targetPath = path.join(session.assembly.outdir, this.relativePath); | ||
|
||
// asset already staged | ||
if (fs.existsSync(targetPath)) { | ||
return; | ||
} | ||
|
||
// copy file/directory to staging directory | ||
const stat = fs.statSync(this.sourcePath); | ||
if (stat.isFile()) { | ||
fs.copyFileSync(this.sourcePath, targetPath); | ||
} else if (stat.isDirectory()) { | ||
fs.mkdirSync(targetPath); | ||
copyDirectory(this.sourcePath, targetPath, this.fingerprintOptions); | ||
} else { | ||
throw new Error(`Unknown file type: ${this.sourcePath}`); | ||
} | ||
super(scope, id, { | ||
sourcePath: props.sourcePath, | ||
exclude: props.exclude, | ||
extraHash: props.extraHash, | ||
follow: toSymlinkFollow(props.follow), | ||
}); | ||
} | ||
} |
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,15 @@ | ||
import { SymlinkFollowMode } from '@aws-cdk/core'; | ||
import { Test } from 'nodeunit'; | ||
import { FollowMode } from '../lib'; | ||
import { toSymlinkFollow } from '../lib/compat'; | ||
|
||
export = { | ||
'FollowMode compatibility'(test: Test) { | ||
test.equal(toSymlinkFollow(undefined), null); | ||
test.equal(toSymlinkFollow(FollowMode.ALWAYS), SymlinkFollowMode.ALWAYS); | ||
test.equal(toSymlinkFollow(FollowMode.BLOCK_EXTERNAL), SymlinkFollowMode.BLOCK_EXTERNAL); | ||
test.equal(toSymlinkFollow(FollowMode.EXTERNAL), SymlinkFollowMode.EXTERNAL); | ||
test.equal(toSymlinkFollow(FollowMode.NEVER), SymlinkFollowMode.NEVER); | ||
test.done(); | ||
}, | ||
}; |
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,98 @@ | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { Construct, ISynthesisSession } from './construct-compat'; | ||
import { FileSystem, FingerprintOptions } from './fs'; | ||
|
||
/** | ||
* Initialization properties for `AssetStaging`. | ||
*/ | ||
export interface AssetStagingProps extends FingerprintOptions { | ||
/** | ||
* The source file or directory to copy from. | ||
*/ | ||
readonly sourcePath: string; | ||
} | ||
|
||
/** | ||
* Stages a file or directory from a location on the file system into a staging | ||
* directory. | ||
* | ||
* This is controlled by the context key 'aws:cdk:asset-staging' and enabled | ||
* by the CLI by default in order to ensure that when the CDK app exists, all | ||
* assets are available for deployment. Otherwise, if an app references assets | ||
* in temporary locations, those will not be available when it exists (see | ||
* https://github.com/aws/aws-cdk/issues/1716). | ||
* | ||
* The `stagedPath` property is a stringified token that represents the location | ||
* of the file or directory after staging. It will be resolved only during the | ||
* "prepare" stage and may be either the original path or the staged path | ||
* depending on the context setting. | ||
* | ||
* 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 { | ||
|
||
/** | ||
* The path to the asset (stringinfied token). | ||
* | ||
* If asset staging is disabled, this will just be the original path. | ||
* If asset staging is enabled it will be the staged path. | ||
*/ | ||
public readonly stagedPath: string; | ||
|
||
/** | ||
* The path of the asset as it was referenced by the user. | ||
*/ | ||
public readonly sourcePath: string; | ||
|
||
/** | ||
* A cryptographic hash of the source document(s). | ||
*/ | ||
public readonly sourceHash: string; | ||
|
||
private readonly fingerprintOptions: FingerprintOptions; | ||
|
||
private readonly relativePath?: string; | ||
|
||
constructor(scope: Construct, id: string, props: AssetStagingProps) { | ||
super(scope, id); | ||
|
||
this.sourcePath = props.sourcePath; | ||
this.fingerprintOptions = props; | ||
this.sourceHash = FileSystem.fingerprint(this.sourcePath, props); | ||
|
||
const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT); | ||
if (stagingDisabled) { | ||
this.stagedPath = this.sourcePath; | ||
} else { | ||
this.relativePath = 'asset.' + this.sourceHash + path.extname(this.sourcePath); | ||
this.stagedPath = this.relativePath; // always relative to outdir | ||
} | ||
} | ||
|
||
protected synthesize(session: ISynthesisSession) { | ||
if (!this.relativePath) { | ||
return; | ||
} | ||
|
||
const targetPath = path.join(session.assembly.outdir, this.relativePath); | ||
|
||
// asset already staged | ||
if (fs.existsSync(targetPath)) { | ||
return; | ||
} | ||
|
||
// copy file/directory to staging directory | ||
const stat = fs.statSync(this.sourcePath); | ||
if (stat.isFile()) { | ||
fs.copyFileSync(this.sourcePath, targetPath); | ||
} else if (stat.isDirectory()) { | ||
fs.mkdirSync(targetPath); | ||
FileSystem.copyDirectory(this.sourcePath, targetPath, this.fingerprintOptions); | ||
} else { | ||
throw new Error(`Unknown file type: ${this.sourcePath}`); | ||
} | ||
} | ||
} |
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.