1
+ /*
2
+ Copyright (c) 2023-2024, Oracle and/or its affiliates.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ import { extensions , workspace } from "vscode" ;
17
+ import { configKeys } from "./configuration" ;
18
+ import { extConstants , NODE_WINDOWS_LABEL } from "../constants" ;
19
+ import * as os from 'os' ;
20
+ import { globalVars , LOGGER } from "../extension" ;
21
+ import { LogLevel } from "../logger" ;
22
+ import * as path from 'path' ;
23
+ import * as fs from 'fs' ;
24
+
25
+ export const getConfigurationValue = < T > ( key : string , defaultValue : T | undefined = undefined ) : T => {
26
+ const conf = workspace . getConfiguration ( extConstants . COMMAND_PREFIX ) ;
27
+ return defaultValue != undefined ? conf . get ( key , defaultValue ) : conf . get ( key ) as T ;
28
+ }
29
+
30
+ export const jdkHomeValueHandler = ( ) : string | null => {
31
+ return getConfigurationValue ( configKeys . jdkHome ) ||
32
+ process . env . JDK_HOME ||
33
+ process . env . JAVA_HOME ||
34
+ null ;
35
+ }
36
+
37
+ export const projectSearchRootsValueHandler = ( ) : string => {
38
+ let projectSearchRoots : string = '' ;
39
+ const isProjectFolderSearchLimited : boolean = ! getConfigurationValue ( configKeys . disableProjSearchLimit , false ) ;
40
+ if ( isProjectFolderSearchLimited ) {
41
+ try {
42
+ projectSearchRoots = os . homedir ( ) as string ;
43
+ } catch ( err : any ) {
44
+ LOGGER . log ( `Failed to obtain the user home directory due to: ${ err } ` , LogLevel . ERROR ) ;
45
+ }
46
+ if ( ! projectSearchRoots ) {
47
+ projectSearchRoots = os . type ( ) === NODE_WINDOWS_LABEL ? '%USERPROFILE%' : '$HOME' ; // The launcher script may perform the env variable substitution
48
+ LOGGER . log ( `Using userHomeDir = "${ projectSearchRoots } " as the launcher script may perform env var substitution to get its value.` ) ;
49
+ }
50
+ const workspaces = workspace . workspaceFolders ;
51
+ if ( workspaces ) {
52
+ workspaces . forEach ( workspace => {
53
+ if ( workspace . uri ) {
54
+ try {
55
+ projectSearchRoots = projectSearchRoots + path . delimiter + path . normalize ( workspace . uri . fsPath ) ;
56
+ } catch ( err : any ) {
57
+ LOGGER . log ( `Failed to get the workspace path: ${ err } ` ) ;
58
+ }
59
+ }
60
+ } ) ;
61
+ }
62
+ }
63
+
64
+ return projectSearchRoots ;
65
+ }
66
+
67
+ export const lspServerVmOptionsHandler = ( ) : string [ ] => {
68
+ let serverVmOptions : string [ ] = getConfigurationValue ( configKeys . lspVmOptions , [ ] ) ;
69
+
70
+ return serverVmOptions . map ( el => `-J${ el } ` ) ;
71
+ }
72
+
73
+ export const isDarkColorThemeHandler = ( ) : boolean => {
74
+ // const themeName = getConfigurationValue(configKeys.vscodeTheme);
75
+ const themeName = workspace . getConfiguration ( 'workbench' ) ?. get ( 'colorTheme' ) ;
76
+ if ( ! themeName ) {
77
+ return false ;
78
+ }
79
+ for ( const ext of extensions . all ) {
80
+ const themeList : object [ ] = ext . packageJSON ?. contributes && ext . packageJSON ?. contributes [ 'themes' ] ;
81
+ if ( ! themeList ) {
82
+ continue ;
83
+ }
84
+ let t : any ;
85
+ for ( t of themeList ) {
86
+ if ( t . id !== themeName ) {
87
+ continue ;
88
+ }
89
+ const uiTheme = t . uiTheme ;
90
+ if ( typeof ( uiTheme ) == 'string' ) {
91
+ if ( uiTheme . includes ( '-dark' ) || uiTheme . includes ( '-black' ) ) {
92
+ return true ;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ return false ;
98
+ }
99
+
100
+ export const userdirHandler = ( ) : string => {
101
+ const userdirScope = process . env [ 'nbcode_userdir' ] || getConfigurationValue ( configKeys . userdir , "local" ) ;
102
+ const userdirParentDir = userdirScope === "local"
103
+ ? globalVars . extensionInfo . getWorkspaceStorage ( ) ?. fsPath
104
+ : globalVars . extensionInfo . getGlobalStorage ( ) . fsPath ;
105
+
106
+ if ( ! userdirParentDir ) {
107
+ throw new Error ( `Cannot create path for ${ userdirScope } directory.` ) ;
108
+ }
109
+
110
+ const userdir = path . join ( userdirParentDir , "userdir" ) ;
111
+
112
+ try {
113
+ if ( ! fs . existsSync ( userdir ) ) {
114
+ fs . mkdirSync ( userdir , { recursive : true } ) ;
115
+ const stats = fs . statSync ( userdir ) ;
116
+ if ( ! stats . isDirectory ( ) ) {
117
+ throw new Error ( `${ userdir } is not a directory` ) ;
118
+ }
119
+ }
120
+
121
+ return userdir ;
122
+ } catch ( error ) {
123
+ throw new Error ( `Failed to create or access ${ userdir } : ${ ( error as Error ) . message } ` ) ;
124
+ }
125
+ }
126
+
127
+ export const isNbJavacDisabledHandler = ( ) : boolean => {
128
+ return getConfigurationValue ( configKeys . verbose , false ) ;
129
+ }
0 commit comments