Skip to content
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

add code-server support (#497) #498

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/common-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/common-issues.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 需要强制刷新浏览器(避免缓存)来使配置生效。

## 不支持的环境

Expand Down
23 changes: 13 additions & 10 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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.'));
}
}
}

Expand All @@ -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();
})
Expand Down
14 changes: 11 additions & 3 deletions src/background/PatchFile/PatchFile.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -119,10 +127,10 @@ export abstract class AbsPatchFile {
*
* @abstract
* @param {string} patch
* @return {*} {Promise<void>}
* @return {*} {Promise<boolean>}
* @memberof AbsPatchFile
*/
public abstract applyPatches(patch: string): Promise<void>;
public abstract applyPatches(patch: string): Promise<boolean>;

/**
* Get the clean content without patches.
Expand Down
4 changes: 2 additions & 2 deletions src/background/PatchFile/PatchFile.javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
let content = await this.getContent();
content = this.cleanPatches(content);
content += [
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
* 等待若干时间
*
Expand Down
26 changes: 17 additions & 9 deletions src/utils/vscodePath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';

import { utils } from './index';
import { vsc } from './vsc';

// 基础目录
Expand All @@ -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 = {
/**
Expand Down