Skip to content

Commit f3c0efe

Browse files
authored
Merge pull request #405 from redhat-developer/notify-if-ansible
Implement a way to determine if an extension is conflicting with VSCo…
2 parents 8663b4d + 90718fa commit f3c0efe

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/extension.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST, SchemaExtensionAPI } fro
2222
import { joinPath } from './paths';
2323
import { getJsonSchemaContent, JSONSchemaDocumentContentProvider } from './json-schema-content-provider';
2424
import { JSONSchemaCache } from './json-schema-cache';
25+
import { getConflictingExtensions, showUninstallConflictsNotification } from './extensionConflicts';
2526

2627
export interface ISchemaAssociations {
2728
[pattern: string]: string[];
@@ -104,13 +105,15 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
104105
workspace.registerTextDocumentContentProvider('json-schema', new JSONSchemaDocumentContentProvider(schemaCache))
105106
);
106107

108+
findConflicts();
107109
client.onReady().then(() => {
108110
// Send a notification to the server with any YAML schema associations in all extensions
109111
client.sendNotification(SchemaAssociationNotification.type, getSchemaAssociations());
110112

111113
// If the extensions change, fire this notification again to pick up on any association changes
112114
extensions.onDidChange(() => {
113115
client.sendNotification(SchemaAssociationNotification.type, getSchemaAssociations());
116+
findConflicts();
114117
});
115118
// Tell the server that the client is ready to provide custom schema content
116119
client.sendNotification(DynamicCustomSchemaRequestRegistration.type);
@@ -131,6 +134,18 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
131134
return schemaExtensionAPI;
132135
}
133136

137+
/**
138+
* Finds extensions that conflict with VSCode-YAML.
139+
* If one or more conflicts are found then show an uninstall notification
140+
* If no conflicts are found then do nothing
141+
*/
142+
function findConflicts(): void {
143+
const conflictingExtensions = getConflictingExtensions();
144+
if (conflictingExtensions.length > 0) {
145+
showUninstallConflictsNotification(conflictingExtensions);
146+
}
147+
}
148+
134149
function getSchemaAssociations(): ISchemaAssociation[] {
135150
const associations: ISchemaAssociation[] = [];
136151
extensions.all.forEach((extension) => {

src/extensionConflicts.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved..
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
import { commands, Extension, extensions, window } from 'vscode';
6+
7+
// A set of VSCode extension ID's that conflict with VSCode-YAML
8+
const azureDeploy = 'ms-vscode-deploy-azure.azure-deploy';
9+
const conflictingIDs = new Set(['vscoss.vscode-ansible', azureDeploy, 'sysninja.vscode-ansible-mod', 'haaaad.ansible']);
10+
11+
// A set of VSCode extension ID's that are currently uninstalling
12+
const uninstallingIDs = new Set();
13+
14+
/**
15+
* Get all of the installed extensions that currently conflict with VSCode-YAML
16+
*/
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18+
export function getConflictingExtensions(): Extension<any>[] {
19+
const conflictingExtensions = [];
20+
conflictingIDs.forEach((extension) => {
21+
const ext = extensions.getExtension(extension);
22+
if (ext && !uninstallingIDs.has(ext.id)) {
23+
conflictingExtensions.push(ext);
24+
}
25+
});
26+
return conflictingExtensions;
27+
}
28+
29+
/**
30+
* Display the uninstall conflicting extension notification if there are any conflicting extensions currently installed
31+
*/
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
export function showUninstallConflictsNotification(conflictingExts: Extension<any>[]): void {
34+
// Add all available conflicting extensions to the uninstalling IDs map
35+
for (const extIndex in conflictingExts) {
36+
const ext = conflictingExts[extIndex];
37+
uninstallingIDs.add(ext.id);
38+
}
39+
40+
const uninstallMsg = 'Uninstall';
41+
42+
// Gather all the conflicting display names
43+
let conflictMsg = '';
44+
if (conflictingExts.length === 1) {
45+
conflictMsg = `${conflictingExts[0].packageJSON.displayName} extension is incompatible with VSCode-YAML. Please uninstall it.`;
46+
} else {
47+
const extNames = [];
48+
conflictingExts.forEach((ext) => {
49+
extNames.push(ext.packageJSON.displayName);
50+
});
51+
conflictMsg = `The ${extNames.join(', ')} extensions are incompatible with VSCode-YAML. Please uninstall them.`;
52+
}
53+
54+
if (conflictingExts.length > 0) {
55+
window.showInformationMessage(conflictMsg, uninstallMsg).then((clickedMsg) => {
56+
if (clickedMsg === uninstallMsg) {
57+
conflictingExts.forEach((ext) => {
58+
commands.executeCommand('workbench.extensions.uninstallExtension', ext.id);
59+
uninstallingIDs.delete(ext.id);
60+
});
61+
62+
// The azure deploy extension must be reloaded in order to be completely uninstalled
63+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64+
if (conflictingExts.findIndex((ext: any) => ext.id === azureDeploy) !== -1) {
65+
commands.executeCommand('workbench.action.reloadWindow');
66+
}
67+
}
68+
});
69+
}
70+
}

0 commit comments

Comments
 (0)