Package com.darrinholst.sass_java

Source Code of com.darrinholst.sass_java.Compiler

package com.darrinholst.sass_java;

import org.jruby.embed.ScriptingContainer;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Compiler {
    private boolean initialized;
    private File configLocation;

    public void compile() {
        initialize();
        new ScriptingContainer().runScriptlet(buildCompileScript());
    }

    private void initialize() {
        if (!initialized) {
            new ScriptingContainer().runScriptlet(buildInitializationScript());
            initialized = true;
        }
    }

    private String buildCompileScript() {
        StringWriter raw = new StringWriter();
        PrintWriter script = new PrintWriter(raw);

        script.println("Dir.chdir(File.dirname('" + getConfigLocation() + "')) do ");
        script.println("  Compass.compiler.run                                    ");
        script.println("end                                                       ");
        script.flush();

        return raw.toString();
    }

    private String buildInitializationScript() {
        StringWriter raw = new StringWriter();
        PrintWriter script = new PrintWriter(raw);

        script.println("require 'compass'                                                          ");
        script.println("Compass.add_project_configuration '" + getConfigLocation() + "'            ");
        script.println("Compass.configure_sass_plugin!                                             ");
        script.flush();

        return raw.toString();
    }

    private String getConfigLocation() {
        return replaceSlashes(configLocation.getAbsolutePath());
    }

    private String replaceSlashes(String path) {
        return path.replaceAll("\\\\", "/");
    }

    public void setConfigLocation(File configLocation) {
        this.configLocation = configLocation;
    }
}
TOP

Related Classes of com.darrinholst.sass_java.Compiler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.