Skip to content

Commit

Permalink
Run in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Jun 4, 2017
1 parent cd76020 commit 753995d
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions tools/gulp/tasks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,13 @@ task('payload', ['material:clean-build'], async () => {
// Open a connection to Firebase. For PRs the connection will be established as a guest.
const database = openFirebaseDashboardApp(!isTravisMasterBuild());
const currentSha = process.env['TRAVIS_PULL_REQUEST_SHA'] || process.env['TRAVIS_COMMIT'];
const authToken = process.env['FIREBASE_ACCESS_TOKEN'];
const previousPayload = await getLastPayloadResults(database);

// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
const currentSize = results.cdk_fesm_2015 + results.material_fesm_2015;
const deltaSize = currentSize - previousSize;

// Publish the results to firebase when it runs on Travis but not as a PR.
if (isTravisMasterBuild()) {
await database.ref('payloads').child(currentSha).set(results);
}

if (authToken) {
// Update the Github status of the current commit by sending a request to the dashboard
// firebase http trigger function.
await updateGithubStatus(currentSha, deltaSize, authToken);
} else {
console.warn('Cannot set the Github status because there is no "FIREBASE_ACCESS_TOKEN" set.');
}
// Upload the payload results and calculate the payload diff in parallel. Otherwise the
// payload task will take much more time inside of Travis builds.
await Promise.all([
uploadPayloadResults(database, currentSha, results),
calculatePayloadDiff(database, currentSha, results)
]);

// Disconnect database connection because Firebase otherwise prevents Gulp from exiting.
database.delete();
Expand All @@ -71,6 +58,31 @@ function getFilesize(filePath: string) {
return statSync(filePath).size / 1000;
}

/**
* Calculates the difference between the last and current library payload.
* The results will be published as a commit status on Github.
*/
async function calculatePayloadDiff(database: any, currentSha: string, currentPayload: any) {
const authToken = process.env['FIREBASE_ACCESS_TOKEN'];

if (!authToken) {
console.error('Cannot calculate Payload diff because there is no "FIREBASE_ACCESS_TOKEN" ' +
'environment variable set.');
return;
}

const previousPayload = await getLastPayloadResults(database);

// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015;
const deltaSize = currentSize - previousSize;

// Update the Github status of the current commit by sending a request to the dashboard
// firebase http trigger function.
await updateGithubStatus(currentSha, deltaSize, authToken);
}

/**
* Updates the Github status of a given commit by sending a request to a Firebase function of
* the dashboard. The function has access to the Github repository and can set status for PRs too.
Expand Down Expand Up @@ -98,6 +110,13 @@ async function updateGithubStatus(commitSha: string, payloadDiff: number, authTo
});
}

/** Uploads the current payload results to the Dashboard database. */
async function uploadPayloadResults(database: any, currentSha: string, currentPayload: any) {
if (isTravisMasterBuild()) {
await database.ref('payloads').child(currentSha).set(currentPayload);
}
}

/** Gets the last payload uploaded from previous Travis builds. */
async function getLastPayloadResults(database: admin.database.Database) {
const snapshot = await database.ref('payloads')
Expand Down

0 comments on commit 753995d

Please sign in to comment.