Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANT script to compile all (css) LESS files in a dir and subdirs with RHINO

Tags:

less

ant

rhino

I want do compile all *.less scripts in a specific folder and it subdirs with less-rhino-1.1.3.js.

There is an example on github for doing this for a specific file, which works perfect. But I want to do the same for a complete folder. I tried a lot, here is my last try.

It doesn't work, propertyregex seems not to be standard ANT, I don't want to use such things. I am not even sure if this code would work.

<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>
<macrodef name="lessjs">
    <attribute name="input" />
    <attribute name="output" />
    <sequential>
        <java jar="${tool.rhino}" fork="true" output="@{output}">
            <arg path="${tool.less}"/>
            <arg path="@{input}"/>
        </java>
        <echo>Lessjs: generated @{output}</echo>
    </sequential>
</macrodef>

<target name="main">
     <echo>compiling less css</echo>
     <fileset dir="${css.dir}" id="myfile">
          <filename name="**/*.less" />
     </fileset>
     <property name="lessfilename" refid="myfile"/>
     <propertyregex property="cssfilename"
          input="${lessfile}"
          regexp="^(.*)\.less$"
          replace="^\1\.css$" 
          casesensitive="true" />
     <lessjs input="lessfile" output="cssfilename"/>
</target>
</project>
like image 393
Christian Steinmann Avatar asked Aug 02 '11 08:08

Christian Steinmann


3 Answers

You could use the <fileset> to include all the less files need to be compiled. Later, you could use<mapper> to mark the corresponding detination css file.

<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>

  <target name="less" description="Convert LESS to CSS then concatenate and Minify any stylesheets">

  <echo message="Converting LESS to CSS..."/>
  <!-- Clear the former compiled css files -->
      <delete includeemptydirs="true">
            <fileset dir="${css.dir}" includes="*.css, **/*.css" defaultexcludes="false"/>
      </delete>

      <apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
  <!-- Give the input bundle of less files-->
          <fileset dir="${css.dir}">
              <include name="*.less"/>
          </fileset>
          <arg value="-jar" />
          <arg path="${tool.rhino}" />
          <arg path="${tool.less}" />
          <srcfile/>
  <!-- Output the compiled css file with corresponding name -->
          <mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
          <targetfile/>
      </apply>

  </target>

</project>
like image 59
steveyang Avatar answered Sep 27 '22 21:09

steveyang


I was able to piece together a working solution with the help of a couple of SO answers:

ANT script to compile all (css) LESS files in a dir and subdirs with RHINO

How to correctly execute lessc-rhino-1.6.3.js from command line

I had to download LESS 1.7.5 from GitHub and modify the Ant target to look like this. The -f argument and LESS JavaScript was key:

<property name="css.dir" value="WebContent/css"/>
<property name="less.dir" value="less"/>
<property name="tool.rhino.jar" value="test-lib/rhino-1.7R4.jar"/>
<property name="tool.rhino.lessc" value="test-lib/lessc-rhino-1.7.5.js"/>
<property name="tool.rhino.less" value="test-lib/less-rhino-1.7.5.js"/>
<target name="compile-less" description="compile css using LESS">
    <apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
        <fileset dir="${less.dir}">
            <include name="styles.less"/>
        </fileset>
        <arg value="-jar"/>
        <arg path="${tool.rhino.jar}"/>
        <arg value="-f"/>
        <arg path="${tool.rhino.less}"/>
        <arg path="${tool.rhino.lessc}"/>
        <srcfile/>
        <mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
        <targetfile/>
    </apply>
</target>
like image 34
duffymo Avatar answered Sep 27 '22 22:09

duffymo


If anyone else is coming to this question recently, as I did, they may find that the less-rhino-1.1.3.js file given in the other answers does not work with the latest version of Rhino (which for me, as of now, is 1.7R4 from MDN). But the 1.4.0 version does, which can be obtained from Github here. So the relevant snippet from my build.xml, using these later versions, is shown. Note that I'm only compiling a single .less file to a single .css file, so no iteration or mappers are used (but obviously you can get those from the other answers). Other tweaks I made were to provide the output file as the final arg to less instead of capturing output from the Ant forked process, and to remove the dependency on ant-contrib stuff (not needed for the simple one-file case).

<property name="tool.rhino" value="build/lesscss/rhino1_7R4/js.jar" />
<property name="tool.less" value="build/lesscss/less-rhino-1.4.0.js" />
<property name="single-input-lesscss-file" value="/path/to/my/style.less" />
<property name="single-output-css-file" value="/output/my/style.css" />

<target name="compileLessCss" description="Compile the single less file to css">
    <sequential>
        <java jar="${tool.rhino}" fork="true">
            <arg path="${tool.less}" />
            <arg path="${single-input-lesscss-file}" />
            <arg path="${single-output-css-file}" />
        </java>
    </sequential>
</target>
like image 41
Jeff Evans Avatar answered Sep 27 '22 23:09

Jeff Evans