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.

CompileTheme.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.buildhelpers;
  17. import java.io.BufferedWriter;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import org.apache.commons.cli.CommandLine;
  23. import org.apache.commons.cli.CommandLineParser;
  24. import org.apache.commons.cli.HelpFormatter;
  25. import org.apache.commons.cli.Options;
  26. import org.apache.commons.cli.ParseException;
  27. import org.apache.commons.cli.PosixParser;
  28. import com.vaadin.sass.internal.ScssStylesheet;
  29. /**
  30. * Helper to combine css divided into separate per component dirs into one to
  31. * optimize http requests.
  32. */
  33. public class CompileTheme {
  34. /**
  35. * @param args
  36. * @throws IOException
  37. * @throws ParseException
  38. */
  39. public static void main(String[] args) throws IOException, ParseException {
  40. Options options = new Options();
  41. options.addOption("t", "theme", true, "the theme to compile");
  42. options.addOption("f", "theme-folder", true,
  43. "the folder containing the theme");
  44. options.addOption("v", "version", true,
  45. "the Vaadin version to compile for");
  46. CommandLineParser parser = new PosixParser();
  47. CommandLine params = parser.parse(options, args);
  48. if (!params.hasOption("theme") || !params.hasOption("theme-folder")
  49. || !params.hasOption("version")) {
  50. // automatically generate the help statement
  51. HelpFormatter formatter = new HelpFormatter();
  52. formatter.printHelp(CompileTheme.class.getName(), options);
  53. return;
  54. }
  55. String themeName = params.getOptionValue("theme");
  56. String themeFolder = params.getOptionValue("theme-folder");
  57. String version = params.getOptionValue("version");
  58. // Regular theme
  59. try {
  60. processSassTheme(themeFolder, themeName, "styles", version);
  61. System.out.println(
  62. "Compiling theme " + themeName + " styles successful");
  63. } catch (Exception e) {
  64. System.err
  65. .println("Compiling theme " + themeName + " styles failed");
  66. e.printStackTrace();
  67. }
  68. // Legacy theme w/o .themename{} wrapping
  69. try {
  70. String legacyFile = themeFolder + File.separator + themeName
  71. + File.separator + "legacy-styles.scss";
  72. if (new File(legacyFile).exists()) {
  73. processSassTheme(themeFolder, themeName, "legacy-styles",
  74. version);
  75. System.out.println("Compiling theme " + themeName
  76. + " legacy-styles successful");
  77. }
  78. } catch (Exception e) {
  79. System.err.println(
  80. "Compiling theme " + themeName + " legacy-styles failed");
  81. e.printStackTrace();
  82. }
  83. }
  84. private static void processSassTheme(String themeFolder, String themeName,
  85. String variant, String version) throws Exception {
  86. StringBuffer cssHeader = new StringBuffer();
  87. String stylesCssDir = themeFolder + File.separator + themeName
  88. + File.separator;
  89. String stylesCssName = stylesCssDir + variant + ".css";
  90. // Process as SASS file
  91. String sassFile = stylesCssDir + variant + ".scss";
  92. ScssStylesheet scss = ScssStylesheet.get(sassFile);
  93. if (scss == null) {
  94. throw new IllegalArgumentException(
  95. "SASS file: " + sassFile + " not found");
  96. }
  97. scss.compile();
  98. String filteredScss = scss.printState().replace("@version@", version);
  99. BufferedWriter out = new BufferedWriter(new FileWriter(stylesCssName));
  100. out.write(cssHeader.toString());
  101. out.write(filteredScss);
  102. out.close();
  103. System.out.println("Compiled CSS to " + stylesCssName + " ("
  104. + filteredScss.length() + " bytes)");
  105. createSprites(themeFolder, themeName);
  106. File oldCss = new File(stylesCssName);
  107. File newCss = new File(stylesCssDir + variant + "-sprite.css");
  108. if (newCss.exists()) {
  109. // Theme contained sprites. Renamed "styles-sprite.css" ->
  110. // "styles.css"
  111. oldCss.delete();
  112. boolean ok = newCss.renameTo(oldCss);
  113. if (!ok) {
  114. throw new RuntimeException(
  115. "Rename " + newCss + " -> " + oldCss + " failed");
  116. }
  117. }
  118. }
  119. private static void createSprites(String themeFolder, String themeName)
  120. throws FileNotFoundException, IOException {
  121. try {
  122. // Try loading the class separately from using it to avoid
  123. // hiding other classpath issues
  124. Class<?> smartSpritesClass = org.carrot2.labs.smartsprites.SmartSprites.class;
  125. } catch (NoClassDefFoundError e) {
  126. System.err.println(
  127. "Could not find smartsprites. No sprites were generated. The theme should still work.");
  128. return;
  129. }
  130. String[] parameters = new String[] { "--sprite-png-depth", "AUTO",
  131. "--css-file-suffix", "-sprite", "--css-file-encoding", "UTF-8",
  132. "--root-dir-path", themeFolder + File.separator + themeName,
  133. "--log-level", "WARN" };
  134. org.carrot2.labs.smartsprites.SmartSprites.main(parameters);
  135. System.out.println("Generated sprites");
  136. }
  137. }