Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Add -compress option to GZIP result (#17773)
Browse files Browse the repository at this point in the history
Support for producing a gzipped version of the output alongside
with the uncompressed version with the option -compress:true .
The default value of the option is false.

Change-Id: I96ee05f1d11e853029495826f30fb51ba5ce916e
  • Loading branch information
Henri Sara committed Nov 25, 2015
1 parent 1a2735c commit ab1591f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/com/vaadin/sass/SassCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.vaadin.sass;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.zip.GZIPOutputStream;

import com.vaadin.sass.internal.ScssContext;
import com.vaadin.sass.internal.ScssStylesheet;
Expand Down Expand Up @@ -50,6 +54,10 @@ public static void main(String[] args) throws Exception {
.defaultValue("true")
.help("Minify the compiled CSS with YUI Compressor");

argp.defineOption("compress").values("true", "false")
.defaultValue("false")
.help("Create also a compressed version of the compiled CSS (only when output file is given)");

argp.defineOption("ignore-warnings").values("true", "false")
.defaultValue("false")
.help("Let compilation succeed even though there are warnings");
Expand All @@ -62,6 +70,8 @@ public static void main(String[] args) throws Exception {
ScssContext.UrlMode urlMode = getUrlMode(argp.getOptionValue("urlMode"));

boolean minify = Boolean.parseBoolean(argp.getOptionValue("minify"));
boolean compress = Boolean
.parseBoolean(argp.getOptionValue("compress"));
boolean ignoreWarnings = Boolean.parseBoolean(argp
.getOptionValue("ignore-warnings"));

Expand Down Expand Up @@ -94,6 +104,11 @@ public static void main(String[] args) throws Exception {
Writer writer = createOutputWriter(output);
scss.write(writer, minify);
writer.close();

if (output != null && compress) {
String outputCompressed = output + ".gz";
compressFile(output, outputCompressed);
}
} catch (Exception e) {
throw e;
}
Expand All @@ -105,6 +120,26 @@ public static void main(String[] args) throws Exception {
}
}

private static void compressFile(String uncompressedFileName,
String compressedFileName) throws FileNotFoundException,
IOException {
FileInputStream uncompressedStream = new FileInputStream(
uncompressedFileName);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(
compressedFileName));

byte[] buffer = new byte[1024];
int len;
while ((len = uncompressedStream.read(buffer)) > 0) {
gzos.write(buffer, 0, len);
}

uncompressedStream.close();

gzos.finish();
gzos.close();
}

private static ScssContext.UrlMode getUrlMode(String urlMode) {
if ("relative".equalsIgnoreCase(urlMode)) {
return ScssContext.UrlMode.RELATIVE;
Expand Down

0 comments on commit ab1591f

Please sign in to comment.