diff --git a/deadlock-plugins/deadlock-extension/README.md b/deadlock-plugins/deadlock-extension/README.md index 01fc6452bd81c2d5c43bbcdb2ffd01b0291c5ab6..c24101e2f3ea48b3dbe46de58278e86a648bc763 100644 --- a/deadlock-plugins/deadlock-extension/README.md +++ b/deadlock-plugins/deadlock-extension/README.md @@ -1,8 +1,15 @@ # Deadlock Coding Vscode extension to show a panel with : -* adresses availables for the current challenge pulled from `/home/config/user-challenge.json` -* challenge instruction pulled from the `README.md` -## Run first the `setup-dev-env.sh` then you can start your plugin with `VsCode Run`. +- adresses availables for the current challenge pulled from `/home/config/user-challenge.json` +- challenge instruction pulled from the `README.md` +## Quick start + +- Run `setup-dev-env.sh` to mock challenge configuration +- Run `install.sh` +- Run `build.sh` +- Press `F5` (or use `Run and Debug` tab). + +If something goes wrong, you may need to kill watch Task terminal between restarts. diff --git a/deadlock-plugins/deadlock-extension/package-lock.json b/deadlock-plugins/deadlock-extension/package-lock.json index bd72d9874bf202ff0f8e82c47a14812ce3d1e991..b333a7a44e20b089514ec4236f37dd0fd07631f8 100644 --- a/deadlock-plugins/deadlock-extension/package-lock.json +++ b/deadlock-plugins/deadlock-extension/package-lock.json @@ -1,12 +1,12 @@ { "name": "deadlock-coding", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "deadlock-coding", - "version": "0.0.1", + "version": "0.0.2", "dependencies": { "async": "^3.2.2", "date-fns": "^2.27.0", @@ -15,7 +15,7 @@ }, "devDependencies": { "@types/marked": "^4.0.1", - "@types/node": "^12.20.37", + "@types/node": "^12.20.47", "@types/vscode": "^1.51.0", "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", @@ -194,9 +194,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "12.20.37", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.37.tgz", - "integrity": "sha512-i1KGxqcvJaLQali+WuypQnXwcplhtNtjs66eNsZpp2P2FL/trJJxx/VWsM0YCL2iMoIJrbXje48lvIQAQ4p2ZA==", + "version": "12.20.47", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.47.tgz", + "integrity": "sha512-BzcaRsnFuznzOItW1WpQrDHM7plAa7GIDMZ6b5pnMbkqEtM/6WCOhvZar39oeMQP79gwvFUWjjptE7/KGcNqFg==", "dev": true }, "node_modules/@types/vscode": { @@ -4295,9 +4295,9 @@ "dev": true }, "@types/node": { - "version": "12.20.37", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.37.tgz", - "integrity": "sha512-i1KGxqcvJaLQali+WuypQnXwcplhtNtjs66eNsZpp2P2FL/trJJxx/VWsM0YCL2iMoIJrbXje48lvIQAQ4p2ZA==", + "version": "12.20.47", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.47.tgz", + "integrity": "sha512-BzcaRsnFuznzOItW1WpQrDHM7plAa7GIDMZ6b5pnMbkqEtM/6WCOhvZar39oeMQP79gwvFUWjjptE7/KGcNqFg==", "dev": true }, "@types/vscode": { diff --git a/deadlock-plugins/deadlock-extension/package.json b/deadlock-plugins/deadlock-extension/package.json index 0045544c8830b94b4bdc278b3ffd898aacb1533d..984302eae3d6e7e54653d9265aaea3b6f79a98e6 100644 --- a/deadlock-plugins/deadlock-extension/package.json +++ b/deadlock-plugins/deadlock-extension/package.json @@ -1,7 +1,7 @@ { "name": "deadlock-coding", "description": "Deadlock Coding", - "version": "0.0.1", + "version": "0.0.2", "publisher": "Deadlock", "engines": { "vscode": "^1.51.0" @@ -72,7 +72,7 @@ }, "devDependencies": { "@types/marked": "^4.0.1", - "@types/node": "^12.20.37", + "@types/node": "^12.20.47", "@types/vscode": "^1.51.0", "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", diff --git a/deadlock-plugins/deadlock-extension/src/core/config.ts b/deadlock-plugins/deadlock-extension/src/core/config.ts index c99f4e45c5393af1b13793dd5182caa51f138b13..ff72a36003b87cf4bceb70f15eb013e319a29219 100644 --- a/deadlock-plugins/deadlock-extension/src/core/config.ts +++ b/deadlock-plugins/deadlock-extension/src/core/config.ts @@ -1,5 +1,5 @@ -import * as path from 'path'; import * as os from 'os'; +import * as path from 'path'; const homeDir = os.homedir(); // if we are on container, means the directory will depend differently @@ -30,3 +30,18 @@ export const USER_CHALLENGE_PATH = path.join( ); export const BRIEFING_FILE_NAME = 'briefing.md'; + +export const ENV_FILE_PATH = path.join( + PROJECT_THEIA_PATH, + '/.env' +); + +export const BASHRC_PATH = path.join( + homeDir, + '/.bashrc' +); + +export const SERVICES_PATHS_PATH = path.join( + PROJECT_THEIA_PATH, + '/paths.json' +); diff --git a/deadlock-plugins/deadlock-extension/src/core/metadataProvider.ts b/deadlock-plugins/deadlock-extension/src/core/metadataProvider.ts new file mode 100644 index 0000000000000000000000000000000000000000..22d50dda5700a4075c72853b8eaa9cc8740c037a --- /dev/null +++ b/deadlock-plugins/deadlock-extension/src/core/metadataProvider.ts @@ -0,0 +1,69 @@ +import { exec, ExecException } from 'child_process'; +import * as fs from 'fs'; +import { userConfig } from '../extension'; +import { error as err, log } from '../recorder/utils'; +import { BASHRC_PATH, ENV_FILE_PATH } from './config'; + +export default class MetadataProvider { + + public static loadPathsToJson(filePath: string) { + const portToPath = userConfig.getPaths(); + const serviceNameToPath = this.getServicePathFromPortPath(portToPath); + const jsonValue = Object.fromEntries(serviceNameToPath); + const fileContent = JSON.stringify(jsonValue, null, 4) + '\n'; + fs.writeFileSync(filePath, fileContent, { flag: 'w+'}); + } + + public static loadPathsToEnvVariables(): void { + const portToPath = userConfig.getPaths(); + const serviceNameToPath = this.getServicePathFromPortPath(portToPath); + let fileContent = ''; + for (const [serviceName, servicePath] of serviceNameToPath.entries()) { + fileContent += `${serviceName}=${servicePath} \n`; + } + fs.writeFileSync(ENV_FILE_PATH, fileContent, { flag: 'w+'}); + const bashrcAppendContent = ` +export $(cat ${ENV_FILE_PATH} | xargs -L 1) +`; + fs.writeFileSync(BASHRC_PATH, bashrcAppendContent, { flag: 'a+'}); + exec(`. ${BASHRC_PATH}`, execCallback); + } + + /** + * two formats to handle: (key="front", value="3001") | (key="8080": value="HXW3fnKwUULrfKS1-cdb")\ + * expected result: (key="front", value="localhost:3001") | (key="cdb", value="HXW3fnKwUULrfKS1-cdb") + * + */ + private static getServicePathFromPortPath(portToPath: Map<number, string>): Map<string, string> { + const serviceToPath = new Map<string, string>(); + for (const [port, path] of Object.entries(portToPath)) { + + let key = ''; + let value = ''; + if (isNumeric(port)) { + key = path.split('-').slice(-1); + value = `${path}.${userConfig.getHost()}`; + } else { + key = port; + value = `localhost:${path}`; + } + serviceToPath.set(key, value); + } + return serviceToPath; + } + +} + +function isNumeric(val: string) { return /^\d+$/.test(val); } + +function execCallback(error: ExecException | null, stdout: string, stderr: string) { + if (error) { + err(`error: ${error.message}`); + return; + } + if (stderr) { + err(`stderr: ${stderr}`); + return; + } + log(stdout); +} diff --git a/deadlock-plugins/deadlock-extension/src/extension.ts b/deadlock-plugins/deadlock-extension/src/extension.ts index 1840979df272bee60ea44bb886bb783f5eabcbee..aa7bc8dc7214bd709f53c830a23678059995602d 100644 --- a/deadlock-plugins/deadlock-extension/src/extension.ts +++ b/deadlock-plugins/deadlock-extension/src/extension.ts @@ -1,9 +1,11 @@ import * as vscode from 'vscode'; -import { DepNodeProvider } from './theia/deadlockPanel'; -import BriefingView, { BRIEFING_ID } from './view/briefingView'; +import { SERVICES_PATHS_PATH } from './core/config'; +import MetadataProvider from './core/metadataProvider'; +import { error } from './recorder/utils'; import { OPEN_BRIEFING_COMMAND } from './theia/command'; +import { DepNodeProvider } from './theia/deadlockPanel'; import UserConfigTheia from './theia/userConfigTheia'; -import { error } from './recorder/utils'; +import BriefingView from './view/briefingView'; export function initViews() { new BriefingView(); @@ -31,4 +33,6 @@ export async function activate(context: vscode.ExtensionContext) { deadlockPanelProvider ); + MetadataProvider.loadPathsToEnvVariables(); + MetadataProvider.loadPathsToJson(`${SERVICES_PATHS_PATH}`); } diff --git a/deadlock-plugins/deadlock-extension/src/view/briefingView.ts b/deadlock-plugins/deadlock-extension/src/view/briefingView.ts index b3e86b95281aa86a3fa0e2332e2812ef4a4e3d76..21a6ae7727d106c1595a71c3c7f6d5f8f12d2074 100644 --- a/deadlock-plugins/deadlock-extension/src/view/briefingView.ts +++ b/deadlock-plugins/deadlock-extension/src/view/briefingView.ts @@ -1,19 +1,12 @@ -import View from './view'; - -import * as vscode from 'vscode'; - +import * as fs from 'fs'; import { marked } from 'marked'; import * as path from 'path'; - -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); - -import { DOCS_PATH, BRIEFING_FILE_NAME } from '../core/config'; -import { WebviewBase } from './webviewBase'; -import { OPEN_BRIEFING_COMMAND } from '../theia/command'; - -import { userConfig } from '../extension'; import { setInterval } from 'timers'; +import * as vscode from 'vscode'; +import { BRIEFING_FILE_NAME, DOCS_PATH, ENV_FILE_PATH, SERVICES_PATHS_PATH } from '../core/config'; +import { userConfig } from '../extension'; +import { OPEN_BRIEFING_COMMAND } from '../theia/command'; +import { WebviewBase } from './webviewBase'; export const BRIEFING_ID = 'brefingView'; export default class BriefingView extends WebviewBase { @@ -129,7 +122,19 @@ export default class BriefingView extends WebviewBase { } if (pathsLength > 0) { - return `<h3>Configuration</h3>You have the following adresses availables for your challenge : <ul>${adresses}</ul>`; + return `<h3>Configuration</h3> + You have the following adresses availables for your challenge : <ul>${adresses}</ul> + </br> + You also have access to these paths from: + <ul> + <li> + environment variables (file <b>${ENV_FILE_PATH}</b> already exported) + </li> + <li> + JSON file <b>${SERVICES_PATHS_PATH}</b> + </li> + </ul> + </br>`; } return '';