Skip to content

Commit fb3d728

Browse files
authored
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
1 parent 6b63596 commit fb3d728

File tree

14 files changed

+43
-66
lines changed

14 files changed

+43
-66
lines changed

docs/common-issues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Whenever there is an extreme situation where vscode crashes, you can manually fi
6868
- mac: `/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench`
6969
- linux: `/usr/share/code/resources/app/out/vs/workbench`
7070
- Some Arch Linux: `/opt/visual-studio-code/resources/app/out/vs/workbench`
71-
2. Replace `workbench.desktop.main.js` with the backup file `workbench.desktop.main.js.background-backup`.
71+
2. Edit `workbench.desktop.main.js`, remove the content at the end: `// vscode-background-start...// vscode-background-end`.
7272

7373
## Prefer v1 default images?
7474

docs/common-issues.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
- mac: `/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench`
6969
- linux: `/usr/share/code/resources/app/out/vs/workbench`
7070
- 一些 Arch Linux: `/opt/visual-studio-code/resources/app/out/vs/workbench`
71-
2. 使用备份文件 `workbench.desktop.main.js.background-backup` 替换掉 `workbench.desktop.main.js`
71+
2. 编辑 `workbench.desktop.main.js`,去掉尾部的这部分:`// vscode-background-start...// vscode-background-end`
7272

7373
## 想继续使用v1版本的默认图片?
7474

l10n/bundle.l10n.ja.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
"Background has been disabled! Please restart.": "Backgroundは無効になっています!再起動してください。",
88
"Configuration has been changed, click to update.": "構成が変更されたので、更新をクリックします。",
99
"Update and restart": "更新と再起動",
10-
"Backup files failed to save.": "Backup files failed to save.",
1110
"Background has been changed! Please restart.": "Backgroundは変わった!再起動してください。"
1211
}

l10n/bundle.l10n.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
"Background has been disabled! Please restart.": "Background has been disabled! Please restart.",
88
"Configuration has been changed, click to update.": "Configuration has been changed, click to update.",
99
"Update and restart": "Update and restart",
10-
"Backup files failed to save.": "Backup files failed to save.",
1110
"Background has been changed! Please restart.": "Background has been changed! Please restart."
1211
}

