Skip to content

Commit 0609e29

Browse files
devversionmmalerba
authored andcommitted
build: create a util directory (#3558)
* Introduces a new directory called `util` in the gulp setup. This should make the gulp setup more clear and maintainable. * The utility `task_helpers` no longer includes random utilities like for Firebase. Those have been moved to their own util file (similar as in `e2e/`) * Refactors the Github Status utility because it can be useful for other things as well (e.g upcoming coverage, payload)
1 parent 529eaf8 commit 0609e29

16 files changed

+111
-89
lines changed

tools/gulp/tasks/aot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {task} from 'gulp';
22
import {join} from 'path';
33
import {DIST_ROOT} from '../constants';
4-
import {execNodeTask, sequenceTask} from '../task_helpers';
4+
import {execNodeTask, sequenceTask} from '../util/task_helpers';
55

66
/** Copies the source files of the demo-app to the dist folder. */
77
task('aot:copy', [':build:devapp:scss', ':build:devapp:assets']);

tools/gulp/tasks/clean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {task} from 'gulp';
22
import {DIST_ROOT} from '../constants';
3-
import {cleanTask} from '../task_helpers';
3+
import {cleanTask} from '../util/task_helpers';
44

55

66
task('clean', cleanTask(DIST_ROOT));

tools/gulp/tasks/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import {
88
sassBuildTask, tsBuildTask, execNodeTask, copyTask, sequenceTask,
99
triggerLivereload
10-
} from '../task_helpers';
10+
} from '../util/task_helpers';
1111

1212
// No typings for these.
1313
const inlineResources = require('../../../scripts/release/inline-resources');

tools/gulp/tasks/coverage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {task} from 'gulp';
22
import {existsSync} from 'fs-extra';
33
import {COVERAGE_RESULT_FILE} from '../constants';
44
import {spawnSync} from 'child_process';
5-
import {isTravisPushBuild, openFirebaseDashboardDatabase} from '../task_helpers';
5+
import {isTravisPushBuild} from '../util/travis-ci';
6+
import {openFirebaseDashboardDatabase} from '../util/firebase';
67

