@@ -23,22 +23,21 @@ function getHaskellConfig() {
23
23
return vscode . workspace . getConfiguration ( 'haskell' ) ;
24
24
}
25
25
26
- function getWorkspaceRoot ( ) {
26
+ function getWorkspaceRoot ( ) : vscode . WorkspaceFolder {
27
27
return vscode . workspace . workspaceFolders ! [ 0 ] ;
28
28
}
29
29
30
- function getWorkspaceFile ( name : string ) {
30
+ function getWorkspaceFile ( name : string ) : vscode . Uri {
31
31
const wsroot = getWorkspaceRoot ( ) . uri ;
32
32
return wsroot . with ( { path : path . posix . join ( wsroot . path , name ) } ) ;
33
33
}
34
34
35
- // function getWorkspaceGhcupCacheDirectory() {
36
- // const wsroot = getWorkspaceRoot().uri;
37
- // return wsroot.with({ path: path.posix.join(wsroot.path, 'bin/.ghcup/cache') });
38
- // }
35
+ function joinUri ( root : vscode . Uri , ...pathSegments : string [ ] ) : vscode . Uri {
36
+ return root . with ( { path : path . posix . join ( root . path , ...pathSegments ) } ) ;
37
+ }
39
38
40
- async function deleteWorkspaceFiles ( pred ?: ( fileType : [ string , vscode . FileType ] ) => boolean ) {
41
- await deleteFiles ( getWorkspaceRoot ( ) . uri , pred ) ;
39
+ async function deleteWorkspaceFiles ( keepDirs : vscode . Uri [ ] , pred ?: ( fileName : string ) => boolean ) : Promise < void > {
40
+ await deleteFiles ( getWorkspaceRoot ( ) . uri , keepDirs , pred ) ;
42
41
}
43
42
44
43
function getExtensionLogContent ( ) : string | undefined {
@@ -52,17 +51,37 @@ function getExtensionLogContent(): string | undefined {
52
51
}
53
52
}
54
53
55
- async function deleteFiles ( dir : vscode . Uri , pred ?: ( fileType : [ string , vscode . FileType ] ) => boolean ) {
54
+ async function deleteFiles ( dir : vscode . Uri , keepDirs : vscode . Uri [ ] , pred ?: ( fileType : string ) => boolean ) {
55
+ if ( keepDirs . includes ( dir ) ) {
56
+ console . log ( `Keeping ${ dir } ` ) ;
57
+ return ;
58
+ } ;
56
59
const dirContents = await vscode . workspace . fs . readDirectory ( dir ) ;
57
60
console . log ( `Deleting ${ dir } contents: ${ dirContents } ` ) ;
58
61
dirContents . forEach ( async ( [ name , type ] ) => {
59
- const uri : vscode . Uri = getWorkspaceFile ( name ) ;
60
- if ( ! pred || pred ( [ name , type ] ) ) {
61
- console . log ( `Deleting ${ uri } ` ) ;
62
- await vscode . workspace . fs . delete ( getWorkspaceFile ( name ) , {
63
- recursive : true ,
64
- useTrash : false ,
65
- } ) ;
62
+ const uri : vscode . Uri = joinUri ( dir , name ) ;
63
+ if ( type === vscode . FileType . File ) {
64
+ if ( ! pred || pred ( name ) ) {
65
+ console . log ( `Deleting ${ uri } ` ) ;
66
+ await vscode . workspace . fs . delete ( joinUri ( dir , name ) , {
67
+ recursive : false ,
68
+ useTrash : false ,
69
+ } ) ;
70
+ }
71
+ } else if ( type === vscode . FileType . Directory ) {
72
+ const subDirectory = joinUri ( dir , name ) ;
73
+ console . log ( `Recursing into ${ subDirectory } ` ) ;
74
+ await deleteFiles ( subDirectory , keepDirs , pred ) ;
75
+
76
+ // remove directory if it is empty now
77
+ const isEmptyNow = await vscode . workspace . fs . readDirectory ( subDirectory )
78
+ . then ( ( contents ) => Promise . resolve ( contents . length === 0 ) ) ;
79
+ if ( isEmptyNow ) {
80
+ await vscode . workspace . fs . delete ( subDirectory , {
81
+ recursive : false ,
82
+ useTrash : false ,
83
+ } ) ;
84
+ }
66
85
}
67
86
} ) ;
68
87
}
@@ -89,21 +108,11 @@ suite('Extension Test Suite', () => {
89
108
vscode . window . showInformationMessage ( 'Start all tests.' ) ;
90
109
91
110
suiteSetup ( async ( ) => {
92
- await deleteWorkspaceFiles ( ( [ fp , type ] ) => {
93
- if ( type === vscode . FileType . Directory && fp === '.vscode' ) {
94
- return false ;
95
- }
96
- if ( type === vscode . FileType . Directory && fp === 'bin' ) {
97
- return false ;
98
- }
99
- if ( type === vscode . FileType . Directory && fp === ( process . platform === 'win32' ? 'ghcup' : '.ghcup' ) ) {
100
- return false ;
101
- }
102
- if ( type === vscode . FileType . Directory && fp === 'cache' ) {
103
- return false ;
104
- }
105
- return true ;
106
- } ) ;
111
+ await deleteWorkspaceFiles (
112
+ [ joinUri ( getWorkspaceRoot ( ) . uri , '.vscode' )
113
+ , joinUri ( getWorkspaceRoot ( ) . uri , 'bin' , process . platform === 'win32' ? 'ghcup' : '.ghcup' , 'cache' )
114
+ ]
115
+ ) ;
107
116
await getHaskellConfig ( ) . update ( 'logFile' , 'hls.log' ) ;
108
117
await getHaskellConfig ( ) . update ( 'trace.server' , 'messages' ) ;
109
118
await getHaskellConfig ( ) . update ( 'releasesDownloadStoragePath' , path . normalize ( getWorkspaceFile ( 'bin' ) . fsPath ) ) ;
@@ -180,6 +189,6 @@ suite('Extension Test Suite', () => {
180
189
console . log ( logContent ) ;
181
190
}
182
191
console . log ( 'Deleting test workspace contents' ) ;
183
- await deleteWorkspaceFiles ( ( [ name , type ] ) => ! name . includes ( '.log' ) ) ;
192
+ await deleteWorkspaceFiles ( [ ] , ( name ) => ! name . includes ( '.log' ) ) ;
184
193
} ) ;
185
194
} ) ;
0 commit comments