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.

https://github.com/flutter/flutter/issues/43285

* Add missing dash to flag.

* Import path with path alias.
This commit is contained in:
godofredoc
2020-04-14 12:57:32 -07:00
committed by GitHub
parent 3893390e76
commit a4a42c548b
2 changed files with 57 additions and 19 deletions

View File

@ -6,8 +6,12 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:fuchsia_ctl/fuchsia_ctl.dart'; import 'package:fuchsia_ctl/fuchsia_ctl.dart';
import 'package:process/process.dart'; import 'package:process/process.dart';
import 'package:path/path.dart' as path;
import 'package:uuid/uuid.dart';
/// A wrapper around the Fuchsia SDK `pm` tool. /// A wrapper around the Fuchsia SDK `pm` tool.
class PackageServer { class PackageServer {
@ -15,8 +19,10 @@ class PackageServer {
PackageServer( PackageServer(
this.pmPath, { this.pmPath, {
this.processManager = const LocalProcessManager(), this.processManager = const LocalProcessManager(),
this.fileSystem = const LocalFileSystem(),
}) : assert(pmPath != null), }) : assert(pmPath != null),
assert(processManager != null); assert(processManager != null),
assert(fileSystem != null);
/// The path on the file system to the `pm` executable. /// The path on the file system to the `pm` executable.
final String pmPath; final String pmPath;
@ -24,8 +30,14 @@ class PackageServer {
/// The process manager to use for launching `pm`. /// The process manager to use for launching `pm`.
final ProcessManager processManager; final ProcessManager processManager;
/// The file sytem for the package server.
final FileSystem fileSystem;
Process _pmServerProcess; Process _pmServerProcess;
/// Path to the port file generated by `pm`.
String portPath;
/// The port the server is listening on, if the server is running. /// The port the server is listening on, if the server is running.
/// ///
/// Throws a [StateError] if accessed when the server is not running. /// Throws a [StateError] if accessed when the server is not running.
@ -84,33 +96,38 @@ class PackageServer {
String repo, { String repo, {
String address = '', String address = '',
int port = 0, int port = 0,
String portFilePath,
}) async { }) async {
assert(repo != null); assert(repo != null);
assert(port != null); assert(port != null);
final String uuid = Uuid().v4();
portPath = portFilePath ??
path.join(fileSystem.systemTempDirectory.path, '${uuid}_port.txt');
final List<String> pmCommand = <String>[ final List<String> pmCommand = <String>[
pmPath, pmPath,
'serve', 'serve',
'-repo', repo, // '-repo',
'-l', '$address:$port', repo,
'-l',
'$address:$port',
'-f',
portPath,
]; ];
stdout.writeln('Running ${pmCommand.join(' ')}'); stdout.writeln('Running ${pmCommand.join(' ')}');
_pmServerProcess = await processManager.start(pmCommand); _pmServerProcess = await processManager.start(pmCommand);
final Completer<void> serverPortCompleter = Completer<void>(); await Future<void>.delayed(const Duration(seconds: 5), () async {
final String portString = await fileSystem.file(portPath).readAsString();
_serverPort = int.parse(portString);
});
_pmServerProcess.stdout _pmServerProcess.stdout
.transform(utf8.decoder) .transform(utf8.decoder)
.transform(const LineSplitter()) .transform(const LineSplitter())
.listen((String line) { .listen(stdout.writeln);
if (line.contains('serving $repo at http://')) {
_serverPort = int.parse(line.substring(line.lastIndexOf(':') + 1));
serverPortCompleter.complete();
}
stdout.writeln(line);
});
_pmServerProcess.stderr _pmServerProcess.stderr
.transform(utf8.decoder) .transform(utf8.decoder)
.transform(const LineSplitter()) .transform(const LineSplitter())
.listen(stderr.writeln); .listen(stderr.writeln);
await serverPortCompleter.future;
} }
/// Closes a running server. /// Closes a running server.
@ -120,6 +137,7 @@ class PackageServer {
if (_pmServerProcess == null) { if (_pmServerProcess == null) {
throw StateError('Must call serveRepo before calling close.'); throw StateError('Must call serveRepo before calling close.');
} }
await fileSystem.file(portPath).delete();
_pmServerProcess.kill(); _pmServerProcess.kill();
final int exitCode = await _pmServerProcess.exitCode; final int exitCode = await _pmServerProcess.exitCode;
_pmServerProcess = null; _pmServerProcess = null;

View File

@ -5,6 +5,8 @@
import 'dart:io' show ProcessResult; import 'dart:io' show ProcessResult;
import 'dart:math' show Random; import 'dart:math' show Random;
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:fuchsia_ctl/fuchsia_ctl.dart'; import 'package:fuchsia_ctl/fuchsia_ctl.dart';
import 'package:fuchsia_ctl/src/package_server.dart'; import 'package:fuchsia_ctl/src/package_server.dart';
import 'package:fuchsia_ctl/src/operation_result.dart'; import 'package:fuchsia_ctl/src/operation_result.dart';
@ -67,8 +69,10 @@ void main() {
pmBin, pmBin,
'publish', 'publish',
'-a', '-a',
'-repo', repoPath, // '-repo',
'-f', farFile, repoPath,
'-f',
farFile,
]); ]);
expect(result.success, true); expect(result.success, true);
}); });
@ -80,8 +84,6 @@ void main() {
0, 0,
<String>[ <String>[
'', '',
'YYYY-MM-DD HH:mm:ss [pm serve] serving $repoPath at http://:$randomPort',
'YYYY-MM-DD HH:mm:ss [pm serve] 200 /',
], ],
<String>[''], <String>[''],
); );
@ -90,14 +92,28 @@ void main() {
return serverProcess; return serverProcess;
}); });
final MemoryFileSystem fs = MemoryFileSystem();
final PackageServer server = PackageServer( final PackageServer server = PackageServer(
pmBin, pmBin,
processManager: processManager, processManager: processManager,
fileSystem: fs,
); );
expect(server.serving, false); expect(server.serving, false);
final File portFile = fs.file(
'port.txt',
)
..create()
..writeAsString(
randomPort.toString(),
);
await server.serveRepo(repoPath, port: 0); await server.serveRepo(
repoPath,
port: 0,
portFilePath: portFile.path,
);
expect(server.serving, true); expect(server.serving, true);
final List<String> capturedStartArgs = final List<String> capturedStartArgs =
@ -109,8 +125,12 @@ void main() {
expect(capturedStartArgs, <String>[ expect(capturedStartArgs, <String>[
pmBin, pmBin,
'serve', 'serve',
'-repo', repoPath, // '-repo',
'-l', ':0', repoPath,
'-l',
':0',
'-f',
'port.txt',
]); ]);
expect(server.serverPort, randomPort); expect(server.serverPort, randomPort);