-
Notifications
You must be signed in to change notification settings - Fork 32
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
feat: allow globbing #23
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,14 @@ If your platform isn't yet supported, you can create the native binary from | |
the latest upstream clang sources, make sure it is stripped and optimized | ||
(should be about 1.4MB as of mid-2015) and send a pull request to add it. | ||
|
||
## Globbing files | ||
|
||
$ clang-format --glob=folder/**/*.js | ||
|
||
This will run `clang-format` once per file result, and show the total | ||
formatted files at the end. | ||
See [node-glob](https://github.com/isaacs/node-glob) for globbing semantics. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized that we don't afford a way to pass options - I was just trying to write up an example and need |
||
|
||
## Compiling clang-format | ||
|
||
For Linux, compile a statically linked MinSizeRel build: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ var os = require('os'); | |
var path = require('path'); | ||
var resolve = require('resolve').sync; | ||
var spawn = require('child_process').spawn; | ||
var glob = require("glob"); | ||
var async = require("async"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like a funny name for it now. Is the library worth having the dependency now, rather than just a for loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the operation itself (spawning a child process and waiting for it to end) is still async so a simple for loop wouldn't work. I could replicate |
||
|
||
var VERSION = require('./package.json').version; | ||
var LOCATION = __filename; | ||
|
@@ -48,11 +50,52 @@ function spawnClangFormat(args, done, stdio) { | |
"Consider installing it with your native package manager instead.\n"; | ||
throw new Error(message); | ||
} | ||
var clangFormatProcess = spawn(nativeBinary, args, {stdio: stdio}); | ||
clangFormatProcess.on('close', function(exit) { | ||
if (exit) done(exit); | ||
}); | ||
return clangFormatProcess; | ||
|
||
// extract glob, if present | ||
var filesGlob = args.filter(function(arg){return arg.indexOf('--glob=') === 0;}) | ||
.map(function(arg){return arg.replace('--glob=', '');}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I understand why you replace with empty string here, but then also filter it out at line 61 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I replace it in a copy of the array to just get the glob itself. Below I remove the glob arg from the args array itself, because otherwise clang would receive it and thus error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I see, it's because the glob pattern is part of the same arg string with the flag |
||
.shift(); | ||
|
||
if (filesGlob) { | ||
// remove glob from arg list | ||
args = args.filter(function(arg){return arg.indexOf('--glob=') === -1;}); | ||
|
||
return glob(filesGlob, function(er, files) { | ||
// split file array into chunks of 30 | ||
var i,j, chunks = [], chunkSize = 30; | ||
for (i=0,j=files.length; i<j; i+=chunkSize) { | ||
chunks.push( files.slice(i,i+chunkSize)); | ||
} | ||
|
||
// launch a new process for each chunk | ||
async.series( | ||
chunks.map(function(chunk) { | ||
return function(callback) { | ||
var clangFormatProcess = spawn(nativeBinary, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you confirm that clang-format can't take multiple files in one execution? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does:
so can we just run the binary a single time? any particular reason you want to do it this way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I didn't check this at the start and when I did I assumed it would be a bad idea to run the binary with a couple hundred individual files. That might be a bad assumption though. I'll try running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think clang-format would have any problem iterating through a large number of files, but you do eventually hit the limit of a command-line argument length. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. I'll do it like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows seems to have a 260 char path length max, and 8191 char cli command max. Dividing one by the other gives 30, so maybe that's reasonable safe value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct fix is to write the paths into a params file, and hand On Fri, Feb 12, 2016 at 6:53 AM Filipe Silva [email protected]
|
||
args.concat(chunk), | ||
{stdio: stdio}); | ||
clangFormatProcess.on('close', function(exit) { | ||
if (exit !== 0) callback(exit); | ||
else callback(null, exit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if the first one errors? Or if I give a badly formatted argument to clang-format? does it spew 200 errors to the command line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will at the moment, yeah. They are being run in a parallel up to a limit, and although I can try looking into not printing that output, or if running it all in one process works then that is fine as well. |
||
}); | ||
}; | ||
}), | ||
function(err, results) { | ||
if (err) done(err); | ||
console.log('\n'); | ||
console.log('ran clang-format with', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the user would rather see Also is there a way to print instead
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the message appearing as duplicate for you? It should only output once, as it does on my setup. The newline is there because otherwise it would appear without separation from the formatted content (when not in As far as I can tell, the only output from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I misread the indent level where the callback is registered, this only happens when async has finished the series? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. If it all of the series elements finish sucessfully, |
||
files.length, | ||
files.length === 1 ? 'file path' : 'files path'); | ||
done(results.shift() || 0); | ||
}); | ||
}); | ||
} else { | ||
var clangFormatProcess = spawn(nativeBinary, args, {stdio: stdio}); | ||
clangFormatProcess.on('close', function(exit) { | ||
if (exit) done(exit); | ||
}); | ||
return clangFormatProcess; | ||
} | ||
} | ||
|
||
function main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,13 @@ | |
}, | ||
"contributors": [ | ||
"Alex Eagle <[email protected]>", | ||
"Martin Probst <[email protected]>" | ||
"Martin Probst <[email protected]>", | ||
"Filipe Silva <[email protected]>" | ||
], | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"async": "^1.5.2", | ||
"glob": "^7.0.0", | ||
"resolve": "^1.1.6" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to document what the globbing semantics are. It's sufficient to point to the docs for the glob library we use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Roger, will add this.