"use strict";var _a,_b;Object.defineProperty(exports,"__esModule",{value:!0}),exports.DockerImageAsset=exports.NetworkMode=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),fs=require("fs"),path=require("path"),ecr=require("../../aws-ecr"),core_1=require("../../core"),cxapi=require("../../cx-api"),constructs_1=require("constructs"),assets_1=require("../../assets");class NetworkMode{constructor(mode){this.mode=mode}static fromContainer(containerId){return new NetworkMode(`container:${containerId}`)}static custom(mode){return new NetworkMode(mode)}}exports.NetworkMode=NetworkMode,_a=JSII_RTTI_SYMBOL_1,NetworkMode[_a]={fqn:"aws-cdk-lib.aws_ecr_assets.NetworkMode",version:"2.24.1"},NetworkMode.DEFAULT=new NetworkMode("default"),NetworkMode.HOST=new NetworkMode("host"),NetworkMode.NONE=new NetworkMode("none");class DockerImageAsset extends constructs_1.Construct{constructor(scope,id,props){var _c,_d,_e,_f,_g,_h,_j,_k,_l,_m,_o;super(scope,id);try{jsiiDeprecationWarnings.aws_cdk_lib_aws_ecr_assets_DockerImageAssetProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.constructor),error}validateProps(props);const dir=path.resolve(props.directory);if(!fs.existsSync(dir))throw new Error(`Cannot find image directory at ${dir}`);this.dockerfilePath=props.file||"Dockerfile";const file=path.join(dir,this.dockerfilePath);if(!fs.existsSync(file))throw new Error(`Cannot find file at ${file}`);const defaultIgnoreMode=core_1.FeatureFlags.of(this).isEnabled(cxapi.DOCKER_IGNORE_SUPPORT)?core_1.IgnoreMode.DOCKER:core_1.IgnoreMode.GLOB;let ignoreMode=(_c=props.ignoreMode)!==null&&_c!==void 0?_c:defaultIgnoreMode,exclude=props.exclude||[];const ignore=path.join(dir,".dockerignore");fs.existsSync(ignore)&&(exclude=[...fs.readFileSync(ignore).toString().split(`
`).filter(e=>!!e),...exclude,"!.dockerignore"]),exclude.push("!"+path.basename(file));const cdkout=(_e=(_d=core_1.Stage.of(this))===null||_d===void 0?void 0:_d.outdir)!==null&&_e!==void 0?_e:"cdk.out";exclude.push(cdkout),props.repositoryName&&core_1.Annotations.of(this).addWarning('DockerImageAsset.repositoryName is deprecated. Override "core.Stack.addDockerImageAsset" to control asset locations');const extraHash={};((_f=props.invalidation)===null||_f===void 0?void 0:_f.extraHash)!==!1&&props.extraHash&&(extraHash.user=props.extraHash),((_g=props.invalidation)===null||_g===void 0?void 0:_g.buildArgs)!==!1&&props.buildArgs&&(extraHash.buildArgs=props.buildArgs),((_h=props.invalidation)===null||_h===void 0?void 0:_h.target)!==!1&&props.target&&(extraHash.target=props.target),((_j=props.invalidation)===null||_j===void 0?void 0:_j.file)!==!1&&props.file&&(extraHash.file=props.file),((_k=props.invalidation)===null||_k===void 0?void 0:_k.repositoryName)!==!1&&props.repositoryName&&(extraHash.repositoryName=props.repositoryName),((_l=props.invalidation)===null||_l===void 0?void 0:_l.networkMode)!==!1&&props.networkMode&&(extraHash.networkMode=props.networkMode),extraHash.version="1.21.0";const staging=new core_1.AssetStaging(this,"Staging",{...props,follow:(_m=props.followSymlinks)!==null&&_m!==void 0?_m:toSymlinkFollow(props.follow),exclude,ignoreMode,sourcePath:dir,extraHash:Object.keys(extraHash).length===0?void 0:JSON.stringify(extraHash)});this.sourceHash=staging.assetHash,this.assetHash=staging.assetHash;const stack=core_1.Stack.of(this);this.assetPath=staging.relativeStagedPath(stack),this.dockerBuildArgs=props.buildArgs,this.dockerBuildTarget=props.target;const location=stack.synthesizer.addDockerImageAsset({directoryName:this.assetPath,dockerBuildArgs:this.dockerBuildArgs,dockerBuildTarget:this.dockerBuildTarget,dockerFile:props.file,sourceHash:staging.assetHash,networkMode:(_o=props.networkMode)===null||_o===void 0?void 0:_o.mode});this.repository=ecr.Repository.fromRepositoryName(this,"Repository",location.repositoryName),this.imageUri=location.imageUri}addResourceMetadata(resource,resourceProperty){try{jsiiDeprecationWarnings.aws_cdk_lib_CfnResource(resource)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addResourceMetadata),error}!this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)||(resource.cfnOptions.metadata=resource.cfnOptions.metadata||{},resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]=this.assetPath,resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY]=this.dockerfilePath,resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY]=this.dockerBuildArgs,resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY]=this.dockerBuildTarget,resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]=resourceProperty)}}exports.DockerImageAsset=DockerImageAsset,_b=JSII_RTTI_SYMBOL_1,DockerImageAsset[_b]={fqn:"aws-cdk-lib.aws_ecr_assets.DockerImageAsset",version:"2.24.1"};function validateProps(props){for(const[key,value]of Object.entries(props))if(core_1.Token.isUnresolved(value))throw new Error(`Cannot use Token as value of '${key}': this value is used before deployment starts`);validateBuildArgs(props.buildArgs)}function validateBuildArgs(buildArgs){for(const[key,value]of Object.entries(buildArgs||{}))if(core_1.Token.isUnresolved(key)||core_1.Token.isUnresolved(value))throw new Error('Cannot use tokens in keys or values of "buildArgs" since they are needed before deployment')}function toSymlinkFollow(follow){switch(follow){case void 0:return;case assets_1.FollowMode.NEVER:return core_1.SymlinkFollowMode.NEVER;case assets_1.FollowMode.ALWAYS:return core_1.SymlinkFollowMode.ALWAYS;case assets_1.FollowMode.BLOCK_EXTERNAL:return core_1.SymlinkFollowMode.BLOCK_EXTERNAL;case assets_1.FollowMode.EXTERNAL:return core_1.SymlinkFollowMode.EXTERNAL}}
//# sourceMappingURL=image-asset.js.map
