From d49966a2d1cf4862759cb85ccbea639bbcb37971 Mon Sep 17 00:00:00 2001 From: xieshuang Date: Wed, 13 Nov 2024 16:49:42 +0800 Subject: [PATCH] add code-server support (#497) --- docs/common-issues.md | 2 ++ docs/common-issues.zh-CN.md | 2 ++ src/background/Background.ts | 23 +++++++++------- src/background/PatchFile/PatchFile.base.ts | 14 +++++++--- .../PatchFile/PatchFile.javascript.ts | 4 +-- src/utils/index.ts | 9 +++++++ src/utils/vscodePath.ts | 26 ++++++++++++------- 7 files changed, 56 insertions(+), 24 deletions(-) diff --git a/docs/common-issues.md b/docs/common-issues.md index f4c1fd0..7d6064c 100644 --- a/docs/common-issues.md +++ b/docs/common-issues.md @@ -50,6 +50,8 @@ three ways: - linux: - Run `sudo chmod -R a+rw /usr/share/code` - Some Arch Linux: `sudo chmod -R a+rw /opt/visual-studio-code` + - Code Server (docker): `sudo chmod -R a+rw '/usr/lib/code-server'` + - code-server needs to force browser refresh (avoid caching) for configuration to take effect. ## Unsupported environment diff --git a/docs/common-issues.zh-CN.md b/docs/common-issues.zh-CN.md index 357176b..f699a5f 100644 --- a/docs/common-issues.zh-CN.md +++ b/docs/common-issues.zh-CN.md @@ -50,6 +50,8 @@ - linux: - 执行 `sudo chmod -R a+rw /usr/share/code`。 - 一些 Arch Linux: `sudo chmod -R a+rw /opt/visual-studio-code` + - Code Server (docker): `sudo chmod -R a+rw '/usr/lib/code-server'` + - code-server 需要强制刷新浏览器(避免缓存)来使配置生效。 ## 不支持的环境 diff --git a/src/background/Background.ts b/src/background/Background.ts index c3461ef..cfca171 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -130,11 +130,13 @@ export class Background implements Disposable { * @memberof Background */ private async removeLegacyCssPatch() { - const hasInstalled = await this.cssFile.hasInstalled(); - if (!hasInstalled) { - return; - } - await this.cssFile.uninstall(); + try { + const hasInstalled = await this.cssFile.hasInstalled(); + if (!hasInstalled) { + return; + } + await this.cssFile.uninstall(); + } catch (ex) {} } /** @@ -180,7 +182,7 @@ export class Background implements Disposable { } const scriptContent = PatchGenerator.create(this.config); - await this.jsFile.applyPatches(scriptContent); + return this.jsFile.applyPatches(scriptContent); } // #endregion @@ -204,8 +206,9 @@ export class Background implements Disposable { if (this.config.enabled) { // 此时一般为 vscode更新、background更新 if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) { - await this.applyPatch(); - vsHelp.showInfoRestart(l10n.t('Background has been changed! Please restart.')); + if (await this.applyPatch()) { + vsHelp.showInfoRestart(l10n.t('Background has been changed! Please restart.')); + } } } @@ -217,9 +220,9 @@ export class Background implements Disposable { return; } - // 0~500ms 的延时,对于可能的多实例,错开对于文件的操作 + // 50~550ms 的延时,对于可能的多实例,错开对于文件的操作 // 虽然有锁了,但这样更安心 =。= - await utils.sleep(200 + ~~(Math.random() * 800)); + await utils.sleep(50 + ~~(Math.random() * 500)); this.onConfigChange(); }) diff --git a/src/background/PatchFile/PatchFile.base.ts b/src/background/PatchFile/PatchFile.base.ts index 0f95017..246d642 100644 --- a/src/background/PatchFile/PatchFile.base.ts +++ b/src/background/PatchFile/PatchFile.base.ts @@ -99,7 +99,15 @@ export abstract class AbsPatchFile { await utils.sudoExec(cmdarg, { name: 'Background Extension' }); return true; } catch (e: any) { - await vsc.window.showErrorMessage(e.message); + vsc.window.showErrorMessage(e.message, { title: 'Common Issue' }).then(confirm => { + if (!confirm) { + return; + } + const helpLink = + 'https://github.com/shalldie/vscode-background/blob/master/docs/common-issues.md#read-only-file-system'; + + vsc!.env!.openExternal(vsc!.Uri.parse(helpLink)); + }); return false; } finally { await fs.promises.rm(tempFilePath, { force: true }); @@ -119,10 +127,10 @@ export abstract class AbsPatchFile { * * @abstract * @param {string} patch - * @return {*} {Promise} + * @return {*} {Promise} * @memberof AbsPatchFile */ - public abstract applyPatches(patch: string): Promise; + public abstract applyPatches(patch: string): Promise; /** * Get the clean content without patches. diff --git a/src/background/PatchFile/PatchFile.javascript.ts b/src/background/PatchFile/PatchFile.javascript.ts index 5568f26..d4f07f0 100644 --- a/src/background/PatchFile/PatchFile.javascript.ts +++ b/src/background/PatchFile/PatchFile.javascript.ts @@ -12,7 +12,7 @@ import { AbsPatchFile } from './PatchFile.base'; * @extends {AbsPatchFile} */ export class JsPatchFile extends AbsPatchFile { - public async applyPatches(patchContent: string) { + public async applyPatches(patchContent: string): Promise { let content = await this.getContent(); content = this.cleanPatches(content); content += [ @@ -22,7 +22,7 @@ export class JsPatchFile extends AbsPatchFile { '// vscode-background-end' ].join('\n'); - await this.write(content); + return this.write(content); } protected cleanPatches(content: string): string { diff --git a/src/utils/index.ts b/src/utils/index.ts index cd7fa72..5ce3fad 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,6 +10,15 @@ export namespace utils { */ export const isZHCN = /^zh/.test(vsc?.env.language || ''); + /** + * if desktop + * + * desktop: `desktop` + * code-server: `server-distro` + * See: https://code.visualstudio.com/api/references/vscode-api#env + */ + export const isDesktop = vsc?.env.appHost === 'desktop'; + /** * 等待若干时间 * diff --git a/src/utils/vscodePath.ts b/src/utils/vscodePath.ts index aa4a9ec..eb8e8c6 100644 --- a/src/utils/vscodePath.ts +++ b/src/utils/vscodePath.ts @@ -1,5 +1,6 @@ import path from 'path'; +import { utils } from './index'; import { vsc } from './vsc'; // 基础目录 @@ -17,18 +18,25 @@ const cssPath = (() => { // https://github.com/microsoft/vscode/pull/141263 const webPath = getCssPath('workbench.web.main.css'); - // See https://code.visualstudio.com/api/references/vscode-api#env - switch (vsc?.env.appHost) { - case 'desktop': - return defPath; - case 'web': - default: - return webPath; + if (utils.isDesktop) { + return defPath; } + return webPath; })(); -// /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js -const jsPath = path.join(base, 'vs/workbench/workbench.desktop.main.js'); +const jsPath = (() => { + // See https://code.visualstudio.com/api/references/vscode-api#env + + // desktop + // /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js + if (utils.isDesktop) { + return path.join(base, 'vs/workbench/workbench.desktop.main.js'); + } + + // code-server + // /usr/lib/code-server/lib/vscode/out/vs/code/browser/workbench/workbench.js + return path.join(base, 'vs/code/browser/workbench/workbench.js'); +})(); export const vscodePath = { /**