Page 1 of 1

Need commands - how to?

Posted: Fri Nov 02, 2007 4:11 pm
by alex.barylski
Here is what I am trying to do:

1) I need a command which recursively scans a directory tree and collates each PHP scripts source and sends to screen:

Code: Select all

cat | find ./opt/lampp/htdocs/
The above doesn't work??? Everything I've Googled suggested using Perl in combination with cat. Is it not possible using CLI only? Ideally I would ike to use the cat option -n to display lines numbers for each file. Removal of comments using regex would be *huge* bonus. :D

2) Something similar to the above except I wish to extract strictly the class and function signatures. Basically:

Code: Select all

class MyBase extends MyChild{
AND

Code: Select all

function someMethod($p1, $p2){

Code: Select all

grep --recursive -n '[(^class(\w)+a-zA-Z(\w)+{)|(^function(\w)+a-zA-Z(\w)+{)]' /opt/lampp/htdocs/rapidcourier/*
This not surprisingly - doesn't work either, although my regex knowledge is limited...

I'd love to get these working, so any help is appreciated.

Cheers :)

Posted: Fri Nov 02, 2007 4:50 pm
by feyd
Look into using PHP's tokenizer.

General idea for (e)grep:

Code: Select all

feyd:/Volumes/Devel/Development feyd$ egrep -rni --include=*.php "^[[:space:]]*((abstract[[:space:]]+)?class|interface|(abstract[[:space:]]+)?((public|protected|private)[[:space:]]+)?function)[[:space:]]+[a-z_]+" ./
./Soverign/Library/Security/Permission.php:7:class DNAPermission extends DNAObject
./Soverign/Library/Security/Permission.php:12:  public function __construct( $foo = null )
./Soverign/Library/Security/Permission.php:23:  public function __get( $value )
./Soverign/Library/Types/Object.php:3:class DNAObject
./Soverign/Library/Types/Object.php:19: public function __construct( DNAProperty $property = null )
./Soverign/Library/Types/Object.php:41: protected function addProperty( $name, $value = null )
./Soverign/Library/Types/Object.php:55: protected function addPropertyEx( $name, $access, $value = null )
./Soverign/Library/Types/Property.php:3:class DNAProperty
./Soverign/Library/Types/Property.php:62:       public function __construct( $owner, $access = self::PROPERTY_PRIVATE )
./Soverign/Library/Types/Property.php:90:       public function set( $value )
./Soverign/Library/Types/Property.php:118:      public function get()
./Soverign/Library/Types/Property.php:137:      protected function allowed()
./Soverign/Library/Types/NameValue.php:4:class DNANamedSet extends DNASet
./Soverign/Library/Types/Set.php:9:class DNASet extends DNACollection
./Soverign/Library/Types/Set.php:18:    public function add( $element )
./Soverign/Library/Types/Set.php:36:    public function addAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:8:class DNACollection implements Iterator
./Soverign/Library/Types/Collection.php:31:     public function __construct( $input )
./Soverign/Library/Types/Collection.php:89:     public function rewind()
./Soverign/Library/Types/Collection.php:99:     public function current()
./Soverign/Library/Types/Collection.php:109:    public function key()
./Soverign/Library/Types/Collection.php:119:    public function next()
./Soverign/Library/Types/Collection.php:129:    public function valid()
./Soverign/Library/Types/Collection.php:141:    public function add( $element )
./Soverign/Library/Types/Collection.php:198:    public function addAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:221:    public function clear()
./Soverign/Library/Types/Collection.php:232:    public function contains( $element )
./Soverign/Library/Types/Collection.php:244:    public function containsAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:259:    public function isEmpty()
./Soverign/Library/Types/Collection.php:270:    public function remove( $element )
./Soverign/Library/Types/Collection.php:290:    public function removeAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:307:    public function retainAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:319:    public function size()
./Soverign/Library/Types/Collection.php:329:    public function toArray()

Posted: Fri Nov 02, 2007 5:28 pm
by Christopher
You need to look into the Unix find command. The syntax is a little weird, but it will find only specific files in a tree and run commands on each.

Posted: Fri Nov 02, 2007 5:36 pm
by alex.barylski
I'm using a PHP token iterator as we speak to extract that information but I figured why not try and keep it to CLI and native LInux commands, so I don't have to worry about dragging scripts around on each project.

Posted: Fri Nov 02, 2007 7:28 pm
by alex.barylski
arborint wrote:You need to look into the Unix find command. The syntax is a little weird, but it will find only specific files in a tree and run commands on each.
I tried using find but couldn't figure out how to pipe commands from find to cat or visa versa... :?

Posted: Fri Nov 02, 2007 9:09 pm
by VladSun

Code: Select all

find ./ -name '*.php' -exec cat {} ';'
cat `find ./ -name *.php`

Posted: Fri Nov 02, 2007 9:18 pm
by Christopher
Bulgarian Linux users rock!!! :)

Posted: Sat Nov 03, 2007 6:47 am
by VladSun
:D :D :D

They really do !

Posted: Sat Nov 10, 2007 6:33 am
by Ollie Saunders
I this is what you're after:

Code: Select all

grep -nr --include='*.php' -E '(function[ \t\r\n\v\f]+&?[_a-zA-Z][_a-zA-Z0-9]*[ \t\r\n\v\f]*\(|class[ \t\r\n\v\f]+[_a-zA-Z][_a-zA-Z0-9]*.*\{)' .
add "| less" to it to view in a more human friendly way. I couldn't seem to get the POSIX bracket expressions such as [:space:] or back-references to work so that's why it's so long. I copped out at the end of the class match with .* where a more complete regex would match specifically for extends and implements etc.