@@ -10,11 +10,10 @@ import { WorkspaceProgress } from '../extension';
1010import { download , fetchRelease } from '../net' ;
1111import * as rustup from '../rustup' ;
1212import { Observable } from '../utils/observable' ;
13+ import { Metadata , readMetadata , writeMetadata } from './persistent_state' ;
1314
1415const stat = promisify ( fs . stat ) ;
1516const mkdir = promisify ( fs . mkdir ) ;
16- const readFile = promisify ( fs . readFile ) ;
17- const writeFile = promisify ( fs . writeFile ) ;
1817
1918const REQUIRED_COMPONENTS = [ 'rust-src' ] ;
2019
@@ -45,27 +44,6 @@ function installDir(): string | undefined {
4544 return undefined ;
4645}
4746
48- /** Returns a path where persistent data for rust-analyzer should be installed. */
49- function metadataDir ( ) : string | undefined {
50- if ( process . platform === 'linux' || process . platform === 'darwin' ) {
51- // Prefer, in this order:
52- // 1. $XDG_CONFIG_HOME/rust-analyzer
53- // 2. $HOME/.config/rust-analyzer
54- const { HOME , XDG_CONFIG_HOME } = process . env ;
55- const baseDir = XDG_CONFIG_HOME || ( HOME && path . join ( HOME , '.config' ) ) ;
56-
57- return baseDir && path . resolve ( path . join ( baseDir , 'rust-analyzer' ) ) ;
58- } else if ( process . platform === 'win32' ) {
59- // %LocalAppData%\rust-analyzer\
60- const { LocalAppData } = process . env ;
61- return (
62- LocalAppData && path . resolve ( path . join ( LocalAppData , 'rust-analyzer' ) )
63- ) ;
64- }
65-
66- return undefined ;
67- }
68-
6947function ensureDir ( path : string ) {
7048 return ! ! path && stat ( path ) . catch ( ( ) => mkdir ( path , { recursive : true } ) ) ;
7149}
@@ -77,40 +55,6 @@ interface RustAnalyzerConfig {
7755 } ;
7856}
7957
80- interface Metadata {
81- releaseTag : string ;
82- }
83-
84- async function readMetadata ( ) : Promise < Metadata | Record < string , unknown > > {
85- const stateDir = metadataDir ( ) ;
86- if ( ! stateDir ) {
87- return { kind : 'error' , code : 'NotSupported' } ;
88- }
89-
90- const filePath = path . join ( stateDir , 'metadata.json' ) ;
91- if ( ! ( await stat ( filePath ) . catch ( ( ) => false ) ) ) {
92- return { kind : 'error' , code : 'FileMissing' } ;
93- }
94-
95- const contents = await readFile ( filePath , 'utf8' ) ;
96- const obj = JSON . parse ( contents ) as unknown ;
97- return typeof obj === 'object' ? ( obj as Record < string , unknown > ) : { } ;
98- }
99-
100- async function writeMetadata ( config : Metadata ) {
101- const stateDir = metadataDir ( ) ;
102- if ( ! stateDir ) {
103- return false ;
104- }
105-
106- if ( ! ( await ensureDir ( stateDir ) ) ) {
107- return false ;
108- }
109-
110- const filePath = path . join ( stateDir , 'metadata.json' ) ;
111- return writeFile ( filePath , JSON . stringify ( config ) ) . then ( ( ) => true ) ;
112- }
113-
11458export async function getServer ( {
11559 askBeforeDownload,
11660 package : pkg ,
0 commit comments