Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit 94ed102

Browse files
committed
Prepare home-grown persistent metadata for merge with RA one
1 parent 9245654 commit 94ed102

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

src/rust-analyzer/persistent_state.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { promisify } from 'util';
4+
5+
const stat = promisify(fs.stat);
6+
const mkdir = promisify(fs.mkdir);
7+
const readFile = promisify(fs.readFile);
8+
const writeFile = promisify(fs.writeFile);
9+
10+
/** Returns a path where persistent data for rust-analyzer should be installed. */
11+
function metadataDir(): string | undefined {
12+
if (process.platform === 'linux' || process.platform === 'darwin') {
13+
// Prefer, in this order:
14+
// 1. $XDG_CONFIG_HOME/rust-analyzer
15+
// 2. $HOME/.config/rust-analyzer
16+
const { HOME, XDG_CONFIG_HOME } = process.env;
17+
const baseDir = XDG_CONFIG_HOME || (HOME && path.join(HOME, '.config'));
18+
19+
return baseDir && path.resolve(path.join(baseDir, 'rust-analyzer'));
20+
} else if (process.platform === 'win32') {
21+
// %LocalAppData%\rust-analyzer\
22+
const { LocalAppData } = process.env;
23+
return (
24+
LocalAppData && path.resolve(path.join(LocalAppData, 'rust-analyzer'))
25+
);
26+
}
27+
28+
return undefined;
29+
}
30+
31+
export interface Metadata {
32+
releaseTag: string;
33+
}
34+
35+
export async function readMetadata(): Promise<
36+
Metadata | Record<string, unknown>
37+
> {
38+
const stateDir = metadataDir();
39+
if (!stateDir) {
40+
return { kind: 'error', code: 'NotSupported' };
41+
}
42+
43+
const filePath = path.join(stateDir, 'metadata.json');
44+
if (!(await stat(filePath).catch(() => false))) {
45+
return { kind: 'error', code: 'FileMissing' };
46+
}
47+
48+
const contents = await readFile(filePath, 'utf8');
49+
const obj = JSON.parse(contents) as unknown;
50+
return typeof obj === 'object' ? (obj as Record<string, unknown>) : {};
51+
}
52+
53+
export async function writeMetadata(config: Metadata) {
54+
const stateDir = metadataDir();
55+
if (!stateDir) {
56+
return false;
57+
}
58+
59+
if (!(await ensureDir(stateDir))) {
60+
return false;
61+
}
62+
63+
const filePath = path.join(stateDir, 'metadata.json');
64+
return writeFile(filePath, JSON.stringify(config)).then(() => true);
65+
}
66+
67+
function ensureDir(path: string) {
68+
return !!path && stat(path).catch(() => mkdir(path, { recursive: true }));
69+
}

src/rust-analyzer/rustAnalyzer.ts

+1-57
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import { WorkspaceProgress } from '../extension';
1010
import { download, fetchRelease } from '../net';
1111
import * as rustup from '../rustup';
1212
import { Observable } from '../utils/observable';
13+
import { Metadata, readMetadata, writeMetadata } from './persistent_state';
1314

1415
const stat = promisify(fs.stat);
1516
const mkdir = promisify(fs.mkdir);
16-
const readFile = promisify(fs.readFile);
17-
const writeFile = promisify(fs.writeFile);
1817

1918
const 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-
6947
function 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-
11458
export async function getServer({
11559
askBeforeDownload,
11660
package: pkg,

0 commit comments

Comments
 (0)