-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpub_action.dart
More file actions
128 lines (119 loc) · 4 KB
/
pub_action.dart
File metadata and controls
128 lines (119 loc) · 4 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
import 'dart:async';
import 'package:celest_cli/src/context.dart';
import 'package:celest_cli/src/exceptions.dart';
import 'package:celest_cli/src/project/celest_project.dart';
import 'package:celest_cli/src/sdk/dart_sdk.dart';
import 'package:celest_core/_internal.dart';
import 'package:cli_script/cli_script.dart';
import 'package:logging/logging.dart';
enum PubAction {
get,
upgrade;
/// Matcher for output of `pub <action>`.
///
/// From: https://github.com/dart-lang/pub/blob/f8b23495666acb275016850f6fd4206a38ad0eb5/test/test_pub.dart#L96
RegExp get matcher => switch (this) {
get => RegExp(r'Got dependencies!|Changed \d+ dependenc(y|ies)!'),
upgrade => RegExp(r'''
(No dependencies changed\.|Changed \d+ dependenc(y|ies)!)($|
\d+ packages? (has|have) newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.$)'''),
};
}
final Logger _logger = Logger('pub');
Future<void> dumpPackageConfig() async {
try {
final packageConfig =
await fileSystem.file(projectPaths.packagesConfig).readAsString();
_logger.finest('Package config:\n$packageConfig');
} on Object catch (e, st) {
_logger.finest('Failed to read package config.', e, st);
}
try {
final pubspec =
await fileSystem.file(projectPaths.pubspecYaml).readAsString();
_logger.finest('Pubspec:\n$pubspec');
} on Object catch (e, st) {
_logger.finest('Failed to read pubspec.', e, st);
}
}
Future<void> runPub({
String? exe,
required PubAction action,
required String workingDirectory,
}) async {
exe ??= kDebugMode
? Sdk.current.dart
: (await celestProject.determineProjectType()).executable;
final command = <String>[exe, 'pub', action.name];
final logger = Logger(command.join(' '));
logger.fine('Running `${command.join(' ')}` in "$workingDirectory"...');
final process = await processManager.start(
command,
environment: {
if (Sdk.current.flutterSdkRoot case final flutterSdkRoot?)
'FLUTTER_ROOT': flutterSdkRoot,
},
workingDirectory: workingDirectory,
);
// TODO(dnys1): Remove when fixed in pub https://github.com/dart-lang/sdk/issues/55289
// and we can rely on the exit code taking a reasonable amount of time.
// Must be sync so that completer only completes once before `finally` block
// cancels subscription.
final completer = Completer<void>.sync();
final stdout = process.stdout.lines.listen((line) {
logger.finest('stdout: $line');
if (action.matcher.hasMatch(line)) {
if (completer.isCompleted) {
logger.finest('Got matching line after completion: $line');
return;
}
completer.complete();
}
});
final stderr = process.stderr.lines.listen((line) {
logger.finest('stderr: $line');
if (line.trim().startsWith('Waiting for another flutter command')) {
return;
}
if (!completer.isCompleted) {
completer.completeError(
CliException(
'Failed to run `pub ${action.name}`. Please run `$exe pub ${action.name}` '
'manually in $workingDirectory and try again.',
additionalContext: {'error': line},
),
);
}
});
try {
await Future.any([
process.exitCode.then((exitCode) {
if (exitCode != 0) {
throw CliException(
'Failed to run `pub get`. Please run `$exe pub get` manually in '
'$workingDirectory and try again.',
);
}
_logger.finer(
'`$exe pub ${action.name}` completed successfully in '
'"$workingDirectory"',
);
}),
completer.future,
]);
final packageConfig = fileSystem
.directory(workingDirectory)
.childDirectory('.dart_tool')
.childFile('package_config.json');
while (!await packageConfig.exists()) {
await Future<void>.delayed(const Duration(milliseconds: 50));
}
} on Object {
// await dumpPackageConfig();
rethrow;
} finally {
unawaited(stdout.cancel());
unawaited(stderr.cancel());
}
}