Change how data-workspace activates project extensions (#21470)

* change how data workspace activates project extensions

* cleanup

* undo whitespace change

* Update extensions/data-workspace/src/services/workspaceService.ts

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>

* activate extensions on data workspace new and open commands

* Update extensions/data-workspace/src/services/workspaceService.ts

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>
This commit is contained in:
Kim Santiago
2023-01-04 11:47:51 -08:00
committed by GitHub
parent a1803a823b
commit 5fbbc3a76b
3 changed files with 33 additions and 7 deletions

View File

@@ -54,6 +54,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<IExten
const registerCommandStartTime = new Date().getTime();
context.subscriptions.push(vscode.commands.registerCommand('projects.new', async () => {
// Make sure all project providing extensions are activated to be sure the project templates show up
await workspaceService.ensureProviderExtensionLoaded(undefined, true);
if (azdataApi) {
const dialog = new NewProjectDialog(workspaceService);
await dialog.open();
@@ -63,6 +66,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<IExten
}));
context.subscriptions.push(vscode.commands.registerCommand('projects.openExisting', async () => {
// Make sure all project providing extensions are activated so that all supported project types show up in the file filter
await workspaceService.ensureProviderExtensionLoaded(undefined, true);
if (azdataApi) {
const dialog = new OpenExistingDialog(workspaceService);
await dialog.open();

View File

@@ -255,8 +255,9 @@ export class WorkspaceService implements IWorkspaceService {
/**
* Ensure the project provider extension for the specified project is loaded
* @param projectType The file extension of the project, if not specified, all project provider extensions will be loaded.
* @param forceActivate forces project providing extensions to be activated if true
*/
private async ensureProviderExtensionLoaded(projectType: string | undefined = undefined): Promise<void> {
public async ensureProviderExtensionLoaded(projectType: string | undefined = undefined, forceActivate: boolean = false): Promise<void> {
const projType = projectType ? projectType.toUpperCase() : undefined;
let extension: vscode.Extension<any>;
for (extension of vscode.extensions.all) {
@@ -265,20 +266,41 @@ export class WorkspaceService implements IWorkspaceService {
if (projectTypes && projectTypes.length > 0) {
if (projType) {
if (projectTypes.findIndex((proj: string) => proj.toUpperCase() === projType) !== -1) {
await this.handleProjectProviderExtension(extension);
await this.handleProjectProviderExtension(extension, forceActivate);
break;
}
} else {
await this.handleProjectProviderExtension(extension);
await this.handleProjectProviderExtension(extension, forceActivate);
}
}
}
}
private async handleProjectProviderExtension(extension: vscode.Extension<any>): Promise<void> {
/**
* Ensures project provider extension is activated and registered
* @param extension Extension to activate and register
* @param forceActivate Activates the extension if true. If false, only activates the extension if the workspace contains a file with the extension's project type
*/
private async handleProjectProviderExtension(extension: vscode.Extension<any>, forceActivate: boolean = false): Promise<void> {
try {
if (!extension.isActive) {
await extension.activate();
if (forceActivate) {
await extension.activate();
} else {
const projectTypes = extension.packageJSON.contributes?.projects as string[] | undefined;
if (!projectTypes) {
return;
}
for (const projType of projectTypes) {
const projFilesInWorkspace = await vscode.workspace.findFiles(`**/*.${projType}`);
if (projFilesInWorkspace.length > 0) {
// only try to activate the extension if the workspace has at least one project with that extension
await extension.activate();
break;
}
}
}
}
} catch (err) {
Logger.error(constants.ExtensionActivationError(extension.id, err));