Skip to content

Commit

Permalink
Examination #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis Bloemendal committed Sep 3, 2017
1 parent d2a09a2 commit a8bab45
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 41 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/onehippo/HSTScaffold.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.onehippo;


import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.onehippo.build.ScaffoldBuilder;
import org.onehippo.examination.Examination;
import org.onehippo.fold.FileFolder;

import java.io.*;
Expand Down Expand Up @@ -38,6 +40,7 @@ public class HSTScaffold {

private static HSTScaffold scaffold;
private ScaffoldBuilder builder;
private Examination examination;
private FileFolder folder;

private List<Route> routes = new ArrayList<Route>();
Expand Down Expand Up @@ -185,6 +188,10 @@ public void read(String config) {

}

public void setExamination(Examination examination) {
this.examination = examination;
}

public void setBuilder(ScaffoldBuilder builder) {
this.builder = builder;
}
Expand Down Expand Up @@ -225,6 +232,17 @@ public Map<String, Route> scaffold(File destination, boolean dryRun) {
return null;
}

public Map<String, Object> examine() {
Map<String, Object> diagnose = new HashedMap();
if (this.examination != null) {
diagnose.putAll(this.examination.diagnoseComponents());
diagnose.putAll(this.examination.diagnoseMenus());
diagnose.putAll(this.examination.diagnoseSitemaps());
diagnose.putAll(this.examination.diagnoseTemplates());
}
return diagnose;
}

public static HSTScaffold instance(String path) throws IOException {
if (scaffold == null) {
scaffold = new HSTScaffold(path);
Expand Down
87 changes: 51 additions & 36 deletions src/main/java/org/onehippo/build/ProjectRollback.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,23 @@ public ProjectRollback(Node hstRoot) throws RepositoryException, IOException {
}

public void backup(boolean dryRun) throws IOException, RepositoryException {
String projectName = HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_NAME);
String javaFilePath = HSTScaffold.properties.getProperty(HSTScaffold.JAVA_COMPONENT_PATH);
String ftlFilePath = HSTScaffold.properties.getProperty(HSTScaffold.TEMPLATE_PATH);

File backup = new File(scaffoldDir, "history/"+System.currentTimeMillis());
log.info(String.format("%s Creating backup directory %s", (dryRun? "DRYRUN " : ""), backup.getPath()));
if (!dryRun) {
backup.mkdirs();
}

String projectName = HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_NAME);

File hstConfFile = new File(backup, projectName+"_hst.xml");
log.info(String.format("%s Export hst \"%s\" config %s", (dryRun? "DRYRUN " : ""), projectName, hstConfFile.getPath()));
if (!dryRun) {
OutputStream out = new FileOutputStream(hstConfFile);
projectHstConfRoot.getSession().exportDocumentView(projectHstConfRoot.getPath(), out, true, false);
}

String javaFilePath = HSTScaffold.properties.getProperty(HSTScaffold.JAVA_COMPONENT_PATH);
String ftlFilePath = HSTScaffold.properties.getProperty(HSTScaffold.TEMPLATE_PATH);

File componentDirectory = new File(projectDir, javaFilePath);
File templateDirectory = new File(projectDir, ftlFilePath);
log.info(String.format("%s Backup java components %s and templates %s", (dryRun? "DRYRUN " : ""), componentDirectory.getPath(), templateDirectory.getPath()));
Expand All @@ -64,49 +63,66 @@ public void backup(boolean dryRun) throws IOException, RepositoryException {
}
}

public void rollback(boolean dryRun) throws IOException, RepositoryException {
private File getLatestBackup() {
// find latest backup folder
File backups = new File(scaffoldDir, "history");

File[] files = backups.listFiles();
Arrays.sort(files);
if (files.length == 0) {
log.info("No backups available.");
return;
return null;
}

return files[files.length-1];
}

public void rollback(boolean dryRun) throws IOException, RepositoryException {
log.info(String.format("%s Move current project conf into trash.", (dryRun? "DRYRUN " : "")));
File trash = new File(scaffoldDir, "trash/"+System.currentTimeMillis());
if (!trash.exists()) {
trash.mkdirs();
}

// move current projects java and templates into .scaffold/.trash/date
String projectName = HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_NAME);
String javaFilePath = HSTScaffold.properties.getProperty(HSTScaffold.JAVA_COMPONENT_PATH);
String ftlFilePath = HSTScaffold.properties.getProperty(HSTScaffold.TEMPLATE_PATH);
File componentDirectory = new File(projectDir, javaFilePath);
File templateDirectory = new File(projectDir, ftlFilePath);

log.info(String.format("%s Move java components %s and templates %s into %s", (dryRun? "DRYRUN " : ""), componentDirectory.getPath(), templateDirectory.getPath(), trash.getPath()));
if (!dryRun) {
FileUtils.moveDirectory(componentDirectory, new File(trash, "java"));
FileUtils.moveDirectory(templateDirectory, new File(trash, "ftl"));
File drafts = new File(scaffoldDir, "drafts/"+System.currentTimeMillis());
if (!drafts.exists()) {
drafts.mkdirs();
}
File hstConfFile = new File(drafts, projectName+"_hst.xml");

// export current projects hst conf to .scaffold/.trash/date
String projectName = HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_NAME);
saveDraft(dryRun, projectName, componentDirectory, templateDirectory, drafts, hstConfFile);

File hstConfFile = new File(trash, projectName+"_hst.xml");
log.info(String.format("%s Export hst \"%s\" config %s", (dryRun? "DRYRUN " : ""), projectName, hstConfFile.getPath()));
log.info("Restore backup");
File latest = getLatestBackup();

restoreFromBackup(dryRun, componentDirectory, templateDirectory, latest);
restoreHstConfig(dryRun, projectName, latest);
removeBackup(dryRun, latest);
}

private void removeBackup(boolean dryRun, File latest) throws IOException {
// remove backup folder
log.info(String.format("%s Delete backup %s", (dryRun? "DRYRUN " : ""), latest));
if (!dryRun) {
OutputStream out = new BufferedOutputStream(new FileOutputStream(hstConfFile));
projectHstConfRoot.getSession().exportDocumentView(projectHstConfRoot.getPath(), out, true, false);
FileUtils.deleteDirectory(latest);
}
}

log.info("Restore backup");
private void restoreHstConfig(boolean dryRun, String projectName, File latest) throws RepositoryException, IOException {
File hstConfFile;// restore hst config
hstConfFile = new File(latest, projectName+"_hst.xml");
log.info(String.format("%s Import hst \"%s\" config %s", (dryRun? "DRYRUN " : ""), projectName, hstConfFile.getPath()));
if (!dryRun) {
projectHstConfRoot.getSession().removeItem(projectHstConfRoot.getPath());

File latest = files[files.length-1];
Node hstConfigurations = hstRoot.getNode("hst:configurations");
InputStream in = new BufferedInputStream(new FileInputStream(hstConfFile));
projectHstConfRoot.getSession().importXML(hstConfigurations.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
}
}

private void restoreFromBackup(boolean dryRun, File componentDirectory, File templateDirectory, File latest) throws IOException {
// copy java and templates from backup folder
File latestJavaFilesBackup = new File(latest, "java");
File latestTemplateFilesBackup = new File(latest, "ftl");
Expand All @@ -117,22 +133,21 @@ public void rollback(boolean dryRun) throws IOException, RepositoryException {
FileUtils.copyDirectory(latestJavaFilesBackup, componentDirectory);
FileUtils.copyDirectory(latestTemplateFilesBackup, templateDirectory);
}
}

// restore hst config
hstConfFile = new File(latest, projectName+"_hst.xml");
log.info(String.format("%s Import hst \"%s\" config %s", (dryRun? "DRYRUN " : ""), projectName, hstConfFile.getPath()));
private void saveDraft(boolean dryRun, String projectName, File componentDirectory, File templateDirectory, File drafts, File hstConfFile) throws IOException, RepositoryException {
log.info(String.format("%s Move java components %s and templates %s into %s", (dryRun? "DRYRUN " : ""), componentDirectory.getPath(), templateDirectory.getPath(), drafts.getPath()));
// move current projects java and templates into .scaffold/drafts/date
if (!dryRun) {
projectHstConfRoot.getSession().removeItem(projectHstConfRoot.getPath());

Node hstConfigurations = hstRoot.getNode("hst:configurations");
InputStream in = new BufferedInputStream(new FileInputStream(hstConfFile));
projectHstConfRoot.getSession().importXML(hstConfigurations.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
FileUtils.moveDirectory(componentDirectory, new File(drafts, "java"));
FileUtils.moveDirectory(templateDirectory, new File(drafts, "ftl"));
}

// remove backup folder
log.info(String.format("%s Delete backup %s", (dryRun? "DRYRUN " : ""), latest));
// export current projects hst conf to .scaffold/drafts/date
log.info(String.format("%s Export hst \"%s\" config %s", (dryRun? "DRYRUN " : ""), projectName, hstConfFile.getPath()));
if (!dryRun) {
FileUtils.deleteDirectory(latest);
OutputStream out = new BufferedOutputStream(new FileOutputStream(hstConfFile));
projectHstConfRoot.getSession().exportDocumentView(projectHstConfRoot.getPath(), out, true, false);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/onehippo/build/RepositoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
Expand All @@ -28,6 +26,7 @@ public class RepositoryBuilder implements ScaffoldBuilder {
private Rollback rollback;
private TemplateBuilder templateBuilder;


public Pattern PATH_SEGMENT = Pattern.compile("[^/]+/");

public RepositoryBuilder(Node hstRoot) throws RepositoryException, IOException {
Expand Down Expand Up @@ -131,8 +130,10 @@ private Node buildWorkspaceComponentHstConf(Route.Component component, boolean d
private void buildPages(Route route, boolean dryRun) throws IOException, RepositoryException {
log.debug(String.format("%s Build page %s", (dryRun? "DRYRUN " : ""), route.getPageConstruct()));

Node pages = projectHstConfRoot.getNode("hst:pages");
buildPageNode(pages, route.getPage(), dryRun);
if (projectHstConfRoot.hasNode("hst:pages")) {
Node pages = projectHstConfRoot.getNode("hst:pages");
buildPageNode(pages, route.getPage(), dryRun);
}
}

private void buildPageNode(Node root, Route.Component component, boolean dryRun) throws RepositoryException, IOException {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/onehippo/examination/Examination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.onehippo.examination;


import java.util.Map;

public interface Examination {

Map<String, Object> diagnoseComponents();

Map<String, Object> diagnoseSitemaps();

Map<String, Object> diagnoseMenus();

Map<String, Object> diagnoseTemplates();

}
125 changes: 125 additions & 0 deletions src/main/java/org/onehippo/examination/ProjectExamination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.onehippo.examination;


import org.apache.log4j.Logger;
import org.onehippo.HSTScaffold;
import org.onehippo.Route;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

// TODO
public class ProjectExamination implements Examination {

final static Logger log = Logger.getLogger(ProjectExamination.class);

private Node hstRoot;
private Node projectHstConfRoot;
private File projectDir;
private File scaffoldDir;
private Map<String, String> references = new HashMap<String, String>();

public Pattern PATH_SEGMENT = Pattern.compile("[^/]+/");

public ProjectExamination(Node hstRoot) throws RepositoryException, IOException {
this.hstRoot = hstRoot;

String projectHstNodeName = HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_NAME);
projectHstConfRoot = hstRoot.getNode("hst:configurations").getNode(projectHstNodeName);

projectDir = new File(HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_DIR));
if (!projectDir.exists()) {
throw new IOException(String.format("Project directory doesn't exist %s.", HSTScaffold.properties.getProperty(HSTScaffold.PROJECT_DIR)));
}

scaffoldDir = new File(projectDir, ".scaffold");
if (!scaffoldDir.exists()) {
scaffoldDir.mkdirs();
}

}

// TODO adapt
private Node diagnoseHstConfig(Route.Component component, boolean dryRun) throws RepositoryException {
if (!component.isLeaf()) {
log.error("Component is not a leaf");
return null;
}

if (!projectHstConfRoot.hasNode("hst:workspace")) {
throw new RepositoryException("HST configuration child hst:workspace misses.");
}

Node workspace = projectHstConfRoot.getNode("hst:workspace");
if (!workspace.hasNode("hst:containers")) {
throw new RepositoryException("HST configuration child hst:containers misses.");
}

log.debug(String.format("%s Build workspace component %s.", (dryRun? "DRYRUN " : ""), component.getComponentPath()));
if (dryRun) {
return null;
}

Node containers = workspace.getNode("hst:containers");

// find container parent
List<Route.Component> parents = component.getParents();
Route.Component page = parents.remove(0);

// root
Node container;
if (!containers.hasNode(page.getName())) {
container = containers.addNode(page.getName(), "hst:containercomponentfolder");
} else {
container = containers.getNode(page.getName());
}

for (Route.Component branch : component.getReferenceBranch()) {
if (container.hasNode(branch.getName())) {
container = container.getNode(branch.getName());
} else {
container = container.addNode(branch.getName(), "hst:containercomponent");
container.setProperty("hst:xtype", "HST.vBox");
}
}

if (container.hasNode(component.getName())) {
return container.getNode(component.getName());
}

// let's create structure, if missing
if (!container.isNodeType("hst:containercomponent")) {
container = container.addNode(component.getName(), "hst:containercomponent");
container.setProperty("hst:xtype", "HST.vBox");
}

Node containerItem = container.addNode(component.getName(), "hst:containeritemcomponent");
containerItem.setProperty("hst:componentclassname", component.getJavaClass());
containerItem.setProperty("hst:template", component.getTemplateName());
containerItem.setProperty("hst:xtype", "HST.Item");

return containerItem;
}

public Map<String, Object> diagnoseComponents() {
return null;
}

public Map<String, Object> diagnoseSitemaps() {
return null;
}

public Map<String, Object> diagnoseMenus() {
return null;
}

public Map<String, Object> diagnoseTemplates() {
return null;
}
}
Loading

0 comments on commit a8bab45

Please sign in to comment.