[flutter_tools] support flutter run -d winuwp (#82373)

Allow flutter run to work end-to-end with a UWP device.

Uses win32/ffi for the actual launch of the application, injected via
the native API class. This is structured to avoid a g3 dependency.

Install and amuid require powershell scripts for now.

Actually connecting to the observatory requires running a command in an
elevated prompt. Instructions are presented to the user if a terminal is
attached.

This is a rebased version of https://github.com/flutter/flutter/pull/79684
by @jonahwilliams, updated to remove `NativeApi` and replace is with calls
to `uwptool`.

Part of https://github.com/flutter/flutter/issues/82085
This commit is contained in:
Chris Bracken
2021-05-12 20:45:30 -07:00
committed by GitHub
parent d6fc44ba67
commit 53c2f7086b
13 changed files with 642 additions and 34 deletions

View File

@@ -0,0 +1,140 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/project.dart';
import '../../src/common.dart';
const String kExampleManifest = r'''
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
IgnorableNamespaces="uap mp">
<Identity Name="@PACKAGE_GUID@" Publisher="CN=CMake Test Cert" Version="2.3.1.4" />
<mp:PhoneIdentity PhoneProductId="@PACKAGE_GUID@" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
<Properties>
<DisplayName>@SHORT_NAME@</DisplayName>
<PublisherDisplayName>CMake Test Cert</PublisherDisplayName>
<Logo>Assets/StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.65535.65535" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="@SHORT_NAME@.App">
<uap:VisualElements
DisplayName="@SHORT_NAME@"
Description="@SHORT_NAME@"
BackgroundColor="#336699"
Square150x150Logo="Assets/Square150x150Logo.png"
Square44x44Logo="Assets/Square44x44Logo.png"
>
<uap:SplashScreen Image="Assets/SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClientServer"/>
<Capability Name="internetClient"/>
<Capability Name="privateNetworkClientServer"/>
<Capability Name="codeGeneration"/></Capabilities>
</Package>
''';
void main() {
testWithoutContext('Project can parse the app version from the appx manifest', () {
final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('winuwp/runner_uwp/appxmanifest.in')
..createSync(recursive: true)
..writeAsStringSync(kExampleManifest);
final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
expect(flutterProject.windowsUwp.packageVersion, '2.3.1.4');
});
testWithoutContext('Project returns null if appx manifest does not exist', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
expect(flutterProject.windowsUwp.packageVersion, null);
});
testWithoutContext('Project throws a tool exit if appxmanifest is not valid xml', () {
final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('winuwp/runner_uwp/appxmanifest.in')
..createSync(recursive: true)
..writeAsStringSync('[');
final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
expect(() => flutterProject.windowsUwp.packageVersion, throwsToolExit());
});
testWithoutContext('Can parse the PACKAGE_GUID from the Cmake manifest', () {
final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('winuwp/runner_uwp/CMakeLists.txt')
..createSync(recursive: true)
..writeAsStringSync(r'''
cmake_minimum_required (VERSION 3.8)
set(CMAKE_SYSTEM_NAME WindowsStore)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
include(CMakePrintHelpers)
project ("TestBedUWP")
set(APP_MANIFEST_NAME Package.appxmanifest)
set(APP_MANIFEST_TARGET_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${APP_MANIFEST_NAME})
set(SHORT_NAME ${BINARY_NAME})
set(PACKAGE_GUID "F941A77F-8AE1-4E3E-9611-68FBD3C62AE8")
''');
final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
expect(flutterProject.windowsUwp.packageGuid, 'F941A77F-8AE1-4E3E-9611-68FBD3C62AE8');
});
testWithoutContext('Returns null if the PACKAGE_GUID cannot be found in the Cmake file', () {
final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('winuwp/runner_uwp/CMakeLists.txt')
..createSync(recursive: true)
..writeAsStringSync(r'''
cmake_minimum_required (VERSION 3.8)
set(CMAKE_SYSTEM_NAME WindowsStore)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
include(CMakePrintHelpers)
project ("TestBedUWP")
set(APP_MANIFEST_NAME Package.appxmanifest)
set(APP_MANIFEST_TARGET_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${APP_MANIFEST_NAME})
set(SHORT_NAME ${BINARY_NAME})
''');
final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
expect(flutterProject.windowsUwp.packageGuid, null);
});
}

View File

