Skip to content

Commit

Permalink
Adds a command to merge layout information.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brown committed Sep 20, 2023
1 parent b2fd0b3 commit 71230e3
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
implementation 'org.apache.logging.log4j:log4j-jcl:2.20.0'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'

}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/structurizr/cli/MergeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.structurizr.cli;

import com.structurizr.Workspace;
import com.structurizr.util.WorkspaceUtils;
import org.apache.commons.cli.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;

class MergeCommand extends AbstractCommand {

private static final Log log = LogFactory.getLog(MergeCommand.class);

public MergeCommand() {
}

public void run(String... args) throws Exception {
Options options = new Options();

Option option = new Option("w", "workspace", true, "Path or URL to the workspace JSON file/DSL file");
option.setRequired(true);
options.addOption(option);

option = new Option("l", "layout", true, "Path or URL to the workspace JSON file that includes layout information");
option.setRequired(true);
options.addOption(option);

option = new Option("o", "output", true, "Path and name of an output file");
option.setRequired(true);
options.addOption(option);

CommandLineParser commandLineParser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();

String workspaceWithLayoutPath = null;
String workspaceWithoutLayoutPath = null;
String outputPath = null;

try {
CommandLine cmd = commandLineParser.parse(options, args);

workspaceWithoutLayoutPath = cmd.getOptionValue("workspace");
workspaceWithLayoutPath = cmd.getOptionValue("layout");
outputPath = cmd.getOptionValue("output");

} catch (ParseException e) {
log.error(e.getMessage());
formatter.setWidth(150);
formatter.printHelp("merge", options);

System.exit(1);
}

log.info("Merging layout");
log.info(" - loading workspace from " + workspaceWithoutLayoutPath);
Workspace workspaceWithoutLayout = loadWorkspace(workspaceWithoutLayoutPath);

log.info(" - loading layout information from " + workspaceWithLayoutPath);
Workspace workspaceWithLayout = loadWorkspace(workspaceWithLayoutPath);

log.info(" - merging layout information");
workspaceWithoutLayout.getViews().copyLayoutInformationFrom(workspaceWithLayout.getViews());
workspaceWithoutLayout.getViews().getConfiguration().copyConfigurationFrom(workspaceWithLayout.getViews().getConfiguration());

File outputFile = new File(outputPath);
log.info(" - writing " + outputFile.getCanonicalPath());
WorkspaceUtils.saveWorkspaceToJson(workspaceWithoutLayout, outputFile);

log.info(" - finished");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class StructurizrCliApplication {
private static final String LOCK_COMMAND = "lock";
private static final String UNLOCK_COMMAND = "unlock";
private static final String EXPORT_COMMAND = "export";
private static final String MERGE_COMMAND = "merge";
private static final String VALIDATE_COMMAND = "validate";
private static final String LIST_COMMAND = "list";
private static final String VERSION_COMMAND = "version";
Expand Down Expand Up @@ -61,6 +62,7 @@ public class StructurizrCliApplication {
COMMANDS.put(LOCK_COMMAND, new LockCommand());
COMMANDS.put(UNLOCK_COMMAND, new UnlockCommand());
COMMANDS.put(EXPORT_COMMAND, new ExportCommand());
COMMANDS.put(MERGE_COMMAND, new MergeCommand());
COMMANDS.put(VALIDATE_COMMAND, new ValidateCommand());
COMMANDS.put(LIST_COMMAND, new ListCommand());
COMMANDS.put(VERSION_COMMAND, new VersionCommand());
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/structurizr/cli/MergeCommandTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.structurizr.cli;

import com.structurizr.Workspace;
import com.structurizr.model.Person;
import com.structurizr.util.WorkspaceUtils;
import com.structurizr.view.ElementView;
import com.structurizr.view.SystemLandscapeView;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.*;

public class MergeCommandTests {

@Test
public void run() throws Exception {
File tmpDir = Files.createTempDirectory("structurizr").toFile();

String[] args = {
"merge",
"-workspace", "src/test/mergeLayout/workspace.dsl",
"-layout", "src/test/mergeLayout/workspace.json",
"-output", new File(tmpDir, "merged.json").getCanonicalPath()
};
new MergeCommand().run(args);

assertTrue(new File(tmpDir, "merged.json").exists());

Workspace workspace = WorkspaceUtils.loadWorkspaceFromJson(new File(tmpDir, "merged.json"));
Person user = workspace.getModel().getPersonWithName("User");
SystemLandscapeView view = workspace.getViews().getSystemLandscapeViews().iterator().next();
ElementView elementView = view.getElementView(user);
assertEquals(123, elementView.getX());
assertEquals(456, elementView.getY());
}

}

This file was deleted.

13 changes: 13 additions & 0 deletions src/test/merge/workspace.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
workspace {

model {
person "User"
}

views {
systemLandscape "key" {
include *
}
}

}
38 changes: 38 additions & 0 deletions src/test/merge/workspace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"id" : 0,
"name" : "Name",
"description" : "Description",
"properties" : {
"structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICBtb2RlbCB7CiAgICAgICAgcGVyc29uICJVc2VyIgogICAgfQoKICAgIHZpZXdzIHsKICAgICAgICBzeXN0ZW1MYW5kc2NhcGUgImtleSIgewogICAgICAgICAgICBpbmNsdWRlICoKICAgICAgICB9CiAgICB9Cgp9Cg=="
},
"configuration" : { },
"model" : {
"people" : [ {
"id" : "1",
"tags" : "Element,Person",
"properties" : {
"structurizr.dsl.identifier" : "40722b6a-ae23-4708-85d7-410c1a2ab053"
},
"name" : "User",
"location" : "Unspecified"
} ]
},
"documentation" : { },
"views" : {
"systemLandscapeViews" : [ {
"key" : "key",
"order" : 1,
"enterpriseBoundaryVisible" : true,
"elements" : [ {
"id" : "1",
"x" : 123,
"y" : 456
} ]
} ],
"configuration" : {
"branding" : { },
"styles" : { },
"terminology" : { }
}
}
}

0 comments on commit 71230e3

Please sign in to comment.