This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathsettings.ts
191 lines (165 loc) · 7.41 KB
/
settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*--------------------------------------------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------*/
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import * as WinReg from "winreg";
import * as constants from "../common/constants";
import * as util from "../common/util";
import { resolveArduinoPath, validateArduinoPath } from "../common/platform";
export interface IArduinoSettings {
arduinoPath: string;
additionalUrls: string | string[];
logLevel: string;
commandPath: string;
packagePath: string;
defaultPackagePath: string;
libPath: string;
defaultLibPath: string;
formatterSettings: IClangFormatterSettings;
}
export interface IClangFormatterSettings {
style: string;
}
export class ArduinoSettings implements IArduinoSettings {
private _arduinoPath: string;
private _packagePath: string;
private _libPath: string;
private _logLevel: string;
private _clangFormatterSettings: IClangFormatterSettings;
public constructor() {
}
public async initialize() {
const platform = os.platform();
if (platform === "win32") {
await this.updateWindowsPath(this.arduinoPath);
} else if (platform === "linux") {
this._packagePath = path.join(process.env.HOME, ".arduino15");
this._libPath = path.join(process.env.HOME, "Arduino/libraries");
} else if (platform === "darwin") {
this._packagePath = path.join(process.env.HOME, "Library/Arduino15");
this._libPath = path.join(process.env.HOME, "Documents/Arduino/libraries");
}
this.loadClangFormatterSettings();
}
public get arduinoPath(): string {
if (this._arduinoPath) {
return this._arduinoPath;
} else {
// Query arduino path sequentially from the following places such as "vscode user settings", "system environment variables",
// "usual software installation directory for each os".
// 1. Search vscode user settings first.
const workspaceConfig = vscode.workspace.getConfiguration();
const configValue = workspaceConfig.get<string>("arduino.path");
if (!configValue || !configValue.trim()) {
// 2 & 3. Resolve arduino path from system environment varialbes and usual software installation directory.
this._arduinoPath = resolveArduinoPath();
} else {
this._arduinoPath = configValue;
}
if (!this._arduinoPath) { // Pop up vscode User Settings page when cannot resolve arduino path.
vscode.window.showErrorMessage(`Cannot find the arduino installation path. Please specify the "arduino.path" in the User Settings.` +
" Requires a restart after change.");
vscode.commands.executeCommand("workbench.action.openGlobalSettings");
} else if (!validateArduinoPath(this._arduinoPath)) { // Validate if arduino path is the correct path.
vscode.window.showErrorMessage(`Cannot find arduino executable program under directory "${this._arduinoPath}". ` +
`Please set the correct "arduino.path" in the User Settings. Requires a restart after change.`);
vscode.commands.executeCommand("workbench.action.openGlobalSettings");
}
return this._arduinoPath;
}
}
public get additionalUrls(): string {
const workspaceConfig = vscode.workspace.getConfiguration();
return workspaceConfig.get<string>("arduino.additionalUrls");
}
public get packagePath(): string {
return this._packagePath;
}
public get defaultPackagePath(): string {
if (os.platform() === "darwin") {
return path.join(this.arduinoPath, "Arduino.app/Contents/Java/hardware");
} else { // linux and win32.
return path.join(this.arduinoPath, "hardware");
}
}
public get libPath(): string {
return this._libPath;
}
public get defaultLibPath(): string {
if (os.platform() === "darwin") {
return path.join(this.arduinoPath, "Arduino.app/Contents/Java/libraries");
} else { // linux and win32
return path.join(this.arduinoPath, "libraries");
}
}
public get logLevel(): string {
const workspaceConfig = vscode.workspace.getConfiguration();
return workspaceConfig.get<string>("arduino.logLevel") || "info";
}
public get commandPath(): string {
const platform = os.platform();
if (platform === "darwin") {
return path.join(this.arduinoPath, path.normalize("Arduino.app/Contents/MacOS/Arduino"));
} else if (platform === "linux") {
return path.join(this.arduinoPath, "arduino");
} else if (platform === "win32") {
return path.join(this.arduinoPath, "arduino_debug.exe");
}
}
public get formatterSettings(): IClangFormatterSettings {
return this._clangFormatterSettings;
}
/**
* For Windows platform, there are two situations here:
* - User change the location of the default *Documents* folder.
* - Use the windows store Arduino app.
*/
private updateWindowsPath(arduinoPath: string): Thenable<boolean> {
let docFolder = path.join(process.env.USERPROFILE, "Documents");
return new Promise((resolve, reject) => {
try {
const regKey = new WinReg({
hive: WinReg.HKCU,
key: "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
});
regKey.valueExists("Personal", (e, exists) => {
if (!e && exists) {
regKey.get("Personal", (err, result) => {
if (!err && result) {
docFolder = result.value;
}
resolve(docFolder);
});
} else {
resolve(docFolder);
}
});
} catch (ex) {
resolve(docFolder);
}
}).then((folder: string) => {
// For some case, docFolder parsed from win32 registry looks like "%USERPROFILE%\Documents,
// Should replace the environment variables with actual value.
folder = folder.replace(/%([^%]+)%/g, (match, p1) => {
return process.env[p1];
});
this._libPath = path.normalize(path.join(folder, "Arduino/libraries"));
if (util.fileExistsSync(path.join(arduinoPath, "AppxManifest.xml"))) {
this._packagePath = path.join(folder, "ArduinoData");
} else {
this._packagePath = path.join(process.env.LOCALAPPDATA, "Arduino15");
}
return true;
});
}
private loadClangFormatterSettings() {
const workspaceConfig = vscode.workspace.getConfiguration();
this._clangFormatterSettings = {
style: workspaceConfig.get<string>("arduino.clangFormatStyle"),
};
}
}