Skip to content

Commit 56fdb85

Browse files
committed
read port from docker-compose, and auto-open terminal
1 parent 34021e1 commit 56fdb85

File tree

7 files changed

+163
-72
lines changed

7 files changed

+163
-72
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,6 @@
446446
"type": "boolean",
447447
"default": false
448448
},
449-
"objectscript.conn.label": {
450-
"description": "Server Name",
451-
"type": "string",
452-
"default": "LOCAL"
453-
},
454449
"objectscript.conn.host": {
455450
"description": "Hostname",
456451
"format": "hostname",
@@ -460,7 +455,7 @@
460455
"objectscript.conn.port": {
461456
"description": "Port number",
462457
"type": "number",
463-
"default": 57772
458+
"default": 52773
464459
},
465460
"objectscript.conn.username": {
466461
"description": "Username",
@@ -477,18 +472,29 @@
477472
"type": "string",
478473
"default": "USER"
479474
},
480-
"objectscript.conn.version": {
481-
"description": "API Version number",
482-
"type": "number",
483-
"default": 3,
484-
"readOnly": true,
485-
"deprecationMessage": "This value loads from server"
486-
},
487475
"objectscript.conn.https": {
488476
"description": "Use SSL to access to API",
489477
"type": "boolean",
490478
"default": false
491479
},
480+
"objectscript.conn.docker-compose.service": {
481+
"description": "Name of service in docker-compose",
482+
"type": "string"
483+
},
484+
"objectscript.conn.docker-compose.port": {
485+
"description": "Target port inside the service in docker-compose",
486+
"type": "number",
487+
"default": 52773
488+
},
489+
"objectscript.conn.docker-compose.file": {
490+
"description": "docker-compose file",
491+
"type": "string",
492+
"default": "docker-compose.yml"
493+
},
494+
"objectscript.conn.docker-compose": {
495+
"description": "Connect to server running in docker-compose",
496+
"type": "object"
497+
},
492498
"objectscript.export": {
493499
"type": "object",
494500
"description": "Export only the necessary stuff ",

src/api/index.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as request from "request-promise";
44
import * as url from "url";
55
import * as vscode from "vscode";
66
import * as Cache from "vscode-cache";
7-
import { config, extensionContext, FILESYSTEM_SCHEMA, workspaceState } from "../extension";
7+
import { config, extensionContext, FILESYSTEM_SCHEMA, workspaceState, panel } from "../extension";
88
import { currentWorkspaceFolder, outputConsole, outputChannel } from "../utils";
99

1010
const DEFAULT_API_VERSION = 1;
@@ -24,6 +24,10 @@ export class AtelierAPI {
2424
return workspaceState.get(this.workspaceFolder + ":apiVersion", DEFAULT_API_VERSION);
2525
}
2626

27+
private get port(): number {
28+
return workspaceState.get(this.workspaceFolder + ":port", this.config.port);
29+
}
30+
2731
private get iris(): boolean {
2832
return workspaceState.get(this.workspaceFolder + ":iris", false);
2933
}
@@ -34,7 +38,7 @@ export class AtelierAPI {
3438
if (wsOrFile instanceof vscode.Uri) {
3539
if (wsOrFile.scheme === FILESYSTEM_SCHEMA) {
3640
workspaceFolderName = wsOrFile.authority;
37-
const query = url.parse(decodeURIComponent(wsOrFile.toString()), true).query;
41+
const { query } = url.parse(wsOrFile.toString(), true);
3842
if (query) {
3943
if (query.ns && query.ns !== "") {
4044
const namespace = query.ns.toString();
@@ -61,8 +65,13 @@ export class AtelierAPI {
6165
return this.cache.get("cookies", []);
6266
}
6367

68+
public clearCookies(): void {
69+
this.cache.set("cookies", []);
70+
}
71+
6472
public xdebugUrl(): string {
65-
const { host, port, username, password, https } = this.config;
73+
const { host, username, password, https } = this.config;
74+
const port = this.port;
6675
const proto = https ? "wss" : "ws";
6776
const auth = this.iris
6877
? `IRISUsername=${username}&IRISPassword=${password}`
@@ -88,7 +97,8 @@ export class AtelierAPI {
8897
this.workspaceFolder = workspaceFolderName;
8998
const conn = config("conn", workspaceFolderName);
9099
this.config = conn;
91-
const { name, host, port } = this.config;
100+
const { name, host } = this.config;
101+
const port = this.port;
92102
this.cache = new Cache(extensionContext, `API:${name}:${host}:${port}`);
93103
}
94104

@@ -134,7 +144,8 @@ export class AtelierAPI {
134144
}
135145
headers["Cache-Control"] = "no-cache";
136146

137-
const { host, port, username, password, https } = this.config;
147+
const { host, username, password, https } = this.config;
148+
const port = this.port;
138149
const proto = this.config.https ? "https" : "http";
139150
const http: any = this.config.https ? httpsModule : httpModule;
140151
const agent = new http.Agent({
@@ -151,11 +162,12 @@ export class AtelierAPI {
151162
} else if (!cookies.length) {
152163
auth = this.request(0, "HEAD");
153164
}
165+
const connInfo = `${host}:${port}[${this.ns}]`;
154166
return auth.then(cookie => {
155167
return (
156168
request({
157169
agent,
158-
auth: { username, password, sendImmediately: false },
170+
auth: { username, password, sendImmediately: true },
159171
body: ["PUT", "POST"].includes(method) ? body : null,
160172
headers: {
161173
...headers,
@@ -170,6 +182,7 @@ export class AtelierAPI {
170182
// .catch(error => error.error)
171183
.then(response => this.updateCookies(response.headers["set-cookie"]).then(() => response))
172184
.then(response => {
185+
panel.text = `${connInfo} - Connected`;
173186
// console.log(`APIResponse: ${method} ${proto}://${host}:${port}${path}`)
174187
if (method === "HEAD") {
175188
return this.cookies;
@@ -203,10 +216,11 @@ export class AtelierAPI {
203216
if (info && info.result && info.result.content && info.result.content.api > 0) {
204217
const data = info.result.content;
205218
const apiVersion = data.api;
206-
if (!data.namespaces.includes(this.ns)) {
219+
if (!data.namespaces.includes(this.ns.toUpperCase())) {
207220
throw {
208221
code: "WrongNamespace",
209-
message: "This server does not have specified namespace.",
222+
message: `This server does not have specified namespace '${this.ns}'.\n
223+
You must select one of the following: ${data.namespaces.join(", ")}.`,
210224
};
211225
}
212226
return Promise.all([

src/commands/studio.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,53 @@ function doMenuAction(uri: vscode.Uri, menuType: string): Promise<any> {
2626
list.concat(
2727
sub.items
2828
.filter(el => el.id !== "" && el.separator == 0 && el.enabled == 1)
29-
.map(el => ({ ...el, id: `${sub.id},${el.id}`, label: el.name }))
29+
.map(el => ({ ...el, id: `${sub.id},${el.id}`, label: el.name, itemId: el.id, type: sub.type }))
3030
),
3131
[]
3232
)
3333
)
34+
.then(menuItems =>
35+
menuItems.filter((item: any, index: number, self: any) => {
36+
if (item && item.type === "main") {
37+
return true;
38+
}
39+
return self.findIndex((el): boolean => el.itemId === item.itemId) === index;
40+
})
41+
)
3442
.then(menuItems => {
3543
return vscode.window.showQuickPick<StudioAction>(menuItems, { canPickMany: false });
3644
})
37-
.then(({ id, label }) => ({
38-
id: id,
39-
label,
40-
name,
41-
}))
4245
.then(action => {
43-
if (action) {
44-
const query = "select * from %Atelier_v1_Utils.Extension_UserAction(?, ?, ?, ?)";
45-
let selectedText = "";
46-
const editor = vscode.window.activeTextEditor;
47-
if (!editor) {
48-
selectedText = ""
49-
}
50-
const selection = editor.selection;
51-
selectedText = editor.document.getText(selection);
52-
53-
const parameters = ["0", action.id, name, selectedText];
54-
return vscode.window.withProgress(
55-
{
56-
cancellable: false,
57-
location: vscode.ProgressLocation.Notification,
58-
title: `Executing user action: ${action.label}`,
59-
},
60-
() =>
61-
api
62-
.actionQuery(query, parameters)
63-
.then(data => data.result.content.pop())
64-
.then(userAction => {
65-
if (userAction && userAction.action != "0") {
66-
outputChannel.appendLine(`Studio Action "${action.label}" not supported`);
67-
outputChannel.show();
68-
}
69-
})
70-
);
46+
if (!action) {
47+
return;
7148
}
49+
const query = "select * from %Atelier_v1_Utils.Extension_UserAction(?, ?, ?, ?)";
50+
let selectedText = "";
51+
const editor = vscode.window.activeTextEditor;
52+
if (!editor) {
53+
selectedText = "";
54+
}
55+
const selection = editor.selection;
56+
selectedText = editor.document.getText(selection);
57+
58+
const parameters = ["0", action.id, name, selectedText];
59+
return vscode.window.withProgress(
60+
{
61+
cancellable: false,
62+
location: vscode.ProgressLocation.Notification,
63+
title: `Executing user action: ${action.label}`,
64+
},
65+
() =>
66+
api
67+
.actionQuery(query, parameters)
68+
.then(data => data.result.content.pop())
69+
.then(userAction => {
70+
if (userAction && userAction.action != "0") {
71+
outputChannel.appendLine(`Studio Action "${action.label}" not supported`);
72+
outputChannel.show();
73+
}
74+
})
75+
);
7276
});
7377
}
7478

src/extension.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { ObjectScriptExplorerProvider } from "./explorer/explorer";
4646
import { WorkspaceNode } from "./explorer/models/workspaceNode";
4747
import { FileSystemProvider } from "./providers/FileSystemPovider/FileSystemProvider";
4848
import { WorkspaceSymbolProvider } from "./providers/WorkspaceSymbolProvider";
49-
import { currentWorkspaceFolder, outputChannel } from "./utils";
49+
import { currentWorkspaceFolder, outputChannel, portFromDockerCompose, terminalWithDocker } from "./utils";
5050
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
5151
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
5252

@@ -59,6 +59,8 @@ export let explorerProvider: ObjectScriptExplorerProvider;
5959
export let documentContentProvider: DocumentContentProvider;
6060
export let workspaceState: vscode.Memento;
6161
export let extensionContext: vscode.ExtensionContext;
62+
export let panel: vscode.StatusBarItem;
63+
export let terminal: vscode.Terminal;
6264

6365
import TelemetryReporter from "vscode-extension-telemetry";
6466

@@ -112,13 +114,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
112114

113115
vscode.window.registerTreeDataProvider("ObjectScriptExplorer", explorerProvider);
114116

115-
const panel = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
117+
panel = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
116118

117119
const debugAdapterFactory = new ObjectScriptDebugAdapterDescriptorFactory();
118120

119121
panel.command = "vscode-objectscript.serverActions";
120122
panel.show();
121-
const checkConnection = () => {
123+
const checkConnection = (clearCookies = false) => {
122124
const conn = config("conn");
123125
const connInfo = `${conn.host}:${conn.port}[${conn.ns}]`;
124126
panel.text = connInfo;
@@ -128,11 +130,22 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
128130
panel.text = `${connInfo} - Disabled`;
129131
return;
130132
}
133+
const { port: dockerPort, docker: withDocker } = portFromDockerCompose(config("conn.docker-compose"), conn.port);
134+
if (withDocker) {
135+
terminal = terminalWithDocker();
136+
if (dockerPort !== conn.port) {
137+
workspaceState.update(currentWorkspaceFolder() + ":port", dockerPort);
138+
}
139+
}
140+
131141
const api = new AtelierAPI(currentWorkspaceFolder());
142+
if (clearCookies) {
143+
api.clearCookies();
144+
}
132145
api
133146
.serverInfo()
134147
.then(async info => {
135-
panel.text = `${connInfo} - Connected`;
148+
// panel.text = `${connInfo} - Connected`;
136149
})
137150
.catch(error => {
138151
let message = error.message;
@@ -154,8 +167,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
154167
});
155168
};
156169
checkConnection();
157-
vscode.workspace.onDidChangeConfiguration(() => {
158-
checkConnection();
170+
vscode.workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
171+
if (affectsConfiguration("objectscript.conn")) {
172+
checkConnection(true);
173+
}
159174
});
160175

161176
workspace.onDidSaveTextDocument(file => {

src/providers/FileSystemPovider/FileSearchProvider.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export class FileSearchProvider implements vscode.FileSearchProvider {
1212
options: vscode.FileSearchOptions,
1313
token: vscode.CancellationToken
1414
): vscode.ProviderResult<vscode.Uri[]> {
15-
console.log("options", options);
1615
return [];
1716
}
1817
}

0 commit comments

Comments
 (0)