Skip to content

Commit 0ecb495

Browse files
bkonyiLongCatIsLooong
authored andcommitted
[flutter_tools] Share FlutterProject metadata with tool extensions via ExtensionFlutterProject (flutter#193749)
Adds `ExtensionFlutterProject` in `package:flutter_tools_core` and `FlutterProject.toExtensionProject()` to pass structured project and manifest metadata to tool extensions over RPC in `DeviceService.isSupportedForProject`. Part of flutter#190755 Part of flutter#190750 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent fb27e18 commit 0ecb495

9 files changed

Lines changed: 625 additions & 26 deletions

File tree

‎packages/flutter_tools/lib/src/experimental/extension_device_manager.dart‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ final class ExtensionDeviceClient extends DeviceService {
4444
}
4545

4646
@override
47-
Future<bool> isSupportedForProject({required String deviceId, required Uri projectRoot}) async {
47+
Future<bool> isSupportedForProject({
48+
required String deviceId,
49+
required ExtensionFlutterProject project,
50+
}) async {
4851
_logger.printTrace(
4952
'ExtensionDeviceClient checking project support for "$deviceId" via RPC '
5053
'("${DeviceService.isSupportedForProjectMethod}")...',
@@ -55,7 +58,7 @@ final class ExtensionDeviceClient extends DeviceService {
5558
DeviceService.isSupportedForProjectMethod,
5659
params: <String, Object?>{
5760
DeviceService.deviceIdParam: deviceId,
58-
DeviceService.projectRootParam: projectRoot.toString(),
61+
DeviceService.projectParam: project.toMap(),
5962
},
6063
)
6164
.timeout(const Duration(seconds: 5));
@@ -160,8 +163,8 @@ class ExtensionBackedDevice extends Device {
160163
Future<bool> isSupported() async => _targetDevice.isSupported;
161164

162165
@override
163-
Future<bool> isSupportedForProject(FlutterProject flutterProject) =>
164-
_deviceService.isSupportedForProject(deviceId: id, projectRoot: flutterProject.directory.uri);
166+
Future<bool> isSupportedForProject(FlutterProject flutterProject) => _deviceService
167+
.isSupportedForProject(deviceId: id, project: flutterProject.toExtensionProject());
165168

166169
@override
167170
Future<CpuArch> get cpuArch async => CpuArch.unknown;

‎packages/flutter_tools/lib/src/project.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:collection';
66

7+
import 'package:flutter_tools_core/flutter_tools_core.dart';
78
import 'package:glob/glob.dart';
89
import 'package:meta/meta.dart';
910
import 'package:package_config/package_config.dart';
@@ -487,6 +488,22 @@ class FlutterProject {
487488
/// True if this project has an example application.
488489
bool get hasExampleApp => _exampleDirectory(directory).existsSync();
489490

491+
/// Converts this project into a serializable [ExtensionFlutterProject] representation
492+
/// for tool extensions.
493+
ExtensionFlutterProject toExtensionProject() {
494+
return ExtensionFlutterProject(
495+
appName: manifest.appName,
496+
buildDirectory: buildDirectory.uri,
497+
directory: directory.uri,
498+
appVersion: manifest.appVersion,
499+
dependencies: <String>{...manifest.dependencies},
500+
isEmpty: manifest.isEmpty,
501+
isModule: isModule,
502+
isPlugin: isPlugin,
503+
workspace: <String>[...manifest.workspace],
504+
);
505+
}
506+
490507
/// Returns a list of platform names that are supported by the project.
491508
List<SupportedPlatform> getSupportedPlatforms({bool includeRoot = false}) {
492509
return <SupportedPlatform>[

‎packages/flutter_tools/packages/flutter_tools_core/lib/flutter_tools_core.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export 'src/config.dart';
1414
export 'src/device.dart';
1515
export 'src/diagnostics.dart';
1616
export 'src/json_utils.dart';
17+
export 'src/project.dart';
1718
export 'src/templates.dart';
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:meta/meta.dart';
6+
7+
import 'json_utils.dart';
8+
9+
/// Validation helpers for file system [Uri]s exchanged over the tool
10+
/// extension protocol.
11+
extension ExtensionUriValidation on Uri? {
12+
/// Whether this URI is a non-null, absolute `file:` directory URI ending
13+
/// with a trailing slash (`/`).
14+
bool get isValidDirectoryUri => switch (this) {
15+
final Uri uri => uri.isScheme('file') && uri.hasAbsolutePath && uri.path.endsWith('/'),
16+
null => false,
17+
};
18+
}
19+
20+
/// Serializable representation of a Flutter project and its manifest metadata
21+
/// for tool extensions.
22+
@immutable
23+
class ExtensionFlutterProject {
24+
ExtensionFlutterProject({
25+
required this.appName,
26+
required this.buildDirectory,
27+
required this.directory,
28+
this.appVersion,
29+
this.dependencies = const <String>{},
30+
this.isEmpty = false,
31+
this.isModule = false,
32+
this.isPlugin = false,
33+
this.workspace = const <String>[],
34+
}) {
35+
if (!buildDirectory.isValidDirectoryUri) {
36+
throw ArgumentError.value(
37+
buildDirectory,
38+
'buildDirectory',
39+
'Must be an absolute file:// directory URI ending with "/".',
40+
);
41+
}
42+
if (!directory.isValidDirectoryUri) {
43+
throw ArgumentError.value(
44+
directory,
45+
'directory',
46+
'Must be an absolute file:// directory URI ending with "/".',
47+
);
48+
}
49+
}
50+
51+
/// Deserializes an [ExtensionFlutterProject] from a JSON-serializable map.
52+
///
53+
/// Throws a [FormatException] if required fields are missing, if any field
54+
/// has an invalid type, or if [buildDirectory] or [directory] are not valid
55+
/// trailing-slash-terminated absolute `file:` directory URIs.
56+
factory ExtensionFlutterProject.fromJson(Map<String, Object?> json) {
57+
return ExtensionFlutterProject(
58+
appName: json.requiredField<String>(appNameKey),
59+
buildDirectory: _parseDirectoryUri(json, buildDirectoryKey),
60+
directory: _parseDirectoryUri(json, directoryKey),
61+
appVersion: json.nullableField<String>(appVersionKey),
62+
dependencies: json.stringIterable(dependenciesKey).toSet(),
63+
isEmpty: json.optionalField<bool>(isEmptyKey, defaultValue: false),
64+
isModule: json.optionalField<bool>(isModuleKey, defaultValue: false),
65+
isPlugin: json.optionalField<bool>(isPluginKey, defaultValue: false),
66+
workspace: json.stringIterable(workspaceKey).toList(),
67+
);
68+
}
69+
70+
static Uri _parseDirectoryUri(Map<String, Object?> json, String key) {
71+
final String rawUri = json.requiredField<String>(key);
72+
final Uri? uri = Uri.tryParse(rawUri);
73+
if (uri != null && uri.isValidDirectoryUri) {
74+
return uri;
75+
}
76+
throw FormatException('Invalid "$key": expected trailing-slash-terminated absolute file: URI.');
77+
}
78+
79+
/// Map key for [appName].
80+
static const String appNameKey = 'appName';
81+
82+
/// Map key for [appVersion].
83+
static const String appVersionKey = 'appVersion';
84+
85+
/// Map key for [buildDirectory].
86+
static const String buildDirectoryKey = 'buildDirectory';
87+
88+
/// Map key for [dependencies].
89+
static const String dependenciesKey = 'dependencies';
90+
91+
/// Map key for [directory].
92+
static const String directoryKey = 'directory';
93+
94+
/// Map key for [isEmpty].
95+
static const String isEmptyKey = 'isEmpty';
96+
97+
/// Map key for [isModule].
98+
static const String isModuleKey = 'isModule';
99+
100+
/// Map key for [isPlugin].
101+
static const String isPluginKey = 'isPlugin';
102+
103+
/// Map key for [workspace].
104+
static const String workspaceKey = 'workspace';
105+
106+
/// The `name` property in the project's `pubspec.yaml` file.
107+
final String appName;
108+
109+
/// The `version` property in the project's `pubspec.yaml` file, if valid.
110+
final String? appVersion;
111+
112+
/// The absolute directory URI of the project's build directory (e.g.,
113+
/// `file:///path/to/my_app/build/`), trailing-slash terminated.
114+
final Uri buildDirectory;
115+
116+
/// Direct dependency package names declared in the project's `pubspec.yaml` file.
117+
final Set<String> dependencies;
118+
119+
/// The absolute root directory URI of the Flutter project (e.g.,
120+
/// `file:///path/to/my_app/`), trailing-slash terminated.
121+
final Uri directory;
122+
123+
/// Whether the project's `pubspec.yaml` file is missing or empty.
124+
final bool isEmpty;
125+
126+
/// Whether this project is a Flutter module project.
127+
final bool isModule;
128+
129+
/// Whether this project is a Flutter plugin project.
130+
final bool isPlugin;
131+
132+
/// Workspace member glob entries declared in the project's `pubspec.yaml` file.
133+
final List<String> workspace;
134+
135+
/// Serializes the project metadata to a JSON-serializable map.
136+
Map<String, Object?> toMap() => <String, Object?>{
137+
appNameKey: appName,
138+
appVersionKey: ?appVersion,
139+
buildDirectoryKey: buildDirectory.toString(),
140+
dependenciesKey: dependencies.toList(),
141+
directoryKey: directory.toString(),
142+
isEmptyKey: isEmpty,
143+
isModuleKey: isModule,
144+
isPluginKey: isPlugin,
145+
workspaceKey: workspace.toList(),
146+
};
147+
148+
@override
149+
String toString() =>
150+
'ExtensionFlutterProject(appName: $appName, appVersion: $appVersion, '
151+
'directory: $directory, buildDirectory: $buildDirectory, '
152+
'isEmpty: $isEmpty, isModule: $isModule, isPlugin: $isPlugin, '
153+
'dependencies: $dependencies, workspace: $workspace)';
154+
155+
@override
156+
bool operator ==(Object other) {
157+
if (identical(this, other)) {
158+
return true;
159+
}
160+
if (other is! ExtensionFlutterProject) {
161+
return false;
162+
}
163+
if (appName != other.appName ||
164+
appVersion != other.appVersion ||
165+
buildDirectory != other.buildDirectory ||
166+
directory != other.directory ||
167+
isEmpty != other.isEmpty ||
168+
isModule != other.isModule ||
169+
isPlugin != other.isPlugin ||
170+
dependencies.length != other.dependencies.length ||
171+
workspace.length != other.workspace.length) {
172+
return false;
173+
}
174+
if (!dependencies.containsAll(other.dependencies)) {
175+
return false;
176+
}
177+
for (var i = 0; i < workspace.length; i++) {
178+
if (workspace[i] != other.workspace[i]) {
179+
return false;
180+
}
181+
}
182+
return true;
183+
}
184+
185+
@override
186+
int get hashCode => Object.hash(
187+
appName,
188+
appVersion,
189+
buildDirectory,
190+
Object.hashAllUnordered(dependencies),
191+
directory,
192+
isEmpty,
193+
isModule,
194+
isPlugin,
195+
Object.hashAll(workspace),
196+
);
197+
}

0 commit comments

Comments
 (0)