Skip to content

Commit

Permalink
fix(dartfmt): don't break win32 command line limit
Browse files Browse the repository at this point in the history
Closes #2420
Closes #1875
  • Loading branch information
caitp committed Jun 10, 2015
1 parent 4530b93 commit 617d693
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions tools/broccoli/broccoli-dartfmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function processToPromise(process) {
class DartFormatter implements DiffingBroccoliPlugin {
private DARTFMT: string;
private verbose: boolean;
private firstBuild: boolean = true;

constructor(public inputPath: string, public cachePath: string, options) {
if (!options.dartSDK) throw new Error("Missing Dart SDK");
Expand All @@ -30,34 +31,58 @@ class DartFormatter implements DiffingBroccoliPlugin {

rebuild(treeDiff: DiffResult): Promise<any> {
let args = ['-w'];
let argsLength = 2;
let argPackages = [];
let firstBuild = this.firstBuild;
treeDiff.addedPaths.concat(treeDiff.changedPaths)
.forEach((changedFile) => {
let sourcePath = path.join(this.inputPath, 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);
});
treeDiff.removedPaths.forEach((removedFile) => {
let destPath = path.join(this.cachePath, removedFile);
fse.removeSync(destPath);
});

if (args.length < 1) {
return Promise.resolve();
if (!firstBuild && args.length > 1) {
argPackages.push(args);
}
return new Promise((resolve, reject) => {
exec(this.DARTFMT + ' ' + args.join(' '), (err, stdout, stderr) => {
if (this.verbose) {
console.log(stdout);
}
if (err) {
console.error(shortenFormatterOutput(stderr));
reject('Formatting failed.');
} else {
resolve();
}

let execute = (args) => {
if (args.length < 2) return Promise.resolve();
return new Promise((resolve, reject) => {
exec(this.DARTFMT + ' ' + args.join(' '), (err, stdout, stderr) => {
if (this.verbose) {
console.log(stdout);
}
if (err) {
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));
}
}

Expand Down

0 comments on commit 617d693

Please sign in to comment.