Skip to content

Commit

Permalink
Fix backup when upgrading vscode through package manager in some Linux (
Browse files Browse the repository at this point in the history
#493)

* rename alias to vsc
* Fix backup file inheritance problem under some linux (#487, #490)

Close #490, #487
  • Loading branch information
shalldie authored Nov 9, 2024
1 parent 6b63596 commit fb3d728
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 66 deletions.
2 changes: 1 addition & 1 deletion docs/common-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
2 changes: 1 addition & 1 deletion docs/common-issues.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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版本的默认图片?

Expand Down
1 change: 0 additions & 1 deletion l10n/bundle.l10n.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -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は変わった!再起動してください。"
}
1 change: 0 additions & 1 deletion l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
1 change: 0 additions & 1 deletion l10n/bundle.l10n.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 已经改变!请重启。"
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 0 additions & 6 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,6 @@ export class Background implements Disposable {
*/
public async setup(): Promise<any> {
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(); // 是否初次加载插件

Expand Down
8 changes: 4 additions & 4 deletions src/background/CssFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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文件修改状态类型
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
58 changes: 19 additions & 39 deletions src/background/PatchFile/PatchFile.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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();
}

/**
* 是否已经修改过
*
Expand Down Expand Up @@ -90,14 +65,6 @@ export abstract class AbsPatchFile {
return EFilePatchType.None;
}

protected async getBackup(): Promise<string> {
if (!this.hasBackup) {
console.error('backup file is missing: ' + this.backupPath);
return '';
}
return fs.promises.readFile(this.backupPath, ENCODING);
}

protected getContent(): Promise<string> {
return fs.promises.readFile(this.filePath, ENCODING);
}
Expand All @@ -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:
Expand All @@ -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;
}
Expand All @@ -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 });
Expand All @@ -157,10 +124,23 @@ export abstract class AbsPatchFile {
*/
public abstract applyPatches(patch: string): Promise<void>;

/**
* 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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/background/PatchFile/PatchFile.javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '');

/**
* 等待若干时间
Expand Down
6 changes: 3 additions & 3 deletions src/utils/vsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 3 additions & 3 deletions src/utils/vscodePath.ts
Original file line number Diff line number Diff line change
@@ -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;
})();
Expand All @@ -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':
Expand Down

0 comments on commit fb3d728

Please sign in to comment.