Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: add gulp task to only format changed files #24969

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions gulpfile.js
Expand Up @@ -29,6 +29,9 @@ function loadTask(fileName, taskName) {

gulp.task('format:enforce', loadTask('format', 'enforce'));
gulp.task('format', loadTask('format', 'format'));
gulp.task('format:untracked', loadTask('format', 'format-untracked'));
gulp.task('format:diff', loadTask('format', 'format-diff'));
gulp.task('format:changed', ['format:untracked', 'format:diff']);
gulp.task('build.sh', loadTask('build', 'all'));
gulp.task('build.sh:no-bundle', loadTask('build', 'no-bundle'));
gulp.task('lint', ['format:enforce', 'validate-commit-messages', 'tslint']);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -89,6 +89,8 @@
"gulp-clang-format": "1.0.23",
"gulp-connect": "5.0.0",
"gulp-conventional-changelog": "^2.0.3",
"gulp-filter": "^5.1.0",
"gulp-git": "^2.7.0",
"gulp-tslint": "8.1.2",
"hammerjs": "2.0.8",
"husky": "^0.14.3",
Expand Down
83 changes: 83 additions & 0 deletions tools/gulp-tasks/format.js
Expand Up @@ -29,6 +29,59 @@ const srcsToFmt = [
'!tools/ts-api-guardian/test/fixtures/**',
];

/**
* Gulp stream that wraps the gulp-git status,
* only returns untracked files, and converts
* the stdout into a stream of files.
*/
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
const RE_STATUS = /((\s\w)|(\w+)|\?{0,2})\s([\w\+\-\/\\\.]+)(\s->\s)?([\w\+\-\/\\\.]+)*\n{0,1}/gm;

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();

// We only care about untracked files and renamed files
if (!new RegExp(/r|\?/i).test(status)) {
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,
}));

RE_STATUS.lastIndex++;
}

srcStream.end();
});

return srcStream;
}

module.exports = {
// Check source code for formatting errors (clang-format)
enforce: (gulp) => () => {
Expand All @@ -45,5 +98,35 @@ module.exports = {
return gulp.src(srcsToFmt, {base: '.'})
.pipe(format.format('file', clangFormat))
.pipe(gulp.dest('.'));
},

// Format only the untracked source code files with clang-format (see .clang-format)
'format-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.format('file', clangFormat))
.pipe(gulp.dest('.'));
},

// 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})
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
.pipe(gulpFilter(srcsToFmt))
.pipe(format.format('file', clangFormat))
.pipe(gulp.dest('.'));
}
};