Skip to content

Commit 5657b98

Browse files
authored
feat(cdk-v2): Adding build scripts for CDK v2 (#353)
* feat(cdk-v2): Adding build scripts for CDK v2 build of Solutions Constructs * feat(cdk-v2): Adding build scripts for CDK v2 build of Solutions Constructs * feat(cdk-v2): setting the version for v2 release * feat(cdk-v2): separating out rewrite import code into standalone script
1 parent fda4298 commit 5657b98

23 files changed

+1014
-2
lines changed

.versionrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"skip": { "tag": true },
2+
"skip": { "tag": true, "commit": true },
33
"packageFiles": [ { "filename": "source/lerna.json", "type": "json" } ],
44
"bumpFiles": [ { "filename": "source/lerna.json", "type": "json" } ]
55
}

CHANGELOG.v2.md

Whitespace-only changes.

deployment/bump.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@
1414
# --------------------------------------------------------------------------------------------------
1515
set -euo pipefail
1616
version=${1:-minor}
17+
deployment_dir=$(cd $(dirname $0) && pwd)
1718

1819
echo "Starting ${version} version bump"
1920

2021
# Generate CHANGELOG and create a commit
21-
npx standard-version --release-as ${version}
22+
npx standard-version --release-as ${version}
23+
24+
# Disabled the autocommit of 'standard-version' due to faulty CHANGELOG.md updates during CDK v2 build
25+
# and hence need to run git add/commit commands outside of 'standard-version'
26+
repoVersion=$(node -p "require('${deployment_dir}/get-version')")
27+
echo "repoVersion=${repoVersion}"
28+
29+
git add source/lerna.json
30+
git add CHANGELOG.md
31+
git commit -m "chore(release): ${repoVersion}"

deployment/v2/align-version.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env node
2+
// It will make following updates to package.json
3+
// 1 - align the version in a package.json file to the version of the repo
4+
// 2 - Remove all entries starting with @aws-cdk/* and constructs from "dependencies": { ... }
5+
// 3 - Remove all entries starting with @aws-cdk/* and constructs from "peerDependencies": { ... }, Add { "aws-cdk-lib": "^2.0.0-rc.1", "constructs": "^10.0.0" }
6+
// 4 - Add { "aws-cdk-lib": "2.0.0-rc.1", "constructs": "^10.0.0" } to "devDependencies"
7+
const fs = require('fs');
8+
9+
const findVersion = process.argv[2];
10+
const replaceVersion = process.argv[3];
11+
12+
// these versions need to be sourced from a config file
13+
const awsCdkLibVersion = '2.0.0-rc.16';
14+
const constructsVersion = '10.0.0';
15+
16+
for (const file of process.argv.splice(4)) {
17+
const pkg = JSON.parse(fs.readFileSync(file).toString());
18+
19+
if (pkg.version !== findVersion && pkg.version !== replaceVersion) {
20+
throw new Error(`unexpected - all package.json files in this repo should have a version of ${findVersion} or ${replaceVersion}: ${file}`);
21+
}
22+
23+
pkg.version = replaceVersion;
24+
25+
pkg.dependencies = processDependencies(pkg.dependencies || { }, file);
26+
pkg.peerDependencies = processPeerDependencies(pkg.peerDependencies || { }, file);
27+
pkg.devDependencies = processDevDependencies(pkg.devDependencies || { }, file);
28+
29+
console.error(`${file} => ${replaceVersion}`);
30+
fs.writeFileSync(file, JSON.stringify(pkg, undefined, 2));
31+
32+
}
33+
34+
function processDependencies(section, file) {
35+
let newdependencies = {};
36+
for (const [ name, version ] of Object.entries(section)) {
37+
// Remove all entries starting with @aws-cdk/* and constructs
38+
if (!name.startsWith('@aws-cdk/') && !name.startsWith('constructs')) {
39+
newdependencies[name] = version.replace(findVersion, replaceVersion);
40+
}
41+
}
42+
return newdependencies;
43+
}
44+
45+
function processPeerDependencies(section, file) {
46+
let newdependencies = {};
47+
for (const [ name, version ] of Object.entries(section)) {
48+
// Remove all entries starting with @aws-cdk/* and constructs
49+
if (!name.startsWith('@aws-cdk/') && !name.startsWith('constructs')) {
50+
newdependencies[name] = version.replace(findVersion, replaceVersion);
51+
}
52+
}
53+
newdependencies["aws-cdk-lib"] = `^${awsCdkLibVersion}`;
54+
newdependencies["constructs"] = `^${constructsVersion}`;
55+
return newdependencies;
56+
}
57+
58+
function processDevDependencies(section, file) {
59+
let newdependencies = section;
60+
for (const [ name, version ] of Object.entries(newdependencies)) {
61+
// Remove all entries starting with @aws-cdk/* and constructs
62+
if (version === findVersion || version === '^' + findVersion) {
63+
newdependencies[name] = version.replace(findVersion, replaceVersion);
64+
}
65+
}
66+
// note: no ^ to make sure we test against the minimum version
67+
newdependencies["aws-cdk-lib"] = `${awsCdkLibVersion}`;
68+
newdependencies["constructs"] = `^${constructsVersion}`;
69+
return newdependencies;
70+
}

