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