From fb3d728593fa16718d068632246bfbda643796f4 Mon Sep 17 00:00:00 2001 From: xieshuang Date: Sat, 9 Nov 2024 16:57:47 +0800 Subject: [PATCH] Fix backup when upgrading vscode through package manager in some Linux (#493) * rename alias to vsc * Fix backup file inheritance problem under some linux (#487, #490) Close #490, #487 --- docs/common-issues.md | 2 +- docs/common-issues.zh-CN.md | 2 +- l10n/bundle.l10n.ja.json | 1 - l10n/bundle.l10n.json | 1 - l10n/bundle.l10n.zh-cn.json | 1 - package-lock.json | 4 +- package.json | 2 +- src/background/Background.ts | 6 -- src/background/CssFile.ts | 8 +-- src/background/PatchFile/PatchFile.base.ts | 58 ++++++------------- .../PatchFile/PatchFile.javascript.ts | 8 ++- src/utils/index.ts | 4 +- src/utils/vsc.ts | 6 +- src/utils/vscodePath.ts | 6 +- 14 files changed, 43 insertions(+), 66 deletions(-) diff --git a/docs/common-issues.md b/docs/common-issues.md index 8a8ee49..f4c1fd0 100644 --- a/docs/common-issues.md +++ b/docs/common-issues.md @@ -68,7 +68,7 @@ Whenever there is an extreme situation where vscode crashes, you can manually fi - mac: `/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench` - linux: `/usr/share/code/resources/app/out/vs/workbench` - Some Arch Linux: `/opt/visual-studio-code/resources/app/out/vs/workbench` -2. Replace `workbench.desktop.main.js` with the backup file `workbench.desktop.main.js.background-backup`. +2. Edit `workbench.desktop.main.js`, remove the content at the end: `// vscode-background-start...// vscode-background-end`. ## Prefer v1 default images? diff --git a/docs/common-issues.zh-CN.md b/docs/common-issues.zh-CN.md index f3358e0..357176b 100644 --- a/docs/common-issues.zh-CN.md +++ b/docs/common-issues.zh-CN.md @@ -68,7 +68,7 @@ - mac: `/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench` - linux: `/usr/share/code/resources/app/out/vs/workbench` - 一些 Arch Linux: `/opt/visual-studio-code/resources/app/out/vs/workbench` -2. 使用备份文件 `workbench.desktop.main.js.background-backup` 替换掉 `workbench.desktop.main.js`。 +2. 编辑 `workbench.desktop.main.js`,去掉尾部的这部分:`// vscode-background-start...// vscode-background-end`。 ## 想继续使用v1版本的默认图片? diff --git a/l10n/bundle.l10n.ja.json b/l10n/bundle.l10n.ja.json index b75c506..0051ab2 100644 --- a/l10n/bundle.l10n.ja.json +++ b/l10n/bundle.l10n.ja.json @@ -7,6 +7,5 @@ "Background has been disabled! Please restart.": "Backgroundは無効になっています!再起動してください。", "Configuration has been changed, click to update.": "構成が変更されたので、更新をクリックします。", "Update and restart": "更新と再起動", - "Backup files failed to save.": "Backup files failed to save.", "Background has been changed! Please restart.": "Backgroundは変わった!再起動してください。" } diff --git a/l10n/bundle.l10n.json b/l10n/bundle.l10n.json index 4b4cf43..505f996 100644 --- a/l10n/bundle.l10n.json +++ b/l10n/bundle.l10n.json @@ -7,6 +7,5 @@ "Background has been disabled! Please restart.": "Background has been disabled! Please restart.", "Configuration has been changed, click to update.": "Configuration has been changed, click to update.", "Update and restart": "Update and restart", - "Backup files failed to save.": "Backup files failed to save.", "Background has been changed! Please restart.": "Background has been changed! Please restart." } diff --git a/l10n/bundle.l10n.zh-cn.json b/l10n/bundle.l10n.zh-cn.json index 71c6a9e..a94e683 100644 --- a/l10n/bundle.l10n.zh-cn.json +++ b/l10n/bundle.l10n.zh-cn.json @@ -7,6 +7,5 @@ "Background has been disabled! Please restart.": "Background 已经禁用! 请重启。", "Configuration has been changed, click to update.": "配置已改变,点击更新。", "Update and restart": "更新并重启", - "Backup files failed to save.": "Backup files failed to save.", "Background has been changed! Please restart.": "Background 已经改变!请重启。" } diff --git a/package-lock.json b/package-lock.json index 9668a28..2414152 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "background", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "background", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "dependencies": { "@vscode/sudo-prompt": "^9.3.1", diff --git a/package.json b/package.json index aa6ae26..f441f1a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "background", "displayName": "background", "description": "Bring background images to your vscode", - "version": "2.0.1", + "version": "2.0.2", "scripts": { "vscode:prepublish": "npm run compile", "vscode:uninstall": "node ./out/uninstall", diff --git a/src/background/Background.ts b/src/background/Background.ts index 4d3fb5e..c3461ef 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -195,12 +195,6 @@ export class Background implements Disposable { */ public async setup(): Promise { await this.removeLegacyCssPatch(); // 移除v1旧版本patch - await this.jsFile.setup(); // backup - - if (!this.jsFile.hasBackup) { - vscode.window.showErrorMessage(l10n.t('Backup files failed to save.')); - return false; - } await this.checkFirstload(); // 是否初次加载插件 diff --git a/src/background/CssFile.ts b/src/background/CssFile.ts index 75a30b1..c156aa2 100644 --- a/src/background/CssFile.ts +++ b/src/background/CssFile.ts @@ -10,7 +10,7 @@ import path from 'path'; import { utils } from '../utils'; import { BACKGROUND_VER, ENCODING, VERSION } from '../utils/constants'; -import { vscode } from '../utils/vsc'; +import { vsc } from '../utils/vsc'; /** * css文件修改状态类型 @@ -98,7 +98,7 @@ export class CssFile { await fs.promises.writeFile(this.filePath, content, ENCODING); return true; } catch (e: any) { - if (!vscode) { + if (!vsc) { return false; } // FIXME: @@ -108,7 +108,7 @@ export class CssFile { // uname -a // Linux code-server-b6cc684df-sqx9h 5.4.0-77-generic #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 x86_64 GNU/Linux const retry = 'Retry with Admin/Sudo'; - const result = await vscode.window.showErrorMessage(e.message, retry); + const result = await vsc.window.showErrorMessage(e.message, retry); if (result !== retry) { return false; } @@ -119,7 +119,7 @@ export class CssFile { await utils.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' }); return true; } catch (e: any) { - await vscode.window.showErrorMessage(e.message); + await vsc.window.showErrorMessage(e.message); return false; } finally { await fs.promises.rm(tempFilePath); diff --git a/src/background/PatchFile/PatchFile.base.ts b/src/background/PatchFile/PatchFile.base.ts index ac8afa0..0f95017 100644 --- a/src/background/PatchFile/PatchFile.base.ts +++ b/src/background/PatchFile/PatchFile.base.ts @@ -5,7 +5,7 @@ import path from 'path'; import { utils } from '../../utils'; import { BACKGROUND_VER, ENCODING, VERSION } from '../../utils/constants'; -import { vscode } from '../../utils/vsc'; +import { vsc } from '../../utils/vsc'; export enum EFilePatchType { /** @@ -32,31 +32,6 @@ export enum EFilePatchType { export abstract class AbsPatchFile { constructor(private filePath: string) {} - private get backupPath() { - return this.filePath + '.background-backup'; - } - - public get hasBackup() { - return fs.existsSync(this.backupPath); - } - - /** - * 初始化,创建备份 - * - * @memberof AbsPatchFile - */ - public async setup() { - // 已包含备份文件,忽略 - if (this.hasBackup) { - return; - } - - await utils.lock(); - const content = await this.getContent(); - await this.saveContentTo(this.backupPath, content); - await utils.unlock(); - } - /** * 是否已经修改过 * @@ -90,14 +65,6 @@ export abstract class AbsPatchFile { return EFilePatchType.None; } - protected async getBackup(): Promise { - if (!this.hasBackup) { - console.error('backup file is missing: ' + this.backupPath); - return ''; - } - return fs.promises.readFile(this.backupPath, ENCODING); - } - protected getContent(): Promise { return fs.promises.readFile(this.filePath, ENCODING); } @@ -110,7 +77,7 @@ export abstract class AbsPatchFile { await fs.promises.writeFile(filePath, content, ENCODING); return true; } catch (e: any) { - if (!vscode) { + if (!vsc) { return false; } // FIXME: @@ -120,7 +87,7 @@ export abstract class AbsPatchFile { // uname -a // Linux code-server-b6cc684df-sqx9h 5.4.0-77-generic #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 x86_64 GNU/Linux const retry = 'Retry with Admin/Sudo'; - const result = await vscode.window.showErrorMessage(e.message, retry); + const result = await vsc.window.showErrorMessage(e.message, retry); if (result !== retry) { return false; } @@ -132,7 +99,7 @@ export abstract class AbsPatchFile { await utils.sudoExec(cmdarg, { name: 'Background Extension' }); return true; } catch (e: any) { - await vscode.window.showErrorMessage(e.message); + await vsc.window.showErrorMessage(e.message); return false; } finally { await fs.promises.rm(tempFilePath, { force: true }); @@ -157,10 +124,23 @@ export abstract class AbsPatchFile { */ public abstract applyPatches(patch: string): Promise; + /** + * Get the clean content without patches. + * 清理补丁,得到「干净」的源文件。 + * + * @protected + * @abstract + * @param {string} content + * @return {*} {string} + * @memberof AbsPatchFile + */ + protected abstract cleanPatches(content: string): string; + public async restore() { await utils.lock(); - const backup = await this.getBackup(); - const ok = await this.write(backup); + let content = await this.getContent(); + content = this.cleanPatches(content); + const ok = await this.write(content); await utils.unlock(); return ok; } diff --git a/src/background/PatchFile/PatchFile.javascript.ts b/src/background/PatchFile/PatchFile.javascript.ts index 659a346..5568f26 100644 --- a/src/background/PatchFile/PatchFile.javascript.ts +++ b/src/background/PatchFile/PatchFile.javascript.ts @@ -13,7 +13,8 @@ import { AbsPatchFile } from './PatchFile.base'; */ export class JsPatchFile extends AbsPatchFile { public async applyPatches(patchContent: string) { - let content = await this.getBackup(); + let content = await this.getContent(); + content = this.cleanPatches(content); content += [ // `\n// vscode-background-start ${BACKGROUND_VER}.${VERSION}`, @@ -23,4 +24,9 @@ export class JsPatchFile extends AbsPatchFile { await this.write(content); } + + protected cleanPatches(content: string): string { + content = content.replace(/\n\/\/ vscode-background-start[\s\S]*\/\/ vscode-background-end/, ''); + return content; + } } diff --git a/src/utils/index.ts b/src/utils/index.ts index 154390e..cd7fa72 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,13 +2,13 @@ import sudo from '@vscode/sudo-prompt'; import lockfile from 'lockfile'; import { LOCK_PATH } from './constants'; -import { vscode } from './vsc'; +import { vsc } from './vsc'; export namespace utils { /** * if zh-CN */ - export const isZHCN = /^zh/.test(vscode?.env.language || ''); + export const isZHCN = /^zh/.test(vsc?.env.language || ''); /** * 等待若干时间 diff --git a/src/utils/vsc.ts b/src/utils/vsc.ts index 0b82ba0..cd3838d 100644 --- a/src/utils/vsc.ts +++ b/src/utils/vsc.ts @@ -5,12 +5,12 @@ import VSCODE_BASE from 'vscode'; -let vscode: typeof VSCODE_BASE | undefined; +let vsc: typeof VSCODE_BASE | undefined; try { - vscode = require('vscode'); + vsc = require('vscode'); } catch { // nothing todo } -export { vscode }; +export { vsc }; diff --git a/src/utils/vscodePath.ts b/src/utils/vscodePath.ts index 81fe228..aa4a9ec 100644 --- a/src/utils/vscodePath.ts +++ b/src/utils/vscodePath.ts @@ -1,11 +1,11 @@ import path from 'path'; -import { vscode } from './vsc'; +import { vsc } from './vsc'; // 基础目录 const base = (() => { const mainFilename = require.main?.filename; - const vscodeInstallPath = vscode?.env.appRoot; + const vscodeInstallPath = vsc?.env.appRoot; const base = mainFilename?.length ? path.dirname(mainFilename) : path.join(vscodeInstallPath!, 'out'); return base; })(); @@ -18,7 +18,7 @@ const cssPath = (() => { const webPath = getCssPath('workbench.web.main.css'); // See https://code.visualstudio.com/api/references/vscode-api#env - switch (vscode?.env.appHost) { + switch (vsc?.env.appHost) { case 'desktop': return defPath; case 'web':