@@ -9,10 +9,12 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/windows/application_package.dart';
import 'package:flutter_tools/src/windows/uwptool.dart';
import 'package:flutter_tools/src/windows/windows_device.dart';
import 'package:flutter_tools/src/windows/windows_workflow.dart';
import 'package:test/fake.dart';
@@ -42,14 +44,14 @@ void main() {
testWithoutContext('WindowsUwpDevice defaults', () async {
final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice();
final PrebuiltWindowsApp windowsApp = PrebuiltWindowsApp(executable: 'foo');
final FakeBuildableUwpApp package = FakeBuildableUwpApp();
expect(await windowsDevice.targetPlatform, TargetPlatform.windows_uwp_x64);
expect(windowsDevice.name, 'Windows (UWP)');
expect(await windowsDevice.installApp(windowsApp), true);
expect(await windowsDevice.uninstallApp(windowsApp), true);
expect(await windowsDevice.isLatestBuildInstalled(windowsApp), true);
expect(await windowsDevice.isAppInstalled(windowsApp), true);
expect(await windowsDevice.installApp(package), true);
expect(await windowsDevice.uninstallApp(package), false);
expect(await windowsDevice.isLatestBuildInstalled(package), false);
expect(await windowsDevice.isAppInstalled(package), false);
expect(windowsDevice.category, Category.desktop);
expect(windowsDevice.supportsRuntimeMode(BuildMode.debug), true);
@@ -69,6 +71,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
uwptool: FakeUwpTool(),
).devices, <Device>[]);
});
@@ -83,6 +86,7 @@ void main() {
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
featureFlags: TestFeatureFlags(isWindowsEnabled: true),
uwptool: FakeUwpTool(),
).devices, hasLength(1));
});
@@ -98,6 +102,7 @@ void main() {
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
featureFlags: featureFlags,
uwptool: FakeUwpTool(),
).devices, hasLength(2));
});
@@ -112,6 +117,7 @@ void main() {
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
featureFlags: TestFeatureFlags(isWindowsEnabled: true),
uwptool: FakeUwpTool(),
);
// Timeout ignored.
final List<Device> devices = await windowsDevices.discoverDevices(timeout: const Duration(seconds: 10));
@@ -159,6 +165,70 @@ void main() {
expect(windowsDevice.executablePathForDevice(fakeApp, BuildMode.profile), 'profile/executable');
expect(windowsDevice.executablePathForDevice(fakeApp, BuildMode.release), 'release/executable');
});
testWithoutContext('WinUWPDevice can launch application', () async {
Cache.flutterRoot = '';
final FakeUwpTool uwptool = FakeUwpTool();
final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[
'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();
final LaunchResult result = await windowsDevice.startApp(
package,
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
prebuiltApplication: true,
platformArgs: <String, Object>{},
);
expect(result.started, true);
expect(uwptool.launchRequests.single.appId, 'PACKAGE-ID_asdfghjkl');
expect(uwptool.launchRequests.single.args, <String>[
'--observatory-port=12345',
'--disable-service-auth-codes',
'--enable-dart-profiling',
'--enable-checked-mode',
'--verify-entry-points',
]);
});
testWithoutContext('WinUWPDevice can launch application in release mode', () async {
Cache.flutterRoot = '';
final FakeUwpTool uwptool = FakeUwpTool();
final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[
'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();
final LaunchResult result = await windowsDevice.startApp(
package,
debuggingOptions: DebuggingOptions.enabled(BuildInfo.release),
prebuiltApplication: true,
platformArgs: <String, Object>{},
);
expect(result.started, true);
expect(uwptool.launchRequests.single.appId, 'PACKAGE-ID_asdfghjkl');
expect(uwptool.launchRequests.single.args, <String>[]);
});
}
FlutterProject setUpFlutterProject(Directory directory) {
@@ -186,12 +256,14 @@ WindowsUWPDevice setUpWindowsUwpDevice({
FileSystem fileSystem,
Logger logger,
ProcessManager processManager,
UwpTool uwptool,
}) {
return WindowsUWPDevice(
fileSystem: fileSystem ?? MemoryFileSystem.test(),
logger: logger ?? BufferLogger.test(),
processManager: processManager ?? FakeProcessManager.any(),
operatingSystemUtils: FakeOperatingSystemUtils(),
uwptool: uwptool ?? FakeUwpTool(),
);
}
@@ -199,3 +271,52 @@ class FakeWindowsApp extends Fake implements WindowsApp {
@override
String executable(BuildMode buildMode) => '${buildMode.name}/executable';
}
class FakeBuildableUwpApp extends Fake implements BuildableUwpApp {
@override
String get id => 'PACKAGE-ID';
@override
String get name => 'testapp';
@override
String get projectVersion => '1.2.3.4';
}
class FakeUwpTool implements UwpTool {
final List<_LaunchRequest> launchRequests = <_LaunchRequest>[];
final List<_LookupAppIdRequest> lookupAppIdRequests = <_LookupAppIdRequest>[];
@override
Future<List<String>> listApps() async {
return <String>[
'fb89bf4f-55db-4bcd-8f0b-d8139953e08b',
'3e556a66-cb7f-4335-9569-35d5f5e37219',
'dfe5d409-a524-4635-b2f8-78a5e9551994',
'51e8a06b-02e8-4f76-9131-f20ce114fc34',
];
}
@override
Future<String> getAppIdFromPackageId(String packageId) async {
lookupAppIdRequests.add(_LookupAppIdRequest(packageId));
return '${packageId}_asdfghjkl';
}
@override
Future<int> launchApp(String appId, List<String> args) async {
launchRequests.add(_LaunchRequest(appId, args));
return 42;
}
}
class _LookupAppIdRequest {
const _LookupAppIdRequest(this.packageId);
final String packageId;
}
class _LaunchRequest {
const _LaunchRequest(this.appId, this.args);
final String appId;
final List<String> args;
}