001/*
002 * Minify Maven Plugin
003 * https://github.com/samaxes/minify-maven-plugin
004 *
005 * Copyright (c) 2009 samaxes.com
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package com.samaxes.maven.minify.plugin;
020
021import com.samaxes.maven.minify.common.YuiConfig;
022import com.samaxes.maven.minify.plugin.MinifyMojo.Engine;
023import com.yahoo.platform.yui.compressor.CssCompressor;
024import org.apache.maven.plugin.logging.Log;
025
026import java.io.*;
027import java.nio.charset.Charset;
028import java.util.List;
029
030/**
031 * Task for merging and compressing CSS files.
032 */
033public class ProcessCSSFilesTask extends ProcessFilesTask {
034
035    /**
036     * Task constructor.
037     *
038     * @param log             Maven plugin log
039     * @param verbose         display additional info
040     * @param bufferSize      size of the buffer used to read source files
041     * @param charset         if a character set is specified, a byte-to-char variant allows the encoding to be selected.
042     *                        Otherwise, only byte-to-byte operations are used
043     * @param suffix          final file name suffix
044     * @param nosuffix        whether to use a suffix for the minified file name or not
045     * @param skipMerge       whether to skip the merge step or not
046     * @param skipMinify      whether to skip the minify step or not
047     * @param webappSourceDir web resources source directory
048     * @param webappTargetDir web resources target directory
049     * @param inputDir        directory containing source files
050     * @param sourceFiles     list of source files to include
051     * @param sourceIncludes  list of source files to include
052     * @param sourceExcludes  list of source files to exclude
053     * @param outputDir       directory to write the final file
054     * @param outputFilename  the output file name
055     * @param engine          minify processor engine selected
056     * @param yuiConfig       YUI Compressor configuration
057     * @throws FileNotFoundException when the given source file does not exist
058     */
059    public ProcessCSSFilesTask(Log log, boolean verbose, Integer bufferSize, Charset charset, String suffix,
060                               boolean nosuffix, boolean skipMerge, boolean skipMinify, String webappSourceDir,
061                               String webappTargetDir, String inputDir, List<String> sourceFiles,
062                               List<String> sourceIncludes, List<String> sourceExcludes, String outputDir,
063                               String outputFilename, Engine engine, YuiConfig yuiConfig) throws FileNotFoundException {
064        super(log, verbose, bufferSize, charset, suffix, nosuffix, skipMerge, skipMinify, webappSourceDir,
065                webappTargetDir, inputDir, sourceFiles, sourceIncludes, sourceExcludes, outputDir, outputFilename,
066                engine, yuiConfig);
067    }
068
069    /**
070     * Minifies a CSS file. Create missing parent directories if needed.
071     *
072     * @param mergedFile   input file resulting from the merged step
073     * @param minifiedFile output file resulting from the minify step
074     * @throws IOException when the minify step fails
075     */
076    @Override
077    protected void minify(File mergedFile, File minifiedFile) throws IOException {
078        if (!minifiedFile.getParentFile().exists() && !minifiedFile.getParentFile().mkdirs()) {
079            throw new RuntimeException("Unable to create target directory for: " + minifiedFile.getParentFile());
080        }
081
082        try (InputStream in = new FileInputStream(mergedFile);
083             OutputStream out = new FileOutputStream(minifiedFile);
084             InputStreamReader reader = new InputStreamReader(in, charset);
085             OutputStreamWriter writer = new OutputStreamWriter(out, charset)) {
086            log.info("Creating the minified file [" + (verbose ? minifiedFile.getPath() : minifiedFile.getName())
087                    + "].");
088
089            switch (engine) {
090                case YUI:
091                    log.debug("Using YUI Compressor engine.");
092
093                    CssCompressor compressor = new CssCompressor(reader);
094                    compressor.compress(writer, yuiConfig.getLineBreak());
095                    break;
096                default:
097                    log.warn("CSS engine not supported.");
098                    break;
099            }
100        } catch (IOException e) {
101            log.error("Failed to compress the CSS file [" + (verbose ? mergedFile.getPath() : mergedFile.getName())
102                    + "].", e);
103            throw e;
104        }
105
106        logCompressionGains(mergedFile, minifiedFile);
107    }
108}