3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*-------------------------------------------------------------------------------------------*/
5
5
6
- import * as fs from "fs" ;
7
6
import * as os from "os" ;
8
7
import * as path from "path" ;
9
8
import * as vscode from "vscode" ;
10
9
import * as WinReg from "winreg" ;
11
-
12
- import * as constants from "../common/constants" ;
13
10
import * as util from "../common/util" ;
14
11
15
12
import { resolveArduinoPath , validateArduinoPath } from "../common/platform" ;
@@ -43,8 +40,9 @@ export class ArduinoSettings implements IArduinoSettings {
43
40
44
41
public async initialize ( ) {
45
42
const platform = os . platform ( ) ;
43
+ await this . tryResolveArduinoPath ( ) ;
46
44
if ( platform === "win32" ) {
47
- await this . updateWindowsPath ( this . arduinoPath ) ;
45
+ await this . updateWindowsPath ( ) ;
48
46
} else if ( platform === "linux" ) {
49
47
this . _packagePath = path . join ( process . env . HOME , ".arduino15" ) ;
50
48
this . _sketchbookPath = this . preferences . get ( "sketchbook.path" ) || path . join ( process . env . HOME , "Arduino" ) ;
@@ -55,38 +53,14 @@ export class ArduinoSettings implements IArduinoSettings {
55
53
}
56
54
57
55
public get arduinoPath ( ) : string {
58
- if ( this . _arduinoPath ) {
59
- return this . _arduinoPath ;
60
- } else {
61
- // Query arduino path sequentially from the following places such as "vscode user settings", "system environment variables",
62
- // "usual software installation directory for each os".
63
- // 1. Search vscode user settings first.
64
- const configValue = VscodeSettings . getIntance ( ) . arduinoPath ;
65
- if ( ! configValue || ! configValue . trim ( ) ) {
66
- // 2 & 3. Resolve arduino path from system environment varialbes and usual software installation directory.
67
- this . _arduinoPath = resolveArduinoPath ( ) ;
68
- } else {
69
- this . _arduinoPath = configValue ;
70
- }
71
-
72
- if ( ! this . _arduinoPath ) { // Pop up vscode User Settings page when cannot resolve arduino path.
73
- vscode . window . showErrorMessage ( `Cannot find the arduino installation path. Please specify the "arduino.path" in the User Settings.` +
74
- " Requires a restart after change." ) ;
75
- vscode . commands . executeCommand ( "workbench.action.openGlobalSettings" ) ;
76
- } else if ( ! validateArduinoPath ( this . _arduinoPath ) ) { // Validate if arduino path is the correct path.
77
- vscode . window . showErrorMessage ( `Cannot find arduino executable program under directory "${ this . _arduinoPath } ". ` +
78
- `Please set the correct "arduino.path" in the User Settings. Requires a restart after change.` ) ;
79
- vscode . commands . executeCommand ( "workbench.action.openGlobalSettings" ) ;
80
- }
81
- return this . _arduinoPath ;
82
- }
56
+ return this . _arduinoPath ;
83
57
}
84
58
85
59
public get defaultExamplePath ( ) : string {
86
60
if ( os . platform ( ) === "darwin" ) {
87
- return path . join ( this . arduinoPath , "Arduino.app/Contents/Java/examples" ) ;
61
+ return path . join ( this . _arduinoPath , "Arduino.app/Contents/Java/examples" ) ;
88
62
} else {
89
- return path . join ( this . arduinoPath , "examples" ) ;
63
+ return path . join ( this . _arduinoPath , "examples" ) ;
90
64
}
91
65
}
92
66
@@ -96,28 +70,28 @@ export class ArduinoSettings implements IArduinoSettings {
96
70
97
71
public get defaultPackagePath ( ) : string {
98
72
if ( os . platform ( ) === "darwin" ) {
99
- return path . join ( this . arduinoPath , "Arduino.app/Contents/Java/hardware" ) ;
73
+ return path . join ( this . _arduinoPath , "Arduino.app/Contents/Java/hardware" ) ;
100
74
} else { // linux and win32.
101
- return path . join ( this . arduinoPath , "hardware" ) ;
75
+ return path . join ( this . _arduinoPath , "hardware" ) ;
102
76
}
103
77
}
104
78
105
79
public get defaultLibPath ( ) : string {
106
80
if ( os . platform ( ) === "darwin" ) {
107
- return path . join ( this . arduinoPath , "Arduino.app/Contents/Java/libraries" ) ;
81
+ return path . join ( this. _arduinoPath , "Arduino.app/Contents/Java/libraries" ) ;
108
82
} else { // linux and win32
109
- return path . join ( this . arduinoPath , "libraries" ) ;
83
+ return path . join ( this . _arduinoPath , "libraries" ) ;
110
84
}
111
85
}
112
86
113
87
public get commandPath ( ) : string {
114
88
const platform = os . platform ( ) ;
115
89
if ( platform === "darwin" ) {
116
- return path . join ( this . arduinoPath , path . normalize ( "Arduino.app/Contents/MacOS/Arduino" ) ) ;
90
+ return path . join ( this . _arduinoPath , path . normalize ( "Arduino.app/Contents/MacOS/Arduino" ) ) ;
117
91
} else if ( platform === "linux" ) {
118
- return path . join ( this . arduinoPath , "arduino" ) ;
92
+ return path . join ( this . _arduinoPath , "arduino" ) ;
119
93
} else if ( platform === "win32" ) {
120
- return path . join ( this . arduinoPath , "arduino_debug.exe" ) ;
94
+ return path . join ( this . _arduinoPath , "arduino_debug.exe" ) ;
121
95
}
122
96
}
123
97
@@ -145,44 +119,50 @@ export class ArduinoSettings implements IArduinoSettings {
145
119
* - User change the location of the default *Documents* folder.
146
120
* - Use the windows store Arduino app.
147
121
*/
148
- private updateWindowsPath ( arduinoPath : string ) : Thenable < boolean > {
149
- let docFolder = path . join ( process . env . USERPROFILE , "Documents" ) ;
150
- return new Promise ( ( resolve , reject ) => {
151
- try {
152
- const regKey = new WinReg ( {
153
- hive : WinReg . HKCU ,
154
- key : "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders" ,
155
- } ) ;
156
-
157
- regKey . valueExists ( "Personal" , ( e , exists ) => {
158
- if ( ! e && exists ) {
159
- regKey . get ( "Personal" , ( err , result ) => {
160
- if ( ! err && result ) {
161
- docFolder = result . value ;
162
- }
163
- resolve ( docFolder ) ;
164
-
165
- } ) ;
166
- } else {
167
- resolve ( docFolder ) ;
168
- }
169
- } ) ;
170
- } catch ( ex ) {
171
- resolve ( docFolder ) ;
172
- }
173
- } ) . then ( ( folder : string ) => {
174
- // For some case, docFolder parsed from win32 registry looks like "%USERPROFILE%\Documents,
175
- // Should replace the environment variables with actual value.
176
- folder = folder . replace ( / % ( [ ^ % ] + ) % / g, ( match , p1 ) => {
177
- return process . env [ p1 ] ;
178
- } ) ;
179
- if ( util . fileExistsSync ( path . join ( arduinoPath , "AppxManifest.xml" ) ) ) {
180
- this . _packagePath = path . join ( folder , "ArduinoData" ) ;
181
- } else {
182
- this . _packagePath = path . join ( process . env . LOCALAPPDATA , "Arduino15" ) ;
183
- }
184
- this . _sketchbookPath = this . preferences . get ( "sketchbook.path" ) || path . join ( folder , "Arduino" ) ;
185
- return true ;
122
+ private async updateWindowsPath ( ) : Promise < void > {
123
+ let folder ;
124
+ try {
125
+ folder = await util . getRegistryValues ( WinReg . HKCU ,
126
+ "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders" ,
127
+ "Personal" ) ;
128
+ } catch ( ex ) {
129
+ }
130
+ if ( ! folder ) {
131
+ folder = path . join ( process . env . USERPROFILE , "Documents" ) ;
132
+ }
133
+ // For some case, docFolder parsed from win32 registry looks like "%USERPROFILE%\Documents,
134
+ // Should replace the environment variables with actual value.
135
+ folder = folder . replace ( / % ( [ ^ % ] + ) % / g, ( match , p1 ) => {
136
+ return process . env [ p1 ] ;
186
137
} ) ;
138
+ if ( util . fileExistsSync ( path . join ( this . _arduinoPath , "AppxManifest.xml" ) ) ) {
139
+ this . _packagePath = path . join ( folder , "ArduinoData" ) ;
140
+ } else {
141
+ this . _packagePath = path . join ( process . env . LOCALAPPDATA , "Arduino15" ) ;
142
+ }
143
+ this . _sketchbookPath = this . preferences . get ( "sketchbook.path" ) || path . join ( folder , "Arduino" ) ;
144
+ }
145
+
146
+ private async tryResolveArduinoPath ( ) : Promise < void > {
147
+ // Query arduino path sequentially from the following places such as "vscode user settings", "system environment variables",
148
+ // "usual software installation directory for each os".
149
+ // 1. Search vscode user settings first.
150
+ const configValue = VscodeSettings . getIntance ( ) . arduinoPath ;
151
+ if ( ! configValue || ! configValue . trim ( ) ) {
152
+ // 2 & 3. Resolve arduino path from system environment varialbes and usual software installation directory.
153
+ this . _arduinoPath = await Promise . resolve ( resolveArduinoPath ( ) ) ;
154
+ } else {
155
+ this . _arduinoPath = configValue ;
156
+ }
157
+
158
+ if ( ! this . _arduinoPath ) { // Pop up vscode User Settings page when cannot resolve arduino path.
159
+ vscode . window . showErrorMessage ( `Cannot find the arduino installation path. Please specify the "arduino.path" in the User Settings.` +
160
+ " Requires a restart after change." ) ;
161
+ vscode . commands . executeCommand ( "workbench.action.openGlobalSettings" ) ;
162
+ } else if ( ! validateArduinoPath ( this . _arduinoPath ) ) { // Validate if arduino path is the correct path.
163
+ vscode . window . showErrorMessage ( `Cannot find arduino executable program under directory "${ this . _arduinoPath } ". ` +
164
+ `Please set the correct "arduino.path" in the User Settings. Requires a restart after change.` ) ;
165
+ vscode . commands . executeCommand ( "workbench.action.openGlobalSettings" ) ;
166
+ }
187
167
}
188
168
}
0 commit comments