New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove extension when folder is removed during runtime #1518
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { ExtensionLoader } from "../extension-loader"; | ||
|
||
const manifestPath = "manifest/path"; | ||
const manifestPath2 = "manifest/path2"; | ||
const manifestPath3 = "manifest/path3"; | ||
|
||
jest.mock( | ||
"electron", | ||
() => ({ | ||
ipcRenderer: { | ||
invoke: jest.fn(async (channel: string, ...args: any[]) => { | ||
if (channel === "extensions:loaded") { | ||
return [ | ||
[ | ||
manifestPath, | ||
{ | ||
manifest: { | ||
name: "TestExtension", | ||
version: "1.0.0", | ||
}, | ||
manifestPath, | ||
isBundled: false, | ||
isEnabled: true, | ||
}, | ||
], | ||
[ | ||
manifestPath2, | ||
{ | ||
manifest: { | ||
name: "TestExtension2", | ||
version: "2.0.0", | ||
}, | ||
manifestPath: manifestPath2, | ||
isBundled: false, | ||
isEnabled: true, | ||
}, | ||
], | ||
]; | ||
} | ||
}), | ||
on: jest.fn( | ||
(channel: string, listener: (event: any, ...args: any[]) => void) => { | ||
if (channel === "extensions:loaded") { | ||
// First initialize with extensions 1 and 2 | ||
// and then broadcast event to remove extensioin 2 and add extension number 3 | ||
setTimeout(() => { | ||
listener({}, [ | ||
[ | ||
manifestPath, | ||
{ | ||
manifest: { | ||
name: "TestExtension", | ||
version: "1.0.0", | ||
}, | ||
manifestPath, | ||
isBundled: false, | ||
isEnabled: true, | ||
}, | ||
], | ||
[ | ||
manifestPath3, | ||
{ | ||
manifest: { | ||
name: "TestExtension3", | ||
version: "3.0.0", | ||
}, | ||
manifestPath3, | ||
isBundled: false, | ||
isEnabled: true, | ||
}, | ||
], | ||
]); | ||
}, 10); | ||
} | ||
} | ||
), | ||
}, | ||
}), | ||
{ | ||
virtual: true, | ||
} | ||
); | ||
|
||
describe("ExtensionLoader", () => { | ||
it("renderer updates extension after ipc broadcast", async (done) => { | ||
const extensionLoader = new ExtensionLoader(); | ||
|
||
expect(extensionLoader.userExtensions).toMatchInlineSnapshot(`Map {}`); | ||
|
||
await extensionLoader.init(); | ||
|
||
setTimeout(() => { | ||
// Assert the extensions after the extension broadcast event | ||
expect(extensionLoader.userExtensions).toMatchInlineSnapshot(` | ||
Map { | ||
"manifest/path" => Object { | ||
"isBundled": false, | ||
"isEnabled": true, | ||
"manifest": Object { | ||
"name": "TestExtension", | ||
"version": "1.0.0", | ||
}, | ||
"manifestPath": "manifest/path", | ||
}, | ||
"manifest/path3" => Object { | ||
"isBundled": false, | ||
"isEnabled": true, | ||
"manifest": Object { | ||
"name": "TestExtension3", | ||
"version": "3.0.0", | ||
}, | ||
"manifestPath3": "manifest/path3", | ||
}, | ||
} | ||
`); | ||
|
||
done(); | ||
}, 10); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { app, ipcRenderer, remote } from "electron"; | ||
import { EventEmitter } from "events"; | ||
import { action, computed, observable, reaction, toJS, when } from "mobx"; | ||
import path from "path"; | ||
import { getHostedCluster } from "../common/cluster-store"; | ||
|
@@ -26,26 +27,32 @@ export class ExtensionLoader { | |
protected instances = observable.map<LensExtensionId, LensExtension>(); | ||
protected readonly requestExtensionsChannel = "extensions:loaded"; | ||
|
||
// emits event "remove" of type LensExtension when the extension is removed | ||
private events = new EventEmitter(); | ||
|
||
@observable isLoaded = false; | ||
whenLoaded = when(() => this.isLoaded); | ||
|
||
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> { | ||
const extensions = this.extensions.toJS(); | ||
|
||
extensions.forEach((ext, extId) => { | ||
if (ext.isBundled) { | ||
extensions.delete(extId); | ||
} | ||
}); | ||
|
||
return extensions; | ||
} | ||
|
||
@action | ||
async init() { | ||
if (ipcRenderer) { | ||
this.initRenderer(); | ||
await this.initRenderer(); | ||
} else { | ||
this.initMain(); | ||
await this.initMain(); | ||
} | ||
|
||
extensionsStore.manageState(this); | ||
} | ||
|
||
|
@@ -57,11 +64,28 @@ export class ExtensionLoader { | |
this.extensions.set(extension.manifestPath as LensExtensionId, extension); | ||
} | ||
|
||
removeInstance(lensExtensionId: LensExtensionId) { | ||
logger.info(`${logModule} deleting extension instance ${lensExtensionId}`); | ||
const instance = this.instances.get(lensExtensionId); | ||
|
||
if (instance) { | ||
try { | ||
instance.disable(); | ||
this.events.emit("remove", instance); | ||
this.instances.delete(lensExtensionId); | ||
} catch (error) { | ||
logger.error(`${logModule}: deactivation extension error`, { lensExtensionId, error }); | ||
} | ||
} | ||
} | ||
|
||
removeExtension(lensExtensionId: LensExtensionId) { | ||
// TODO: Remove the extension properly (from menus etc.) | ||
this.removeInstance(lensExtensionId); | ||
|
||
if (!this.extensions.delete(lensExtensionId)) { | ||
throw new Error(`Can't remove extension ${lensExtensionId}, doesn't exist.`); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New line at the end of a function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
|
||
protected async initMain() { | ||
|
@@ -79,14 +103,25 @@ export class ExtensionLoader { | |
} | ||
|
||
protected async initRenderer() { | ||
const extensionListHandler = ( extensions: [LensExtensionId, InstalledExtension][]) => { | ||
const extensionListHandler = (extensions: [LensExtensionId, InstalledExtension][]) => { | ||
this.isLoaded = true; | ||
const receivedExtensionIds = extensions.map(([lensExtensionId]) => lensExtensionId); | ||
|
||
// Add new extensions | ||
extensions.forEach(([extId, ext]) => { | ||
if (!this.extensions.has(extId)) { | ||
this.extensions.set(extId, ext); | ||
} | ||
}); | ||
|
||
// Remove deleted extensions | ||
this.extensions.forEach((_, lensExtensionId) => { | ||
if (!receivedExtensionIds.includes(lensExtensionId)) { | ||
this.removeExtension(lensExtensionId); | ||
} | ||
}); | ||
}; | ||
|
||
requestMain(this.requestExtensionsChannel).then(extensionListHandler); | ||
subscribeToBroadcast(this.requestExtensionsChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => { | ||
extensionListHandler(extensions); | ||
|
@@ -95,36 +130,73 @@ export class ExtensionLoader { | |
|
||
loadOnMain() { | ||
logger.info(`${logModule}: load on main`); | ||
this.autoInitExtensions(async (ext: LensMainExtension) => [ | ||
registries.menuRegistry.add(ext.appMenus) | ||
]); | ||
this.autoInitExtensions(async (extension: LensMainExtension) => { | ||
// Each .add returns a function to remove the item | ||
const removeItems = [ | ||
registries.menuRegistry.add(extension.appMenus) | ||
]; | ||
|
||
this.events.on("remove", (removedExtension: LensRendererExtension) => { | ||
// manifestPath is considered the id | ||
if (removedExtension.manifestPath === extension.manifestPath) { | ||
removeItems.forEach(remove => { | ||
remove(); | ||
}); | ||
} | ||
}); | ||
|
||
return removeItems; | ||
}); | ||
} | ||
|
||
loadOnClusterManagerRenderer() { | ||
logger.info(`${logModule}: load on main renderer (cluster manager)`); | ||
this.autoInitExtensions(async (ext: LensRendererExtension) => [ | ||
registries.globalPageRegistry.add(ext.globalPages, ext), | ||
registries.globalPageMenuRegistry.add(ext.globalPageMenus, ext), | ||
registries.appPreferenceRegistry.add(ext.appPreferences), | ||
registries.clusterFeatureRegistry.add(ext.clusterFeatures), | ||
registries.statusBarRegistry.add(ext.statusBarItems), | ||
]); | ||
this.autoInitExtensions(async (extension: LensRendererExtension) => { | ||
const removeItems = [ | ||
registries.globalPageRegistry.add(extension.globalPages, extension), | ||
registries.globalPageMenuRegistry.add(extension.globalPageMenus, extension), | ||
registries.appPreferenceRegistry.add(extension.appPreferences), | ||
registries.clusterFeatureRegistry.add(extension.clusterFeatures), | ||
registries.statusBarRegistry.add(extension.statusBarItems), | ||
]; | ||
|
||
this.events.on("remove", (removedExtension: LensRendererExtension) => { | ||
if (removedExtension.manifestPath === extension.manifestPath) { | ||
removeItems.forEach(remove => { | ||
remove(); | ||
}); | ||
} | ||
}); | ||
|
||
return removeItems; | ||
}); | ||
} | ||
|
||
loadOnClusterRenderer() { | ||
logger.info(`${logModule}: load on cluster renderer (dashboard)`); | ||
const cluster = getHostedCluster(); | ||
this.autoInitExtensions(async (ext: LensRendererExtension) => { | ||
if (await ext.isEnabledForCluster(cluster) === false) { | ||
this.autoInitExtensions(async (extension: LensRendererExtension) => { | ||
if (await extension.isEnabledForCluster(cluster) === false) { | ||
return []; | ||
} | ||
return [ | ||
registries.clusterPageRegistry.add(ext.clusterPages, ext), | ||
registries.clusterPageMenuRegistry.add(ext.clusterPageMenus, ext), | ||
registries.kubeObjectMenuRegistry.add(ext.kubeObjectMenuItems), | ||
registries.kubeObjectDetailRegistry.add(ext.kubeObjectDetailItems), | ||
registries.kubeObjectStatusRegistry.add(ext.kubeObjectStatusTexts) | ||
|
||
const removeItems = [ | ||
registries.clusterPageRegistry.add(extension.clusterPages, extension), | ||
registries.clusterPageMenuRegistry.add(extension.clusterPageMenus, extension), | ||
registries.kubeObjectMenuRegistry.add(extension.kubeObjectMenuItems), | ||
registries.kubeObjectDetailRegistry.add(extension.kubeObjectDetailItems), | ||
registries.kubeObjectStatusRegistry.add(extension.kubeObjectStatusTexts) | ||
]; | ||
|
||
this.events.on("remove", (removedExtension: LensRendererExtension) => { | ||
if (removedExtension.manifestPath === extension.manifestPath) { | ||
removeItems.forEach(remove => { | ||
remove(); | ||
}); | ||
} | ||
}); | ||
|
||
return removeItems; | ||
}); | ||
} | ||
|
||
|
@@ -148,13 +220,7 @@ export class ExtensionLoader { | |
logger.error(`${logModule}: activation extension error`, { ext, err }); | ||
} | ||
} else if (!ext.isEnabled && alreadyInit) { | ||
try { | ||
const instance = this.instances.get(extId); | ||
instance.disable(); | ||
this.instances.delete(extId); | ||
} catch (err) { | ||
logger.error(`${logModule}: deactivation extension error`, { ext, err }); | ||
} | ||
this.removeInstance(extId); | ||
} | ||
} | ||
}, { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about the
if (!instance) { return; }
style instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see that being necessarily better, but fixed