Added support for creating a missing component folder to PackageWriter
@@ -41,6 +41,7 @@ import com.google.common.collect.Streams;
import com.exadel.aem.toolkit.api.annotations.main.AemComponent;
import com.exadel.aem.toolkit.api.annotations.main.Dialog;
+import com.exadel.aem.toolkit.api.annotations.main.WriteMode;
import com.exadel.aem.toolkit.api.annotations.meta.Scopes;
import com.exadel.aem.toolkit.core.CoreConstants;
import com.exadel.aem.toolkit.plugin.exceptions.InvalidSettingException;
@@ -160,6 +161,15 @@ public class PackageWriter implements AutoCloseable {
} else {
fullComponentPath = fileSystem.getPath(componentsPathBase, providedComponentPath);
}
+
+ if (!Files.exists(fullComponentPath) && isAllowedToCreateFolder(componentClass)) {
+ try {
+ Files.createDirectory(fullComponentPath);
+ } catch (IOException ex) {
+ PluginRuntime.context().getExceptionHandler().handle(ex);
+ return;
+ }
+ }
if (!Files.isWritable(fullComponentPath)) {
PluginRuntime.context().getExceptionHandler().handle(new UnknownComponentException(fullComponentPath));
return;
@@ -235,7 +245,7 @@ public class PackageWriter implements AutoCloseable {
--------------- */
/**
- * Retrieves the path specified for the current component class in either {@link AemComponent} or {@link Dialog} annotation
+ * Retrieves the path specified for the current component in either {@link AemComponent} or {@link Dialog} annotation
* @param componentClass The {@code Class<?>} to get the path for
* @return String value
*/
@@ -250,6 +260,17 @@ public class PackageWriter implements AutoCloseable {
return StringUtils.firstNonBlank(pathByComponent, pathByDialog);
}
+ /**
+ * Gets whether a new folder can be created for the component which is reflected by the current class
+ * @param componentClass The component-backing {@code Class<?>}; is expected to carry the {@link AemComponent}
+ * annotation
+ * @return True or false
+ */
+ private static boolean isAllowedToCreateFolder(Class<?> componentClass) {
+ return componentClass.isAnnotationPresent(AemComponent.class)
+ && componentClass.getAnnotation(AemComponent.class).writeMode().equals(WriteMode.CREATE);
+ }
+
/* ---------------
Factory methods
