Skip to content

Commit

Permalink
feat: add support for capturing and managing global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 23, 2024
1 parent e991c5a commit d91600f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 16 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@
| `vscode-hurl-runner.rerunLastCommand` | Hurl Runner: Rerun Last Command |
| `vscode-hurl-runner.runHurlFromBegin` | Hurl Runner: Run from Begin to Current |
| `vscode-hurl-runner.viewLastResponse` | Hurl Runner: View Last Response |
| `vscode-hurl-runner.removeGlobalVariable` | Remove Global Variable |

<!-- commands -->

### Configs

<!-- configs -->

| Key | Description | Type | Default |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ----------- |
| `vscode-hurl-runner.hurlPath` | Path to the Hurl executable | `string` | `"hurl"` |
| `vscode-hurl-runner.verboseMode` | Set the verbosity level for Hurl execution. 'verbose' provides basic information about requests and responses. 'very-verbose' includes detailed information, including timing data. | `string` | `"verbose"` |
| Key | Description | Type | Default |
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ----------- |
| `vscode-hurl-runner.hurlPath` | Path to the Hurl executable | `string` | `"hurl"` |
| `vscode-hurl-runner.verboseMode` | Set the verbosity level for Hurl execution. 'verbose' provides basic information about requests and responses. 'very-verbose' includes detailed information, including timing data. | `string` | `"verbose"` |
| `vscode-hurl-runner.captureToGlobalVariable` | When enabled, captured values will be set as global variables. | `boolean` | `false` |

<!-- configs -->

Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
{
"command": "vscode-hurl-runner.viewLastResponse",
"title": "Hurl Runner: View Last Response"
},
{
"command": "vscode-hurl-runner.removeGlobalVariable",
"title": "Remove Global Variable"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -108,6 +112,11 @@
"enum": ["verbose", "very-verbose"],
"default": "verbose",
"description": "Set the verbosity level for Hurl execution. 'verbose' provides basic information about requests and responses. 'very-verbose' includes detailed information, including timing data."
},
"vscode-hurl-runner.captureToGlobalVariable": {
"type": "boolean",
"default": false,
"description": "When enabled, captured values will be set as global variables."
}
}
},
Expand Down Expand Up @@ -181,14 +190,22 @@
"scopeName": "source.hurl",
"path": "./syntaxes/hurl.tmLanguage.json"
}
]
],
"menus": {
"view/item/context": [
{
"command": "vscode-hurl-runner.removeGlobalVariable",
"when": "view == hurlVariables && viewItem == globalVariable"
}
]
}
},
"activationEvents": ["onLanguage:hurl"],
"devDependencies": {
"@antfu/ni": "0.23.0",
"@biomejs/biome": "1.9.4",
"@types/node": "22.7.9",
"@types/vscode": "1.94.0",
"@types/vscode": "1.93.0",
"@vscode/vsce": "3.2.0",
"bumpp": "9.7.1",
"esno": "4.8.0",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/hurl-variables-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class HurlVariablesProvider {
private variables: Map<string, Map<string, string>> = new Map();
private inlineVariables: Map<string, Map<string, string>> = new Map();
private globalVariables: Map<string, string> = new Map();

public getVariablesBy(filePath: string): Record<string, string> {
return Object.fromEntries(this.variables.get(filePath) || new Map());
Expand Down Expand Up @@ -54,9 +55,22 @@ export class HurlVariablesProvider {
this.inlineVariables.set(filePath, new Map(Object.entries(variables)));
}

public getGlobalVariables(): Record<string, string> {
return Object.fromEntries(this.globalVariables);
}

public setGlobalVariable(name: string, value: string): void {
this.globalVariables.set(name, value);
}

public removeGlobalVariable(name: string): void {
this.globalVariables.delete(name);
}

public getAllVariablesBy(filePath: string): Record<string, string> {
const envVariables = this.getVariablesBy(filePath);
const inlineVariables = this.getInlineVariablesBy(filePath);
return { ...envVariables, ...inlineVariables };
const globalVariables = this.getGlobalVariables();
return { ...envVariables, ...globalVariables, ...inlineVariables };
}
}
22 changes: 22 additions & 0 deletions src/hurl-variables-tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class HurlVariablesTreeProvider
const envVariables = this.hurlVariablesProvider.getVariablesBy(filePath);
const inlineVariables =
this.hurlVariablesProvider.getInlineVariablesBy(filePath);
const globalVariables = this.hurlVariablesProvider.getGlobalVariables();

if (this.envFile) {
const envFileVariables = await this.loadEnvFileVariables(this.envFile);
Expand Down Expand Up @@ -124,6 +125,27 @@ export class HurlVariablesTreeProvider
),
);
}

