@@ -5,7 +5,7 @@ import path from 'path';
55
66import { utils } from '../../utils' ;
77import { BACKGROUND_VER , ENCODING , VERSION } from '../../utils/constants' ;
8- import { vscode } from '../../utils/vsc' ;
8+ import { vsc } from '../../utils/vsc' ;
99
1010export enum EFilePatchType {
1111 /**
@@ -32,31 +32,6 @@ export enum EFilePatchType {
3232export 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