deployment/v2/align-version.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
deployment_dir=$(cd $(dirname $0) && pwd)
5+
source_dir="$deployment_dir/../../source"
6+
7+
cd $deployment_dir
8+
# Retrieve version numbers for marker and repo
9+
marker=$(node -p "require('./get-version-marker')")
10+
repoVersion=$(node -p "require('./get-version')")
11+
12+
cd $source_dir/
13+
14+
# Align versions in ALL package.json with the one in lerna.json
15+
files=$(find . -name package.json |\
16+
grep -v node_modules)
17+
18+
if [ $# -eq 0 ]; then
19+
echo "Updating ALL package.json for CDK v2"
20+
${deployment_dir}/align-version.js ${marker} ${repoVersion} ${files}
21+
else
22+
echo "Reverting back CDK v2 updatesfrom ALL package.json files"
23+
git checkout `find . -name package.json | grep -v node_modules`
24+
fi

deployment/v2/build-cdk-dist.sh

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
deployment_dir=$(cd $(dirname $0) && pwd)
5+
source_dir="$deployment_dir/../source"
6+
dist_dir="$deployment_dir/dist"
7+
8+
cd $source_dir/
9+
export PATH=$(npm bin):$PATH
10+
export NODE_OPTIONS="--max-old-space-size=4096 ${NODE_OPTIONS:-}"
11+
12+
cd $deployment_dir/
13+
14+
echo "------------------------------------------------------------------------------"
15+
echo "[Copy] CDK templates for all patterns into the deployment dir for CfnNagScan"
16+
echo "------------------------------------------------------------------------------"
17+
18+
echo "mkdir -p $dist_dir"
19+
mkdir -p $dist_dir
20+
21+
for subdir in $source_dir/patterns/\@aws-solutions-constructs/* ; do
22+
if [ -d "$subdir" -a `basename $subdir` != "node_modules" ]; then
23+
cd $subdir/test
24+
25+
echo "Checking integ CFN templates in $subdir/test"
26+
cnt=`find . -name "*expected.json" -type f | wc -l`
27+
prefix=`basename $subdir`
28+
if [ "$prefix" != "core" ]
29+
then
30+
if [ "$cnt" -eq "0" ]
31+
then
32+
echo "************** [ERROR] ************* Did not find any integ CFN templates in $subdir; please add atleast one by writing an integ test case and running cdk-integ command to generate the CFN template for it"
33+
exit 1
34+
fi
35+
fi
36+
37+
echo "Copying templates from $subdir/test"
38+
for i in `find . -name "*expected.json" -type f`; do
39+
prefix=`basename $subdir`
40+
suffix=`basename $i`
41+
cp $subdir/test/$i $dist_dir/$prefix-$suffix.template
42+
done
43+
cd $source_dir
44+
fi
45+
done
46+
47+
echo "------------------------------------------------------------------------------"
48+
echo "[Copy] packages for all patterns into the deployment dir"
49+
echo "------------------------------------------------------------------------------"
50+
51+
echo "mkdir -p $dist_dir"
52+
mkdir -p $dist_dir
53+
54+
for dir in $(find $source_dir/patterns/\@aws-solutions-constructs/ -name dist | grep -v node_modules | grep -v coverage); do
55+
echo "Merging ${dir} into ${dist_dir}" >&2
56+
rsync -a $dir/ ${dist_dir}/
57+
done
58+
59+
echo "------------------------------------------------------------------------------"
60+
echo "[Create] build.json file"
61+
echo "------------------------------------------------------------------------------"
62+
# Get commit hash from CodePipeline env variable CODEBUILD_RESOLVED_SOURCE_VERSION
63+
echo $deployment_dir
64+
version=$(node -p "require('$deployment_dir/get-version.js')")
65+
commit="${CODEBUILD_RESOLVED_SOURCE_VERSION:-}"
66+
67+
cat > ${dist_dir}/build.json <<HERE
68+
{
69+
"name": "aws-solutions-constructs",
70+
"version": "${version}",
71+
"commit": "${commit}"
72+
}
73+
HERE
74+
75+
# copy CHANGELOG.md to dist/ for github releases
76+
changelog_file=$deployment_dir/../CHANGELOG.md
77+
cp ${changelog_file} ${dist_dir}/CHANGELOG.md
78+
79+
echo "------------------------------------------------------------------------------"
80+
echo "[List] deployment/dist contents"
81+
echo "------------------------------------------------------------------------------"
82+
83+
find $dist_dir

deployment/v2/build-patterns.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
deployment_dir=$(cd $(dirname $0) && pwd)
5+
source_dir="$deployment_dir/../../source"
6+
7+
echo "============================================================================================="
8+
echo "aligning versions and updating package.json for CDK v2..."
9+
/bin/bash $deployment_dir/align-version.sh
10+
11+
echo "============================================================================================="
12+
echo "updating Import statements for CDK v2..."
13+
/bin/bash $deployment_dir/rewrite-imports.sh
14+
15+
echo "============================================================================================="
16+
echo "building cdk-integ-tools..."
17+
cd $source_dir/tools/cdk-integ-tools
18+
npm install
19+
npm run build
20+
npm link
21+
22+
bail="--bail"
23+
runtarget="build+lint+test"
24+
cd $source_dir/
25+
26+
export PATH=$(npm bin):$PATH
27+
export NODE_OPTIONS="--max-old-space-size=4096 ${NODE_OPTIONS:-}"
28+
29+
echo "============================================================================================="
30+
echo "installing..."
31+
yarn install --frozen-lockfile
32+
33+
echo "============================================================================================="
34+
echo "building..."
35+
time lerna run $bail --stream $runtarget || fail
36+
37+
echo "============================================================================================="
38+
echo "packaging..."
39+
time lerna run --bail --stream jsii-pacmak || fail
40+
41+
echo "============================================================================================="
42+
echo "reverting back versions and updates to package.json for CDK v2..."
43+
/bin/bash $deployment_dir/align-version.sh revert
44+
45+
echo "============================================================================================="
46+
echo "reverting back Import statements for CDK v2..."
47+
/bin/bash $deployment_dir/rewrite-imports.sh revert

deployment/v2/bump.sh

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
# --------------------------------------------------------------------------------------------------
3+
#
4+
# This script is intended to be used to bump the version of the CDK modules, update package.json,
5+
# package-lock.json, and create a commit.
6+
#
7+
# to start a version bump, run:
8+
# bump.sh <version | version Type>
9+
#
10+
# If a version is not provided, the 'minor' version will be bumped.
11+
# The version can be an explicit version _or_ one of:
12+
# 'major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', or 'prerelease'.
13+
#
14+
# --------------------------------------------------------------------------------------------------
15+
set -euo pipefail
16+
version=${1:-prerelease}
17+
deployment_dir=$(cd $(dirname $0) && pwd)
18+
19+
echo "Starting ${version} version bump"
20+
echo "Loading ${deployment_dir}/get-version"
21+
22+
# Rename CHANGELOG.md
23+
echo "Rename CHANGELOG.md to CHANGELOG.md.bak"
24+
mv CHANGELOG.md CHANGELOG.md.bak
25+
echo "Rename CHANGELOG.v2.md to CHANGELOG.md"
26+
mv CHANGELOG.v2.md CHANGELOG.md
27+
28+
# Rename lerna.json
29+
echo "Rename source/lerna.json to source/lerna.json.bak"
30+
mv source/lerna.json source/lerna.json.bak
31+
echo "Rename source/lerna.v2.json to source/lerna.json"
32+
mv source/lerna.v2.json source/lerna.json
33+
34+
# `standard-release` will -- among other things -- create the changelog.
35+
# However, on the v2 branch, `conventional-changelog` (which `standard-release` uses) gets confused
36+
# and creates really muddled changelogs with both v1 and v2 releases intermingled, and lots of missing data.
37+
# A super HACK here is to locally remove all version tags that don't match this major version prior
38+
# to doing the bump, and then later fetching to restore those tags.
39+
git tag -d `git tag -l | grep -v '^v2.'`
40+
41+
# Generate CHANGELOG and create a commit
42+
npx standard-version --release-as ${version}
43+
44+
# fetch back the tags, and only the tags, removed locally above
45+
git fetch origin "refs/tags/*:refs/tags/*"
46+
47+
# Restore CHANGELOG.md
48+
echo "Rename CHANGELOG.md to CHANGELOG.v2.md"
49+
mv CHANGELOG.md CHANGELOG.v2.md
50+
echo "Rename CHANGELOG.md.bak to CHANGELOG.md"
51+
mv CHANGELOG.md.bak CHANGELOG.md
52+
53+
# Restore lerna.json
54+
echo "Rename source/lerna.json to source/lerna.v2.json"
55+
mv source/lerna.json source/lerna.v2.json
56+
echo "Rename source/lerna.json.bak to source/lerna.json"
57+
mv source/lerna.json.bak source/lerna.json
58+
59+
# Disabled the autocommit of 'standard-version' due to faulty CHANGELOG.md updates during CDK v2 build
60+
# and hence need to run git add/commit commands outside of 'standard-version'
61+
repoVersion=$(node -p "require('${deployment_dir}/get-version')")
62+
echo "repoVersion=${repoVersion}"
63+
64+
git add source/lerna.v2.json
65+
git add CHANGELOG.v2.md
66+
git commit -m "chore(release): ${repoVersion}"

deployment/v2/get-version-marker.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Returns the version marker used to indicate this is a local dependency.
3+
*
4+
* Usage:
5+
*
6+
* const version = require('./get-version-marker');
7+
*
8+
* Or from the command line:
9+
*
10+
* node -p require('./get-version-marker')
11+
*
12+
*/
13+
module.exports = '0.0.0';

deployment/v2/get-version.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Returns the current repo version.
3+
*
4+
* Usage:
5+
*
6+
* const version = require('./get-version');
7+
*
8+
* Or from the command line:
9+
*
10+
* node -p require('./get-version')
11+
*
12+
*/
13+
const versionFile = 'source/lerna.v2.json';
14+
if (!versionFile) {
15+
throw new Error(`unable to determine version filename from .versionrc.json at the root of the repo`);
16+
}
17+
18+
module.exports = require(`../../${versionFile}`).version;

deployment/v2/rewrite-imports.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
deployment_dir=$(cd $(dirname $0) && pwd)
5+
source_dir="$deployment_dir/../../source"
6+
7+
if [ ! -d $source_dir/tools/aws-cdk-migration/node_modules ]; then
8+
echo "============================================================================================="
9+
echo "building aws-cdk-migration..."
10+
cd $source_dir/tools/aws-cdk-migration
11+
npm install
12+
npm run build
13+
npm link
14+
else
15+
cd $source_dir/tools/aws-cdk-migration
16+
npm link
17+
fi
18+
19+
cd $source_dir/
20+
21+
if [ $# -eq 0 ]; then
22+
echo "Migrating TypeScript import statements from modular CDK (i.e. @aws-cdk/aws-s3) to aws-cdk-lib (i.e. aws-cdk-lib)"
23+
for subdir in $source_dir/patterns/\@aws-solutions-constructs/* ; do
24+
if [ -d "$subdir" -a `basename $subdir` != "node_modules" ]; then
25+
echo $subdir
26+
rewrite-imports-v2 $subdir/**/*.ts
27+
fi
28+
done
29+
else
30+
echo "Reverting back TypeScript import statements for CDK v2"
31+
git checkout `find . -name *.ts | grep -v node_modules | grep -v -F .d.ts`
32+
fi

0 commit comments

Comments
 (0)