1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.templatingkit.setup;
35
36 import java.io.IOException;
37
38 import info.magnolia.cms.util.ClasspathResourcesUtil;
39 import info.magnolia.importexport.BootstrapUtil;
40 import info.magnolia.module.InstallContext;
41 import info.magnolia.module.delta.AbstractTask;
42 import info.magnolia.module.delta.TaskExecutionException;
43 import info.magnolia.module.resources.ResourceTypes;
44 import info.magnolia.module.resources.setup.InstallBinaryResourcesTask;
45 import info.magnolia.module.resources.setup.InstallTextResourceTask;
46 import info.magnolia.module.resources.setup.InstallTextResourcesTask;
47 import info.magnolia.module.templatingkit.resources.STKResourceModel;
48
49 import javax.jcr.ImportUUIDBehavior;
50 import javax.jcr.RepositoryException;
51
52 import org.apache.commons.lang.StringUtils;
53
54
55
56
57
58
59
60
61
62 public class ThemeInstallTask extends AbstractTask {
63
64 protected static final String THEME_NAME_PROPERTY = "themeName";
65 protected static final String DEFAULT_THEME_PATH_PATTERN = "/templating-kit/themes/%s/%s/.*";
66
67
68
69
70
71 boolean isUpdate;
72 private boolean stripExtensions;
73
74 public ThemeInstallTask() {
75 this(false);
76 }
77
78 public ThemeInstallTask(boolean isUpdate) {
79 this(false, true);
80 }
81
82 public ThemeInstallTask(boolean isUpdate, boolean stripExtensions) {
83 super("Install theme", "Installs css, js files and images.");
84 this.isUpdate = isUpdate;
85 this.stripExtensions = stripExtensions;
86 }
87
88
89 @Override
90 public void execute(final InstallContext ctx) throws TaskExecutionException {
91 final String themeName = getThemeName(ctx);
92
93 ResourceNameFilter filter = new ResourceNameFilter(ctx.getCurrentModuleDefinition().getName(), themeName);
94
95 final String[] resourcesToBootstrap = ClasspathResourcesUtil.findResources(filter);
96
97 try {
98 if (resourcesToBootstrap != null) {
99 BootstrapUtil.bootstrap(resourcesToBootstrap, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
100 }
101 } catch (IOException e) {
102 throw new TaskExecutionException("Could not bootstrap: " + e.getMessage(), e);
103 } catch (RepositoryException e) {
104 throw new TaskExecutionException("Could not bootstrap: " + e.getMessage(), e);
105 }
106
107 new InstallTextResourcesTask("", "", InstallTextResourceTask.DEFAULT_ENCODING, String.format(DEFAULT_THEME_PATH_PATTERN, themeName, ResourceTypes.CSS_SUFFIX),
108 ResourceTypes.PROCESSED_CSS, true, STKResourceModel.class.getName(), false, stripExtensions).execute(ctx);
109
110 new InstallTextResourcesTask("", "", InstallTextResourceTask.DEFAULT_ENCODING, String.format(DEFAULT_THEME_PATH_PATTERN, themeName, ResourceTypes.JS_SUFFIX),
111 ResourceTypes.PROCESSED_JS, true, STKResourceModel.class.getName(), false, stripExtensions).execute(ctx);
112
113 new InstallBinaryResourcesTask("", "", String.format(DEFAULT_THEME_PATH_PATTERN, themeName, "img"), false, stripExtensions).execute(ctx);
114
115 if (isUpdate && resourcesToBootstrap != null) {
116 String aggNames = "";
117 for (String name : resourcesToBootstrap) {
118 aggNames = aggNames + StringUtils.substringAfterLast(StringUtils.removeEnd(name, ".xml"), ".") + ", ";
119 }
120 ctx.warn("Theme(s) " + StringUtils.removeEnd(aggNames, ", ") + " have been overwritten completely");
121 }
122 }
123
124 protected String getThemeName(InstallContext ctx) {
125 return ctx.getCurrentModuleDefinition().getProperty(THEME_NAME_PROPERTY);
126 }
127
128
129
130
131 public class ResourceNameFilter implements info.magnolia.cms.util.ClasspathResourcesUtil.Filter {
132
133 private String moduleName;
134
135 private String themeName;
136
137 public ResourceNameFilter(String moduleName, String themeName) {
138 this.moduleName = moduleName;
139 this.themeName = themeName;
140 }
141
142 @Override
143 public boolean accept(String name) {
144 return name.startsWith("/mgnl-bootstrap/" + this.moduleName + "/config.modules.standard-templating-kit.config.themes.") && name.endsWith(".xml") && name.contains(this.themeName);
145 }
146 }
147 }