Search code examples
phing

Locating a program in a Phing build file


In a Phing build file, I would like to invoke an external program (lessc in this case), but the program might live in a different location depending on which system the script is run on.

This is what I am doing currently. It works, but it smells to me:

<property name="lessc" value="/usr/bin/lessc" override="true" />

<if>
  <not>
    <available file="${lessc}" />
  </not>
  <then>
    <fail msg="Could not find LESS compiler at ${lessc}." />
  </then>
</if>

<exec command="${lessc} ..." />

I am using a property so that the user can specify an alternate path to lessc for her system, but I would prefer it if the script could automatically determine the location of the executable via e.g. hash or which.

Is this possible to do in Phing?


Solution

  • I came across Use `env` on Sean Coates's blog the other day, which deals with a very similar problem; in his case he found that PHP scripts were failing when run via CLI on different systems because PHP was installed in different locations.

    His solution was to replace the #!/path/to/php header with #!/usr/bin/env php. Since env is (probably) always located at /usr/bin/env, the only configuration required is to ensure that the PHP binary is on the $PATH for whatever user is executing the CLI script. No server-specific code changes necessary!

    Well, this got me thinking.

    I changed my build file to look like this, and everything is working beautifully now:

    <property name="env" value="/usr/bin/env" override="true" />
    
    <if>
      <not>
        <available file="${env}" />
      </not>
      <then>
        <fail msg="Unable to locate env executable at ${env}." />
      </then>
    </if>
    
    <exec command="${env} lessc ..." />