"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BuildSpec=void 0,exports.mergeBuildSpecs=mergeBuildSpecs;const JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti");var yaml_cfn=()=>{var tmp=require("./private/yaml-cfn");return yaml_cfn=()=>tmp,tmp},s3_assets=()=>{var tmp=require("../../aws-s3-assets");return s3_assets=()=>tmp,tmp},core_1=()=>{var tmp=require("../../core");return core_1=()=>tmp,tmp},literal_string_1=()=>{var tmp=require("../../core/lib/private/literal-string");return literal_string_1=()=>tmp,tmp};class BuildSpec{static[JSII_RTTI_SYMBOL_1]={fqn:"aws-cdk-lib.aws_codebuild.BuildSpec",version:"2.256.1"};static fromObject(value){return new ObjectBuildSpec(value)}static fromObjectToYaml(value){return new YamlBuildSpec(value)}static fromSourceFilename(filename){return new FilenameBuildSpec(filename)}static fromAsset(path){return new AssetBuildSpec(path)}constructor(){}}exports.BuildSpec=BuildSpec;class AssetBuildSpec extends BuildSpec{path;options;isImmediate=!0;asset;constructor(path,options={}){super(),this.path=path,this.options=options}toBuildSpec(scope){if(!scope)throw new(core_1()).UnscopedValidationError((0,literal_string_1().lit)`AssetBuildSpecRequiresScopeArgument`,"`AssetBuildSpec` requires a `scope` argument");if(!this.asset)this.asset=new(s3_assets()).Asset(scope,"Code",{path:this.path,...this.options});else if(core_1().Stack.of(this.asset)!==core_1().Stack.of(scope))throw new(core_1()).UnscopedValidationError((0,literal_string_1().lit)`AssetAlreadyAssociatedWithAnotherStack`,`Asset is already associated with another stack '${core_1().Stack.of(this.asset).stackName}'. Create a new BuildSpec instance for every stack.`);return this.asset.grantRead(scope),this.asset.bucket.arnForObjects(this.asset.s3ObjectKey)}toString(){return`<buildspec file: ${this.path}>`}}class FilenameBuildSpec extends BuildSpec{filename;isImmediate=!1;constructor(filename){super(),this.filename=filename}toBuildSpec(){return this.filename}toString(){return`<buildspec file: ${this.filename}>`}}class ObjectBuildSpec extends BuildSpec{spec;isImmediate=!0;constructor(spec){super(),this.spec=spec}toBuildSpec(){return core_1().Lazy.uncachedString({produce:ctx=>core_1().Stack.of(ctx.scope).toJsonString(this.spec,2)})}}class YamlBuildSpec extends BuildSpec{spec;isImmediate=!0;constructor(spec){super(),this.spec=spec}toBuildSpec(){return yaml_cfn().serialize(this.spec)}}function mergeBuildSpecs(lhs,rhs){if(!(lhs instanceof ObjectBuildSpec)||!(rhs instanceof ObjectBuildSpec))throw new(core_1()).UnscopedValidationError((0,literal_string_1().lit)`CanOnlyMergeBuildSpecsFromObject`,"Can only merge buildspecs created using BuildSpec.fromObject()");if(lhs.spec.version==="0.1")throw new(core_1()).UnscopedValidationError((0,literal_string_1().lit)`CannotExtendBuildSpecVersionZeroOne`,'Cannot extend buildspec at version "0.1". Set the version to "0.2" or higher instead.');if(lhs.spec.artifacts&&rhs.spec.artifacts)throw new(core_1()).UnscopedValidationError((0,literal_string_1().lit)`OnlyOneBuildSpecCanSpecifyArtifacts`,"Only one build spec is allowed to specify artifacts.");const lhsSpec=JSON.parse(JSON.stringify(lhs.spec)),rhsSpec=JSON.parse(JSON.stringify(rhs.spec));normalizeSpec(lhsSpec),normalizeSpec(rhsSpec);const merged=mergeDeep(lhsSpec,rhsSpec);return lhsSpec.reports&&rhsSpec.reports&&(merged.reports={...lhsSpec.reports,...rhsSpec.reports}),new ObjectBuildSpec(merged)}function normalizeSpec(spec){spec.env&&typeof spec.env["exported-variables"]=="string"&&(spec.env["exported-variables"]=[spec.env["exported-variables"]]);for(const key in spec.phases)Object.prototype.hasOwnProperty.call(spec.phases,key)&&normalizeSpecPhase(spec.phases[key]);if(spec.reports){for(const key in spec.reports)if(Object.prototype.hasOwnProperty.call(spec.reports,key)){const report=spec.reports[key];typeof report.files=="string"&&(report.files=[report.files])}}if(spec.artifacts){typeof spec.artifacts.files=="string"&&(spec.artifacts.files=[spec.artifacts.files]);for(const key in spec.artifacts["secondary-artifacts"])if(Object.prototype.hasOwnProperty.call(spec.artifacts["secondary-artifacts"],key)){const secArtifact=spec.artifacts["secondary-artifacts"][key];typeof secArtifact.files=="string"&&(secArtifact.files=[secArtifact.files])}}spec.cache&&typeof spec.cache.paths=="string"&&(spec.cache.paths=[spec.cache.paths])}function normalizeSpecPhase(phase){phase.commands&&typeof phase.commands=="string"&&(phase.commands=[phase.commands]),phase.finally&&typeof phase.finally=="string"&&(phase.finally=[phase.finally])}function mergeDeep(lhs,rhs){if(Array.isArray(lhs)&&Array.isArray(rhs))return[...lhs,...rhs];if(Array.isArray(lhs)||Array.isArray(rhs))return rhs;const isObject=obj=>obj&&typeof obj=="object";if(isObject(lhs)&&isObject(rhs)){const ret={...lhs};for(const k of Object.keys(rhs))ret[k]=k in lhs?mergeDeep(lhs[k],rhs[k]):rhs[k];return ret}return rhs}
