Skip to content

Commit 3827598

Browse files
authored
Merge pull request #15 from badsyntax/dbcontext-info
Add dbcontext info command
2 parents 5684c00 + 0ee570a commit 3827598

File tree

11 files changed

+189
-15
lines changed

11 files changed

+189
-15
lines changed

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ A VS Code extension to manage Entity Framework migrations.
88

99
## Features
1010

11-
- List migrations by [DbContext](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext)
11+
- List migrations by [`DbContext`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext)
1212
- Add/remove/run/undo migrations
1313
- Show migration applied status
14-
- Export [DbContext](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext) as SQL script
14+
- Export `DbContext` as SQL script
15+
- View `DbContext` information
1516

1617
## Requirements
1718

@@ -23,7 +24,9 @@ A VS Code extension to manage Entity Framework migrations.
2324

2425
This extension contributes the following settings:
2526

26-
- `entityframework.commands`: Custom commands, for example:
27+
- `entityframework.commands`: Custom commands
28+
<details><summary>Example</summary>
29+
2730
```json
2831
{
2932
"entityframework.commands": {
@@ -96,11 +99,28 @@ This extension contributes the following settings:
9699
"\"$project\"",
97100
"--no-color",
98101
"--json"
102+
],
103+
"dbContextInfo": [
104+
"dotnet",
105+
"ef",
106+
"dbcontext",
107+
"info",
108+
"--context",
109+
"\"$dbContext\"",
110+
"--project",
111+
"\"$project\"",
112+
"--no-color",
113+
"--json"
99114
]
100115
}
101116
}
102117
```
103-
- `entityframework.env`: Custom environment variables, for example:
118+
119+
</details>
120+
121+
- `entityframework.env`: Custom environment variables
122+
<details><summary>Example</summary>
123+
104124
```json
105125
{
106126
"entityframework.env": {
@@ -110,9 +130,11 @@ This extension contributes the following settings:
110130
}
111131
```
112132

133+
</details>
134+
113135
## Performance
114136

115-
The EF tools execute application code at design time to get information about the project, thus performance on large projects can be slow.
137+
The EF tools execute application code at design time to get information about the project, thus _performance on large projects can be slow_.
116138

117139
## Support
118140

icons/help_dark.svg

Lines changed: 12 additions & 0 deletions
Loading

icons/help_light.svg

Lines changed: 23 additions & 0 deletions
Loading

package.json

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
"dark": "icons/trash_dark.svg"
5151
}
5252
},
53+
{
54+
"command": "entityframework.dbContextInfo",
55+
"title": "Information",
56+
"icon": {
57+
"light": "icons/help_light.svg",
58+
"dark": "icons/help_dark.svg"
59+
}
60+
},
5361
{
5462
"command": "entityframework.refreshTree",
5563
"title": "Refresh Migrations",
@@ -109,6 +117,10 @@
109117
"command": "entityframework.removeMigration",
110118
"when": "false"
111119
},
120+
{
121+
"command": "entityframework.dbContextInfo",
122+
"when": "false"
123+
},
112124
{
113125
"command": "entityframework.addMigration",
114126
"when": "false"
@@ -165,18 +177,34 @@
165177
"group": "context@1"
166178
},
167179
{
168-
"command": "entityframework.generateScript",
180+
"command": "entityframework.addMigration",
169181
"when": "viewItem == dbContext",
170-
"group": "inline@1"
182+
"group": "context@3"
171183
},
172184
{
173185
"command": "entityframework.addMigration",
174186
"when": "viewItem == dbContext",
175-
"group": "inline@2"
187+
"group": "inline@3"
176188
},
177189
{
178190
"command": "entityframework.generateScript",
179-
"when": "viewItem == dbContext"
191+
"when": "viewItem == dbContext",
192+
"group": "context@2"
193+
},
194+
{
195+
"command": "entityframework.generateScript",
196+
"when": "viewItem == dbContext",
197+
"group": "inline@2"
198+
},
199+
{
200+
"command": "entityframework.dbContextInfo",
201+
"when": "viewItem == dbContext",
202+
"group": "inline@1"
203+
},
204+
{
205+
"command": "entityframework.dbContextInfo",
206+
"when": "viewItem == dbContext",
207+
"group": "context@1"
180208
}
181209
]
182210
},
@@ -263,6 +291,18 @@
263291
"\"$project\"",
264292
"--no-color",
265293
"--json"
294+
],
295+
"dbContextInfo": [
296+
"dotnet",
297+
"ef",
298+
"dbcontext",
299+
"info",
300+
"--context",
301+
"\"$dbContext\"",
302+
"--project",
303+
"\"$project\"",
304+
"--no-color",
305+
"--json"
266306
]
267307
},
268308
"description": "Environment variables when interacting with Entity Framework."
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
3+
import { CLI } from '../cli/CLI';
4+
import { getCommandsConfig } from '../config/config';
5+
6+
import type { TerminalProvider } from '../terminal/TerminalProvider';
7+
import { TextDocumentProvider } from '../util/TextDocumentProvider';
8+
import { TerminalAction } from './TerminalAction';
9+
10+
export class DBContextInfoCommandAction extends TerminalAction {
11+
constructor(
12+
terminalProvider: TerminalProvider,
13+
workspaceRoot: string,
14+
dbContext: string,
15+
project: string,
16+
) {
17+
super(
18+
terminalProvider,
19+
getCommandsConfig().dbContextInfo,
20+
{
21+
dbContext,
22+
project,
23+
},
24+
workspaceRoot,
25+
);
26+
}
27+
28+
public async run() {
29+
const output = CLI.getDataFromStdOut(await super.run());
30+
const uri = vscode.Uri.parse(`${TextDocumentProvider.scheme}:${output}`);
31+
const doc = await vscode.workspace.openTextDocument(uri);
32+
await vscode.languages.setTextDocumentLanguage(doc, 'json');
33+
await vscode.window.showTextDocument(doc, { preview: false });
34+
return output;
35+
}
36+
}

