Skip to content

Commit 41d1d05

Browse files
committed
Add support for variables in demo steps and update documentation #2
1 parent 2007514 commit 41d1d05

File tree

9 files changed

+113
-32
lines changed

9 files changed

+113
-32
lines changed

.demo/demo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "https://elio.dev/demo-time.schema.json",
23
"title": "The demo of the demo",
34
"description": "This is a demo of the demo",
45
"demos": [{

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## [0.0.36] - 2025-12-09
4+
5+
- [#2](https://github.com/estruyf/vscode-demo-time/issues/2): Added the ability to have a `variables.json` file to store variables/constants that can be used in the demo steps
6+
37
## [0.0.35] - 2025-12-09
48

59
- Added the `setSetting` action to update a setting value

README.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424

2525
Currently the extension supports the following features:
2626

27-
- Multiple demo files located in `.demo` folder
27+
- Multiple demo files located in `.demo` folder.
2828
- Support for code/snippet files in the `.demo` folder. These files can be referenced in the demo steps, instead of adding the code in the JSON file.
29-
- Explorer panel to execute your demo steps
30-
- Add new demo steps (execute the `Demo Time: Add as demo step` command)
31-
- Run through the demo steps (execute the `Demo Time: Start` command)
32-
- Presentation mode which allows you to use a clicker to navigate through the demo steps
33-
- Run a specific demo step from a command execution with the `demo-time.runById` command
29+
- Explorer panel to execute your demo steps.
30+
- Add new demo steps (execute the `Demo Time: Add as demo step` command).
31+
- Run through the demo steps (execute the `Demo Time: Start` command).
32+
- Presentation mode which allows you to use a clicker to navigate through the demo steps.
33+
- Run a specific demo step from a command execution with the `demo-time.runById` command.
34+
- Place your variables in a `variables.json` file in the `.demo` folder. You can reference these variables like `{variable_name}` in your demo steps.
3435

35-
### Supported demo steps
36+
## Supported demo step actions
3637

37-
#### File actions
38+
### File actions
3839

3940
<table>
4041
<tr>
@@ -100,7 +101,7 @@ Currently the extension supports the following features:
100101
</tr>
101102
</table>
102103

103-
#### Code actions
104+
### Code actions
104105

105106
<table>
106107
<tr>
@@ -212,7 +213,7 @@ Currently the extension supports the following features:
212213
</tr>
213214
</table>
214215

215-
#### Setting actions
216+
### Setting actions
216217

217218
<table>
218219
<tr>
@@ -243,7 +244,7 @@ Currently the extension supports the following features:
243244
</tr>
244245
</table>
245246

246-
##### Setting update example
247+
#### Setting update example
247248

248249
Here is an example of how you can hide the activity and status bar in Visual Studio Code.
249250

@@ -283,7 +284,7 @@ To reset the settings, you can use the following steps:
283284
}
284285
```
285286

286-
#### Time actions
287+
### Time actions
287288

288289
<table>
289290
<tr>
@@ -328,7 +329,7 @@ To reset the settings, you can use the following steps:
328329
</tr>
329330
</table>
330331

331-
#### VSCode actions
332+
### VS Code actions
332333

333334
<table>
334335
<tr>
@@ -376,7 +377,7 @@ To reset the settings, you can use the following steps:
376377
</tr>
377378
</table>
378379

379-
#### Terminal actions
380+
### Terminal actions
380381

381382
<table>
382383
<tr>
@@ -404,7 +405,7 @@ To reset the settings, you can use the following steps:
404405
</tr>
405406
</table>
406407

407-
#### Snippets
408+
### Snippets
408409

409410
<table>
410411
<tr>
@@ -436,7 +437,7 @@ To reset the settings, you can use the following steps:
436437
</tr>
437438
</table>
438439

439-
##### Snippet example
440+
#### Snippet example
440441

441442
In the demo file, you can reference a snippet file. The snippet file can contain multiple steps which can be reused in multiple demos.
442443

@@ -455,6 +456,8 @@ In the demo file, you can reference a snippet file. The snippet file can contain
455456

456457
> The `contentPath` property its value is relative to the `.demo` folder. So, in the example above, the snippet file is located in the `.demo/snippets` folder.
457458
459+
> In the `args` property, you can define the arguments/variables which you want to use in the snippet file. In the snippet file, you can reference these arguments with curly braces `{argument name}`.
460+
458461
In the `insert_and_highlight.json` file, you can define the steps you want to execute.
459462

460463
```json
@@ -481,6 +484,37 @@ In the `insert_and_highlight.json` file, you can define the steps you want to ex
481484

482485
To use the extension, you need to create a `.demo` folder in your workspace. Once created, you can add a JSON file which contains the demo and its steps.
483486

487+
```json
488+
{
489+
"$schema": "https://elio.dev/demo-time.schema.json",
490+
"title": "<title>",
491+
"description": "<description>",
492+
"demos": []
493+
}
494+
```
495+
496+
### Working with variables
497+
498+
You can define variables in a `variables.json` file in the `.demo` folder. You can reference these variables in your demo steps by using curly braces `{variable_name}`.
499+
500+
#### Example variables file
501+
502+
```json
503+
{
504+
"SLIDES_URL": "http://localhost:3030"
505+
}
506+
```
507+
508+
#### Example demo step
509+
510+
```json
511+
{
512+
"action": "executeVSCodeCommand",
513+
"command": "simpleBrowser.show",
514+
"args": "{SLIDES_URL}"
515+
}
516+
```
517+
484518
## Settings
485519

486520
| Setting | Description | Default |
@@ -491,9 +525,9 @@ To use the extension, you need to create a `.demo` folder in your workspace. Onc
491525
| `demoTime.timer` | Count down timer for how long the session should last. If not set, it will not count down. The value is the number of minutes. | `null` |
492526
| `demoTime.insertLineSpeed` | The speed in milliseconds for inserting lines. If you set it to `0`, it will insert its content immediately. | `25` |
493527

494-
### Tips
528+
## Tips and tricks
495529

496-
#### Position
530+
### Position
497531

498532
For the position you can use the following formats:
499533

@@ -503,7 +537,7 @@ For the position you can use the following formats:
503537
- `start` will be replaced by the first line number
504538
- `end` will be replaced by the last line number
505539

506-
#### Adding content to a file
540+
### Adding content to a file
507541

508542
When you want to insert content to a file, you can use the `content` or `contentPath` properties in the demo step.
509543

@@ -512,7 +546,7 @@ When you want to insert content to a file, you can use the `content` or `content
512546
| `content` | This property allows you to add the content directly in the JSON file, but this can make your JSON file quite big and it can be hard to read. |
513547
| `contentPath` | This property allows you to reference a file in the `.demo` folder. This way you can keep your JSON file clean and add the content in separate files. **Important**: the path is relative to the `.demo` folder. |
514548

515-
### Example
549+
### Example demo file
516550

517551
Here is an example demo:
518552

src/constants/General.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const General = {
22
demoFolder: ".demo",
3+
variablesFile: "variables.json",
34
};

src/services/DemoRunner.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { FileProvider } from "./FileProvider";
2020
import { DemoPanel } from "../panels/DemoPanel";
2121
import {
22+
getVariables,
2223
getFileContents,
2324
getLineInsertionSpeed,
2425
getLineRange,
@@ -32,6 +33,7 @@ import { DecoratorService } from "./DecoratorService";
3233
import { Notifications } from "./Notifications";
3334
import { parse as jsonParse } from "jsonc-parser";
3435
import { Logger } from "./Logger";
36+
import { insertVariables } from "../utils/insertVariables";
3537

3638
const DEFAULT_START_VALUE = {
3739
filePath: "",
@@ -245,6 +247,13 @@ export class DemoRunner {
245247
return;
246248
}
247249

250+
let variables = await getVariables(workspaceFolder);
251+
if (variables && Object.keys(variables)) {
252+
let tempSteps = JSON.stringify(demoSteps);
253+
tempSteps = insertVariables(tempSteps, variables);
254+
demoSteps = jsonParse(tempSteps);
255+
}
256+
248257
// Replace the snippets in the demo steps
249258
const stepsToExecute: Step[] = [];
250259
if (demoSteps.some((step) => step.action === "snippet")) {
@@ -255,10 +264,13 @@ export class DemoRunner {
255264
return;
256265
}
257266

267+
// Replace the argument variables in the snippet
258268
const args = step.args || {};
259-
for (const key in args) {
260-
let regex = new RegExp(`{${key}}`, "g");
261-
snippet = snippet.replace(regex, args[key]);
269+
snippet = insertVariables(snippet, args);
270+
271+
// Replace the variables in the snippet
272+
if (variables && Object.keys(variables)) {
273+
snippet = insertVariables(snippet, variables);
262274
}
263275

264276
const newSteps = jsonParse(snippet);

src/services/FileProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ export class FileProvider {
3030
* @returns A promise that resolves to an object containing the demo files, or null if no files are found.
3131
*/
3232
public static async getFiles(): Promise<DemoFiles | null> {
33-
const files = await workspace.findFiles(`${General.demoFolder}/*.json`, `**/node_modules/**`);
33+
let files = await workspace.findFiles(`${General.demoFolder}/*.json`, `**/node_modules/**`);
3434

3535
if (files.length <= 0) {
3636
return null;
3737
}
3838

39+
// Exclude the constants file
40+
files = files.filter((file) => !file.path.endsWith(General.variablesFile));
41+
3942
const demoFiles: DemoFiles = {};
4043

4144
for (const file of files) {

src/utils/getVariables.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Uri, WorkspaceFolder, workspace } from "vscode";
2+
import { General } from "../constants";
3+
import { parse as jsonParse } from "jsonc-parser";
4+
5+
export const getVariables = async (workspaceFolder: WorkspaceFolder): Promise<{ [key: string]: any} | undefined> => {
6+
try {
7+
const contentUri = Uri.joinPath(workspaceFolder.uri, General.demoFolder, General.variablesFile);
8+
if (!contentUri) {
9+
return;
10+
}
11+
12+
const contentEditor = await workspace.openTextDocument(contentUri);
13+
const content = contentEditor.getText();
14+
return jsonParse(content);
15+
} catch (error) {
16+
console.error(error);
17+
}
18+
};

src/utils/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
export * from "./getLineInsertionSpeed";
2-
export * from "./getLineRange";
3-
export * from "./getfileContents";
4-
export * from "./insertContent";
5-
export * from "./insertLineByLine";
6-
export * from "./parseWinPath";
7-
export * from "./replaceContent";
8-
export * from "./sleep";
1+
export * from './getLineInsertionSpeed';
2+
export * from './getLineRange';
3+
export * from './getVariables';
4+
export * from './getfileContents';
5+
export * from './insertContent';
6+
export * from './insertLineByLine';
7+
export * from './parseWinPath';
8+
export * from './replaceContent';
9+
export * from './sleep';

src/utils/insertVariables.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const insertVariables = (text: string, variables: { [key: string]: any }) => {
2+
for (const key in variables) {
3+
let regex = new RegExp(`{${key}}`, "g");
4+
text = text.replace(regex, variables[key]);
5+
}
6+
return text;
7+
};

0 commit comments

Comments
 (0)