|
| 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