l10n/bundle.l10n.zh-cn.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
"Background has been disabled! Please restart.": "Background 已经禁用! 请重启。",
88
"Configuration has been changed, click to update.": "配置已改变,点击更新。",
99
"Update and restart": "更新并重启",
10-
"Backup files failed to save.": "Backup files failed to save.",
1110
"Background has been changed! Please restart.": "Background 已经改变!请重启。"
1211
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "background",
33
"displayName": "background",
44
"description": "Bring background images to your vscode",
5-
"version": "2.0.1",
5+
"version": "2.0.2",
66
"scripts": {
77
"vscode:prepublish": "npm run compile",
88
"vscode:uninstall": "node ./out/uninstall",

src/background/Background.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,6 @@ export class Background implements Disposable {
195195
*/
196196
public async setup(): Promise<any> {
197197
await this.removeLegacyCssPatch(); // 移除v1旧版本patch
198-
await this.jsFile.setup(); // backup
199-
200-
if (!this.jsFile.hasBackup) {
201-
vscode.window.showErrorMessage(l10n.t('Backup files failed to save.'));
202-
return false;
203-
}
204198

205199
await this.checkFirstload(); // 是否初次加载插件
206200

src/background/CssFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import path from 'path';
1010

1111
import { utils } from '../utils';
1212
import { BACKGROUND_VER, ENCODING, VERSION } from '../utils/constants';
13-
import { vscode } from '../utils/vsc';
13+
import { vsc } from '../utils/vsc';
1414

1515
/**
1616
* css文件修改状态类型
@@ -98,7 +98,7 @@ export class CssFile {
9898
await fs.promises.writeFile(this.filePath, content, ENCODING);
9999
return true;
100100
} catch (e: any) {
101-
if (!vscode) {
101+
if (!vsc) {
102102
return false;
103103
}
104104
// FIXME:
@@ -108,7 +108,7 @@ export class CssFile {
108108
// uname -a
109109
// 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
110110
const retry = 'Retry with Admin/Sudo';
111-
const result = await vscode.window.showErrorMessage(e.message, retry);
111+
const result = await vsc.window.showErrorMessage(e.message, retry);
112112
if (result !== retry) {
113113
return false;
114114
}
@@ -119,7 +119,7 @@ export class CssFile {
119119
await utils.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' });
120120
return true;
121121
} catch (e: any) {
122-
await vscode.window.showErrorMessage(e.message);
122+
await vsc.window.showErrorMessage(e.message);
123123
return false;
124124
} finally {
125125
await fs.promises.rm(tempFilePath);

src/background/PatchFile/PatchFile.base.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'path';
55

66
import { utils } from '../../utils';
77
import { BACKGROUND_VER, ENCODING, VERSION } from '../../utils/constants';
8-
import { vscode } from '../../utils/vsc';
8+
import { vsc } from '../../utils/vsc';
99

1010
export enum EFilePatchType {
1111
/**
@@ -32,31 +32,6 @@ export enum EFilePatchType {
3232
export abstract class AbsPatchFile {
3333
constructor(private filePath: string) {}
3434

35-
private get backupPath() {
36-
return this.filePath + '.background-backup';
37-
}
38-
39-
public get hasBackup() {
40-
return fs.existsSync(this.backupPath);
41-
}
42-
43-
/**
44-
* 初始化,创建备份
45-
*
46-
* @memberof AbsPatchFile
47-
*/
48-
public async setup() {
49-
// 已包含备份文件,忽略
50-
if (this.hasBackup) {
51-
return;
52-
}
53-
54-
await utils.lock();
55-
const content = await this.getContent();
56-
await this.saveContentTo(this.backupPath, content);
57-
await utils.unlock();
58-
}
59-
6035
/**
6136
* 是否已经修改过
6237
*
@@ -90,14 +65,6 @@ export abstract class AbsPatchFile {
9065
return EFilePatchType.None;
9166
}
9267

93-
protected async getBackup(): Promise<string> {
94-
if (!this.hasBackup) {
95-
console.error('backup file is missing: ' + this.backupPath);
96-
return '';
97-
}
98-
return fs.promises.readFile(this.backupPath, ENCODING);
99-
}
100-
10168
protected getContent(): Promise<string> {
10269
return fs.promises.readFile(this.filePath, ENCODING);
10370
}
@@ -110,7 +77,7 @@ export abstract class AbsPatchFile {
11077
await fs.promises.writeFile(filePath, content, ENCODING);
11178
return true;
11279
} catch (e: any) {
113-
if (!vscode) {
80+
if (!vsc) {
11481
return false;
11582
}
11683
// FIXME:
@@ -120,7 +87,7 @@ export abstract class AbsPatchFile {
12087
// uname -a
12188
// 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
12289
const retry = 'Retry with Admin/Sudo';
123-
const result = await vscode.window.showErrorMessage(e.message, retry);
90+
const result = await vsc.window.showErrorMessage(e.message, retry);
12491
if (result !== retry) {
12592
return false;
12693
}
@@ -132,7 +99,7 @@ export abstract class AbsPatchFile {
13299
await utils.sudoExec(cmdarg, { name: 'Background Extension' });
133100
return true;
134101
} catch (e: any) {
135-
await vscode.window.showErrorMessage(e.message);
102+
await vsc.window.showErrorMessage(e.message);
136103
return false;
137104
} finally {
138105
await fs.promises.rm(tempFilePath, { force: true });
@@ -157,10 +124,23 @@ export abstract class AbsPatchFile {
157124
*/
158125
public abstract applyPatches(patch: string): Promise<void>;
159126

127+
/**
128+
* Get the clean content without patches.
129+
* 清理补丁,得到「干净」的源文件。
130+
*
131+
* @protected
132+
* @abstract
133+
* @param {string} content
134+
* @return {*} {string}
135+
* @memberof AbsPatchFile
136+
*/
137+
protected abstract cleanPatches(content: string): string;
138+
160139
public async restore() {
161140
await utils.lock();
162-
const backup = await this.getBackup();
163-
const ok = await this.write(backup);
141+
let content = await this.getContent();
142+
content = this.cleanPatches(content);
143+
const ok = await this.write(content);
164144
await utils.unlock();
165145
return ok;
166146
}

0 commit comments

Comments
 (0)