Skip to content

Commit a4a42c5

Browse files
authored
Update package server to use the port file. (#137)
* Update package server to use the port file. This is to simplify the package server removing the logic to get the port from stdout. flutter/flutter#43285 * Add missing dash to flag. * Import path with path alias.
1 parent 3893390 commit a4a42c5

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

‎packages/fuchsia_ctl/lib/src/package_server.dart‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,38 @@ import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
88

9+
import 'package:file/file.dart';
10+
import 'package:file/local.dart';
911
import 'package:fuchsia_ctl/fuchsia_ctl.dart';
1012
import 'package:process/process.dart';
13+
import 'package:path/path.dart' as path;
14+
import 'package:uuid/uuid.dart';
1115

1216
/// A wrapper around the Fuchsia SDK `pm` tool.
1317
class PackageServer {
1418
/// Creates a new package server.
1519
PackageServer(
1620
this.pmPath, {
1721
this.processManager = const LocalProcessManager(),
22+
this.fileSystem = const LocalFileSystem(),
1823
}) : assert(pmPath != null),
19-
assert(processManager != null);
24+
assert(processManager != null),
25+
assert(fileSystem != null);
2026

2127
/// The path on the file system to the `pm` executable.
2228
final String pmPath;
2329

2430
/// The process manager to use for launching `pm`.
2531
final ProcessManager processManager;
2632

33+
/// The file sytem for the package server.
34+
final FileSystem fileSystem;
35+
2736
Process _pmServerProcess;
2837

38+
/// Path to the port file generated by `pm`.
39+
String portPath;
40+
2941
/// The port the server is listening on, if the server is running.
3042
///
3143
/// Throws a [StateError] if accessed when the server is not running.
@@ -84,33 +96,38 @@ class PackageServer {
8496
String repo, {
8597
String address = '',
8698
int port = 0,
99+
String portFilePath,
87100
}) async {
88101
assert(repo != null);
89102
assert(port != null);
103+
104+
final String uuid = Uuid().v4();
105+
portPath = portFilePath ??
106+
path.join(fileSystem.systemTempDirectory.path, '${uuid}_port.txt');
90107
final List<String> pmCommand = <String>[
91108
pmPath,
92109
'serve',
93-
'-repo', repo, //
94-
'-l', '$address:$port',
110+
'-repo',
111+
repo,
112+
'-l',
113+
'$address:$port',
114+
'-f',
115+
portPath,
95116
];
96117
stdout.writeln('Running ${pmCommand.join(' ')}');
97118
_pmServerProcess = await processManager.start(pmCommand);
98-
final Completer<void> serverPortCompleter = Completer<void>();
119+
await Future<void>.delayed(const Duration(seconds: 5), () async {
120+
final String portString = await fileSystem.file(portPath).readAsString();
121+
_serverPort = int.parse(portString);
122+
});
99123
_pmServerProcess.stdout
100124
.transform(utf8.decoder)
101125
.transform(const LineSplitter())
102-
.listen((String line) {
103-
if (line.contains('serving $repo at http://')) {
104-
_serverPort = int.parse(line.substring(line.lastIndexOf(':') + 1));
105-
serverPortCompleter.complete();
106-
}
107-
stdout.writeln(line);
108-
});
126+
.listen(stdout.writeln);
109127
_pmServerProcess.stderr
110128
.transform(utf8.decoder)
111129
.transform(const LineSplitter())
112130
.listen(stderr.writeln);
113-
await serverPortCompleter.future;
114131
}
115132

116133
/// Closes a running server.
@@ -120,6 +137,7 @@ class PackageServer {
120137
if (_pmServerProcess == null) {
121138
throw StateError('Must call serveRepo before calling close.');
122139
}
140+
await fileSystem.file(portPath).delete();
123141
_pmServerProcess.kill();
124142
final int exitCode = await _pmServerProcess.exitCode;
125143
_pmServerProcess = null;

‎packages/fuchsia_ctl/test/package_server_test.dart‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'dart:io' show ProcessResult;
66
import 'dart:math' show Random;
77

8+
import 'package:file/file.dart';
9+
import 'package:file/memory.dart';
810
import 'package:fuchsia_ctl/fuchsia_ctl.dart';
911
import 'package:fuchsia_ctl/src/package_server.dart';
1012
import 'package:fuchsia_ctl/src/operation_result.dart';
@@ -67,8 +69,10 @@ void main() {
6769
pmBin,
6870
'publish',
6971
'-a',
70-
'-repo', repoPath, //
71-
'-f', farFile,
72+
'-repo',
73+
repoPath,
74+
'-f',
75+
farFile,
7276
]);
7377
expect(result.success, true);
7478
});
@@ -80,8 +84,6 @@ void main() {
8084
0,
8185
<String>[
8286
'',
83-
'YYYY-MM-DD HH:mm:ss [pm serve] serving $repoPath at http://:$randomPort',
84-
'YYYY-MM-DD HH:mm:ss [pm serve] 200 /',
8587
],
8688
<String>[''],
8789
);
@@ -90,14 +92,28 @@ void main() {
9092
return serverProcess;
9193
});
9294

95+
final MemoryFileSystem fs = MemoryFileSystem();
96+
9397
final PackageServer server = PackageServer(
9498
pmBin,
9599
processManager: processManager,
100+
fileSystem: fs,
96101
);
97102

98103
expect(server.serving, false);
99-
100-
await server.serveRepo(repoPath, port: 0);
104+
final File portFile = fs.file(
105+
'port.txt',
106+
)
107+
..create()
108+
..writeAsString(
109+
randomPort.toString(),
110+
);
111+
112+
await server.serveRepo(
113+
repoPath,
114+
port: 0,
115+
portFilePath: portFile.path,
116+
);
101117
expect(server.serving, true);
102118

103119
final List<String> capturedStartArgs =
@@ -109,8 +125,12 @@ void main() {
109125
expect(capturedStartArgs, <String>[
110126
pmBin,
111127
'serve',
112-
'-repo', repoPath, //
113-
'-l', ':0',
128+
'-repo',
129+
repoPath,
130+
'-l',
131+
':0',
132+
'-f',
133+
'port.txt',
114134
]);
115135
expect(server.serverPort, randomPort);
116136

0 commit comments

Comments
 (0)