Unverified Commit 94f71f37 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

[flutter_tools] Refactor Android Studio Detection on Windows (support 4.x and...

[flutter_tools] Refactor Android Studio Detection on Windows (support 4.x and 202x releases detection) (#86624)
parent 8802e325
...@@ -10,6 +10,7 @@ import '../base/version.dart'; ...@@ -10,6 +10,7 @@ import '../base/version.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals_null_migrated.dart' as globals; import '../globals_null_migrated.dart' as globals;
import '../ios/plist_parser.dart'; import '../ios/plist_parser.dart';
import 'android_studio_validator.dart';
// Android Studio layout: // Android Studio layout:
...@@ -373,51 +374,37 @@ class AndroidStudio implements Comparable<AndroidStudio> { ...@@ -373,51 +374,37 @@ class AndroidStudio implements Comparable<AndroidStudio> {
} }
} }
// 4.1 has a different location for AndroidStudio installs on Windows. // Discover Android Studio > 4.1
if (globals.platform.isWindows && globals.platform.environment.containsKey('LOCALAPPDATA')) { if (globals.platform.isWindows && globals.platform.environment.containsKey('LOCALAPPDATA')) {
final File homeDot = globals.fs.file(globals.fs.path.join( final Directory cacheDir = globals.fs.directory(globals.fs.path.join(globals.platform.environment['LOCALAPPDATA']!, 'Google'));
globals.platform.environment['LOCALAPPDATA']!, if (!cacheDir.existsSync()) {
'Google', return studios;
'AndroidStudio4.1',
'.home',
));
if (homeDot.existsSync()) {
final String installPath = homeDot.readAsStringSync();
if (globals.fs.isDirectorySync(installPath)) {
final AndroidStudio studio = AndroidStudio(
installPath,
version: Version(4, 1, 0),
studioAppName: 'Android Studio 4.1',
);
if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) {
studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
studios.add(studio);
}
}
} }
} for (final Directory dir in cacheDir.listSync().whereType<Directory>()) {
final String name = globals.fs.path.basename(dir.path);
// 4.2 has a different location for AndroidStudio installs on Windows. AndroidStudioValidator.idToTitle.forEach((String id, String title) {
if (globals.platform.isWindows && globals.platform.environment.containsKey('LOCALAPPDATA')) { if (name.startsWith(id)) {
final File homeDot = globals.fs.file(globals.fs.path.join( final String version = name.substring(id.length);
globals.platform.environment['LOCALAPPDATA']!, String? installPath;
'Google',
'AndroidStudio4.2', try {
'.home', installPath = globals.fs.file(globals.fs.path.join(dir.path, '.home')).readAsStringSync();
)); } on FileSystemException {
if (homeDot.existsSync()) { // ignored
final String installPath = homeDot.readAsStringSync(); }
if (globals.fs.isDirectorySync(installPath)) { if (installPath != null && globals.fs.isDirectorySync(installPath)) {
final AndroidStudio studio = AndroidStudio( final AndroidStudio studio = AndroidStudio(
installPath, installPath,
version: Version(4, 2, 0), version: Version.parse(version),
studioAppName: 'Android Studio 4.2', studioAppName: title,
); );
if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) { if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) {
studios.removeWhere((AndroidStudio other) => other.directory == studio.directory); studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
studios.add(studio); studios.add(studio);
}
}
} }
} });
} }
} }
......
...@@ -11,6 +11,11 @@ import '../doctor_validator.dart'; ...@@ -11,6 +11,11 @@ import '../doctor_validator.dart';
import '../intellij/intellij.dart'; import '../intellij/intellij.dart';
import 'android_studio.dart'; import 'android_studio.dart';
const String _androidStudioTitle = 'Android Studio';
const String _androidStudioId = 'AndroidStudio';
const String _androidStudioPreviewTitle = 'Android Studio Preview';
const String _androidStudioPreviewId = 'AndroidStudioPreview';
class AndroidStudioValidator extends DoctorValidator { class AndroidStudioValidator extends DoctorValidator {
AndroidStudioValidator(this._studio, { required FileSystem fileSystem }) AndroidStudioValidator(this._studio, { required FileSystem fileSystem })
: _fileSystem = fileSystem, : _fileSystem = fileSystem,
...@@ -19,6 +24,11 @@ class AndroidStudioValidator extends DoctorValidator { ...@@ -19,6 +24,11 @@ class AndroidStudioValidator extends DoctorValidator {
final AndroidStudio _studio; final AndroidStudio _studio;
final FileSystem _fileSystem; final FileSystem _fileSystem;
static const Map<String, String> idToTitle = <String, String>{
_androidStudioId: _androidStudioTitle,
_androidStudioPreviewId: _androidStudioPreviewTitle,
};
static List<DoctorValidator> allValidators(Config config, Platform platform, FileSystem fileSystem, UserMessages userMessages) { static List<DoctorValidator> allValidators(Config config, Platform platform, FileSystem fileSystem, UserMessages userMessages) {
final List<AndroidStudio> studios = AndroidStudio.allInstalled(); final List<AndroidStudio> studios = AndroidStudio.allInstalled();
return <DoctorValidator>[ return <DoctorValidator>[
......
...@@ -402,7 +402,7 @@ void main() { ...@@ -402,7 +402,7 @@ void main() {
final AndroidStudio studio = AndroidStudio.allInstalled().single; final AndroidStudio studio = AndroidStudio.allInstalled().single;
expect(studio.version, Version(4, 1, 0)); expect(studio.version, Version(4, 1, 0));
expect(studio.studioAppName, 'Android Studio 4.1'); expect(studio.studioAppName, 'Android Studio');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => windowsPlatform, Platform: () => windowsPlatform,
FileSystem: () => windowsFileSystem, FileSystem: () => windowsFileSystem,
...@@ -420,7 +420,25 @@ void main() { ...@@ -420,7 +420,25 @@ void main() {
final AndroidStudio studio = AndroidStudio.allInstalled().single; final AndroidStudio studio = AndroidStudio.allInstalled().single;
expect(studio.version, Version(4, 2, 0)); expect(studio.version, Version(4, 2, 0));
expect(studio.studioAppName, 'Android Studio 4.2'); expect(studio.studioAppName, 'Android Studio');
}, overrides: <Type, Generator>{
Platform: () => windowsPlatform,
FileSystem: () => windowsFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('Can discover Android Studio 2020.3 location on Windows', () {
windowsFileSystem.file(r'C:\Users\Dash\AppData\Local\Google\AndroidStudio2020.3\.home')
..createSync(recursive: true)
..writeAsStringSync(r'C:\Program Files\AndroidStudio');
windowsFileSystem
.directory(r'C:\Program Files\AndroidStudio')
.createSync(recursive: true);
final AndroidStudio studio = AndroidStudio.allInstalled().single;
expect(studio.version, Version(2020, 3, 0));
expect(studio.studioAppName, 'Android Studio');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => windowsPlatform, Platform: () => windowsPlatform,
FileSystem: () => windowsFileSystem, FileSystem: () => windowsFileSystem,
...@@ -463,6 +481,24 @@ void main() { ...@@ -463,6 +481,24 @@ void main() {
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
testUsingContext('Does not discover Android Studio 2020.3 location on Windows if LOCALAPPDATA is null', () {
windowsFileSystem.file(r'C:\Users\Dash\AppData\Local\Google\AndroidStudio2020.3\.home')
..createSync(recursive: true)
..writeAsStringSync(r'C:\Program Files\AndroidStudio');
windowsFileSystem
.directory(r'C:\Program Files\AndroidStudio')
.createSync(recursive: true);
expect(AndroidStudio.allInstalled(), isEmpty);
}, overrides: <Type, Generator>{
Platform: () => FakePlatform(
operatingSystem: 'windows',
environment: <String, String>{}, // Does not include LOCALAPPDATA
),
FileSystem: () => windowsFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
group('Installation detection on Linux', () { group('Installation detection on Linux', () {
FileSystemUtils fsUtils; FileSystemUtils fsUtils;
......
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