Skip to content

Commit

Permalink
fix(cli): move Android Studio detection to avoid displaying registry …
Browse files Browse the repository at this point in the history
…errors (#2364)
  • Loading branch information
jcesarmobile authored Jan 22, 2020
1 parent 2e24d8d commit 60cd80b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
29 changes: 24 additions & 5 deletions cli/src/android/open.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Config } from '../config';
import { OS } from '../definitions';
import { logError, logInfo } from '../common';
import { existsAsync } from '../util/fs';
import { logError, logInfo, runCommand } from '../common';
import { existsAsync, existsSync } from '../util/fs';
import { resolve } from 'path';

export async function openAndroid(config: Config) {
Expand All @@ -20,10 +20,29 @@ export async function openAndroid(config: Config) {
await opn(dir, { app: 'android studio', wait: false });
break;
case OS.Windows:
if (config.windows.androidStudioPath) {
opn(dir, { app: config.windows.androidStudioPath, wait: false });
let androidStudioPath = config.windows.androidStudioPath;
try {
if (!existsSync(androidStudioPath)) {
let commandResult = await runCommand('REG QUERY "HKEY_LOCAL_MACHINE\\SOFTWARE\\Android Studio" /v Path');
commandResult = commandResult.replace(/(\r\n|\n|\r)/gm, '');
const ix = commandResult.indexOf('REG_SZ');
if (ix > 0) {
androidStudioPath = commandResult.substring(ix + 6).trim() + '\\bin\\studio64.exe';
}
}
} catch (e) {
androidStudioPath = '';
}
if (androidStudioPath) {
opn(dir, { app: androidStudioPath, wait: false });
} else {
logError('Unable to launch Android Studio. Make sure the latest version of Android Studio is installed');
logError('Android Studio not found. Make sure it\'s installed and configure "windowsAndroidStudioPath" ' +
'in your capacitor.config.json to point to the location of studio64.exe, using JavaScript-escaped paths:\n' +

'Example:\n' +
'{\n' +
' "windowsAndroidStudioPath": "C:\\\\Program Files\\\\Android\\\\Android Studio\\\\bin\\\\studio64.exe"\n' +
'}');
}
break;
case OS.Linux:
Expand Down
19 changes: 2 additions & 17 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { accessSync, existsSync, readFileSync } from 'fs';
import { accessSync, readFileSync } from 'fs';
import { basename, join, resolve } from 'path';
import { logFatal, readJSON } from './common';
import { CliConfig, ExternalConfig, OS, PackageJson } from './definitions';
import { execSync } from 'child_process';

let Package: PackageJson;
let ExtConfig: ExternalConfig;
Expand Down Expand Up @@ -197,21 +196,7 @@ export class Config implements CliConfig {
if (this.cli.os !== OS.Windows) {
return;
}
if (this.app.windowsAndroidStudioPath) {
try {
if (!existsSync(this.app.windowsAndroidStudioPath)) {
const buffer = execSync('REG QUERY "HKEY_LOCAL_MACHINE\\SOFTWARE\\Android Studio" /v Path');
const bufferString = buffer.toString('utf-8').replace(/(\r\n|\n|\r)/gm, '');
const ix = bufferString.indexOf('REG_SZ');
if (ix > 0) {
this.app.windowsAndroidStudioPath = bufferString.substring(ix + 6).trim() + '\\bin\\studio64.exe';
}
}
this.windows.androidStudioPath = this.app.windowsAndroidStudioPath;
} catch (e) {
this.windows.androidStudioPath = '';
}
}
this.windows.androidStudioPath = this.app.windowsAndroidStudioPath;
}

private initLinuxConfig() {
Expand Down

0 comments on commit 60cd80b

Please sign in to comment.