简体   繁体   English

通过ANT任务+目录创建SASS到CSS

[英]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: 我最近开始在基于Java的项目中使用SASS [ http://sass-lang.com/ ],并希望创建一个Ant任务:

  • for each .scss file + subdirectories containing .scss files in a top-level scss directory: 对于在顶级scss目录中包含.scss文件的每个.scss文件+子目录:
    • create the appropriate directory in a main CSS dir 在主CSS目录中创建适当的目录
    • compile the .scss file and place the .css file where it belongs 编译.scss文件并将.css文件放在它所属的位置

How do I go about doing this? 我该怎么做呢?

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 build.properties

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

scss directory structure: scss目录结构:

/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 : 这是Ant任务

<!-- 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. 运行任务后,结果如下所示:.scss文件被编译为与它们创建的路径相同的路径。 If the file parent directory did not yet exist under ${css.dir} then it gets created accordingly. 如果${css.dir}下的文件父目录尚不存在, ${css.dir}相应地创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM