All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.cleydyr.dart.command.AbstractSassCommand Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package com.github.cleydyr.dart.command;

import com.github.cleydyr.dart.command.enums.SourceMapURLs;
import com.github.cleydyr.dart.command.enums.Style;
import com.github.cleydyr.dart.command.exception.SassCommandException;
import com.github.cleydyr.dart.command.parameter.ParameterPair;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class AbstractSassCommand implements SassCommand {
    private List loadPaths;

    public void setLoadPaths(List loadPaths) {
        this.loadPaths = loadPaths;
    }

    public void setStyle(Style style) {
        this.style = style;
    }

    public void setNoCharsetEnabled(boolean noCharsetEnabled) {
        this.noCharsetEnabled = noCharsetEnabled;
    }

    public void setErrorCSSEnabled(boolean errorCSSEnabled) {
        this.errorCSSEnabled = errorCSSEnabled;
    }

    public void setUpdateEnabled(boolean updateEnabled) {
        this.updateEnabled = updateEnabled;
    }

    public void setNoSourceMapEnabled(boolean noSourceMapEnabled) {
        this.noSourceMapEnabled = noSourceMapEnabled;
    }

    public void setSourceMapURLs(SourceMapURLs sourceMapURLs) {
        this.sourceMapURLs = sourceMapURLs;
    }

    public void setEmbedSourcesEnabled(boolean embedSourcesEnabled) {
        this.embedSourcesEnabled = embedSourcesEnabled;
    }

    public void setEmbedSourceMapEnabled(boolean embedSourceMapEnabled) {
        this.embedSourceMapEnabled = embedSourceMapEnabled;
    }

    public void setStopOnErrorEnabled(boolean stopOnErrorEnabled) {
        this.stopOnErrorEnabled = stopOnErrorEnabled;
    }

    public void setColorEnabled(boolean colorEnabled) {
        this.colorEnabled = colorEnabled;
    }

    public void setNoUnicodeEnabled(boolean noUnicodeEnabled) {
        this.noUnicodeEnabled = noUnicodeEnabled;
    }

    public void setQuietEnabled(boolean quietEnabled) {
        this.quietEnabled = quietEnabled;
    }

    public void setQuietDepsEnabled(boolean quietDepsEnabled) {
        this.quietDepsEnabled = quietDepsEnabled;
    }

    public void setTraceEnabled(boolean traceEnabled) {
        this.traceEnabled = traceEnabled;
    }

    public void setParameterPairs(Collection parameterPairs) {
        this.parameterPairs = parameterPairs;
    }

    public void setWatchEnabled(boolean watchEnabled) {
        this.watchEnabled = watchEnabled;
    }

    public void setPollEnabled(boolean pollEnabled) {
        this.pollEnabled = pollEnabled;
    }

    private Style style;

    private boolean noCharsetEnabled;

    private boolean errorCSSEnabled;

    private boolean updateEnabled;

    private boolean noSourceMapEnabled;

    private SourceMapURLs sourceMapURLs;

    private boolean embedSourcesEnabled;

    private boolean embedSourceMapEnabled;

    private boolean stopOnErrorEnabled;

    private boolean colorEnabled;

    private boolean noUnicodeEnabled;

    private boolean quietEnabled;

    private boolean quietDepsEnabled;

    private boolean traceEnabled;

    private boolean watchEnabled;

    private Collection parameterPairs = new ArrayList<>();

    private boolean pollEnabled;

    @Override
    public void execute() throws SassCommandException {
        List commands = new ArrayList<>();

        setExecutable(commands);

        setOptions(commands);

        setArguments(commands);

        try {
            ProcessBuilder processBuilder = new ProcessBuilder(commands).inheritIO();

            processBuilder.redirectErrorStream(true);

            processBuilder.inheritIO();

            Process process = processBuilder.start();

            int exitCode = process.waitFor();

            if (exitCode != 0) {
                StringBuilder sb = new StringBuilder(4);

                sb.append("Process exited with code ");
                sb.append(exitCode);
                sb.append("\n");

                throw new SassCommandException(sb.toString());
            }
        } catch (InterruptedException interruptedException) {
            throw new SassCommandException(interruptedException);
        } catch (IOException ioException) {
            throw new SassCommandException("Can't execute sass command", ioException);
        }
    }

    protected void setArguments(List commands) {
        for (ParameterPair parameterPair : parameterPairs) {
            commands.add(parameterPair.getInputPath() + ":" + parameterPair.getOutputPath());
        }
    }

    protected abstract void setExecutable(List commands);

    protected void setOptions(List commands) {
        _setLoadPaths(commands);

        _setStyle(commands);

        _setSourceMapURLs(commands);

        _setFlags(commands);
    }

    private void _setFlags(List commands) {
        if (noCharsetEnabled) {
            commands.add("--no-charset");
        }

        if (errorCSSEnabled) {
            commands.add("--error-css");
        }

        if (updateEnabled) {
            commands.add("--update");
        }

        if (noSourceMapEnabled) {
            commands.add("--no-source-map");
        }

        if (embedSourcesEnabled) {
            commands.add("--embed-sources");
        }

        if (embedSourceMapEnabled) {
            commands.add("--embed-source-map");
        }

        if (stopOnErrorEnabled) {
            commands.add("--stop-on-error");
        }

        if (!colorEnabled) {
            commands.add("--no-color");
        }

        if (noUnicodeEnabled) {
            commands.add("--no-unicode");
        }

        if (quietEnabled) {
            commands.add("--quiet");
        }

        if (quietDepsEnabled) {
            commands.add("--quiet-deps");
        }

        if (traceEnabled) {
            commands.add("--trace");
        }

        if (watchEnabled) {
            commands.add("--watch");

            if (pollEnabled) {
                commands.add("--poll");
            }
        }
    }

    private void _setSourceMapURLs(List commands) {
        if (sourceMapURLs == null) {
            return;
        }

        switch (sourceMapURLs) {
            case ABSOLUTE:
                commands.add("--source-map-urls=absolute");
                break;
            case RELATIVE:
                commands.add("--source-map-urls=relative");
                break;
            default:
                break;
        }
    }

    private void _setStyle(List commands) {
        switch (style) {
            case COMPRESSED:
                commands.add("--style=compressed");
                break;
            case EXPANDED:
                commands.add("--style=expanded");
                break;
        }
    }

    private void _setLoadPaths(List commands) {
        for (Path loadPath : loadPaths) {
            commands.add("--load-path");

            commands.add(loadPath.toString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy