From d72287522b8f9a0ed6443faa11767afd012cfe2f Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 18 Aug 2021 12:02:03 +0300 Subject: [PATCH] [tools] Fix Android Studio duplicate detection (#88384) --- .../lib/src/android/android_studio.dart | 20 ++-- .../android/android_studio_test.dart | 96 ++++++++----------- 2 files changed, 46 insertions(+), 70 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_studio.dart b/packages/flutter_tools/lib/src/android/android_studio.dart index 485c77da2b..18f5678c4f 100644 --- a/packages/flutter_tools/lib/src/android/android_studio.dart +++ b/packages/flutter_tools/lib/src/android/android_studio.dart @@ -40,17 +40,13 @@ class AndroidStudio implements Comparable { _init(version: version); } - factory AndroidStudio.fromMacOSBundle(String bundlePath) { - String studioPath = globals.fs.path.join(bundlePath, 'Contents'); - String plistFile = globals.fs.path.join(studioPath, 'Info.plist'); - Map plistValues = globals.plistParser.parseFile(plistFile); - // As AndroidStudio managed by JetBrainsToolbox could have a wrapper pointing to the real Android Studio. - // Check if we've found a JetBrainsToolbox wrapper and deal with it properly. - final String? jetBrainsToolboxAppBundlePath = plistValues['JetBrainsToolboxApp'] as String?; - if (jetBrainsToolboxAppBundlePath != null) { - studioPath = globals.fs.path.join(jetBrainsToolboxAppBundlePath, 'Contents'); - plistFile = globals.fs.path.join(studioPath, 'Info.plist'); - plistValues = globals.plistParser.parseFile(plistFile); + static AndroidStudio? fromMacOSBundle(String bundlePath) { + final String studioPath = globals.fs.path.join(bundlePath, 'Contents'); + final String plistFile = globals.fs.path.join(studioPath, 'Info.plist'); + final Map plistValues = globals.plistParser.parseFile(plistFile); + // If we've found a JetBrainsToolbox wrapper, ignore it. + if (plistValues.containsKey('JetBrainsToolboxApp')) { + return null; } final String versionString = plistValues[PlistParser.kCFBundleShortVersionStringKey] as String; @@ -317,7 +313,7 @@ class AndroidStudio implements Comparable { } return candidatePaths - .map((FileSystemEntity e) => AndroidStudio.fromMacOSBundle(e.path)) + .map((FileSystemEntity e) => AndroidStudio.fromMacOSBundle(e.path)) .whereType() .toList(); } diff --git a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart index cadf688756..aeade943c5 100644 --- a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart @@ -157,6 +157,7 @@ void main() { Platform: () => platform, PlistParser: () => plistUtils, }); + testUsingContext('Can discover Android Studio >=2020.3 location on Mac', () { final String studioInApplicationPlistFolder = globals.fs.path.join( '/', @@ -222,6 +223,43 @@ void main() { PlistParser: () => plistUtils, }); + testUsingContext('Does not discover Android Studio with JetBrainsToolboxApp wrapper', () { + final String applicationPlistFolder = globals.fs.path.join( + '/', + 'Applications', + 'Android Studio.app', + 'Contents', + ); + globals.fs.directory(applicationPlistFolder).createSync(recursive: true); + + final String applicationsPlistFilePath = globals.fs.path.join(applicationPlistFolder, 'Info.plist'); + const Map jetbrainsInfoPlist = { + 'JetBrainsToolboxApp': 'ignored', + }; + plistUtils.fileContents[applicationsPlistFilePath] = jetbrainsInfoPlist; + + final String homeDirectoryPlistFolder = globals.fs.path.join( + globals.fsUtils.homeDirPath, + 'Applications', + 'Android Studio.app', + 'Contents', + ); + globals.fs.directory(homeDirectoryPlistFolder).createSync(recursive: true); + + final String homeDirectoryPlistFilePath = globals.fs.path.join(homeDirectoryPlistFolder, 'Info.plist'); + plistUtils.fileContents[homeDirectoryPlistFilePath] = macStudioInfoPlist2020_3; + + expect(AndroidStudio.allInstalled().length, 1); + }, overrides: { + FileSystem: () => fileSystem, + FileSystemUtils: () => fsUtils, + ProcessManager: () => FakeProcessManager.any(), + // Custom home paths are not supported on macOS nor Windows yet, + // so we force the platform to fake Linux here. + Platform: () => platform, + PlistParser: () => plistUtils, + }); + testUsingContext('Can discover installation from Spotlight query', () { // One in expected location. final String studioInApplication = fileSystem.path.join( @@ -370,64 +408,6 @@ void main() { Platform: () => platform, PlistParser: () => plistUtils, }); - - testUsingContext('extracts custom paths for Android Studio downloaded by JetBrainsToolbox on Mac', () { - final String jetbrainsStudioInApplicationPlistFolder = globals.fs.path.join( - homeMac, - 'Application', - 'JetBrains Toolbox', - 'Android Studio.app', - 'Contents', - ); - globals.fs.directory(jetbrainsStudioInApplicationPlistFolder).createSync(recursive: true); - const Map jetbrainsInfoPlist = { - 'CFBundleLongVersionString': '3.3', - 'CFBundleShortVersionString': '3.3', - 'CFBundleVersion': '3.3', - 'JetBrainsToolboxApp': '$homeMac/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/183.5256920/Android Studio 3.3.app', - }; - final String jetbrainsPlistFilePath = globals.fs.path.join( - jetbrainsStudioInApplicationPlistFolder, - 'Info.plist', - ); - plistUtils.fileContents[jetbrainsPlistFilePath] = jetbrainsInfoPlist; - - final String studioInApplicationPlistFolder = globals.fs.path.join( - globals.fs.path.join(homeMac,'Library','Application Support'), - 'JetBrains', - 'Toolbox', - 'apps', - 'AndroidStudio', - 'ch-0', - '183.5256920', - globals.fs.path.join('Android Studio 3.3.app', 'Contents'), - ); - globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true); - final String studioPlistFilePath = globals.fs.path.join( - studioInApplicationPlistFolder, - 'Info.plist', - ); - plistUtils.fileContents[studioPlistFilePath] = macStudioInfoPlist; - - final AndroidStudio studio = AndroidStudio.fromMacOSBundle( - globals.fs.directory(jetbrainsStudioInApplicationPlistFolder)?.parent?.path, - ); - expect(studio, isNotNull); - expect(studio.pluginsPath, equals(globals.fs.path.join( - homeMac, - 'Library', - 'Application Support', - 'AndroidStudio3.3', - ))); - }, overrides: { - FileSystem: () => fileSystem, - FileSystemUtils: () => fsUtils, - ProcessManager: () => FakeProcessManager.any(), - // Custom home paths are not supported on macOS nor Windows yet, - // so we force the platform to fake Linux here. - Platform: () => platform, - PlistParser: () => plistUtils, - }); }); FileSystem windowsFileSystem;