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
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions webdev/lib/src/command/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:logging/logging.dart';
import '../logging.dart';
import '../serve/injected/configuration.dart';

export '../serve/injected/configuration.dart' show ReloadConfiguration;

const autoOption = 'auto';
const chromeDebugPortFlag = 'chrome-debug-port';
const debugFlag = 'debug';
Expand Down
1 change: 1 addition & 0 deletions webdev/lib/src/command/daemon_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class DaemonCommand extends Command<int> {
configuration,
buildOptions,
{'web': port},
null,
);
daemon.registerDomain(AppDomain(daemon, workflow.serverManager));
await daemon.onExit;
Expand Down
2 changes: 1 addition & 1 deletion webdev/lib/src/command/serve_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ in the `<directory>:<port>` format, such as `webdev serve test:8080`.
}

var workflow =
await DevWorkflow.start(configuration, buildOptions, targetPorts);
await DevWorkflow.start(configuration, buildOptions, targetPorts, null);
await workflow.done;
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion 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 @@ -69,6 +70,7 @@ Future<ServerManager> _startServerManager(
String workingDirectory,
BuildDaemonClient client,
DevTools devTools,
Handler handler,
) async {
var assetPort = daemonPort(workingDirectory);
var serverOptions = Set<ServerOptions>();
Expand All @@ -78,6 +80,7 @@ Future<ServerManager> _startServerManager(
targetPorts[target],
target,
assetPort,
handler,
));
}
logWriter(logging.Level.INFO, 'Starting resource servers...');
Expand Down Expand Up @@ -175,6 +178,7 @@ class DevWorkflow {
Configuration configuration,
List<String> buildOptions,
Map<String, int> targetPorts,
Handler handler,
) async {
var workingDirectory = Directory.current.path;
var client = await _startBuildDaemon(workingDirectory, buildOptions);
Expand All @@ -184,7 +188,7 @@ class DevWorkflow {
client.startBuild();
var devTools = await _startDevTools(configuration);
var serverManager = await _startServerManager(
configuration, targetPorts, workingDirectory, client, devTools);
configuration, targetPorts, workingDirectory, client, devTools, handler);
var chrome = await _startChrome(configuration, serverManager, client);
return DevWorkflow._(client, chrome, devTools, serverManager);
}
Expand Down
10 changes: 9 additions & 1 deletion webdev/lib/src/serve/webdev_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class ServerOptions {
final int port;
final String target;
final int daemonPort;
final Handler handler;

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

Expand Down Expand Up @@ -78,7 +80,13 @@ class WebDevServer {
assetHandler,
options.configuration.hostname,
);
cascade = cascade.add(devHandler.handler).add(assetHandler.handler);
cascade = cascade
.add(devHandler.handler)
.add(assetHandler.handler);

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

var hostname = options.configuration.hostname;
var server = await HttpMultiServer.bind(hostname, options.port);
Expand Down
61 changes: 61 additions & 0 deletions webdev/lib/webdev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 'package:build_daemon/data/build_status.dart';
import 'package:shelf/shelf.dart';
import 'package:webdev/src/serve/handlers/dev_handler.dart';

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

export 'src/command/configuration.dart' show Configuration, ReloadConfiguration;
export 'src/daemon_client.dart' show daemonPort;
export 'src/serve/debugger/app_debug_services.dart' show AppDebugServices;
export 'src/serve/debugger/devtools.dart' show DevTools;

Future<WebDevHandler> connectToWebdev(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first stab at a basic API - I tried to expose as little as possible - really only the configuration needed to initialize the server and the interfaces we use to get the vm service or debug. See https://github.com/flutter/flutter/pull/34252/files#diff-01a7653ade6cc1a5182fdebf929ad64bR291 for example usage

Configuration configuration,
int port,
int assetPort,
String target,
Stream<BuildResults> buildResults,
DevTools devTools, {
Handler optionalHandler,
}) async {
var serverOptions = ServerOptions(
configuration,
port,
target,
assetPort,
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;
var connection = await devHandler.connectedApps.first;
var appDebugServices = await devHandler.loadAppServices(
connection.request.appId, connection.request.instanceId);
return WebDevHandler(appDebugServices, connection, chrome, webDevServer);
}

class WebDevHandler {
final AppDebugServices appDebugServices;
final DevConnection devConnection;
final Chrome _chrome;
final WebDevServer _webDevServer;

WebDevHandler(this.appDebugServices, this.devConnection, this._chrome, this._webDevServer);

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