Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit ab438d8

Browse files
awmicheladiazulay
andauthored
Fix arduinoPath resolution on MacOS (#1227)
* Fix arduinoPath resolution in MacOS. The `resolveMacArduinoAppPath` utility was automatically adding "Arduino.app" to the resolved `arduinoPath` regardless of whether `useAndroidCli` was enabled. This was breaking installations of `arduino-cli` made through Homebrew. The path to the command ends up being either `/usr/local/bin/arduino-cli` or `/opt/homebrew/bin/arduino-cli`. This function would resolve an `arduinoPath` of `/opt/homebrew/bin` to `/opt/homebrew/bin/Arduino.app/arduino-cli`. Co-authored-by: Adi Azulay <[email protected]>
1 parent 25caa39 commit ab438d8

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

src/arduino/arduinoSettings.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class ArduinoSettings implements IArduinoSettings {
104104

105105
public get defaultExamplePath(): string {
106106
if (os.platform() === "darwin") {
107-
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/examples");
107+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath, this._useArduinoCli), "/Contents/Java/examples");
108108
} else {
109109
return path.join(this._arduinoPath, "examples");
110110
}
@@ -116,15 +116,15 @@ export class ArduinoSettings implements IArduinoSettings {
116116

117117
public get defaultPackagePath(): string {
118118
if (os.platform() === "darwin") {
119-
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/hardware");
119+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath, this._useArduinoCli), "/Contents/Java/hardware");
120120
} else { // linux and win32.
121121
return path.join(this._arduinoPath, "hardware");
122122
}
123123
}
124124

125125
public get defaultLibPath(): string {
126126
if (os.platform() === "darwin") {
127-
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/libraries");
127+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath, this._useArduinoCli), "/Contents/Java/libraries");
128128
} else { // linux and win32
129129
return path.join(this._arduinoPath, "libraries");
130130
}
@@ -133,7 +133,7 @@ export class ArduinoSettings implements IArduinoSettings {
133133
public get commandPath(): string {
134134
const platform = os.platform();
135135
if (platform === "darwin") {
136-
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), path.normalize(this._commandPath));
136+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath, this._useArduinoCli), path.normalize(this._commandPath));
137137
} else {
138138
return path.join(this._arduinoPath, path.normalize(this._commandPath));
139139
}

src/common/sys/darwin.ts

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
2-
// Licensed under the MIT license.
3-
4-
import * as childProcess from "child_process";
5-
import * as path from "path";
6-
import { directoryExistsSync, fileExistsSync, resolveMacArduinoAppPath } from "../util";
7-
8-
export function resolveArduinoPath(): string {
9-
let result;
10-
11-
const defaultCommonPaths = [path.join(process.env.HOME, "Applications"), "/Applications"];
12-
for (const scanPath of defaultCommonPaths) {
13-
if (directoryExistsSync(path.join(scanPath, "Arduino.app"))) {
14-
result = scanPath;
15-
break;
16-
}
17-
}
18-
return result || "";
19-
}
20-
21-
export function validateArduinoPath(arduinoPath: string, useArduinoCli = false): boolean {
22-
return fileExistsSync(path.join(resolveMacArduinoAppPath(arduinoPath), useArduinoCli ? "arduino-cli" : "/Contents/MacOS/Arduino"));
23-
24-
}
25-
26-
export function findFile(fileName: string, cwd: string): string {
27-
let pathString;
28-
try {
29-
pathString = childProcess.execSync(`find ${cwd} -name ${fileName} -type f`, { encoding: "utf8" }).split("\n");
30-
31-
if (pathString && pathString[0] && fileExistsSync(pathString[0].trim())) {
32-
pathString = path.normalize(pathString[0].trim());
33-
} else {
34-
pathString = null;
35-
}
36-
} catch (ex) {
37-
// Ignore the errors.
38-
}
39-
return pathString;
40-
}
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import * as childProcess from "child_process";
5+
import * as path from "path";
6+
import { directoryExistsSync, fileExistsSync, resolveMacArduinoAppPath } from "../util";
7+
8+
export function resolveArduinoPath(): string {
9+
let result;
10+
11+
const defaultCommonPaths = [path.join(process.env.HOME, "Applications"), "/Applications"];
12+
for (const scanPath of defaultCommonPaths) {
13+
if (directoryExistsSync(path.join(scanPath, "Arduino.app"))) {
14+
result = scanPath;
15+
break;
16+
}
17+
}
18+
return result || "";
19+
}
20+
21+
export function validateArduinoPath(arduinoPath: string, useArduinoCli = false): boolean {
22+
return fileExistsSync(path.join(resolveMacArduinoAppPath(arduinoPath, useArduinoCli), useArduinoCli ? "arduino-cli" : "/Contents/MacOS/Arduino"));
23+
}
24+
25+
export function findFile(fileName: string, cwd: string): string {
26+
let pathString;
27+
try {
28+
pathString = childProcess.execSync(`find ${cwd} -name ${fileName} -type f`, { encoding: "utf8" }).split("\n");
29+
30+
if (pathString && pathString[0] && fileExistsSync(pathString[0].trim())) {
31+
pathString = path.normalize(pathString[0].trim());
32+
} else {
33+
pathString = null;
34+
}
35+
} catch (ex) {
36+
// Ignore the errors.
37+
}
38+
return pathString;
39+
}

src/common/util.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ export function convertToHex(number, width = 0) {
426426
* in case you named Arduino with a version number
427427
* @argument {string} arduinoPath
428428
*/
429-
export function resolveMacArduinoAppPath(arduinoPath: string): string {
430-
if (/Arduino.*\.app/.test(arduinoPath)) {
429+
export function resolveMacArduinoAppPath(arduinoPath: string, useArduinoCli = false): string {
430+
if (useArduinoCli || /Arduino.*\.app/.test(arduinoPath)) {
431431
return arduinoPath;
432432
} else {
433433
return path.join(arduinoPath, "Arduino.app");

0 commit comments

Comments
 (0)