78
task('coverage:upload', () => {
89
if (!existsSync(COVERAGE_RESULT_FILE)) {

tools/gulp/tasks/development.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {DIST_ROOT, SOURCE_ROOT} from '../constants';
55
import {
66
sassBuildTask, tsBuildTask, copyTask, buildAppTask, vendorTask,
77
serverTask, sequenceTask, triggerLivereload
8-
} from '../task_helpers';
8+
} from '../util/task_helpers';
99

1010

1111
const appDir = path.join(SOURCE_ROOT, 'demo-app');

tools/gulp/tasks/e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {SOURCE_ROOT, DIST_ROOT, PROJECT_ROOT} from '../constants';
55
import {
66
tsBuildTask, copyTask, buildAppTask, execNodeTask,
77
vendorTask, sequenceTask, serverTask
8-
} from '../task_helpers';
8+
} from '../util/task_helpers';
99

1010
const gulpRunSequence = require('run-sequence');
1111
const gulpConnect = require('gulp-connect');

tools/gulp/tasks/lint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import gulp = require('gulp');
2-
import {execNodeTask} from '../task_helpers';
2+
import {execNodeTask} from '../util/task_helpers';
33

44
gulp.task('lint', ['tslint', 'stylelint', 'madge']);
55

tools/gulp/tasks/payload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {task} from 'gulp';
22
import {join} from 'path';
33
import {statSync, readFileSync} from 'fs';
44
import {DIST_COMPONENTS_ROOT} from '../constants';
5-
import {openFirebaseDashboardDatabase, isTravisPushBuild} from '../task_helpers';
65
import {spawnSync} from 'child_process';
6+
import {isTravisPushBuild} from '../util/travis-ci';
7+
import {openFirebaseDashboardDatabase} from '../util/firebase';
78

89
// Those imports lack types.
910
const uglifyJs = require('uglify-js');

tools/gulp/tasks/release.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import gulpRunSequence = require('run-sequence');
55
import path = require('path');
66
import minimist = require('minimist');
77

8-
import {execTask, cleanTask} from '../task_helpers';
8+
import {execTask, cleanTask} from '../util/task_helpers';
99
import {DIST_COMPONENTS_ROOT} from '../constants';
1010

1111
const argv = minimist(process.argv.slice(3));

tools/gulp/tasks/screenshots.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {task} from 'gulp';
22
import {readdirSync, statSync, existsSync, mkdirp} from 'fs-extra';
33
import * as path from 'path';
44
import * as admin from 'firebase-admin';
5-
import {openScreenshotsBucket, openFirebaseScreenshotsDatabase} from '../task_helpers';
6-
import {updateGithubStatus} from '../util-functions';
5+
import {openScreenshotsBucket, openFirebaseScreenshotsDatabase} from '../util/firebase';
6+
import {setGithubStatus} from '../util/github';
77

88
const imageDiff = require('image-diff');
99

@@ -19,7 +19,7 @@ task('screenshots', () => {
1919
return getScreenshotFiles(database)
2020
.then((files: any[]) => downloadAllGoldsAndCompare(files, database, prNumber))
2121
.then((results: boolean) => updateResult(database, prNumber, results))
22-
.then((result: boolean) => updateGithubStatus(result, prNumber))
22+
.then((result: boolean) => updateGithubStatus(prNumber, result))
2323
.then(() => uploadScreenshots('diff', prNumber))
2424
.then(() => uploadScreenshots('test', prNumber))
2525
.then(() => updateTravis(database, prNumber))
@@ -159,3 +159,13 @@ function setScreenFilenames(database: admin.database.Database,
159159
database.ref(FIREBASE_FILELIST);
160160
return filelistDatabase.set(filenames);
161161
}
162+
163+
/** Updates the Github Status of the given Pullrequest. */
164+
function updateGithubStatus(prNumber: number, result: boolean) {
165+
setGithubStatus(process.env['TRAVIS_PULL_REQUEST_SHA'], {
166+
result: result,
167+
name: 'Screenshot Tests',
168+
description: `Screenshot Tests ${result ? 'passed' : 'failed'})`,
169+
url: `http://material2-screenshots.firebaseapp.com/${prNumber}`
170+
});
171+
}

tools/gulp/tasks/serve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {task} from 'gulp';
2-
import {serverTask} from '../task_helpers';
2+
import {serverTask} from '../util/task_helpers';
33

44

55
task('serve', serverTask());

tools/gulp/tasks/unit-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path = require('path');
33
import gulpMerge = require('merge2');
44

55
import {PROJECT_ROOT, COMPONENTS_DIR} from '../constants';
6-
import {sequenceTask} from '../task_helpers';
6+
import {sequenceTask} from '../util/task_helpers';
77

88
const karma = require('karma');
99
const runSequence = require('run-sequence');

tools/gulp/util/firebase.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const firebaseAdmin = require('firebase-admin');
2+
const gcloud = require('google-cloud');
3+
4+
/** Opens a connection to the firebase realtime database. */
5+
export function openFirebaseDashboardDatabase() {
6+
// Initialize the Firebase application with admin credentials.
7+
// Credentials need to be for a Service Account, which can be created in the Firebase console.
8+
firebaseAdmin.initializeApp({
9+
credential: firebaseAdmin.credential.cert({
10+
project_id: 'material2-dashboard',
11+
client_email: 'firebase-adminsdk-ch1ob@material2-dashboard.iam.gserviceaccount.com',
12+
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
13+
// The line-breaks need to persist in the service account private key.
14+
private_key: (process.env['MATERIAL2_FIREBASE_PRIVATE_KEY'] || '').replace(/\\n/g, '\n')
15+
}),
16+
databaseURL: 'https://material2-dashboard.firebaseio.com'
17+
});
18+
19+
return firebaseAdmin.database();
20+
}
21+
22+
/**
23+
* Open Google Cloud Storage for screenshots.
24+
* The files uploaded to google cloud are also available to firebase storage.
25+
*/
26+
export function openScreenshotsBucket() {
27+
let gcs = gcloud.storage({
28+
projectId: 'material2-screenshots',
29+
credentials: {
30+
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
31+
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
32+
},
33+
});
34+
35+
// Reference the existing appspot bucket.
36+
return gcs.bucket('material2-screenshots.appspot.com');
37+
}
38+
39+
/** Opens a connection to the firebase database for screenshots. */
40+
export function openFirebaseScreenshotsDatabase() {
41+
// Initialize the Firebase application with admin credentials.
42+
// Credentials need to be for a Service Account, which can be created in the Firebase console.
43+
let screenshotApp = firebaseAdmin.initializeApp({
44+
credential: firebaseAdmin.credential.cert({
45+
project_id: 'material2-screenshots',
46+
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
47+
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
48+
}),
49+
databaseURL: 'https://material2-screenshots.firebaseio.com'
50+
}, 'material2-screenshots');
51+
52+
return screenshotApp.database();
53+
}
54+
55+
/** Decodes a Travis CI variable that is public in favor for PRs. */
56+
export function decode(str: string): string {
57+
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
58+
// The line-breaks need to persist in the service account private key.
59+
return (str || '').split('\\n').reverse().join('\\n').replace(/\\n/g, '\n');
60+
}
61+

tools/gulp/util-functions.ts renamed to tools/gulp/util/github.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
1+
import {MATERIAL_VERSION} from '../constants';
2+
13
const request = require('request');
24

3-
/** Update github pr status to success/failure */
4-
export function updateGithubStatus(result: boolean, prNumber: string) {
5-
let state = result ? 'success' : 'failure';
6-
let sha = process.env['TRAVIS_PULL_REQUEST_SHA'];
5+
/** Data that must be specified to set a Github PR status. */
6+
export type GithubStatusData = {
7+
result: boolean;
8+
name: string;
9+
description: string;
10+
url: string;
11+
};
12+
13+
/** Function that sets a Github commit status */
14+
export function setGithubStatus(commitSHA: number, statusData: GithubStatusData) {
15+
let state = statusData.result ? 'success' : 'failure';
716
let token = decode(process.env['MATERIAL2_GITHUB_STATUS_TOKEN']);
817

918
let data = JSON.stringify({
1019
state: state,
11-
target_url: `http://material2-screenshots.firebaseapp.com/${prNumber}`,
12-
context: 'screenshot-diff',
13-
description: `Screenshot test ${state}`
20+
target_url: statusData.url,
21+
context: statusData.name,
22+
description: statusData.description
1423
});
1524

1625
let headers = {
1726
'Authorization': `token ${token}`,
18-
'User-Agent': 'ScreenshotDiff/1.0.0',
27+
'User-Agent': `${statusData.name}/${MATERIAL_VERSION}`,
1928
'Content-Type': 'application/json',
2029
'Content-Length': Buffer.byteLength(data)
2130
};
2231

2332
return new Promise((resolve) => {
2433
request({
25-
url: `https://api.github.com/repos/angular/material2/statuses/${sha}`,
34+
url: `https://api.github.com/repos/angular/material2/statuses/${commitSHA}`,
2635
method: 'POST',
2736
form: data,
2837
headers: headers

tools/gulp/task_helpers.ts renamed to tools/gulp/util/task_helpers.ts

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as child_process from 'child_process';
22
import * as fs from 'fs';
33
import * as gulp from 'gulp';
44
import * as path from 'path';
5-
import {NPM_VENDOR_FILES, PROJECT_ROOT, DIST_ROOT, SASS_AUTOPREFIXER_OPTIONS} from './constants';
5+
import {NPM_VENDOR_FILES, PROJECT_ROOT, DIST_ROOT, SASS_AUTOPREFIXER_OPTIONS} from '../constants';
66

77

88
/** Those imports lack typings. */
@@ -14,8 +14,7 @@ const gulpSourcemaps = require('gulp-sourcemaps');
1414
const gulpAutoprefixer = require('gulp-autoprefixer');
1515
const gulpConnect = require('gulp-connect');
1616
const resolveBin = require('resolve-bin');
17-
const firebaseAdmin = require('firebase-admin');
18-
const gcloud = require('google-cloud');
17+
1918

2019

2120
/** If the string passed in is a glob, returns it, otherwise append '**\/*' to it. */
@@ -185,66 +184,3 @@ export function sequenceTask(...args: any[]) {
185184
);
186185
};
187186
}
188-
189-
/** Opens a connection to the firebase realtime database. */
190-
export function openFirebaseDashboardDatabase() {
191-
// Initialize the Firebase application with admin credentials.
192-
// Credentials need to be for a Service Account, which can be created in the Firebase console.
193-
firebaseAdmin.initializeApp({
194-
credential: firebaseAdmin.credential.cert({
195-
project_id: 'material2-dashboard',
196-
client_email: 'firebase-adminsdk-ch1ob@material2-dashboard.iam.gserviceaccount.com',
197-
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
198-
// The line-breaks need to persist in the service account private key.
199-
private_key: (process.env['MATERIAL2_FIREBASE_PRIVATE_KEY'] || '').replace(/\\n/g, '\n')
200-
}),
201-
databaseURL: 'https://material2-dashboard.firebaseio.com'
202-
});
203-
204-
return firebaseAdmin.database();
205-
}
206-
207-
/** Whether gulp currently runs inside of Travis as a push. */
208-
export function isTravisPushBuild() {
209-
return process.env['TRAVIS_PULL_REQUEST'] === 'false';
210-
}
211-
212-
/**
213-
* Open Google Cloud Storage for screenshots.
214-
* The files uploaded to google cloud are also available to firebase storage.
215-
*/
216-
export function openScreenshotsBucket() {
217-
let gcs = gcloud.storage({
218-
projectId: 'material2-screenshots',
219-
credentials: {
220-
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
221-
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
222-
},
223-
});
224-
225-
// Reference an existing bucket.
226-
return gcs.bucket('material2-screenshots.appspot.com');
227-
}
228-
229-
/** Opens a connection to the firebase realtime database for screenshots. */
230-
export function openFirebaseScreenshotsDatabase() {
231-
// Initialize the Firebase application with admin credentials.
232-
// Credentials need to be for a Service Account, which can be created in the Firebase console.
233-
let screenshotApp = firebaseAdmin.initializeApp({
234-
credential: firebaseAdmin.credential.cert({
235-
project_id: 'material2-screenshots',
236-
client_email: 'firebase-adminsdk-t4209@material2-screenshots.iam.gserviceaccount.com',
237-
private_key: decode(process.env['MATERIAL2_SCREENSHOT_FIREBASE_KEY'])
238-
}),
239-
databaseURL: 'https://material2-screenshots.firebaseio.com'
240-
}, 'material2-screenshots');
241-
242-
return screenshotApp.database();
243-
}
244-
245-
/** Decode the token for Travis to use. */
246-
function decode(str: string): string {
247-
// In Travis CI the private key will be incorrect because the line-breaks are escaped.
248-
// The line-breaks need to persist in the service account private key.
249-
return (str || '').split('\\n').reverse().join('\\n').replace(/\\n/g, '\n');
250-
}

tools/gulp/util/travis-ci.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Whether gulp currently runs inside of Travis as a push. */
2+
export function isTravisPushBuild() {
3+
return process.env['TRAVIS_PULL_REQUEST'] === 'false';
4+
}

0 commit comments

Comments
 (0)