diff options
author | Veetaha <[email protected]> | 2020-02-08 22:27:04 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-08 22:27:04 +0000 |
commit | 539daf4454e3f11424a469e8fba26cacb325176a (patch) | |
tree | 28f37d3adbfedc75b9d4cf25d50ab20469f48cf3 /editors/code | |
parent | bdd88c2fad272f96a8212e5230010f7c02b4d15d (diff) |
vscode: refactor platform artifact name query to switch statement, move BinarySource union variants into a namespace
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/client.ts | 2 | ||||
-rw-r--r-- | editors/code/src/config.ts | 89 | ||||
-rw-r--r-- | editors/code/src/installation/interfaces.ts | 58 | ||||
-rw-r--r-- | editors/code/src/installation/language_server.ts | 10 |
4 files changed, 89 insertions, 70 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 7639ed44b..2e3d4aba2 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts | |||
@@ -10,7 +10,7 @@ export async function createClient(config: Config): Promise<null | lc.LanguageCl | |||
10 | // It might be a good idea to test if the uri points to a file. | 10 | // It might be a good idea to test if the uri points to a file. |
11 | const workspaceFolderPath = workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; | 11 | const workspaceFolderPath = workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; |
12 | 12 | ||
13 | const raLspServerPath = await ensureLanguageServerBinary(config.raLspServerSource); | 13 | const raLspServerPath = await ensureLanguageServerBinary(config.langServerSource); |
14 | if (!raLspServerPath) return null; | 14 | if (!raLspServerPath) return null; |
15 | 15 | ||
16 | const run: lc.Executable = { | 16 | const run: lc.Executable = { |
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index aca5dab5a..f216ab461 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as os from "os"; | 1 | import * as os from "os"; |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import { BinarySource, BinarySourceType } from "./installation/interfaces"; | 3 | import { BinarySource } from "./installation/interfaces"; |
4 | 4 | ||
5 | const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; | 5 | const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; |
6 | 6 | ||
@@ -18,20 +18,7 @@ export interface CargoFeatures { | |||
18 | } | 18 | } |
19 | 19 | ||
20 | export class Config { | 20 | export class Config { |
21 | readonly raLspServerGithubArtifactName = { | 21 | langServerSource!: null | BinarySource; |
22 | linux: "ra_lsp_server-linux", | ||
23 | darwin: "ra_lsp_server-mac", | ||
24 | win32: "ra_lsp_server-windows.exe", | ||
25 | aix: null, | ||
26 | android: null, | ||
27 | freebsd: null, | ||
28 | openbsd: null, | ||
29 | sunos: null, | ||
30 | cygwin: null, | ||
31 | netbsd: null, | ||
32 | }[process.platform]; | ||
33 | |||
34 | raLspServerSource!: null | BinarySource; | ||
35 | 22 | ||
36 | highlightingOn = true; | 23 | highlightingOn = true; |
37 | rainbowHighlightingOn = false; | 24 | rainbowHighlightingOn = false; |
@@ -72,6 +59,56 @@ export class Config { | |||
72 | return path; | 59 | return path; |
73 | } | 60 | } |
74 | 61 | ||
62 | /** | ||
63 | * Name of the binary artifact for `ra_lsp_server` that is published for | ||
64 | * `platform` on GitHub releases. (It is also stored under the same name when | ||
65 | * downloaded by the extension). | ||
66 | */ | ||
67 | private static prebuiltLangServerFileName(platform: NodeJS.Platform): null | string { | ||
68 | switch (platform) { | ||
69 | case "linux": return "ra_lsp_server-linux"; | ||
70 | case "darwin": return "ra_lsp_server-mac"; | ||
71 | case "win32": return "ra_lsp_server-windows.exe"; | ||
72 | |||
73 | // Users on these platforms yet need to manually build from sources | ||
74 | case "aix": | ||
75 | case "android": | ||
76 | case "freebsd": | ||
77 | case "openbsd": | ||
78 | case "sunos": | ||
79 | case "cygwin": | ||
80 | case "netbsd": return null; | ||
81 | // The list of platforms is exhaustive see (`NodeJS.Platform` type definition) | ||
82 | } | ||
83 | } | ||
84 | |||
85 | private static langServerBinarySource( | ||
86 | ctx: vscode.ExtensionContext, | ||
87 | config: vscode.WorkspaceConfiguration | ||
88 | ): null | BinarySource { | ||
89 | const raLspServerPath = RA_LSP_DEBUG ?? config.get<null | string>("raLspServerPath"); | ||
90 | |||
91 | if (raLspServerPath) { | ||
92 | return { | ||
93 | type: BinarySource.Type.ExplicitPath, | ||
94 | path: Config.expandPathResolving(raLspServerPath) | ||
95 | }; | ||
96 | } | ||
97 | |||
98 | const prebuiltBinaryName = Config.prebuiltLangServerFileName(process.platform); | ||
99 | |||
100 | return !prebuiltBinaryName ? null : { | ||
101 | type: BinarySource.Type.GithubRelease, | ||
102 | dir: ctx.globalStoragePath, | ||
103 | file: prebuiltBinaryName, | ||
104 | repo: { | ||
105 | name: "rust-analyzer", | ||
106 | owner: "rust-analyzer", | ||
107 | } | ||
108 | }; | ||
109 | } | ||
110 | |||
111 | |||
75 | // FIXME: revisit the logic for `if (.has(...)) config.get(...)` set default | 112 | // FIXME: revisit the logic for `if (.has(...)) config.get(...)` set default |
76 | // values only in one place (i.e. remove default values from non-readonly members declarations) | 113 | // values only in one place (i.e. remove default values from non-readonly members declarations) |
77 | private refresh(ctx: vscode.ExtensionContext) { | 114 | private refresh(ctx: vscode.ExtensionContext) { |
@@ -107,27 +144,7 @@ export class Config { | |||
107 | this.prevEnhancedTyping = this.enableEnhancedTyping; | 144 | this.prevEnhancedTyping = this.enableEnhancedTyping; |
108 | } | 145 | } |
109 | 146 | ||
110 | { | 147 | this.langServerSource = Config.langServerBinarySource(ctx, config); |
111 | const raLspServerPath = RA_LSP_DEBUG ?? config.get<null | string>("raLspServerPath"); | ||
112 | if (raLspServerPath) { | ||
113 | this.raLspServerSource = { | ||
114 | type: BinarySourceType.ExplicitPath, | ||
115 | path: Config.expandPathResolving(raLspServerPath) | ||
116 | }; | ||
117 | } else if (this.raLspServerGithubArtifactName) { | ||
118 | this.raLspServerSource = { | ||
119 | type: BinarySourceType.GithubBinary, | ||
120 | dir: ctx.globalStoragePath, | ||
121 | file: this.raLspServerGithubArtifactName, | ||
122 | repo: { | ||
123 | name: "rust-analyzer", | ||
124 | owner: "rust-analyzer", | ||
125 | } | ||
126 | }; | ||
127 | } else { | ||
128 | this.raLspServerSource = null; | ||
129 | } | ||
130 | } | ||
131 | 148 | ||
132 | if (config.has('cargo-watch.enable')) { | 149 | if (config.has('cargo-watch.enable')) { |
133 | this.cargoWatchOptions.enable = config.get<boolean>( | 150 | this.cargoWatchOptions.enable = config.get<boolean>( |
diff --git a/editors/code/src/installation/interfaces.ts b/editors/code/src/installation/interfaces.ts index 03eac5b79..8039d0b90 100644 --- a/editors/code/src/installation/interfaces.ts +++ b/editors/code/src/installation/interfaces.ts | |||
@@ -12,42 +12,44 @@ export interface ArtifactMetadata { | |||
12 | } | 12 | } |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * Type tag for `BinarySource` discriminated union. | ||
16 | */ | ||
17 | export enum BinarySourceType { ExplicitPath, GithubBinary } | ||
18 | |||
19 | /** | ||
20 | * Represents the source of a binary artifact which is either specified by the user | 15 | * Represents the source of a binary artifact which is either specified by the user |
21 | * explicitly, or bundled by this extension from GitHub releases. | 16 | * explicitly, or bundled by this extension from GitHub releases. |
22 | */ | 17 | */ |
23 | export type BinarySource = ExplicitPathSource | GithubBinarySource; | 18 | export type BinarySource = BinarySource.ExplicitPath | BinarySource.GithubRelease; |
24 | |||
25 | |||
26 | export interface ExplicitPathSource { | ||
27 | type: BinarySourceType.ExplicitPath; | ||
28 | 19 | ||
20 | export namespace BinarySource { | ||
29 | /** | 21 | /** |
30 | * Filesystem path to the binary specified by the user explicitly. | 22 | * Type tag for `BinarySource` discriminated union. |
31 | */ | 23 | */ |
32 | path: string; | 24 | export const enum Type { ExplicitPath, GithubRelease } |
33 | } | ||
34 | 25 | ||
35 | export interface GithubBinarySource { | 26 | export interface ExplicitPath { |
36 | type: BinarySourceType.GithubBinary; | 27 | type: Type.ExplicitPath; |
37 | 28 | ||
38 | /** | 29 | /** |
39 | * Repository where the binary is stored. | 30 | * Filesystem path to the binary specified by the user explicitly. |
40 | */ | 31 | */ |
41 | repo: GithubRepo; | 32 | path: string; |
33 | } | ||
42 | 34 | ||
43 | /** | 35 | export interface GithubRelease { |
44 | * Directory on the filesystem where the bundled binary is stored. | 36 | type: Type.GithubRelease; |
45 | */ | 37 | |
46 | dir: string; | 38 | /** |
39 | * Repository where the binary is stored. | ||
40 | */ | ||
41 | repo: GithubRepo; | ||
42 | |||
43 | /** | ||
44 | * Directory on the filesystem where the bundled binary is stored. | ||
45 | */ | ||
46 | dir: string; | ||
47 | |||
48 | /** | ||
49 | * Name of the binary file. It is stored under the same name on GitHub releases | ||
50 | * and in local `.dir`. | ||
51 | */ | ||
52 | file: string; | ||
53 | } | ||
47 | 54 | ||
48 | /** | ||
49 | * Name of the binary file. It is stored under the same name on GitHub releases | ||
50 | * and in local `.dir`. | ||
51 | */ | ||
52 | file: string; | ||
53 | } | 55 | } |
diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts index b75d3a00a..522d59eb5 100644 --- a/editors/code/src/installation/language_server.ts +++ b/editors/code/src/installation/language_server.ts | |||
@@ -5,12 +5,12 @@ import * as path from "path"; | |||
5 | import { strict as assert } from "assert"; | 5 | import { strict as assert } from "assert"; |
6 | import { promises as fs } from "fs"; | 6 | import { promises as fs } from "fs"; |
7 | 7 | ||
8 | import { BinarySource, BinarySourceType, GithubBinarySource } from "./interfaces"; | 8 | import { BinarySource } from "./interfaces"; |
9 | import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata"; | 9 | import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata"; |
10 | import { downloadFile } from "./download_file"; | 10 | import { downloadFile } from "./download_file"; |
11 | 11 | ||
12 | export async function downloadLatestLanguageServer( | 12 | export async function downloadLatestLanguageServer( |
13 | {file: artifactFileName, dir: installationDir, repo}: GithubBinarySource | 13 | {file: artifactFileName, dir: installationDir, repo}: BinarySource.GithubRelease |
14 | ) { | 14 | ) { |
15 | const binaryMetadata = await fetchLatestArtifactMetadata(repo, artifactFileName); | 15 | const binaryMetadata = await fetchLatestArtifactMetadata(repo, artifactFileName); |
16 | 16 | ||
@@ -67,7 +67,7 @@ export async function ensureLanguageServerBinary( | |||
67 | } | 67 | } |
68 | 68 | ||
69 | switch (langServerSource.type) { | 69 | switch (langServerSource.type) { |
70 | case BinarySourceType.ExplicitPath: { | 70 | case BinarySource.Type.ExplicitPath: { |
71 | if (isBinaryAvailable(langServerSource.path)) { | 71 | if (isBinaryAvailable(langServerSource.path)) { |
72 | return langServerSource.path; | 72 | return langServerSource.path; |
73 | } | 73 | } |
@@ -78,7 +78,7 @@ export async function ensureLanguageServerBinary( | |||
78 | ); | 78 | ); |
79 | return null; | 79 | return null; |
80 | } | 80 | } |
81 | case BinarySourceType.GithubBinary: { | 81 | case BinarySource.Type.GithubRelease: { |
82 | const bundledBinaryPath = path.join(langServerSource.dir, langServerSource.file); | 82 | const bundledBinaryPath = path.join(langServerSource.dir, langServerSource.file); |
83 | 83 | ||
84 | if (!isBinaryAvailable(bundledBinaryPath)) { | 84 | if (!isBinaryAvailable(bundledBinaryPath)) { |
@@ -106,7 +106,7 @@ export async function ensureLanguageServerBinary( | |||
106 | ); | 106 | ); |
107 | 107 | ||
108 | vscode.window.showInformationMessage( | 108 | vscode.window.showInformationMessage( |
109 | "Rust analyzer language server was successfully installed" | 109 | "Rust analyzer language server was successfully installed 🦀" |
110 | ); | 110 | ); |
111 | } | 111 | } |
112 | return bundledBinaryPath; | 112 | return bundledBinaryPath; |