if (Object.keys(globalVariables).length > 0) {
const globalItems = Object.entries(globalVariables).map(
([key, value]) =>
new VariableItem(
key,
value,
vscode.TreeItemCollapsibleState.None,
"globalVariable",
),
);
rootItems.push(
new VariableItem(
"Captured Variables",
"",
vscode.TreeItemCollapsibleState.Expanded,
"category",
globalItems,
),
);
}
}
return rootItems;
}
Expand Down
33 changes: 32 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { HurlCodeLensProvider } from "./hurl-code-lens-provider";
import { parseHurlOutput } from "./hurl-parser";
import { HurlVariablesProvider } from "./hurl-variables-provider";
import { HurlVariablesTreeProvider } from "./hurl-variables-tree-provider";
import { chooseEnvFile, manageEnvVariables } from "./manage-variables";
import {
chooseEnvFile,
manageEnvVariables,
saveCapturedValues,
} from "./manage-variables";
import {
type LastResponseInfo,
executeHurl,
Expand Down Expand Up @@ -221,6 +225,19 @@ const { activate, deactivate } = defineExtension(() => {
resultPanel.reveal(vscode.ViewColumn.Two);
};

const saveCapturedValuesFromLastResponse = (
stderr: string,
stdout: string,
) => {
const parsedOutput = parseHurlOutput(stderr, stdout);
for (const entry of parsedOutput.entries) {
const captures = entry.captures ?? {};
if (Object.keys(captures).length > 0) {
saveCapturedValues(hurlVariablesProvider, captures);
}
}
};

// Run hurl at the current line
useCommand(commands.runHurl, async (lineNumber?: number) => {
const runHurlCommand = async (entryNumber?: number) => {
Expand Down Expand Up @@ -267,6 +284,7 @@ const { activate, deactivate } = defineExtension(() => {
});

showResultInWebView(result);
saveCapturedValuesFromLastResponse(result.stderr, result.stdout);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
Expand Down Expand Up @@ -321,6 +339,7 @@ const { activate, deactivate } = defineExtension(() => {
});

showResultInWebView(result);
saveCapturedValuesFromLastResponse(result.stderr, result.stdout);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
Expand Down Expand Up @@ -367,6 +386,7 @@ const { activate, deactivate } = defineExtension(() => {
});

showResultInWebView(result);
saveCapturedValuesFromLastResponse(result.stderr, result.stdout);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
Expand Down Expand Up @@ -505,6 +525,7 @@ const { activate, deactivate } = defineExtension(() => {
});

showResultInWebView(result);
saveCapturedValuesFromLastResponse(result.stderr, result.stdout);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
Expand Down Expand Up @@ -594,6 +615,16 @@ const { activate, deactivate } = defineExtension(() => {
}
});

useCommand(commands.removeGlobalVariable, async (item) => {
if (item.contextValue === "globalVariable") {
hurlVariablesProvider.removeGlobalVariable(item.label);
hurlVariablesTreeProvider.refresh();
vscode.window.showInformationMessage(
`Removed global variable: ${item.label}`,
);
}
});

return {
dispose: () => {
if (resultPanel) {
Expand Down
13 changes: 13 additions & 0 deletions src/manage-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as vscode from "vscode";

import { config } from "./config";
import type { HurlVariablesProvider } from "./hurl-variables-provider";
import { logger } from "./utils";

Expand Down Expand Up @@ -361,3 +362,15 @@ async function removeInlineVariable(
);
}
}

export async function saveCapturedValues(
hurlVariablesProvider: HurlVariablesProvider,
captures: Record<string, string>,
): Promise<void> {
if (config.captureToGlobalVariable) {
for (const [key, value] of Object.entries(captures)) {
hurlVariablesProvider.setGlobalVariable(key, value);
logger.info(`Captured value set as global variable: ${key} = ${value}`);
}
}
}

0 comments on commit d91600f

Please sign in to comment.