diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index 8769d1f969..d79fd396d6 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -209,6 +209,7 @@ Future runInContext( ), uwptool: UwpTool( artifacts: globals.artifacts, + fileSystem: globals.fs, logger: globals.logger, processManager: globals.processManager, ), diff --git a/packages/flutter_tools/lib/src/windows/uwptool.dart b/packages/flutter_tools/lib/src/windows/uwptool.dart index e7522365b1..b9ed8a41cb 100644 --- a/packages/flutter_tools/lib/src/windows/uwptool.dart +++ b/packages/flutter_tools/lib/src/windows/uwptool.dart @@ -10,6 +10,7 @@ import 'package:meta/meta.dart'; import 'package:process/process.dart'; import '../artifacts.dart'; +import '../base/file_system.dart'; import '../base/logger.dart'; import '../base/process.dart'; @@ -22,13 +23,16 @@ import '../base/process.dart'; class UwpTool { UwpTool({ @required Artifacts artifacts, + @required FileSystem fileSystem, @required Logger logger, @required ProcessManager processManager, }) : _artifacts = artifacts, + _fileSystem = fileSystem, _logger = logger, _processUtils = ProcessUtils(processManager: processManager, logger: logger); final Artifacts _artifacts; + final FileSystem _fileSystem; final Logger _logger; final ProcessUtils _processUtils; @@ -44,44 +48,74 @@ class UwpTool { _logger.printError('Failed to list installed UWP apps: ${result.stderr}'); return []; } - final List appIds = []; + final List packageFamilies = []; for (final String line in result.stdout.toString().split('\n')) { - final String appId = line.trim(); - if (appId.isNotEmpty) { - appIds.add(appId); + final String packageFamily = line.trim(); + if (packageFamily.isNotEmpty) { + packageFamilies.add(packageFamily); } } - return appIds; + return packageFamilies; } - /// Returns the app ID for the specified package ID. + /// Returns the package family name for the specified package name. /// - /// If no installed application on the system matches the specified GUID, - /// returns null. - Future getAppIdFromPackageId(String packageId) async { - for (final String appId in await listApps()) { - if (appId.startsWith(packageId)) { - return appId; + /// If no installed application on the system matches the specified package + /// name, returns null. + Future getPackageFamilyName(String packageName) async { + for (final String packageFamily in await listApps()) { + if (packageFamily.startsWith(packageName)) { + return packageFamily; } } return null; } - /// Launches the app with the specified app ID. + /// Launches the app with the specified package family name. /// /// On success, returns the process ID of the launched app, otherwise null. - Future launchApp(String appId, List args) async { + Future launchApp(String packageFamily, List args) async { final List launchCommand = [ _binaryPath, 'launch', - appId + packageFamily ] + args; final RunResult result = await _processUtils.run(launchCommand); if (result.exitCode != 0) { - _logger.printError('Failed to launch app $appId: ${result.stderr}'); + _logger.printError('Failed to launch app $packageFamily: ${result.stderr}'); return null; } // Read the process ID from stdout. return int.tryParse(result.stdout.toString().trim()); } + + /// Installs the app with the specified build directory. + /// + /// Returns `true` on success. + Future installApp(String buildDirectory) async { + final List launchCommand = [ + 'powershell.exe', + _fileSystem.path.join(buildDirectory, 'install.ps1'), + ]; + final RunResult result = await _processUtils.run(launchCommand); + if (result.exitCode != 0) { + _logger.printError(result.stdout.toString()); + _logger.printError(result.stderr.toString()); + } + return result.exitCode == 0; + } + + Future uninstallApp(String packageFamily) async { + final List launchCommand = [ + _binaryPath, + 'uninstall', + packageFamily + ]; + final RunResult result = await _processUtils.run(launchCommand); + if (result.exitCode != 0) { + _logger.printError('Failed to uninstall $packageFamily'); + return false; + } + return true; + } } diff --git a/packages/flutter_tools/lib/src/windows/windows_device.dart b/packages/flutter_tools/lib/src/windows/windows_device.dart index b6577397ed..3bbe1525ad 100644 --- a/packages/flutter_tools/lib/src/windows/windows_device.dart +++ b/packages/flutter_tools/lib/src/windows/windows_device.dart @@ -11,7 +11,6 @@ import 'package:process/process.dart'; import '../application_package.dart'; import '../base/file_system.dart'; -import '../base/io.dart'; import '../base/logger.dart'; import '../base/os.dart'; import '../base/utils.dart'; @@ -144,19 +143,16 @@ class WindowsUWPDevice extends Device { } final String config = toTitleCase(getNameForBuildMode(_buildMode ?? BuildMode.debug)); final String generated = '${binaryName}_${packageVersion}_${config}_Test'; - final ProcessResult result = await _processManager.run([ - 'powershell.exe', - _fileSystem.path.join('build', 'winuwp', 'runner_uwp', 'AppPackages', binaryName, generated, 'install.ps1'), - ]); - if (result.exitCode != 0) { - _logger.printError(result.stdout.toString()); - _logger.printError(result.stderr.toString()); - } - return result.exitCode == 0; + final String buildDirectory = _fileSystem.path.join( + 'build', 'winuwp', 'runner_uwp', 'AppPackages', binaryName, generated); + return _uwptool.installApp(buildDirectory); } @override - Future isAppInstalled(covariant ApplicationPackage app, {String userIdentifier}) async => false; + Future isAppInstalled(covariant ApplicationPackage app, {String userIdentifier}) async { + final String packageName = app.id; + return await _uwptool.getPackageFamilyName(packageName) != null; + } @override Future isLatestBuildInstalled(covariant ApplicationPackage app) async => false; @@ -193,16 +189,16 @@ class WindowsUWPDevice extends Device { return LaunchResult.failed(); } - final String guid = package.id; - if (guid == null) { + final String packageName = package.id; + if (packageName == null) { _logger.printError('Could not find PACKAGE_GUID in ${package.project.runnerCmakeFile.path}'); return LaunchResult.failed(); } - final String appId = await _uwptool.getAppIdFromPackageId(guid); + final String packageFamily = await _uwptool.getPackageFamilyName(packageName); if (debuggingOptions.buildInfo.mode.isRelease) { - _processId = await _uwptool.launchApp(appId, []); + _processId = await _uwptool.launchApp(packageFamily, []); return _processId != null ? LaunchResult.succeeded() : LaunchResult.failed(); } @@ -210,7 +206,7 @@ class WindowsUWPDevice extends Device { if (_logger.terminal.stdinHasTerminal) { await _logger.terminal.promptForCharInput(['Y', 'y'], logger: _logger, prompt: 'To continue start an admin cmd prompt and run the following command:\n' - ' checknetisolation loopbackexempt -is -n=$appId\n' + ' checknetisolation loopbackexempt -is -n=$packageFamily\n' 'Press "Y/y" once this is complete.' ); } @@ -238,7 +234,7 @@ class WindowsUWPDevice extends Device { if (debuggingOptions.purgePersistentCache) '--purge-persistent-cache', if (platformArgs['trace-startup'] as bool ?? false) '--trace-startup', ]; - _processId = await _uwptool.launchApp(appId, args); + _processId = await _uwptool.launchApp(packageFamily, args); if (_processId == null) { return LaunchResult.failed(); } @@ -255,7 +251,17 @@ class WindowsUWPDevice extends Device { @override Future uninstallApp(covariant BuildableUwpApp app, {String userIdentifier}) async { - return false; + final String packageName = app.id; + if (packageName == null) { + _logger.printError('Could not find PACKAGE_GUID in ${app.project.runnerCmakeFile.path}'); + return false; + } + final String packageFamily = await _uwptool.getPackageFamilyName(packageName); + if (packageFamily == null) { + // App is not installed. + return true; + } + return _uwptool.uninstallApp(packageFamily); } @override diff --git a/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart b/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart index 41637a8c50..691f177c6d 100644 --- a/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart +++ b/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart @@ -43,13 +43,14 @@ void main() { }); testWithoutContext('WindowsUwpDevice defaults', () async { - final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(); + final FakeUwpTool uwptool = FakeUwpTool(); + final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(uwptool: uwptool); final FakeBuildableUwpApp package = FakeBuildableUwpApp(); expect(await windowsDevice.targetPlatform, TargetPlatform.windows_uwp_x64); expect(windowsDevice.name, 'Windows (UWP)'); expect(await windowsDevice.installApp(package), true); - expect(await windowsDevice.uninstallApp(package), false); + expect(await windowsDevice.uninstallApp(package), true); expect(await windowsDevice.isLatestBuildInstalled(package), false); expect(await windowsDevice.isAppInstalled(package), false); expect(windowsDevice.category, Category.desktop); @@ -170,15 +171,8 @@ void main() { Cache.flutterRoot = ''; final FakeUwpTool uwptool = FakeUwpTool(); final FileSystem fileSystem = MemoryFileSystem.test(); - final FakeProcessManager processManager = FakeProcessManager.list([ - const FakeCommand(command: [ - 'powershell.exe', - 'build/winuwp/runner_uwp/AppPackages/testapp/testapp_1.2.3.4_Debug_Test/install.ps1', - ]), - ]); final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice( fileSystem: fileSystem, - processManager: processManager, uwptool: uwptool, ); final FakeBuildableUwpApp package = FakeBuildableUwpApp(); @@ -191,7 +185,7 @@ void main() { ); expect(result.started, true); - expect(uwptool.launchRequests.single.appId, 'PACKAGE-ID_asdfghjkl'); + expect(uwptool.launchRequests.single.packageFamily, 'PACKAGE-ID_publisher'); expect(uwptool.launchRequests.single.args, [ '--observatory-port=12345', '--disable-service-auth-codes', @@ -205,15 +199,8 @@ void main() { Cache.flutterRoot = ''; final FakeUwpTool uwptool = FakeUwpTool(); final FileSystem fileSystem = MemoryFileSystem.test(); - final FakeProcessManager processManager = FakeProcessManager.list([ - const FakeCommand(command: [ - 'powershell.exe', - 'build/winuwp/runner_uwp/AppPackages/testapp/testapp_1.2.3.4_Release_Test/install.ps1', - ]), - ]); final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice( fileSystem: fileSystem, - processManager: processManager, uwptool: uwptool, ); final FakeBuildableUwpApp package = FakeBuildableUwpApp(); @@ -226,7 +213,7 @@ void main() { ); expect(result.started, true); - expect(uwptool.launchRequests.single.appId, 'PACKAGE-ID_asdfghjkl'); + expect(uwptool.launchRequests.single.packageFamily, 'PACKAGE-ID_publisher'); expect(uwptool.launchRequests.single.args, []); }); } @@ -279,44 +266,71 @@ class FakeBuildableUwpApp extends Fake implements BuildableUwpApp { String get name => 'testapp'; @override String get projectVersion => '1.2.3.4'; + + // Test helper to get the expected package family. + static const String packageFamily = 'PACKAGE-ID_publisher'; } class FakeUwpTool implements UwpTool { + bool isInstalled = false; + final List<_GetPackageFamilyRequest> getPackageFamilyRequests = <_GetPackageFamilyRequest>[]; final List<_LaunchRequest> launchRequests = <_LaunchRequest>[]; - final List<_LookupAppIdRequest> lookupAppIdRequests = <_LookupAppIdRequest>[]; + final List<_InstallRequest> installRequests = <_InstallRequest>[]; + final List<_UninstallRequest> uninstallRequests = <_UninstallRequest>[]; @override Future> listApps() async { - return [ - 'fb89bf4f-55db-4bcd-8f0b-d8139953e08b', - '3e556a66-cb7f-4335-9569-35d5f5e37219', - 'dfe5d409-a524-4635-b2f8-78a5e9551994', - '51e8a06b-02e8-4f76-9131-f20ce114fc34', - ]; + return isInstalled ? [FakeBuildableUwpApp.packageFamily] : []; } @override - Future getAppIdFromPackageId(String packageId) async { - lookupAppIdRequests.add(_LookupAppIdRequest(packageId)); - return '${packageId}_asdfghjkl'; + Future getPackageFamilyName(String packageName) async { + getPackageFamilyRequests.add(_GetPackageFamilyRequest(packageName)); + return isInstalled ? FakeBuildableUwpApp.packageFamily : null; } @override - Future launchApp(String appId, List args) async { - launchRequests.add(_LaunchRequest(appId, args)); + Future launchApp(String packageFamily, List args) async { + launchRequests.add(_LaunchRequest(packageFamily, args)); return 42; } + + @override + Future installApp(String buildDirectory) async { + installRequests.add(_InstallRequest(buildDirectory)); + isInstalled = true; + return true; + } + + @override + Future uninstallApp(String packageFamily) async { + uninstallRequests.add(_UninstallRequest(packageFamily)); + isInstalled = false; + return true; + } } -class _LookupAppIdRequest { - const _LookupAppIdRequest(this.packageId); +class _GetPackageFamilyRequest { + const _GetPackageFamilyRequest(this.packageId); final String packageId; } class _LaunchRequest { - const _LaunchRequest(this.appId, this.args); + const _LaunchRequest(this.packageFamily, this.args); - final String appId; + final String packageFamily; final List args; } + +class _InstallRequest { + const _InstallRequest(this.buildDirectory); + + final String buildDirectory; +} + +class _UninstallRequest { + const _UninstallRequest(this.packageFamily); + + final String packageFamily; +}