|
1 | 1 | import * as vscode from 'vscode'
|
2 |
| -import { getExtensionSetting } from 'vscode-framework' |
| 2 | +import { extensionCtx, getExtensionSetting, registerExtensionCommand } from 'vscode-framework' |
3 | 3 | import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
|
4 |
| -import { doPatch } from './patch' |
| 4 | +import { doPatch, removeAllPatches } from './patch' |
5 | 5 |
|
6 | 6 | export const activate = async () => {
|
7 |
| - const upConfig = () => { |
8 |
| - process.env.VSC_CONTROL_EXT_CONFIG = JSON.stringify({ disableProviders: getExtensionSetting('disableProviders') }) |
| 7 | + const getNewConfig = () => { |
| 8 | + return { |
| 9 | + disableProviders: getExtensionSetting('disableProviders'), |
| 10 | + ignoreMessages: getExtensionSetting('ignoreMessages'), |
| 11 | + version: extensionCtx.extension.packageJSON.version, |
| 12 | + } |
| 13 | + } |
| 14 | + const updateConfig = async (restartExtHost = false) => { |
| 15 | + const config = getNewConfig() |
| 16 | + process.env.VSC_CONTROL_EXT_CONFIG = JSON.stringify(config) |
| 17 | + await patchNow(config, restartExtHost) |
9 | 18 | }
|
10 | 19 |
|
11 |
| - watchExtensionSettings(['disableProviders'], upConfig) |
| 20 | + watchExtensionSettings(['disableProviders', 'ignoreMessages'], async () => { |
| 21 | + await updateConfig() |
| 22 | + }) |
| 23 | + |
| 24 | + // #region commands |
| 25 | + registerExtensionCommand('forcePatch', () => updateConfig()) |
| 26 | + registerExtensionCommand('removeAllPatches', () => { |
| 27 | + removeAllPatches() |
| 28 | + }) |
| 29 | + registerExtensionCommand('logExtensionsActivationOrder', () => { |
| 30 | + console.log(JSON.parse(process.env.VSC_EXT_ACT_ORDER ?? 'null')) |
| 31 | + }) |
| 32 | + // #endregion |
| 33 | + |
| 34 | + // Main activation actions |
12 | 35 |
|
13 |
| - if (process.env.VSC_CONTROL_EXT_CONFIG) { |
14 |
| - upConfig() |
| 36 | + // todo continue impl |
| 37 | + // for (const [id, expected] of Object.entries(getExtensionSetting('overrideActivationEvents'))) { |
| 38 | + // } |
| 39 | + |
| 40 | + const extVersion = extensionCtx.extension.packageJSON.version |
| 41 | + const currentLoadedConfig = process.env.VSC_CONTROL_EXT_CONFIG && JSON.parse(process.env.VSC_CONTROL_EXT_CONFIG) |
| 42 | + const patchedVersion = currentLoadedConfig?.version |
| 43 | + if (patchedVersion && patchedVersion === extVersion) { |
| 44 | + if (process.env.VSC_CONTROL_EXT_CONFIG !== JSON.stringify(getNewConfig())) await updateConfig() |
15 | 45 | } else {
|
16 | 46 | if (
|
17 | 47 | !getExtensionSetting('autoApplyPatch') &&
|
18 | 48 | !(await vscode.window.showWarningMessage('Extensions Control needs to apply VS Code patch', 'Patch now'))
|
19 | 49 | ) {
|
20 | 50 | return
|
21 | 51 | }
|
| 52 | + if (patchedVersion && patchedVersion !== extVersion) { |
| 53 | + // force save unpatched version after update |
| 54 | + removeAllPatches() |
| 55 | + } |
22 | 56 | vscode.window.showInformationMessage('Patching & restarting extension host...')
|
23 | 57 | setTimeout(async () => {
|
24 |
| - try { |
25 |
| - await doPatch() |
26 |
| - await vscode.commands.executeCommand('fixChecksums.apply') |
27 |
| - await vscode.commands.executeCommand('workbench.action.restartExtensionHost') |
28 |
| - } catch (err) { |
29 |
| - vscode.window.showErrorMessage(`Failed to apply patch: ${err.message ?? err}`) |
30 |
| - throw err |
31 |
| - } |
| 58 | + await updateConfig(true) |
32 | 59 | }, 0)
|
33 | 60 | }
|
34 | 61 | }
|
| 62 | + |
| 63 | +async function patchNow(config, restart: boolean) { |
| 64 | + await doPatch(config) |
| 65 | + await vscode.commands.executeCommand('fixChecksums.apply') |
| 66 | + if (!restart) return |
| 67 | + await vscode.commands.executeCommand('workbench.action.restartExtensionHost') |
| 68 | +} |
0 commit comments