I was listening to “Modernize or Die® - CFML News for October 15th, 2019” this morning, and heard Andrew Davis ask “Is there anything in ColdFusion somewhere to grep, aside from just doing a
cfexecute on the grep?”. The link to the exact point in the awesome podcast video stream is here.
This got me thinking.. yeah, not normally a good sign.
I know that you can read files in Java 8 using the streams, so in theory you could perform at least a regex filter on file contents as a pseudo-grep.
So, I got writing during my lunch break and came up with
Before anyone judges me on anything, this was literally a 20 minute hack to play around and see what I could come up with.
As an aside, I only started listening to the podcast yesterday, so I have a lot to catch up on, but I already love it and I think the Ortus team are doing an amazing job with it! Needless to say, I’m now a subscriber.
At the moment it only handles a grep filter on a single file, although I plan to add directory support to it as iterating over directories using streams will be relatively straight-forward and fast.
I’ve broken the component down into simple chained method calls.
So, example one and I’m searching for every instance of
public within the given file. In these examples, I’m actually grepping against the
grep.cfc file itself; I’m not sure if that is “grep-ception”.
Firstly we provide a pattern to match (
pattern()), the we set a relative path to the file to search (
Finally, to complete the process we call the
grep() method which creates the initial Java stream on the file, pulling out all of the lines contained within. The component then creates a Java Predicate using the pattern string value sent via the
pattern() method call. This predicate is then used to filter any lines that match the given pattern, before terminating the stream by returning an array of results.
The resulting dump from the above example is here:
Running a second example, I’m trying to grep on my domain name:
This results in just the one item coming back in the array:
It doesn’t return line numbers when perhaps it should. Agsin, before anyone judges me on anything, this was literally a 20 minute hack to play around and see what I could come up with.
You all know I love
cbStreams. It could do most if not all of this as well, but I wanted to try and come up with something quickly without dependencies for an extra challenge.
It is available on Github, but the bare bones are below in the gist.
That’s about it. Just another quick experiment to see if something was possible.