fix(dartfmt): don't break win32 command line limit

Closes #2420
Closes #1875
This commit is contained in:
Caitlin Potter 2015-06-08 17:52:05 -04:00
parent 4530b93a6e
commit 617d693102
1 changed files with 40 additions and 15 deletions

View File

@ -21,6 +21,7 @@ function processToPromise(process) {
class DartFormatter implements DiffingBroccoliPlugin { class DartFormatter implements DiffingBroccoliPlugin {
private DARTFMT: string; private DARTFMT: string;
private verbose: boolean; private verbose: boolean;
private firstBuild: boolean = true;
constructor(public inputPath: string, public cachePath: string, options) { constructor(public inputPath: string, public cachePath: string, options) {
if (!options.dartSDK) throw new Error("Missing Dart SDK"); if (!options.dartSDK) throw new Error("Missing Dart SDK");
@ -30,11 +31,23 @@ class DartFormatter implements DiffingBroccoliPlugin {
rebuild(treeDiff: DiffResult): Promise<any> { rebuild(treeDiff: DiffResult): Promise<any> {
let args = ['-w']; let args = ['-w'];
let argsLength = 2;
let argPackages = [];
let firstBuild = this.firstBuild;
treeDiff.addedPaths.concat(treeDiff.changedPaths) treeDiff.addedPaths.concat(treeDiff.changedPaths)
.forEach((changedFile) => { .forEach((changedFile) => {
let sourcePath = path.join(this.inputPath, changedFile); let sourcePath = path.join(this.inputPath, changedFile);
let destPath = path.join(this.cachePath, changedFile); let destPath = path.join(this.cachePath, changedFile);
if (/\.dart$/.test(changedFile)) args.push(destPath); if (!firstBuild && /\.dart$/.test(changedFile)) {
if ((argsLength + destPath.length + 2) >= 0x2000) {
// Win32 command line arguments length
argPackages.push(args);
args = ['-w'];
argsLength = 2;
}
args.push(destPath);
argsLength += destPath.length + 2;
}
fse.copySync(sourcePath, destPath); fse.copySync(sourcePath, destPath);
}); });
treeDiff.removedPaths.forEach((removedFile) => { treeDiff.removedPaths.forEach((removedFile) => {
@ -42,22 +55,34 @@ class DartFormatter implements DiffingBroccoliPlugin {
fse.removeSync(destPath); fse.removeSync(destPath);
}); });
if (args.length < 1) { if (!firstBuild && args.length > 1) {
return Promise.resolve(); argPackages.push(args);
} }
return new Promise((resolve, reject) => {
exec(this.DARTFMT + ' ' + args.join(' '), (err, stdout, stderr) => { let execute = (args) => {
if (this.verbose) { if (args.length < 2) return Promise.resolve();
console.log(stdout); return new Promise((resolve, reject) => {
} exec(this.DARTFMT + ' ' + args.join(' '), (err, stdout, stderr) => {
if (err) { if (this.verbose) {
console.error(shortenFormatterOutput(stderr)); console.log(stdout);
reject('Formatting failed.'); }
} else { if (err) {
resolve(); console.error(shortenFormatterOutput(stderr));
} reject('Formatting failed.');
} else {
resolve();
}
});
}); });
}); };
if (firstBuild) {
// On firstBuild, format the entire cachePath
this.firstBuild = false;
return execute(['-w', this.cachePath]);
}
return Promise.all(argPackages.map(execute));
} }
} }