Skip to content

Commit

Permalink
Fix backup file inheritance problem under some linux (#487, 490)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalldie committed Nov 9, 2024
1 parent 3e531d0 commit 5178f7f
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 50 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
50 changes: 15 additions & 35 deletions src/background/PatchFile/PatchFile.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -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;
}
}

0 comments on commit 5178f7f

Please sign in to comment.