Skip to content

Commit

Permalink
feat: custom android-studio path to support nixos using new env.NATIV…
Browse files Browse the repository at this point in the history
…ESCRIPT_ANDROID_STUDIO_PATH (#5816)
  • Loading branch information
srghma authored Dec 27, 2024
1 parent 86d9f29 commit 661b653
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions lib/key-commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ export class ShiftA implements IKeyCommand {
private $projectData: IProjectData
) {}

getAndroidStudioPath(): string | null {
const os = currentPlatform();

if (os === "darwin") {
const possibleStudioPaths = [
"/Applications/Android Studio.app",
`${process.env.HOME}/Applications/Android Studio.app`,
];

return possibleStudioPaths.find((p) => fs.existsSync(p)) || null;
} else if (os === "win32") {
const studioPath = path.join(
"C:",
"Program Files",
"Android",
"Android Studio",
"bin",
"studio64.exe"
);
return fs.existsSync(studioPath) ? studioPath : null;
} else if (os === "linux") {
const studioPath = "/usr/local/android-studio/bin/studio.sh";
return fs.existsSync(studioPath) ? studioPath : null;
}

return null;
}

async execute(): Promise<void> {
this.$liveSyncCommandHelper.validatePlatform(this.platform);
this.$projectData.initializeProjectData();
Expand All @@ -65,53 +93,32 @@ export class ShiftA implements IKeyCommand {
}
}

const os = currentPlatform();
let studioPath = null;

if (os === "darwin") {
const possibleStudioPaths = [
"/Applications/Android Studio.app",
`${process.env.HOME}/Applications/Android Studio.app`,
];
studioPath = process.env.NATIVESCRIPT_ANDROID_STUDIO_PATH;

const studioPath = possibleStudioPaths.find((p) => {
this.$logger.trace(`Checking for Android Studio at ${p}`);
return fs.existsSync(p);
});
if (!studioPath) {
studioPath = this.getAndroidStudioPath();

if (!studioPath) {
this.$logger.error(
"Android Studio is not installed, or not in a standard location."
"Android Studio is not installed, or is not in a standard location. Use NATIVESCRIPT_ANDROID_STUDIO_PATH."
);
return;
}
}

const os = currentPlatform();
if (os === "darwin") {
this.$childProcess.exec(`open -a "${studioPath}" ${androidDir}`);
} else if (os === "win32") {
const studioPath = path.join(
"C:",
"Program Files",
"Android",
"Android Studio",
"bin",
"studio64.exe"
);
if (!fs.existsSync(studioPath)) {
this.$logger.error("Android Studio is not installed");
return;
}

const child = this.$childProcess.spawn(studioPath, [androidDir], {
detached: true,
stdio: "ignore",
});
child.unref();
} else if (os === "linux") {
if (!fs.existsSync(`/usr/local/android-studio/bin/studio.sh`)) {
this.$logger.error("Android Studio is not installed");
return;
}
this.$childProcess.exec(
`/usr/local/android-studio/bin/studio.sh ${androidDir}`
);
this.$childProcess.exec(`${studioPath} ${androidDir}`);
}
}
}
Expand Down

0 comments on commit 661b653

Please sign in to comment.