Skip to content

[WIP] Expose minimal API for flutter_tools consumption #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
25 changes: 13 additions & 12 deletions webdev/lib/src/serve/dev_workflow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:build_daemon/data/build_status.dart';
import 'package:build_daemon/data/build_target.dart';
import 'package:build_daemon/data/server_log.dart';
import 'package:logging/logging.dart' as logging;
import 'package:shelf/shelf.dart';

import '../command/configuration.dart';
import '../daemon_client.dart';
Expand Down Expand Up @@ -64,12 +65,12 @@ Future<Chrome> _startChrome(
}

Future<ServerManager> _startServerManager(
Configuration configuration,
Map<String, int> targetPorts,
String workingDirectory,
BuildDaemonClient client,
DevTools devTools,
) async {
Configuration configuration,
Map<String, int> targetPorts,
String workingDirectory,
BuildDaemonClient client,
DevTools devTools,
{Handler optionalHandler}) async {
var assetPort = daemonPort(workingDirectory);
var serverOptions = Set<ServerOptions>();
for (var target in targetPorts.keys) {
Expand All @@ -78,6 +79,7 @@ Future<ServerManager> _startServerManager(
targetPorts[target],
target,
assetPort,
optionalHandler: optionalHandler,
));
}
logWriter(logging.Level.INFO, 'Starting resource servers...');
Expand Down Expand Up @@ -171,11 +173,9 @@ class DevWorkflow {

Future<void> get done => _doneCompleter.future;

static Future<DevWorkflow> start(
Configuration configuration,
List<String> buildOptions,
Map<String, int> targetPorts,
) async {
static Future<DevWorkflow> start(Configuration configuration,
List<String> buildOptions, Map<String, int> targetPorts,
{Handler optionalHandler}) async {
var workingDirectory = Directory.current.path;
var client = await _startBuildDaemon(workingDirectory, buildOptions);
logWriter(logging.Level.INFO, 'Registering build targets...');
Expand All @@ -184,7 +184,8 @@ class DevWorkflow {
client.startBuild();
var devTools = await _startDevTools(configuration);
var serverManager = await _startServerManager(
configuration, targetPorts, workingDirectory, client, devTools);
configuration, targetPorts, workingDirectory, client, devTools,
optionalHandler: optionalHandler);
var chrome = await _startChrome(configuration, serverManager, client);
return DevWorkflow._(client, chrome, devTools, serverManager);
}
Expand Down
13 changes: 7 additions & 6 deletions webdev/lib/src/serve/webdev_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ class ServerOptions {
final int port;
final String target;
final int daemonPort;
final Handler optionalHandler;

ServerOptions(
this.configuration,
this.port,
this.target,
this.daemonPort,
);
ServerOptions(this.configuration, this.port, this.target, this.daemonPort,
{this.optionalHandler});
}

class WebDevServer {
Expand Down Expand Up @@ -80,6 +77,10 @@ class WebDevServer {
);
cascade = cascade.add(devHandler.handler).add(assetHandler.handler);

if (options.optionalHandler != null) {
cascade = cascade.add(options.optionalHandler);
}

var hostname = options.configuration.hostname;
var server = await HttpMultiServer.bind(hostname, options.port);
shelf_io.serveRequests(server, pipeline.addHandler(cascade.handler));
Expand Down
84 changes: 84 additions & 0 deletions webdev/lib/src/webdev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';

import 'package:build_daemon/data/build_status.dart';
import 'package:shelf/shelf.dart';
import 'package:vm_service_lib/vm_service_lib.dart';
import 'package:webdev/src/serve/handlers/dev_handler.dart';

import 'command/configuration.dart';
import 'serve/chrome.dart';
import 'serve/debugger/app_debug_services.dart';
import 'serve/debugger/devtools.dart';
import 'serve/injected/configuration.dart';
import 'serve/webdev_server.dart';

Stream<WebDevHandle> connectToWebdev(
int port,
int assetPort,
String target,
Stream<BuildResults> buildResults, {
Handler optionalHandler,
}) async* {
var hostname = 'localhost';
var configuration = Configuration(
hostname: hostname,
requireBuildWebCompilers: false,
debug: true,
autoRun: true,
reload: ReloadConfiguration.none,
);
var devtools = await DevTools.start(hostname);
var serverOptions = ServerOptions(
configuration,
port,
target,
assetPort,
optionalHandler: optionalHandler,
);
var webDevServer =
await WebDevServer.start(serverOptions, buildResults, devtools);
var chrome = await Chrome.start(
<String>['http://${webDevServer.host}:${webDevServer.port}/'],
port: configuration.chromeDebugPort);
var devHandler = webDevServer.devHandler;
await for (var connection in devHandler.connectedApps) {
var appDebugServices = await devHandler.loadAppServices(
connection.request.appId, connection.request.instanceId);
yield WebDevHandle(
appDebugServices, connection, chrome, webDevServer, devtools);
}
}

class WebDevHandle {
final AppDebugServices _appDebugServices;
final DevConnection _devConnection;
final Chrome _chrome;
final WebDevServer _webDevServer;
final DevTools _devTools;

WebDevHandle(this._appDebugServices, this._devConnection, this._chrome,
this._webDevServer, this._devTools);

Future<void> close() async {
await _webDevServer.stop();
await _chrome.close();
await _devTools.close();
await _appDebugServices.close();
}

VmService get vmService {
return _appDebugServices.webdevClient.client;
}

Stream<void> get onTabClose {
return _appDebugServices.chromeProxyService.tabConnection.onClose;
}

Uri get wsUri => Uri.parse(_appDebugServices.debugService.wsUri);

void runMain() => _devConnection.runMain();
}