Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS to CSS via an ANT task + directory creation

I recently started playing with SASS [http://sass-lang.com/] in a Java-based project and wanted to create an Ant task that:

  • for each .scss file + subdirectories containing .scss files in a top-level scss directory:
    • create the appropriate directory in a main CSS dir
    • compile the .scss file and place the .css file where it belongs

How do I go about doing this?

like image 201
rewinder Avatar asked Aug 31 '11 19:08

rewinder


People also ask

How does Sass compile to CSS?

Once you start tinkering with Sass, it will take your preprocessed Sass file and save it as a normal CSS file that you can use in your website. The most direct way to make this happen is in your terminal. Once Sass is installed, you can compile your Sass to CSS using the sass command.

Can you write CSS in Sass?

Speaking of saving time, Sass reduces the repetition of writing CSS code. This is thanks to its features like functions, variables, inheritance, and so on. Finally, Sass is compiled to CSS and adds all the necessary vendor prefixes so you don't have to worry about writing them manually.


1 Answers

Original answer by rewinder:


It ended up taking me some time to figure it out so I thought I'd post how I accomplished it. Here's my setup:

build.properties

css.dir=./template/ver1-0/css/v3
sass.dir=./template/ver1-0/css/v3/scss

scss directory structure:

/template/ver1-0/css/v3/scss
    + widgets
        - widgettest.scss
        - widgettest2.scss
    + global
        - globaltest.scss
        - globaltest2.css
    - file1.scss
    - file2.scss
    - _partial1.scss
    - _partial2.scss

Here's the Ant task:

<!-- scss to CSS -->
<!-- via: http://workingonthecoolstuff.blogspot.com/2011/02/using-sass-in-ant-build.html -->
<target name="sass-compile-to-css">
    <echo message="Compiling scss files to css..." />
    <!-- create the css destination dir if it doesn't already exist -->
    <property name="css-dest" location="${css.dir}"/>
    <echo message="Creating directory at ${css.dir} [if it doesn't yet exist]" />
    <mkdir dir="${css-dest}" />
    <!-- create subdirs if necessary
        via: https://stackoverflow.com/questions/536511/how-to-create-directories-specified-by-a-mapper-in-ant -->
    <echo message="Creating css directories (and temporary .css files) for .scss to be compiled..." />
    <touch mkdirs="true">
        <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
        <mapper type="glob" from="*.scss" to="${css.dir}/*.css"/>
    </touch>
    <echo message="Running sass executable against sass files and compiling to CSS directory [${css-dest}] " />
    <!-- run sass executable -->
    <apply executable="sass" dest="${css-dest}" verbose="true" force="true" failonerror="true">
        <arg value="--unix-newlines" />
        <srcfile />
        <targetfile />
        <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
        <mapper type="glob" from="*.scss" to="*.css"/>
    </apply>
    <echo message="Done compiling scss files!" />
</target>

After running the task the results are as hoped for: .scss files are compiled to the same path that they were created under. If the file parent directory did not yet exist under ${css.dir} then it gets created accordingly.

like image 89
2 revs Avatar answered Sep 16 '22 14:09

2 revs