You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SassLinker.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.vaadin.sass.linker;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. import org.w3c.css.sac.CSSException;
  12. import com.google.gwt.core.ext.LinkerContext;
  13. import com.google.gwt.core.ext.TreeLogger;
  14. import com.google.gwt.core.ext.UnableToCompleteException;
  15. import com.google.gwt.core.ext.linker.AbstractLinker;
  16. import com.google.gwt.core.ext.linker.ArtifactSet;
  17. import com.google.gwt.core.ext.linker.EmittedArtifact;
  18. import com.google.gwt.core.ext.linker.LinkerOrder;
  19. import com.google.gwt.core.ext.linker.LinkerOrder.Order;
  20. import com.google.gwt.core.ext.linker.Shardable;
  21. import com.vaadin.sass.internal.ScssStylesheet;
  22. /**
  23. * Pre-linker that checks for the existence of SASS files in public folders,
  24. * compiles them to CSS files with the SassCompiler from Vaadin and adds the CSS
  25. * back into the artifact.
  26. *
  27. */
  28. @LinkerOrder(Order.PRE)
  29. @Shardable
  30. public class SassLinker extends AbstractLinker {
  31. @Override
  32. public String getDescription() {
  33. return "Compiling SCSS files in public folders to standard CSS";
  34. }
  35. @Override
  36. public ArtifactSet link(TreeLogger logger, LinkerContext context,
  37. ArtifactSet artifacts, boolean onePermutation)
  38. throws UnableToCompleteException {
  39. if (!onePermutation) {
  40. // The artifact to return
  41. ArtifactSet toReturn = new ArtifactSet(artifacts);
  42. // The temporary scss files provided from the artefacts
  43. List<FileInfo> scssFiles = new ArrayList<FileInfo>();
  44. // The public files are provided as inputstream, but the compiler
  45. // needs real files, as they can contain references to other
  46. // files. They will be stored here, with their relative paths intact
  47. String tempFolderName = new Date().getTime() + File.separator;
  48. File tempFolder = createTempDir(tempFolderName);
  49. // Can't search here specifically for public resources, as the type
  50. // is different during compilation. This means we have to loop
  51. // through all the artifacts
  52. for (EmittedArtifact resource : artifacts
  53. .find(EmittedArtifact.class)) {
  54. // Create the temporary files.
  55. String partialPath = resource.getPartialPath();
  56. if (partialPath.endsWith(".scss")) {
  57. // In my opinion, the SCSS file does not need to be
  58. // output to the web content folder, as they can't
  59. // be used there
  60. toReturn.remove(resource);
  61. String fileName = partialPath;
  62. File path = tempFolder;
  63. int separatorIndex = fileName.lastIndexOf(File.separator);
  64. if (-1 != separatorIndex) {
  65. fileName = fileName.substring(separatorIndex + 1);
  66. String filePath = partialPath.substring(0,
  67. separatorIndex);
  68. path = createTempDir(tempFolderName + filePath);
  69. }
  70. File tempfile = new File(path, fileName);
  71. try {
  72. boolean fileCreated = tempfile.createNewFile();
  73. if (fileCreated) {
  74. // write the received inputstream to the temp file
  75. writeFromInputStream(resource.getContents(logger),
  76. tempfile);
  77. // Store the file info for the compilation
  78. scssFiles.add(new FileInfo(tempfile, partialPath));
  79. } else {
  80. logger.log(TreeLogger.WARN, "Duplicate file "
  81. + tempfile.getPath());
  82. }
  83. } catch (IOException e) {
  84. logger.log(TreeLogger.ERROR,
  85. "Could not write temporary file " + fileName, e);
  86. }
  87. }
  88. }
  89. // Compile the files and store them in the artifact
  90. logger.log(TreeLogger.INFO, "Processing " + scssFiles.size()
  91. + " Sass file(s)");
  92. for (FileInfo fileInfo : scssFiles) {
  93. logger.log(TreeLogger.INFO, " " + fileInfo.originalScssPath
  94. + " -> " + fileInfo.getOriginalCssPath());
  95. try {
  96. ScssStylesheet scss = ScssStylesheet.get(fileInfo
  97. .getAbsolutePath());
  98. if (!fileInfo.isMixin()) {
  99. scss.compile();
  100. InputStream is = new ByteArrayInputStream(scss
  101. .toString().getBytes());
  102. toReturn.add(this.emitInputStream(logger, is,
  103. fileInfo.getOriginalCssPath()));
  104. }
  105. fileInfo.getFile().delete();
  106. } catch (CSSException e) {
  107. logger.log(TreeLogger.ERROR, "SCSS compilation failed for "
  108. + fileInfo.getOriginalCssPath(), e);
  109. } catch (IOException e) {
  110. logger.log(
  111. TreeLogger.ERROR,
  112. "Could not write CSS file for "
  113. + fileInfo.getOriginalCssPath(), e);
  114. } catch (Exception e) {
  115. logger.log(TreeLogger.ERROR, "SCSS compilation failed for "
  116. + fileInfo.getOriginalCssPath(), e);
  117. }
  118. }
  119. return toReturn;
  120. }
  121. return artifacts;
  122. }
  123. /**
  124. * Writes the contents of an InputStream out to a file.
  125. *
  126. * @param contents
  127. * @param tempfile
  128. * @throws IOException
  129. */
  130. private void writeFromInputStream(InputStream contents, File tempfile)
  131. throws IOException {
  132. // write the inputStream to a FileOutputStream
  133. OutputStream out = new FileOutputStream(tempfile);
  134. int read = 0;
  135. byte[] bytes = new byte[1024];
  136. while ((read = contents.read(bytes)) != -1) {
  137. out.write(bytes, 0, read);
  138. }
  139. contents.close();
  140. out.flush();
  141. out.close();
  142. }
  143. /**
  144. * Create folder in temporary space on disk.
  145. *
  146. * @param partialPath
  147. * @return
  148. */
  149. private File createTempDir(String partialPath) {
  150. String baseTempPath = System.getProperty("java.io.tmpdir");
  151. File tempDir = new File(baseTempPath + File.separator + partialPath);
  152. if (!tempDir.exists()) {
  153. tempDir.mkdirs();
  154. }
  155. tempDir.deleteOnExit();
  156. return tempDir;
  157. }
  158. /**
  159. * Temporal storage for file info from Artifact.
  160. */
  161. private class FileInfo {
  162. private String originalScssPath;
  163. private File file;
  164. public FileInfo(File file, String originalScssPath) {
  165. this.file = file;
  166. this.originalScssPath = originalScssPath;
  167. }
  168. public boolean isMixin() {
  169. return file.getName().startsWith("_");
  170. }
  171. public String getAbsolutePath() {
  172. return file.getAbsolutePath();
  173. }
  174. public String getOriginalCssPath() {
  175. return originalScssPath.substring(0, originalScssPath.length() - 5)
  176. + ".css";
  177. }
  178. public File getFile() {
  179. return file;
  180. }
  181. }
  182. }