| 
									
										
										
										
											2017-07-20 10:54:07 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const {I18N_FOLDER, I18N_DATA_FOLDER} = require('./cldr/extract'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  | // clang-format entry points
 | 
					
						
							|  |  |  | const srcsToFmt = [ | 
					
						
							| 
									
										
										
										
											2020-03-23 09:29:27 -07:00
										 |  |  |   'dev-infra/**/*.{js,ts}', | 
					
						
							| 
									
										
										
										
											2017-03-07 11:04:30 -08:00
										 |  |  |   'packages/**/*.{js,ts}', | 
					
						
							| 
									
										
										
										
											2019-06-26 21:22:03 +03:00
										 |  |  |   '!packages/zone.js',  // Ignore the `zone.js/` directory itself. (The contents are still matched.)
 | 
					
						
							| 
									
										
										
										
											2019-06-26 21:19:42 +03:00
										 |  |  |   `!${I18N_DATA_FOLDER}/**/*.{js,ts}`, | 
					
						
							|  |  |  |   `!${I18N_FOLDER}/available_locales.ts`, | 
					
						
							|  |  |  |   `!${I18N_FOLDER}/currencies.ts`, | 
					
						
							|  |  |  |   `!${I18N_FOLDER}/locale_en.ts`, | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   'modules/benchmarks/**/*.{js,ts}', | 
					
						
							|  |  |  |   'modules/e2e_util/**/*.{js,ts}', | 
					
						
							|  |  |  |   'modules/playground/**/*.{js,ts}', | 
					
						
							|  |  |  |   'tools/**/*.{js,ts}', | 
					
						
							| 
									
										
										
										
											2019-06-26 21:19:42 +03:00
										 |  |  |   '!tools/gulp-tasks/cldr/extract.js', | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   '!tools/public_api_guard/**/*.d.ts', | 
					
						
							| 
									
										
										
										
											2019-06-26 21:19:42 +03:00
										 |  |  |   '!tools/ts-api-guardian/test/fixtures/**', | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   './*.{js,ts}', | 
					
						
							| 
									
										
										
										
											2017-08-02 19:15:30 -07:00
										 |  |  |   '!**/node_modules/**', | 
					
						
							|  |  |  |   '!**/dist/**', | 
					
						
							|  |  |  |   '!**/built/**', | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   '!shims_for_IE.js', | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-09-10 20:19:14 -05:00
										 |  |  |  * Gulp stream that wraps the gulp-git status, | 
					
						
							|  |  |  |  * only returns untracked files, and converts | 
					
						
							|  |  |  |  * the stdout into a stream of files. | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  |  */ | 
					
						
							|  |  |  | function gulpStatus() { | 
					
						
							|  |  |  |   const Vinyl = require('vinyl'); | 
					
						
							|  |  |  |   const path = require('path'); | 
					
						
							|  |  |  |   const gulpGit = require('gulp-git'); | 
					
						
							|  |  |  |   const through = require('through2'); | 
					
						
							|  |  |  |   const srcStream = through.obj(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const opt = {cwd: process.cwd()}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // https://git-scm.com/docs/git-status#_short_format
 | 
					
						
							| 
									
										
										
										
											2018-09-10 20:19:14 -05:00
										 |  |  |   const RE_STATUS = /((\s\w)|(\w+)|\?{0,2})\s([\w\+\-\/\\\.]+)(\s->\s)?([\w\+\-\/\\\.]+)*\n{0,1}/gm; | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   gulpGit.status({args: '--porcelain', quiet: true}, function(err, stdout) { | 
					
						
							|  |  |  |     if (err) return srcStream.emit('error', err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = stdout.toString(); | 
					
						
							|  |  |  |     let currentMatch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((currentMatch = RE_STATUS.exec(data)) !== null) { | 
					
						
							|  |  |  |       // status
 | 
					
						
							|  |  |  |       const status = currentMatch[1].trim().toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 20:19:14 -05:00
										 |  |  |       // We only care about untracked files and renamed files
 | 
					
						
							|  |  |  |       if (!new RegExp(/r|\?/i).test(status)) { | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // file path
 | 
					
						
							|  |  |  |       const currentFilePath = currentMatch[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // new file path in case its been moved
 | 
					
						
							|  |  |  |       const newFilePath = currentMatch[6]; | 
					
						
							|  |  |  |       const filePath = newFilePath || currentFilePath; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       srcStream.write(new Vinyl({ | 
					
						
							|  |  |  |         path: path.resolve(opt.cwd, filePath), | 
					
						
							|  |  |  |         cwd: opt.cwd, | 
					
						
							|  |  |  |       })); | 
					
						
							| 
									
										
										
										
											2018-09-10 20:19:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       RE_STATUS.lastIndex++; | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     srcStream.end(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return srcStream; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-03-23 08:53:42 -07:00
										 |  |  |   // Check source code for formatting errors with clang-format
 | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   enforce: (gulp) => () => { | 
					
						
							|  |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     return gulp.src(srcsToFmt).pipe( | 
					
						
							|  |  |  |         format.checkFormat('file', clangFormat, {verbose: true, fail: true})); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 08:53:42 -07:00
										 |  |  |   // Check only the untracked source code files for formatting errors with .clang-format
 | 
					
						
							|  |  |  |   'enforce-untracked': (gulp) => () => { | 
					
						
							|  |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     const gulpFilter = require('gulp-filter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return gulpStatus() | 
					
						
							|  |  |  |         .pipe(gulpFilter(srcsToFmt)) | 
					
						
							|  |  |  |         .pipe(format.checkFormat('file', clangFormat, {verbose: true, fail: true})); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check only the changed source code files diffed from the provided branch for formatting
 | 
					
						
							|  |  |  |   // errors with clang-format
 | 
					
						
							|  |  |  |   'enforce-diff': (gulp) => () => { | 
					
						
							|  |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     const gulpFilter = require('gulp-filter'); | 
					
						
							|  |  |  |     const minimist = require('minimist'); | 
					
						
							|  |  |  |     const gulpGit = require('gulp-git'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const args = minimist(process.argv.slice(2)); | 
					
						
							|  |  |  |     const branch = args.branch || 'master'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return gulpGit.diff(branch, {log: false}) | 
					
						
							|  |  |  |         .pipe(gulpFilter(srcsToFmt)) | 
					
						
							|  |  |  |         .pipe(format.checkFormat('file', clangFormat, {verbose: true, fail: true})); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   // Format the source code with clang-format (see .clang-format)
 | 
					
						
							|  |  |  |   format: (gulp) => () => { | 
					
						
							|  |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     return gulp.src(srcsToFmt, {base: '.'}) | 
					
						
							|  |  |  |         .pipe(format.format('file', clangFormat)) | 
					
						
							|  |  |  |         .pipe(gulp.dest('.')); | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 20:19:14 -05:00
										 |  |  |   // Format only the untracked source code files with clang-format (see .clang-format)
 | 
					
						
							|  |  |  |   'format-untracked': (gulp) => () => { | 
					
						
							| 
									
										
										
										
											2018-07-18 11:09:55 -05:00
										 |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     const gulpFilter = require('gulp-filter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return gulpStatus() | 
					
						
							|  |  |  |         .pipe(gulpFilter(srcsToFmt)) | 
					
						
							|  |  |  |         .pipe(format.format('file', clangFormat)) | 
					
						
							|  |  |  |         .pipe(gulp.dest('.')); | 
					
						
							| 
									
										
										
										
											2018-09-07 11:54:47 -05:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Format only the changed source code files diffed from the provided branch with clang-format
 | 
					
						
							|  |  |  |   // (see .clang-format)
 | 
					
						
							|  |  |  |   'format-diff': (gulp) => () => { | 
					
						
							|  |  |  |     const format = require('gulp-clang-format'); | 
					
						
							|  |  |  |     const clangFormat = require('clang-format'); | 
					
						
							|  |  |  |     const gulpFilter = require('gulp-filter'); | 
					
						
							|  |  |  |     const minimist = require('minimist'); | 
					
						
							|  |  |  |     const gulpGit = require('gulp-git'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const args = minimist(process.argv.slice(2)); | 
					
						
							|  |  |  |     const branch = args.branch || 'master'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return gulpGit.diff(branch, {log: false}) | 
					
						
							|  |  |  |         .pipe(gulpFilter(srcsToFmt)) | 
					
						
							|  |  |  |         .pipe(format.format('file', clangFormat)) | 
					
						
							|  |  |  |         .pipe(gulp.dest('.')); | 
					
						
							| 
									
										
										
										
											2017-02-03 08:10:41 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | }; |