Skip to content

Commit a435968

Browse files
Merge pull request #18 from tech-beeline/commands
2 parents 1ef4be6 + 3aacf48 commit a435968

8 files changed

Lines changed: 64 additions & 35 deletions

File tree

c4-server/src/main/java/ru/beeatlas/c4/custom/Custom.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import ch.qos.logback.core.FileAppender;
7575
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
7676
import ru.beeatlas.c4.utils.C4Utils;
77+
import ru.beeatlas.c4.dto.CodeLensCommandArgs;
7778
import ru.beeatlas.c4.model.C4DocumentModel;
7879
import ru.beeatlas.c4.model.C4ObjectWithContext;
7980
import ru.beeatlas.c4.model.C4DocumentModel.C4CompletionScope;
@@ -882,8 +883,9 @@ public void closeScope(C4CompletionScope scope, C4DocumentModel model) {
882883
List<LineToken> tokens = LineTokenizer.tokenize(line);
883884
if (tokens.size() == 3) {
884885
Command commandStructurizr = new Command("$(link-external) Export Environment", "c4.export.deployment");
885-
String name = C4Utils.trimStringByString(tokens.get(1).token(), "\"");
886-
commandStructurizr.setArguments(Arrays.asList(name));
886+
String deploymentEnvironment = C4Utils.trimStringByString(tokens.get(1).token(), "\"");
887+
CodeLensCommandArgs args = new CodeLensCommandArgs(null, null, null, deploymentEnvironment, null,null,null);
888+
commandStructurizr.setArguments(Arrays.asList(args));
887889
int pos = C4Utils.findFirstNonWhitespace(line, 0, true);
888890
Range range = new Range(new Position(scope.start() - 1, pos), new Position(scope.start() - 1, pos));
889891
model.addCodeLens(new CodeLens(range, commandStructurizr, null));
@@ -923,7 +925,8 @@ public void closeScope(C4CompletionScope scope, C4DocumentModel model) {
923925
int lastLine = scope.end() - i;
924926
int startLine = tokens.get(0).start();
925927
Command commandStructurizr = new Command("$(link-external) Insert SLA", "c4.insert.sla");
926-
commandStructurizr.setArguments(Arrays.asList(apiUrl, lastLine, startLine));
928+
CodeLensCommandArgs args = new CodeLensCommandArgs(null, null, null, null, apiUrl, lastLine, startLine);
929+
commandStructurizr.setArguments(Arrays.asList(args));
927930
String line = model.getLineAt(lineNumber - 1);
928931
int pos = C4Utils.findFirstNonWhitespace(line, 0, true);
929932

c4-server/src/main/java/ru/beeatlas/c4/dto/CodeLensCommandArgs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.gson.Gson;
2020
import com.google.gson.JsonObject;
2121

22-
public record CodeLensCommandArgs(String encodedWorkspace, String diagramKey, String diagramAsDot) {
22+
public record CodeLensCommandArgs(String encodedWorkspace, String diagramKey, String diagramAsDot, String deploymentEnvironment, String apiUrl, Integer lastLine, Integer padding) {
2323
private static final Gson GSON = new Gson();
2424
public static CodeLensCommandArgs fromJson(JsonObject jsonObject) {
2525
return GSON.fromJson(jsonObject, CodeLensCommandArgs.class);

c4-server/src/main/java/ru/beeatlas/c4/model/C4DocumentModel.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public void addView(int lineNumber, C4ObjectWithContext<View> view) {
365365
createToken(getIdentifierOfView(view.getObject()), lineNumber - 1).ifPresent(t -> tokens.add(t));
366366
viewToLineNumber.put(lineNumber, view);
367367
Command command = new Command("$(link-external) Show as Structurizr Diagram", "c4.show.diagram");
368-
CodeLensCommandArgs args = new CodeLensCommandArgs(null, view.getObject().getKey(), null);
368+
CodeLensCommandArgs args = new CodeLensCommandArgs(null, view.getObject().getKey(), null, null, null, null, null);
369369
command.setArguments(Arrays.asList(args));
370370
codeLenses.add(new CodeLens(view.getCodeLensRange(), command, null));
371371
}
@@ -415,16 +415,22 @@ public List<CodeLens> calcCodeLenses() {
415415
codeLenses.forEach(cl -> {
416416
Command command = cl.getCommand();
417417
CodeLensCommandArgs args = (CodeLensCommandArgs)command.getArguments().get(0);
418-
String dot = args.diagramAsDot();
419-
if(dot == null) {
420-
View view = workspace.getViews().getViewWithKey(args.diagramKey());
421-
if (view != null && view instanceof ModelView) {
422-
ModelView modelView = (ModelView) view;
423-
dot = C4Utils.export2Dot(modelView);
418+
if(command.getCommand().equals("c4.show.diagram")) {
419+
String dot = args.diagramAsDot();
420+
if(dot == null) {
421+
View view = workspace.getViews().getViewWithKey(args.diagramKey());
422+
if (view != null && view instanceof ModelView) {
423+
ModelView modelView = (ModelView) view;
424+
dot = C4Utils.export2Dot(modelView);
425+
}
424426
}
427+
args = new CodeLensCommandArgs(encodedWorkspace, args.diagramKey(), dot, null, null,null,null);
428+
command.setArguments(Arrays.asList(args));
429+
} else if(command.getCommand().equals("c4.export.deployment")) {
430+
String deploymentEnvironment = args.deploymentEnvironment();
431+
args = new CodeLensCommandArgs(encodedWorkspace, null, null, deploymentEnvironment, null,null,null);
432+
command.setArguments(Arrays.asList(args));
425433
}
426-
args = new CodeLensCommandArgs(encodedWorkspace, args.diagramKey(), dot);
427-
command.setArguments(Arrays.asList(args));
428434
});
429435
return codeLenses;
430436
} catch (Exception e) {

extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"license": "Apache-2.0",
1111
"icon": "images/logo.png",
12-
"version": "1.0.0",
12+
"version": "1.0.1",
1313
"engines": {
1414
"vscode": "^1.73.0"
1515
},

extension/src/custom/C4ExportDeployment.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { HttpClient } from 'typed-rest-client/HttpClient';
2828
import { IRequestOptions } from "typed-rest-client/Interfaces";
2929
import { BEELINE_API_URL, NOTLS } from "../config";
3030
import { generateHmac } from "./hmac";
31+
import { CodeLensCommandArgs } from "../types/CodeLensCommandArgs";
3132

3233
const CONF_VEGA_TOKEN = "c4.vega.token";
3334

@@ -41,7 +42,7 @@ class Detail {
4142

4243
export function c4ExportDeployment() {
4344

44-
commands.registerCommand("c4.export.deployment", async (...args: string[]) => {
45+
commands.registerCommand("c4.export.deployment", async (args : CodeLensCommandArgs) => {
4546

4647
const vegaToken = workspace.getConfiguration().get(CONF_VEGA_TOKEN) as string;
4748

@@ -59,10 +60,9 @@ export function c4ExportDeployment() {
5960

6061
const beelineApiUrl = workspace.getConfiguration().get(BEELINE_API_URL) as string;
6162
const path = '/structurizr-backend/api/v1/workspace/terraform/generate';
62-
const encodedWorkspaceJson = args[0];
63-
const name = '?environment=' + args[1];
63+
const name = '?environment=' + args.deploymentEnvironment;
6464
progress.report({ message: "Распаковка модели данных..." });
65-
const content = Buffer.from(encodedWorkspaceJson, 'base64').toString('utf8');
65+
const content = Buffer.from(args.encodedWorkspace, 'base64').toString('utf8');
6666
const contentType = 'text/plain';
6767
const headers = generateHmac('POST', path, content, contentType);
6868
headers['Content-Type'] = contentType;
@@ -86,16 +86,20 @@ export function c4ExportDeployment() {
8686
const detial = JSON.parse(body) as Detail;
8787
try {
8888
const errors = JSON.parse(detial.detail) as ErrorMsg[];
89-
errors.forEach(e => {
90-
writeFile(filepath, e.error_msg, function (error) { });
91-
});
89+
var os = require('os');
90+
const message = errors.map(obj => obj.error_msg).join(os.EOL) + os.EOL;
91+
window.showErrorMessage(message);
9292
} catch (e) {
9393
writeFile(filepath, detial.detail, function (error) { });
94+
workspace.openTextDocument(filepath).then((doc) => { window.showTextDocument(doc, ViewColumn.Beside); });
9495
}
9596
} catch (e) {
96-
writeFile(filepath, body, function (error) { });
97+
if (e instanceof Error) {
98+
window.showErrorMessage(body);
99+
} else {
100+
window.showErrorMessage("An unknown error occurred: " + e);
101+
}
97102
}
98-
workspace.openTextDocument(filepath).then((doc) => { window.showTextDocument(doc, ViewColumn.Beside); });
99103
}
100104
}).catch((error) => {
101105
window.showErrorMessage(error.message);

extension/src/custom/C4InsertSla.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ import * as httpm from 'typed-rest-client/HttpClient';
2727
import { IRequestOptions } from "typed-rest-client/Interfaces";
2828
import { BEELINE_API_URL, NOTLS } from "../config";
2929
import { generateHmac } from "./hmac";
30+
import { CodeLensCommandArgs } from "../types/CodeLensCommandArgs";
3031

3132
export function c4InsertSla() {
32-
commands.registerCommand("c4.insert.sla", async (...args: any[]) => {
33-
/* const encodedWorkspaceJson = args[0]; */
34-
const apiUrl = args[1];
35-
const lastLine = args[2];
36-
const padding = args[3];
37-
33+
commands.registerCommand("c4.insert.sla", async (args : CodeLensCommandArgs) => {
3834
const options: IRequestOptions = <IRequestOptions>{};
3935
options.ignoreSslError = workspace.getConfiguration().get(NOTLS) as boolean;
4036
const httpc = new httpm.HttpClient('vscode-c4-dsl-plugin', [], options);
@@ -56,12 +52,26 @@ export function c4InsertSla() {
5652
progress.report({ message: "Формирование SLA..." });
5753
httpc.post(beelineApiUrl + path, content, headers)
5854
.then((result) => { return result.readBody() }).then((body) => {
59-
var lines = body.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0).map((line) => " ".repeat(padding) + line);
55+
var lines = body.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0).map((line) => " ".repeat(args.padding) + line);
6056
const editor = window.activeTextEditor;
6157
if (editor) {
6258
editor.edit(editBuilder => {
59+
if(lines.length === 1) {
60+
const json = lines.at(0);
61+
if(json !== undefined) {
62+
try {
63+
let obj = JSON.parse(json);
64+
if (obj.message !== undefined) {
65+
window.showErrorMessage(obj.message);
66+
}
67+
} catch (e) {
68+
window.showErrorMessage(body);
69+
}
70+
return;
71+
}
72+
}
6373
var os = require('os');
64-
editBuilder.insert(new Position(lastLine, 0), lines.join(os.EOL) + os.EOL);
74+
editBuilder.insert(new Position(args.lastLine, 0), lines.join(os.EOL) + os.EOL);
6575
});
6676
}
6777
})
@@ -71,16 +81,16 @@ export function c4InsertSla() {
7181
};
7282

7383
progress.report({ message: "Запрос описания API..." });
74-
if(apiUrl.startsWith("http://") || apiUrl.startsWith("https://")) {
75-
httpc.get(apiUrl).then((result) => { return result.readBody() }).then((body) => {
84+
if(args.apiUrl.startsWith("http://") || args.apiUrl.startsWith("https://")) {
85+
httpc.get(args.apiUrl).then((result) => { return result.readBody() }).then((body) => {
7686
insertSla(body);
7787
}).catch((error) => {
7888
window.showErrorMessage(error.message);
7989
}).finally(() => {
8090
resolve();
8191
});
8292
} else {
83-
fs.readFile(apiUrl, 'utf8', (error, data) => {
93+
fs.readFile(args.apiUrl, 'utf8', (error, data) => {
8494
if (error) {
8595
window.showErrorMessage(error.message);
8696
} else {

extension/src/types/CodeLensCommandArgs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
limitations under the License.
1515
*/
1616

17+
import { integer } from "vscode-languageclient";
18+
1719
type CodeLensCommandArgs = {
1820
encodedWorkspace: string;
1921
diagramKey: string;
2022
diagramAsDot: string;
23+
deploymentEnvironment: string;
24+
apiUrl: string;
25+
lastLine: integer;
26+
padding: integer;
2127
};
2228

2329
export { CodeLensCommandArgs };

extension/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"strict": true,
2020
"sourceMap": true,
2121
"strictPropertyInitialization": false,
22-
"target": "es5"
22+
"target": "es6"
2323
},
2424
"include": [
2525
"src"

0 commit comments

Comments
 (0)