src/actions/GenerateScriptAction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CLI } from '../cli/CLI';
33

44
import { getCommandsConfig } from '../config/config';
55
import type { TerminalProvider } from '../terminal/TerminalProvider';
6+
import { TextDocumentProvider } from '../util/TextDocumentProvider';
67
import { TerminalAction } from './TerminalAction';
78

89
export class GenerateScriptAction extends TerminalAction {
@@ -25,7 +26,7 @@ export class GenerateScriptAction extends TerminalAction {
2526

2627
public async run() {
2728
const output = CLI.getDataFromStdOut(await super.run());
28-
const uri = vscode.Uri.parse('ef-script:' + output);
29+
const uri = vscode.Uri.parse(`${TextDocumentProvider.scheme}:${output}`);
2930
const doc = await vscode.workspace.openTextDocument(uri);
3031
await vscode.languages.setTextDocumentLanguage(doc, 'sql');
3132
await vscode.window.showTextDocument(doc, { preview: false });

src/commands/CommandProvider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { TreeDataProvider } from '../treeView/TreeDataProvider';
77
import { Disposable } from '../util/Disposable';
88
import { AddMigrationCommand } from './AddMigrationCommand';
99
import type { Command } from './Command';
10+
import { DBContextInfoCommand } from './DBContextInfoCommand';
1011
import { GenerateScriptCommand } from './GenerateScriptCommand';
1112
import { OpenMigrationFileCommand } from './OpenMigrationFileCommand';
1213
import { RefreshTreeCommand } from './RefreshTreeCommand';
@@ -55,6 +56,11 @@ export class CommandProvider extends Disposable {
5556
OpenMigrationFileCommand.commandName,
5657
(item?: MigrationTreeItem) => new OpenMigrationFileCommand(item),
5758
);
59+
this.registerCommand(
60+
DBContextInfoCommand.commandName,
61+
(item?: DbContextTreeItem) =>
62+
new DBContextInfoCommand(terminalProvider, item),
63+
);
5864
}
5965

6066
public static getCommandName(name: string) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { DBContextInfoCommandAction } from '../actions/DBContextInfoCommandAction';
2+
import type { TerminalProvider } from '../terminal/TerminalProvider';
3+
import { type DbContextTreeItem } from '../treeView/DbContextTreeItem';
4+
import { Command } from './Command';
5+
6+
export class DBContextInfoCommand extends Command {
7+
public static commandName = 'dbContextInfo';
8+
9+
constructor(
10+
private readonly terminalProvider: TerminalProvider,
11+
private readonly item?: DbContextTreeItem,
12+
) {
13+
super();
14+
}
15+
16+
public async run() {
17+
if (!this.item) {
18+
return;
19+
}
20+
return new DBContextInfoCommandAction(
21+
this.terminalProvider,
22+
this.item.workspaceRoot,
23+
this.item.label,
24+
this.item.project,
25+
).run();
26+
}
27+
}

src/config/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type CommandsConfig = {
1212
generateScript: string[];
1313
listDbContexts: string[];
1414
listMigrations: string[];
15+
dbContextInfo: string[];
1516
};
1617

1718
export function getEnvConfig() {
@@ -30,5 +31,6 @@ export function getCommandsConfig() {
3031
generateScript: [],
3132
listDbContexts: [],
3233
listMigrations: [],
34+
dbContextInfo: [],
3335
});
3436
}

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CommandProvider } from './commands/CommandProvider';
55
import { MigrationTreeItemDecorationProvider } from './treeView/MigrationTreeItemDecorationProvider';
66
import { Terminal } from './terminal/Terminal';
77
import { TerminalProvider } from './terminal/TerminalProvider';
8-
import { ScriptFileProvider } from './util/ScriptFileProvider';
8+
import { TextDocumentProvider } from './util/TextDocumentProvider';
99
import { ProjectFilesProvider } from './solution/ProjectFilesProvider';
1010
import { Logger } from './util/Logger';
1111
import { OUTPUT_CHANNEL_ID } from './constants/constants';
@@ -21,7 +21,7 @@ export async function activate(_context: vscode.ExtensionContext) {
2121

2222
const cli = new CLI(logger);
2323
const projectFiles = await ProjectFilesProvider.getProjectFiles();
24-
const scriptFileProvider = new ScriptFileProvider();
24+
const textDocumentProvider = new TextDocumentProvider();
2525
const migrationTreeItemDecorationProvider =
2626
new MigrationTreeItemDecorationProvider();
2727
const treeDataProvider = new TreeDataProvider(projectFiles, cli);
@@ -35,7 +35,7 @@ export async function activate(_context: vscode.ExtensionContext) {
3535
treeDataProvider,
3636
commandProvider,
3737
terminalProvider,
38-
scriptFileProvider,
38+
textDocumentProvider,
3939
);
4040
}
4141

0 commit comments

Comments
 (0)