-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathdeeplink_manager.dart
More file actions
263 lines (240 loc) · 7.86 KB
/
Copy pathdeeplink_manager.dart
File metadata and controls
263 lines (240 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2023 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'dart:convert';
import 'dart:io';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
class DeeplinkManager {
/// A regex to retrieve the json part from the stdout of Android analyzer.
///
/// Example stdout:
///
/// Running Gradle task 'printBuildVariants'... 10.4s
/// ["debug","release","profile"]
static final _androidBuildVariantJsonRegex = RegExp(r'(\[.*\])');
/// A regex to retrieve the json part of the stdout of iOS analyzer.
///
/// Example stdout:
///
/// {"configurations":["Debug","Release","Profile"],"targets":["Runner","RunnerTests"]}
static final _iosBuildOptionsJsonRegex = RegExp(r'({.*})');
/// The key to retrieve error message from the returning map of this class's
/// APIs.
static const kErrorField = 'error';
/// The key to retrieve output json from the returning map of this class's
/// APIs.
static const kOutputJsonField = 'json';
// TODO(https://github.com/flutter/devtools/issues/9702): Use the `DashTool`
// and `DashEnvVar` enums and `getEnvironment()` helper directly from
// `package:unified_analytics` once the pinned Flutter candidate SDK in this
// repository is bumped to a stable Dart SDK version >= 3.10.0 (resolving the
// dev SDK version solving conflict on CI).
/// Mappings from case-insensitive IDE query parameter values to their
/// corresponding DashTool canonical label strings used by `package:unified_analytics`.
///
/// Contains multiple spelling and format variations (with/without hyphens
/// or suffixes) passed by different IDE integrations to ensure O(1) lookup.
static const _ideToDashToolMap = <String, String>{
'vs-code': 'vscode-plugins',
'vscode': 'vscode-plugins',
'vscodeplugins': 'vscode-plugins',
'intellij-idea': 'intellij-plugins',
'intellij': 'intellij-plugins',
'intellijplugins': 'intellij-plugins',
'android-studio': 'android-studio-plugins',
'androidstudio': 'android-studio-plugins',
'androidstudioplugins': 'android-studio-plugins',
};
/// A regex to retrieve the file path from the stdout of iOS or Android
/// analyzers.
///
/// Example stdout:
///
/// result saved in /path/to/json/file.json
static final _outputFilePathRegex = RegExp(r'result saved in (.*.json)');
@visibleForTesting
Future<ProcessResult> runProcess(
String executable, {
required List<String> arguments,
String? ide,
bool suppressAnalytics = false,
}) {
final environment = <String, String>{
...Platform.environment,
'DASH__SUPPRESS_ANALYTICS': suppressAnalytics.toString(),
'DASH__TOOL': ide != null ? _mapIdeToDashToolLabel(ide) : 'devtools',
};
return Process.run(
executable,
arguments,
environment: environment,
);
}
String _mapIdeToDashToolLabel(String ide) {
final lowerIde = ide.toLowerCase();
final mappedTool = _ideToDashToolMap[lowerIde];
if (mappedTool != null) {
return mappedTool;
}
return 'devtools';
}
@visibleForTesting
String getFlutterBinary() {
// FLUTTER_ROOT can be set by Dart-Code VSCode extension or dart shell
// script shipped with flutter sdk.
var flutterRoot = Platform.environment['FLUTTER_ROOT'];
if (flutterRoot == null) {
// Attempt to find flutter root from dart binary path.
final dartPathSegments = path.split(Platform.resolvedExecutable);
final flutterFolderSegmentIndex = dartPathSegments.lastIndexOf('flutter');
if (flutterFolderSegmentIndex != -1 &&
dartPathSegments[flutterFolderSegmentIndex + 1] == 'bin') {
flutterRoot = path.joinAll(
dartPathSegments.sublist(0, flutterFolderSegmentIndex + 1),
);
}
}
if (flutterRoot == null) {
// Fallback to use flutter from PATH.
return Platform.isWindows ? 'flutter.bat' : 'flutter';
}
return path.join(
flutterRoot,
'bin',
Platform.isWindows ? 'flutter.bat' : 'flutter',
);
}
Future<String> _runFlutterCommand(
List<String> arguments, {
required RegExp outputMatcher,
String? ide,
bool suppressAnalytics = false,
}) async {
final flutterPath = getFlutterBinary();
final result = await runProcess(
flutterPath,
arguments: arguments,
ide: ide,
suppressAnalytics: suppressAnalytics,
);
if (result.exitCode != 0) {
throw _FlutterProcessError(
'Flutter command exit with non-zero error code ${result.exitCode}\n${result.stderr}',
);
}
final match = outputMatcher.firstMatch(result.stdout);
if (match == null) {
throw _FlutterProcessError("Can't parse output: ${result.stdout}");
} else {
return match.group(1)!; //await File(match.group(1)!).readAsString();
}
}
Map<String, Object?> _handleRunFlutterError(
covariant _FlutterProcessError error,
) {
return <String, String?>{
kErrorField: error.message,
};
}
Future<Map<String, Object?>> _handleReadJsonFile(String filePath) {
return File(filePath)
.readAsString()
.then<Map<String, Object?>>(_handleJsonOutput);
}
Future<Map<String, Object?>> _handleJsonOutput(String jsonOutput) async {
try {
jsonEncode(jsonOutput);
} on Error catch (e) {
return <String, String?>{
kErrorField: e.toString(),
};
}
return <String, String?>{
kOutputJsonField: jsonOutput,
};
}
Future<Map<String, Object?>> getAndroidBuildVariants({
required String rootPath,
String? ide,
bool suppressAnalytics = false,
}) {
return _runFlutterCommand(
<String>['analyze', '--android', '--list-build-variants', rootPath],
outputMatcher: _androidBuildVariantJsonRegex,
ide: ide,
suppressAnalytics: suppressAnalytics,
).then<Map<String, Object?>>(
_handleJsonOutput,
onError: _handleRunFlutterError,
);
}
Future<Map<String, Object?>> getAndroidAppLinkSettings({
required String rootPath,
required String buildVariant,
String? ide,
bool suppressAnalytics = false,
}) {
return _runFlutterCommand(
<String>[
'analyze',
'--android',
'--output-app-link-settings',
'--build-variant=$buildVariant',
rootPath,
],
outputMatcher: _outputFilePathRegex,
ide: ide,
suppressAnalytics: suppressAnalytics,
).then<Map<String, Object?>>(
_handleReadJsonFile,
onError: _handleRunFlutterError,
);
}
Future<Map<String, Object?>> getIosBuildOptions({
required String rootPath,
String? ide,
bool suppressAnalytics = false,
}) {
return _runFlutterCommand(
<String>['analyze', '--ios', '--list-build-options', rootPath],
outputMatcher: _iosBuildOptionsJsonRegex,
ide: ide,
suppressAnalytics: suppressAnalytics,
).then<Map<String, Object?>>(
_handleJsonOutput,
onError: _handleRunFlutterError,
);
}
Future<Map<String, Object?>> getIosUniversalLinkSettings({
required String rootPath,
required String configuration,
required String target,
String? ide,
bool suppressAnalytics = false,
}) {
return _runFlutterCommand(
<String>[
'analyze',
'--ios',
'--output-universal-link-settings',
'--configuration=$configuration',
'--target=$target',
rootPath,
],
outputMatcher: _outputFilePathRegex,
ide: ide,
suppressAnalytics: suppressAnalytics,
).then<Map<String, Object?>>(
_handleReadJsonFile,
onError: _handleRunFlutterError,
);
}
}
class _FlutterProcessError extends Error {
_FlutterProcessError(this.message);
/// The error message.
final String message;
@override
String toString() => 'Error: $message';
}