Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lint all the files recursive while printing out only files that have an error?

I want to lint all the files in the current (recursive) directory while printing out only files that have an error, and assign a variable to 1 to be used after the linting is finished.

#!/bin/bash

lint_failed=0
find . -path ./vendor -prune -o -name '*.php' | parallel -j 4 sh -c 'php -l {} || echo -e "[FAIL] {}" && lint_failed=1';

if [ "$lint_failed" -eq "1" ]; then
    exit 1
fi

Example:

[FAIL] ./app/Model/Example.php

The above code doesn't find any errors, but if I run php -l ./app/Model/Example.php an error is returned.

like image 378
Michael Delle Avatar asked Jan 03 '18 20:01

Michael Delle


2 Answers

The parallel command already does what you want: it exits 0 if all jobs exit 0, and it exits non-zero if any one job exits non-zero. parallel's exit options are configurable, see the EXIT STATUS section of man parallel for details.

In your script, the use of || echo obscures the exit status of the jobs, but you can expose this again doing something like this (tested bash 4.4.7 on ubuntu):

#!/bin/bash

php_lint_file()
{
    local php_file="$1"
    php -l "$php_file" &> /dev/null
    if [ "$?" -ne 0 ]
    then
        echo -e "[FAIL] $php_file"
        return 1
    fi
}

export -f php_lint_file

find . -path ./vendor -prune -o -name '*.php' | parallel -j 4 php_lint_file {}

if [ "$?" -ne 0 ]
then
    exit 1
fi
like image 122
astrangeloop Avatar answered Oct 28 '22 16:10

astrangeloop


You can use PHP Parallel Lint tool which checks the syntax of PHP files faster and with a fancier output by running parallel jobs while printing out only files with the errors.

Example usage:

./bin/parallel-lint --exclude app --exclude vendor .

Or using Ant's build.xml:

<condition property="parallel-lint" value="${basedir}/bin/parallel-lint.bat" else="${basedir}/bin/parallel-lint">
    <os family="windows"/>
</condition>

<target name="parallel-lint" description="Run PHP parallel lint">
    <exec executable="${parallel-lint}" failonerror="true">
        <arg line="--exclude" />
        <arg path="${basedir}/app/" />
        <arg line="--exclude" />
        <arg path="${basedir}/vendor/" />
        <arg path="${basedir}" />
    </exec>
</target>
like image 43
kenorb Avatar answered Oct 28 '22 15:10

kenorb