Commit 7f9a53b1 authored by Jakob Andersen's avatar Jakob Andersen Committed by GitHub

Relax Android Studio version check. (#8389)

It's not just $HOME/.AndroidStudio2.2, it might also be
.AndroidStudioPreview2.3, or .AndroidStudioFooBar1.7, or whatever.

Made the Version parser less throw-happy, and relaxed the directory name
checks to allow for the above.

Fixes #8353.
parent 6a9ea16e
...@@ -28,6 +28,9 @@ AndroidStudio get androidStudio => ...@@ -28,6 +28,9 @@ AndroidStudio get androidStudio =>
final Version minGradleVersion = new Version(2, 14, 1); final Version minGradleVersion = new Version(2, 14, 1);
final RegExp _dotHomeStudioVersionMatcher =
new RegExp(r'^\.AndroidStudio([^\d]*)([\d.]+)');
/// Locate Gradle. /// Locate Gradle.
String get gradleExecutable { String get gradleExecutable {
// See if the user has explicitly configured gradle-dir. // See if the user has explicitly configured gradle-dir.
...@@ -66,15 +69,22 @@ class AndroidStudio implements Comparable<AndroidStudio> { ...@@ -66,15 +69,22 @@ class AndroidStudio implements Comparable<AndroidStudio> {
} }
factory AndroidStudio.fromHomeDot(Directory homeDotDir) { factory AndroidStudio.fromHomeDot(Directory homeDotDir) {
Version version = new Version.parse( Match versionMatch =
homeDotDir.basename.substring('.AndroidStudio'.length)); _dotHomeStudioVersionMatcher.firstMatch(homeDotDir.basename);
if (versionMatch?.groupCount != 2) {
return null;
}
Version version = new Version.parse(versionMatch[2]);
if (version == null) {
return null;
}
String installPath; String installPath;
try { try {
installPath = fs installPath = fs
.file(fs.path.join(homeDotDir.path, 'system', '.home')) .file(fs.path.join(homeDotDir.path, 'system', '.home'))
.readAsStringSync(); .readAsStringSync();
} catch (e) { } catch (e) {
// ignored // ignored, installPath will be null, which is handled below
} }
if (installPath != null && fs.isDirectorySync(installPath)) { if (installPath != null && fs.isDirectorySync(installPath)) {
return new AndroidStudio(installPath, version: version); return new AndroidStudio(installPath, version: version);
...@@ -176,7 +186,7 @@ class AndroidStudio implements Comparable<AndroidStudio> { ...@@ -176,7 +186,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {
}); });
} }
// Read all $HOME/AndroidStudio*/system/.home files. There may be several // Read all $HOME/.AndroidStudio*/system/.home files. There may be several
// pointing to the same installation, so we grab only the latest one. // pointing to the same installation, so we grab only the latest one.
for (FileSystemEntity entity in fs.directory(homeDirPath).listSync()) { for (FileSystemEntity entity in fs.directory(homeDirPath).listSync()) {
if (entity is Directory && entity.basename.startsWith('.AndroidStudio')) { if (entity is Directory && entity.basename.startsWith('.AndroidStudio')) {
...@@ -231,7 +241,8 @@ class AndroidStudio implements Comparable<AndroidStudio> { ...@@ -231,7 +241,8 @@ class AndroidStudio implements Comparable<AndroidStudio> {
for (FileSystemEntity entry in gradlePaths.where((FileSystemEntity e) => for (FileSystemEntity entry in gradlePaths.where((FileSystemEntity e) =>
e.basename.startsWith('gradle-') && e is Directory)) { e.basename.startsWith('gradle-') && e is Directory)) {
Version version = Version version =
new Version.parse(entry.basename.substring('gradle-'.length)); new Version.parse(entry.basename.substring('gradle-'.length)) ??
Version.unknown;
if (latestGradleVersion == null || version > latestGradleVersion) { if (latestGradleVersion == null || version > latestGradleVersion) {
latestGradleVersion = version; latestGradleVersion = version;
if (version >= minGradleVersion) { if (version >= minGradleVersion) {
......
...@@ -100,22 +100,23 @@ class ConfiguredGradleValidator extends DoctorValidator { ...@@ -100,22 +100,23 @@ class ConfiguredGradleValidator extends DoctorValidator {
gradleExecutable = fs.path.join( gradleExecutable = fs.path.join(
cfgGradleDir, 'bin', platform.isWindows ? 'gradle.bat' : 'gradle'); cfgGradleDir, 'bin', platform.isWindows ? 'gradle.bat' : 'gradle');
} }
String version; String versionString;
if (processManager.canRun(gradleExecutable)) { if (processManager.canRun(gradleExecutable)) {
type = ValidationType.partial; type = ValidationType.partial;
ProcessResult result = ProcessResult result =
processManager.runSync(<String>[gradleExecutable, '--version']); processManager.runSync(<String>[gradleExecutable, '--version']);
if (result.exitCode == 0) { if (result.exitCode == 0) {
version = result.stdout versionString = result.stdout
.toString() .toString()
.split('\n') .split('\n')
.firstWhere((String s) => s.startsWith('Gradle ')) .firstWhere((String s) => s.startsWith('Gradle '))
.substring('Gradle '.length); .substring('Gradle '.length);
if (new Version.parse(version) >= minGradleVersion) { Version version = new Version.parse(versionString) ?? Version.unknown;
if (version >= minGradleVersion) {
type = ValidationType.installed; type = ValidationType.installed;
} else { } else {
messages.add(new ValidationMessage.error( messages.add(new ValidationMessage.error(
'Gradle version $minGradleVersion required. Found version $version.')); 'Gradle version $minGradleVersion required. Found version $versionString.'));
} }
} else { } else {
messages messages
...@@ -128,6 +129,6 @@ class ConfiguredGradleValidator extends DoctorValidator { ...@@ -128,6 +129,6 @@ class ConfiguredGradleValidator extends DoctorValidator {
messages.add(new ValidationMessage( messages.add(new ValidationMessage(
'Consider removing the gradle-dir setting to use Gradle from Android Studio.')); 'Consider removing the gradle-dir setting to use Gradle from Android Studio.'));
return new ValidationResult(type, messages, statusInfo: version); return new ValidationResult(type, messages, statusInfo: versionString);
} }
} }
...@@ -45,7 +45,7 @@ class Version implements Comparable<Version> { ...@@ -45,7 +45,7 @@ class Version implements Comparable<Version> {
factory Version.parse(String text) { factory Version.parse(String text) {
Match match = versionPattern.firstMatch(text); Match match = versionPattern.firstMatch(text);
if (match == null) { if (match == null) {
throw new FormatException('Could not parse "$text".'); return null;
} }
try { try {
...@@ -54,7 +54,7 @@ class Version implements Comparable<Version> { ...@@ -54,7 +54,7 @@ class Version implements Comparable<Version> {
int patch = int.parse(match[5] ?? '0'); int patch = int.parse(match[5] ?? '0');
return new Version._(major, minor, patch, text); return new Version._(major, minor, patch, text);
} on FormatException { } on FormatException {
throw new FormatException('Could not parse "$text".'); return null;
} }
} }
......
...@@ -111,6 +111,8 @@ baz=qux ...@@ -111,6 +111,8 @@ baz=qux
Version v5 = new Version(1, 2, 0, text: 'foo'); Version v5 = new Version(1, 2, 0, text: 'foo');
expect(v5, equals(v2)); expect(v5, equals(v2));
expect(new Version.parse('Preview2.2'), isNull);
}); });
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment