@@ -2,16 +2,18 @@ import {task} from 'gulp';
2
2
import { join } from 'path' ;
3
3
import { statSync } from 'fs' ;
4
4
import { DIST_ROOT } from '../constants' ;
5
- import { spawnSync } from 'child_process' ;
6
- import { isTravisMasterBuild } from '../util/travis-ci' ;
5
+ import { isTravisBuild , isTravisMasterBuild } from '../util/travis-ci' ;
7
6
import { openFirebaseDashboardDatabase } from '../util/firebase' ;
7
+ import { setGithubStatus } from '../util/github' ;
8
+
9
+ // These imports lack of types.
10
+ const firebase = require ( 'firebase' ) ;
8
11
9
12
const bundlesDir = join ( DIST_ROOT , 'bundles' ) ;
10
13
11
14
/** Task which runs test against the size of material. */
12
- task ( 'payload' , [ 'material:clean-build' ] , ( ) => {
13
-
14
- let results = {
15
+ task ( 'payload' , [ 'material:clean-build' ] , async ( ) => {
16
+ const results = {
15
17
timestamp : Date . now ( ) ,
16
18
// Material bundles
17
19
material_umd : getBundleSize ( 'material.umd.js' ) ,
@@ -28,11 +30,32 @@ task('payload', ['material:clean-build'], () => {
28
30
// Print the results to the console, so we can read it from the CI.
29
31
console . log ( 'Payload Results:' , JSON . stringify ( results , null , 2 ) ) ;
30
32
31
- // Publish the results to firebase when it runs on Travis and not as a PR.
32
- if ( isTravisMasterBuild ( ) ) {
33
- return publishResults ( results ) ;
34
- }
33
+ if ( isTravisBuild ( ) ) {
34
+ // Open a connection to Firebase. For master builds the database will use an admin account.
35
+ const database = openFirebaseDashboardDatabase ( isTravisMasterBuild ( ) ) ;
36
+ const currentSha = process . env [ 'TRAVIS_COMMIT' ] ;
37
+ const previousPayload = await getLastPayloadResults ( database ) ;
38
+
39
+ // Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
40
+ const previousSize = previousPayload . cdk_fesm_2015 + previousPayload . material_fesm_2015 ;
41
+ const currentSize = results . cdk_fesm_2015 + results . material_fesm_2015 ;
42
+ const deltaSize = currentSize - previousSize ;
35
43
44
+ setGithubStatus ( process . env [ 'TRAVIS_COMMIT' ] , {
45
+ result : true ,
46
+ name : 'Library Payload' ,
47
+ url : `https://travis-ci.org/angular/material2/jobs/${ process . env [ 'TRAVIS_JOB_ID' ] } ` ,
48
+ description : `${ deltaSize > 0 ? `+` : '' } ${ deltaSize . toFixed ( 2 ) } KB`
49
+ } ) ;
50
+
51
+ // Publish the results to firebase when it runs on Travis and not as a PR.
52
+ if ( isTravisMasterBuild ( ) ) {
53
+ await database . ref ( 'payloads' ) . child ( currentSha ) . set ( results ) ;
54
+ }
55
+
56
+ // Disconnect database connection because Firebase otherwise prevents Gulp from exiting.
57
+ database . goOffline ( ) ;
58
+ }
36
59
} ) ;
37
60
38
61
/** Returns the size of the given library bundle. */
@@ -45,12 +68,14 @@ function getFilesize(filePath: string) {
45
68
return statSync ( filePath ) . size / 1000 ;
46
69
}
47
70
48
- /** Publishes the given results to the firebase database. */
49
- function publishResults ( results : any ) {
50
- let latestSha = spawnSync ( 'git' , [ 'rev-parse' , 'HEAD' ] ) . stdout . toString ( ) . trim ( ) ;
51
- let database = openFirebaseDashboardDatabase ( ) ;
71
+ /** Gets the last payload uploaded from previous Travis builds. */
72
+ async function getLastPayloadResults ( database : admin . database . Database ) {
73
+ const snapshot = await database . ref ( 'payloads' )
74
+ . orderByChild ( 'timestamp' )
75
+ . limitToLast ( 1 )
76
+ . once ( 'value' ) ;
52
77
53
- // Write the results to the payloads object with the latest Git SHA as key.
54
- return database . ref ( 'payloads' ) . child ( latestSha ) . set ( results )
55
- . then ( ( ) => database . goOffline ( ) , ( ) => database . goOffline ( ) ) ;
78
+ // The value of the DataSnapshot is an object with the SHA as a key. Only return the
79
+ // value of the object because the SHA is not necessary.
80
+ return snapshot . val ( ) [ Object . keys ( snapshot . val ( ) ) [ 0 ] ] ;
56
81
}
0 commit comments