您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SASSAddonImportFileCreator.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.server.themeutils;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.PrintStream;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.vaadin.server.widgetsetutils.ClassPathExplorer;
  30. import com.vaadin.server.widgetsetutils.ClassPathExplorer.LocationInfo;
  31. /**
  32. * Helper class for managing the addon imports and creating an a SCSS file for
  33. * importing all your addon themes. The helper method searches the classpath for
  34. * Vaadin addons and uses the 'Vaadin-Themes' metadata to create the imports.
  35. *
  36. * <p>
  37. * The addons.scss is always overwritten when this tool is invoked.
  38. * </p>
  39. *
  40. * @since 7.1
  41. */
  42. public class SASSAddonImportFileCreator {
  43. private static final String ADDON_IMPORTS_FILE = "addons.scss";
  44. private static final String ADDON_IMPORTS_FILE_TEXT = "This file is automatically managed and "
  45. + "will be overwritten from time to time.";
  46. /**
  47. *
  48. * @param args
  49. * Theme directory where the addons.scss file should be created
  50. */
  51. public static void main(String[] args) throws IOException {
  52. if (args.length == 0) {
  53. printUsage();
  54. } else {
  55. String themeDirectory = args[0];
  56. updateTheme(themeDirectory);
  57. }
  58. }
  59. /**
  60. * Updates a themes addons.scss with the addon themes found on the classpath
  61. *
  62. * @param themeDirectory
  63. * The target theme directory
  64. */
  65. public static void updateTheme(String themeDirectory) throws IOException {
  66. File addonImports = new File(themeDirectory, ADDON_IMPORTS_FILE);
  67. if (!addonImports.exists()) {
  68. // Ensure directory exists
  69. addonImports.getParentFile().mkdirs();
  70. // Ensure file exists
  71. addonImports.createNewFile();
  72. }
  73. LocationInfo info = ClassPathExplorer
  74. .getAvailableWidgetSetsAndStylesheets();
  75. try {
  76. PrintStream printStream = new PrintStream(
  77. new FileOutputStream(addonImports));
  78. printStream.println("/* " + ADDON_IMPORTS_FILE_TEXT + " */");
  79. printStream.println("/* Do not manually edit this file. */");
  80. printStream.println();
  81. Map<String, URL> addonThemes = info.getAddonStyles();
  82. // Sort addon styles so that CSS imports are first and SCSS import
  83. // last
  84. List<String> paths = new ArrayList<>(addonThemes.keySet());
  85. Collections.sort(paths, (String path1, String path2) -> {
  86. if (path1.toLowerCase().endsWith(".css")
  87. && path2.toLowerCase().endsWith(".scss")) {
  88. return -1;
  89. }
  90. if (path1.toLowerCase().endsWith(".scss")
  91. && path2.toLowerCase().endsWith(".css")) {
  92. return 1;
  93. }
  94. return 0;
  95. });
  96. List<String> mixins = new ArrayList<>();
  97. for (String path : paths) {
  98. mixins.addAll(
  99. addImport(printStream, path, addonThemes.get(path)));
  100. printStream.println();
  101. }
  102. createAddonsMixin(printStream, mixins);
  103. } catch (FileNotFoundException e) {
  104. // Should not happen since file is checked before this
  105. getLogger().log(Level.WARNING, "Error updating addons.scss", e);
  106. }
  107. }
  108. private static Logger getLogger() {
  109. return Logger.getLogger(SASSAddonImportFileCreator.class.getName());
  110. }
  111. private static List<String> addImport(PrintStream stream, String file,
  112. URL location) {
  113. // Add import comment
  114. printImportComment(stream, location);
  115. List<String> foundMixins = new ArrayList<>();
  116. if (file.endsWith(".css")) {
  117. stream.print("@import url(\"../../../" + file + "\");\n");
  118. } else {
  119. // Assume SASS
  120. stream.print("@import \"../../../" + file + "\";\n");
  121. // Convention is to name the mixin after the stylesheet. Strip
  122. // .scss from filename
  123. String mixin = file.substring(file.lastIndexOf('/') + 1,
  124. file.length() - ".scss".length());
  125. foundMixins.add(mixin);
  126. }
  127. stream.println();
  128. return foundMixins;
  129. }
  130. private static void printImportComment(PrintStream stream, URL location) {
  131. // file:/absolute/path/to/addon.jar!/
  132. String path = location.getPath();
  133. try {
  134. // Try to parse path for better readability
  135. path = path.substring(path.lastIndexOf(':') + 1,
  136. path.lastIndexOf('!'));
  137. // Extract jar archive filename
  138. path = path.substring(path.lastIndexOf('/') + 1);
  139. } catch (Exception e) {
  140. // Parsing failed but no worries, we then use whatever
  141. // location.getPath() returns
  142. }
  143. stream.println("/* Provided by " + path + " */");
  144. }
  145. private static void createAddonsMixin(PrintStream stream,
  146. List<String> mixins) {
  147. stream.println(
  148. "/* Import and include this mixin into your project theme to include the addon themes */");
  149. stream.println("@mixin addons {");
  150. for (String addon : mixins) {
  151. stream.println("\t@include " + addon + ";");
  152. }
  153. stream.println("}");
  154. stream.println();
  155. }
  156. private static void printUsage() {
  157. String className = SASSAddonImportFileCreator.class.getSimpleName();
  158. PrintStream o = System.out;
  159. o.println(className + " usage:");
  160. o.println();
  161. o.println("./" + className + " [Path to target theme folder]");
  162. }
  163. }