Skip to content

Commit

Permalink
Use tar Module and Cleanup
Browse files Browse the repository at this point in the history
Use the tar node module instead of shelling out to tar. This allows us
to stream the download contents directly to unpacking and removes the
need to manually parse the beginning of the commit id hash to traverse
into the build directory.
  • Loading branch information
MrArnoldPalmer committed Feb 10, 2020
1 parent 30143b3 commit 6560d16
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
1 change: 0 additions & 1 deletion buildspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ phases:
build:
commands:
- yarn build && yarn test
- yarn test:integ
post_build:
commands:
- '[ ${CODEBUILD_BUILD_SUCCEEDING} = 1 ] && yarn package'
Expand Down
5 changes: 3 additions & 2 deletions packages/@jsii/integ-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"dotenv": "^8.2.0",
"fs-extra": "^8.1.0",
"jest": "^25.1.0",
"jsii": "^0.21.2",
"jsii-pacmak": "^0.21.2",
"jsii": "^0.22.0",
"jsii-pacmak": "^0.22.0",
"tar": "^6.0.1",
"typescript": "~3.7.5"
},
"jest": {
Expand Down
74 changes: 49 additions & 25 deletions packages/@jsii/integ-test/test/build-cdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mkdtemp, remove } from 'fs-extra';
import * as path from 'path';
import * as Octokit from '@octokit/rest';
import { downloadReleaseAsset, minutes, ProcessManager, writeFileStream } from '../utils';
import { downloadReleaseAsset, minutes, ProcessManager, extractFileStream } from '../utils';
import * as dotenv from 'dotenv';

dotenv.config();
Expand All @@ -28,45 +28,69 @@ describe('Build CDK', () => {

test('can build latest cdk release', async () => {
// download latest release info
console.time('cdkbuild');
const release = await octokit.repos.getLatestRelease({
owner: 'aws',
repo: 'aws-cdk'
});

// save code to tmp dir
const fileName = 'cdk.tar.gz';
const tarFile = path.join(buildDir, fileName);
// download and extract code
const code = await downloadReleaseAsset(`https://api.github.com/repos/aws/aws-cdk/tarball/${release.data.tag_name}`);

await writeFileStream(code, tarFile);

// unzip tar archive
await processes.spawn('tar', ['-xzf', fileName], {
cwd: buildDir
});

// root dir of extracted src
// `${buildDir}/${owner}-${repo}-${first 7 chars of commit hash}
const srcDir = path.join(buildDir, `aws-aws-cdk-${release.data.target_commitish.substring(0, 7)}`);
await extractFileStream(code, buildDir);

// install cdk dependencies
await processes.spawn('yarn', ['install'], {
cwd: srcDir
});

// link local jsii/jsii-pacmak builds
await processes.spawn('rm', ['-rf', './node_modules/jsii'], { cwd: srcDir });
await processes.spawn('rm', ['-rf', './node_modules/jsii-pacmak'], { cwd: srcDir });
await processes.spawn('ln', ['-s', JSII_DIR, './node_modules'], { cwd: srcDir });
await processes.spawn('ln', ['-s', JSII_PACMAK_DIR, './node_modules'], { cwd: srcDir });
// build cdk build tools
await processes.spawn('npx', [
'lerna',
'run',
'--scope',
'cdk-build-tools',
'--scope',
'pkglint',
'--scope',
'awslint',
'build',
], { cwd: srcDir });

// build cdk
await processes.spawn('npx', ['lerna', 'run', 'build', '--stream'], { cwd: srcDir });
// build jsii modules
await processes.spawn('npx', [
'lerna',
'run',
'--stream',
'--scope',
'@aws-cdk/*',
'--scope',
'aws-cdk',
'build',
'--',
'--jsii',
path.join(JSII_DIR, 'bin', 'jsii'),
], { cwd: srcDir });

// package modules
await processes.spawn('yarn', ['run', 'pack'], { cwd: srcDir });
// build the rest
await processes.spawn('npx', [
'lerna',
'run',
'--stream',
'--ignore',
'@aws-cdk/*',
'--ignore',
'aws-cdk',
'--ignore',
'cdk-build-tools',
'build',
], { cwd: srcDir });

console.timeEnd('cdkbuild');
// package modules
await processes.spawn('./pack.sh', [], {
cwd: srcDir,
env: {
PACMAK: path.join(JSII_PACMAK_DIR, 'bin', 'jsii-pacmak')
}
});
}, minutes(60));
});
10 changes: 5 additions & 5 deletions packages/@jsii/integ-test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from 'stream';
import { createWriteStream } from 'fs';
import { extract } from 'tar';
import * as cp from 'child_process';
import * as https from 'https';
import { IncomingMessage } from 'http';
Expand Down Expand Up @@ -27,7 +27,7 @@ export class ProcessManager {
/**
* kill all still running processes
*
* @param [signal] - signal sent to terminate process
* @param signal sent to terminate process
*/
async killAll(signal?: string) {
const values = Object.values(this.processes);
Expand Down Expand Up @@ -85,11 +85,11 @@ export class ProcessManager {
* write downloaded asset to file
*
* @param source stream
* @param destination of saved file
* @param destination directory for extracted files
*/
export function writeFileStream(source: Readable, destination: string) {
export function extractFileStream(source: Readable, destination: string) {
return new Promise((ok, ko) => {
const destStream = createWriteStream(destination);
const destStream = extract({ cwd: destination });
destStream.once('close', ok);
destStream.once('error', ko);
source.once('error', ko);
Expand Down

0 comments on commit 6560d16

Please sign in to comment.