1
1
import * as vscode from "vscode" ;
2
2
import * as cp from "child_process" ;
3
+ import fs from 'fs' ;
4
+ import path from 'path' ;
3
5
4
6
import {
5
7
createStdioOptions ,
@@ -34,50 +36,68 @@ export function isRunningOnWeb() : boolean {
34
36
// Web environment is detected with no fallback on child process which is not supported there.
35
37
return typeof cp . spawn !== 'function' || typeof process === 'undefined' ;
36
38
}
37
-
38
- function getPlatformBinaryDirectoryPath ( platform : ServerPlatform ) : string {
39
- // CI is handling the copy to bin folder to avoid storage of exe on git.
40
- switch ( platform ) {
41
- case ServerPlatform . windows :
42
- return "bin/windows/" ;
43
- case ServerPlatform . linux :
44
- return "bin/linux/" ;
45
- case ServerPlatform . wasi :
46
- return "bin/wasi/" ;
39
+ function getUserServerPath ( ) : string | null {
40
+ if ( isRunningOnWeb ( ) ) {
41
+ return null ;
42
+ } else {
43
+ // Check configuration.
44
+ let serverPath = vscode . workspace . getConfiguration ( "shader-validator" ) . get < string > ( "serverPath" ) ;
45
+ if ( serverPath && serverPath . length > 0 ) {
46
+ if ( fs . existsSync ( serverPath ) ) {
47
+ console . info ( `User server path found: ${ serverPath } ` ) ;
48
+ return serverPath ;
49
+ } else {
50
+ vscode . window . showErrorMessage ( `User server path is invalid: ${ serverPath } ` ) ;
51
+ }
52
+ }
53
+ // Check environment variables
54
+ if ( process . env . SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH !== undefined ) {
55
+ let envPath = process . env . SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH ;
56
+ if ( fs . existsSync ( envPath ) ) {
57
+ console . info ( `SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH found: ${ envPath } ` ) ;
58
+ return envPath ;
59
+ } else {
60
+ vscode . window . showErrorMessage ( `SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH is invalid: ${ serverPath } ` ) ;
61
+ }
62
+ }
63
+ // Use bundled executables.
64
+ return null ;
47
65
}
48
66
}
49
- function getPlatformBinaryName ( platform : ServerPlatform ) : string {
50
- switch ( platform ) {
67
+ function getPlatformBinaryDirectoryPath ( extensionUri : vscode . Uri , platform : ServerPlatform ) : vscode . Uri {
68
+ let serverPath = getUserServerPath ( ) ;
69
+ if ( serverPath ) {
70
+ return vscode . Uri . file ( path . dirname ( serverPath ) ) ;
71
+ } else {
72
+ // CI is handling the copy to bin folder to avoid storage of exe on git.
73
+ switch ( platform ) {
51
74
case ServerPlatform . windows :
52
- return "shader-language-server.exe" ;
75
+ return vscode . Uri . joinPath ( extensionUri , "bin/windows/" ) ;
53
76
case ServerPlatform . linux :
54
- return "shader-language-server" ;
77
+ return vscode . Uri . joinPath ( extensionUri , "bin/linux/" ) ;
55
78
case ServerPlatform . wasi :
56
- return "shader-language-server.wasm" ;
79
+ return vscode . Uri . joinPath ( extensionUri , "bin/wasi/" ) ;
80
+ }
57
81
}
58
82
}
59
- function getPlatformBinaryDirectoryUri ( context : vscode . ExtensionContext , platform : ServerPlatform ) : vscode . Uri {
60
- // process is undefined on the wasi.
61
- if ( platform !== ServerPlatform . wasi && context . extensionMode === vscode . ExtensionMode . Development ) {
62
- console . info ( "Running extension in dev mode. Looking for environment variable SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH targetting server." ) ;
63
- if ( process . env . SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH !== undefined ) {
64
- console . info ( `SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH found: ${ process . env . SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH } ` ) ;
65
- return vscode . Uri . file ( process . env . SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH + '/' ) ;
66
- } else {
67
- console . warn ( 'SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH environment variable not found. Trying to launch from bin folder' ) ;
68
- return vscode . Uri . joinPath ( context . extensionUri , getPlatformBinaryDirectoryPath ( platform ) ) ;
83
+ function getPlatformBinaryName ( platform : ServerPlatform ) : string {
84
+ let serverPath = getUserServerPath ( ) ;
85
+ if ( serverPath ) {
86
+ return path . basename ( serverPath ) ;
87
+ } else {
88
+ switch ( platform ) {
89
+ case ServerPlatform . windows :
90
+ return "shader-language-server.exe" ;
91
+ case ServerPlatform . linux :
92
+ return "shader-language-server" ;
93
+ case ServerPlatform . wasi :
94
+ return "shader-language-server.wasm" ;
69
95
}
70
- } else { // Running in production or test mode
71
- return vscode . Uri . joinPath ( context . extensionUri , getPlatformBinaryDirectoryPath ( platform ) ) ;
72
96
}
73
97
}
74
- // Relative path from extension directory
75
- export function getPlatformBinaryPath ( platform : ServerPlatform ) : string {
76
- return getPlatformBinaryDirectoryPath ( platform ) + getPlatformBinaryName ( platform ) ;
77
- }
78
98
// Absolute path as uri
79
- export function getPlatformBinaryUri ( context : vscode . ExtensionContext , platform : ServerPlatform ) : vscode . Uri {
80
- return vscode . Uri . joinPath ( getPlatformBinaryDirectoryUri ( context , platform ) , getPlatformBinaryName ( platform ) ) ;
99
+ export function getPlatformBinaryUri ( extensionUri : vscode . Uri , platform : ServerPlatform ) : vscode . Uri {
100
+ return vscode . Uri . joinPath ( getPlatformBinaryDirectoryPath ( extensionUri , platform ) , getPlatformBinaryName ( platform ) ) ;
81
101
}
82
102
83
103
export function getServerPlatform ( ) : ServerPlatform {
@@ -99,8 +119,11 @@ export function getServerPlatform() : ServerPlatform {
99
119
function notifyConfigurationChange ( context : vscode . ExtensionContext , client : LanguageClient ) {
100
120
context . subscriptions . push (
101
121
vscode . workspace . onDidChangeConfiguration ( async ( event : vscode . ConfigurationChangeEvent ) => {
102
- if ( event . affectsConfiguration ( "shader-validator" ) )
103
- {
122
+ if ( event . affectsConfiguration ( "shader-validator" ) ) {
123
+ if ( event . affectsConfiguration ( "shader-validator.trace.server" ) ||
124
+ event . affectsConfiguration ( "shader-validator.serverPath" ) ) {
125
+ //restartServer();
126
+ }
104
127
await client . sendNotification ( DidChangeConfigurationNotification . type , {
105
128
settings : "" ,
106
129
} ) ;
@@ -171,7 +194,9 @@ export async function createLanguageClient(context: vscode.ExtensionContext): Pr
171
194
}
172
195
}
173
196
async function createLanguageClientStandard ( context : vscode . ExtensionContext , platform : ServerPlatform ) : Promise < LanguageClient | null > {
174
- const executable = getPlatformBinaryUri ( context , platform ) ;
197
+ const executable = getPlatformBinaryUri ( context . extensionUri , platform ) ;
198
+ const cwd = getPlatformBinaryDirectoryPath ( context . extensionUri , platform ) ;
199
+ console . info ( `Executing server ${ executable } with working directory ${ cwd } ` ) ;
175
200
const trace = vscode . workspace . getConfiguration ( "shader-validator" ) . get < string > ( "trace.server" ) ;
176
201
const defaultEnv = { } ;
177
202
const env = ( trace === "verbose" ) ? {
@@ -187,7 +212,7 @@ async function createLanguageClientStandard(context: vscode.ExtensionContext, pl
187
212
command : executable . fsPath ,
188
213
transport : TransportKind . stdio ,
189
214
options : {
190
- cwd : getPlatformBinaryDirectoryUri ( context , platform ) . fsPath ,
215
+ cwd : cwd . fsPath ,
191
216
env : env
192
217
}
193
218
} ;
@@ -234,7 +259,8 @@ async function createLanguageClientWASI(context: vscode.ExtensionContext) : Prom
234
259
// Load the WASM module. It is stored alongside the extension's JS code.
235
260
// So we can use VS Code's file system API to load it. Makes it
236
261
// independent of whether the code runs in the desktop or the web.
237
- const executable = getPlatformBinaryUri ( context , ServerPlatform . wasi ) ;
262
+ const executable = getPlatformBinaryUri ( context . extensionUri , ServerPlatform . wasi ) ;
263
+ console . info ( `Executing wasi server ${ executable } ` ) ;
238
264
const bits = await vscode . workspace . fs . readFile ( executable ) ;
239
265
const module = await WebAssembly . compile ( bits ) ;
240
266
0 commit comments