1
- import * as fs from 'fs' ;
2
- import * as path from 'path' ;
3
- import { promisify } from 'util' ;
4
1
import * as vscode from 'vscode' ;
5
2
6
- const stat = promisify ( fs . stat ) ;
7
- const mkdir = promisify ( fs . mkdir ) ;
8
- const readFile = promisify ( fs . readFile ) ;
9
- const writeFile = promisify ( fs . writeFile ) ;
10
-
11
- /** Returns a path where persistent data for rust-analyzer should be installed. */
12
- function metadataDir ( ) : string | undefined {
13
- if ( process . platform === 'linux' || process . platform === 'darwin' ) {
14
- // Prefer, in this order:
15
- // 1. $XDG_CONFIG_HOME/rust-analyzer
16
- // 2. $HOME/.config/rust-analyzer
17
- const { HOME , XDG_CONFIG_HOME } = process . env ;
18
- const baseDir = XDG_CONFIG_HOME || ( HOME && path . join ( HOME , '.config' ) ) ;
19
-
20
- return baseDir && path . resolve ( path . join ( baseDir , 'rust-analyzer' ) ) ;
21
- } else if ( process . platform === 'win32' ) {
22
- // %LocalAppData%\rust-analyzer\
23
- const { LocalAppData } = process . env ;
24
- return (
25
- LocalAppData && path . resolve ( path . join ( LocalAppData , 'rust-analyzer' ) )
26
- ) ;
27
- }
28
-
29
- return undefined ;
30
- }
31
-
32
- export interface Metadata {
33
- releaseTag : string ;
34
- }
35
-
36
- export async function readMetadata ( ) : Promise < Partial < Metadata > > {
37
- const stateDir = metadataDir ( ) ;
38
- if ( ! stateDir ) {
39
- throw new Error ( 'Not supported' ) ;
40
- }
41
-
42
- const filePath = path . join ( stateDir , 'metadata.json' ) ;
43
- if ( ! ( await stat ( filePath ) . catch ( ( ) => false ) ) ) {
44
- throw new Error ( 'File missing' ) ;
45
- }
46
-
47
- const contents = await readFile ( filePath , 'utf8' ) ;
48
- const obj = JSON . parse ( contents ) as unknown ;
49
- return typeof obj === 'object' ? obj || { } : { } ;
50
- }
51
-
52
- export async function writeMetadata ( config : Metadata ) {
53
- const stateDir = metadataDir ( ) ;
54
- if ( ! stateDir ) {
55
- return false ;
56
- }
57
-
58
- if ( ! ( await ensureDir ( stateDir ) ) ) {
59
- return false ;
60
- }
61
-
62
- const filePath = path . join ( stateDir , 'metadata.json' ) ;
63
- return writeFile ( filePath , JSON . stringify ( config ) ) . then ( ( ) => true ) ;
64
- }
65
-
66
- function ensureDir ( path : string ) {
67
- return ! ! path && stat ( path ) . catch ( ( ) => mkdir ( path , { recursive : true } ) ) ;
68
- }
69
-
70
3
export class PersistentState {
71
4
constructor ( private readonly globalState : vscode . Memento ) {
72
- const { lastCheck, releaseId, serverVersion } = this ;
5
+ const { lastCheck, releaseId, releaseTag : serverVersion } = this ;
73
6
console . info ( 'PersistentState:' , { lastCheck, releaseId, serverVersion } ) ;
74
7
}
75
8
@@ -95,13 +28,13 @@ export class PersistentState {
95
28
}
96
29
97
30
/**
98
- * Version of the extension that installed the server.
31
+ * Release tag of the installed server.
99
32
* Used to check if we need to update the server.
100
33
*/
101
- get serverVersion ( ) : string | undefined {
102
- return this . globalState . get ( 'serverVersion ' ) ;
34
+ get releaseTag ( ) : string | undefined {
35
+ return this . globalState . get ( 'releaseTag ' ) ;
103
36
}
104
- async updateServerVersion ( value : string | undefined ) {
105
- await this . globalState . update ( 'serverVersion ' , value ) ;
37
+ async updateReleaseTag ( value : string | undefined ) {
38
+ await this . globalState . update ( 'releaseTag ' , value ) ;
106
39
}
107
40
}
0 commit comments