-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
* Move server code to devtools_server package
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,111 +2,8 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:args/args.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:shelf/shelf.dart' as shelf; | ||
import 'package:shelf/shelf_io.dart' as shelf; | ||
import 'package:shelf_static/shelf_static.dart'; | ||
|
||
const argHelp = 'help'; | ||
const argMachine = 'machine'; | ||
const argPort = 'port'; | ||
|
||
final argParser = new ArgParser() | ||
..addFlag( | ||
argHelp, | ||
negatable: false, | ||
abbr: 'h', | ||
help: 'Prints help output.', | ||
) | ||
..addOption( | ||
argPort, | ||
defaultsTo: '9100', | ||
abbr: 'p', | ||
help: 'Port to serve DevTools on. ' | ||
'Pass 0 to automatically assign an available port.', | ||
) | ||
..addFlag( | ||
argMachine, | ||
negatable: false, | ||
abbr: 'm', | ||
help: 'Sets output format to JSON for consumption in tools.', | ||
); | ||
import 'package:devtools_server/devtools_server.dart'; | ||
|
||
void main(List<String> arguments) async { | ||
final args = argParser.parse(arguments); | ||
if (args[argHelp]) { | ||
print('Dart DevTools version ${await _getVersion()}'); | ||
print(''); | ||
print('usage: devtools <options>'); | ||
print(''); | ||
print(argParser.usage); | ||
return; | ||
} | ||
|
||
final bool machineMode = args[argMachine]; | ||
final port = args[argPort] != null ? int.tryParse(args[argPort]) ?? 0 : 0; | ||
|
||
final Uri resourceUri = await Isolate.resolvePackageUri( | ||
Uri(scheme: 'package', path: 'devtools/devtools.dart')); | ||
final packageDir = path.dirname(path.dirname(resourceUri.toFilePath())); | ||
|
||
// Default static handler for all non-package requests. | ||
final String buildDir = path.join(packageDir, 'build'); | ||
final buildHandler = | ||
createStaticHandler(buildDir, defaultDocument: 'index.html'); | ||
|
||
// The packages folder is renamed in the pub package so this handler serves | ||
// out of the `pack` folder. | ||
final String packagesDir = path.join(packageDir, 'build', 'pack'); | ||
final packHandler = | ||
createStaticHandler(packagesDir, defaultDocument: 'index.html'); | ||
|
||
// Make a handler that delegates to the correct handler based on path. | ||
final handler = (shelf.Request request) { | ||
return request.url.path.startsWith('packages/') | ||
// request.change here will strip the `packages` prefix from the path | ||
// so it's relative to packHandler's root. | ||
? packHandler(request.change(path: 'packages')) | ||
: buildHandler(request); | ||
}; | ||
|
||
final server = await shelf.serve(handler, '127.0.0.1', port); | ||
|
||
printOutput( | ||
'Serving DevTools at http://${server.address.host}:${server.port}', | ||
{ | ||
'method': 'server.started', | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
devoncarew
Member
|
||
'params': {'host': server.address.host, 'port': server.port, 'pid': pid} | ||
}, | ||
machineMode: machineMode, | ||
); | ||
} | ||
|
||
Future<String> _getVersion() async { | ||
final Uri resourceUri = await Isolate.resolvePackageUri( | ||
Uri(scheme: 'package', path: 'devtools/devtools.dart')); | ||
final String packageDir = | ||
path.dirname(path.dirname(resourceUri.toFilePath())); | ||
final File pubspecFile = File(path.join(packageDir, 'pubspec.yaml')); | ||
final String versionLine = | ||
pubspecFile.readAsLinesSync().firstWhere((String line) { | ||
return line.startsWith('version: '); | ||
}, orElse: () => null); | ||
return versionLine == null | ||
? 'unknown' | ||
: versionLine.substring('version: '.length).trim(); | ||
} | ||
|
||
void printOutput( | ||
String message, | ||
Object json, { | ||
@required bool machineMode, | ||
}) { | ||
print(machineMode ? jsonEncode(json) : message); | ||
serveDevToolsWithArgs(arguments); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Use the same analysis options that we use in package:devtools | ||
include: package:devtools/analysis_options.yaml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
export 'src/server.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2019 The Chromium Authors. 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:convert'; | ||
import 'dart:io'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:args/args.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:shelf/shelf.dart' as shelf; | ||
import 'package:shelf/shelf_io.dart' as shelf; | ||
import 'package:shelf_static/shelf_static.dart'; | ||
|
||
const argHelp = 'help'; | ||
const argMachine = 'machine'; | ||
const argPort = 'port'; | ||
|
||
final argParser = new ArgParser() | ||
..addFlag( | ||
argHelp, | ||
negatable: false, | ||
abbr: 'h', | ||
help: 'Prints help output.', | ||
) | ||
..addOption( | ||
argPort, | ||
defaultsTo: '9100', | ||
abbr: 'p', | ||
help: 'Port to serve DevTools on. ' | ||
'Pass 0 to automatically assign an available port.', | ||
) | ||
..addFlag( | ||
argMachine, | ||
negatable: false, | ||
abbr: 'm', | ||
help: 'Sets output format to JSON for consumption in tools.', | ||
); | ||
|
||
void serveDevToolsWithArgs(List<String> arguments) async { | ||
final args = argParser.parse(arguments); | ||
|
||
final help = args[argHelp]; | ||
final bool machineMode = args[argMachine]; | ||
final port = args[argPort] != null ? int.tryParse(args[argPort]) ?? 0 : 0; | ||
|
||
serveDevTools(help: help, machineMode: machineMode, port: port); | ||
} | ||
|
||
void serveDevTools({ | ||
bool help = false, | ||
bool machineMode = false, | ||
int port = 0, | ||
}) async { | ||
if (help) { | ||
print('Dart DevTools version ${await _getVersion()}'); | ||
print(''); | ||
print('usage: devtools <options>'); | ||
print(''); | ||
print(argParser.usage); | ||
return; | ||
} | ||
|
||
final Uri resourceUri = await Isolate.resolvePackageUri( | ||
Uri(scheme: 'package', path: 'devtools/devtools.dart')); | ||
final packageDir = path.dirname(path.dirname(resourceUri.toFilePath())); | ||
|
||
// Default static handler for all non-package requests. | ||
final String buildDir = path.join(packageDir, 'build'); | ||
final buildHandler = createStaticHandler( | ||
buildDir, | ||
defaultDocument: 'index.html', | ||
); | ||
|
||
// The packages folder is renamed in the pub package so this handler serves | ||
// out of the `pack` folder. | ||
final String packagesDir = path.join(packageDir, 'build', 'pack'); | ||
final packHandler = createStaticHandler( | ||
packagesDir, | ||
defaultDocument: 'index.html', | ||
); | ||
|
||
// Make a handler that delegates to the correct handler based on path. | ||
final handler = (shelf.Request request) { | ||
return request.url.path.startsWith('packages/') | ||
// request.change here will strip the `packages` prefix from the path | ||
// so it's relative to packHandler's root. | ||
? packHandler(request.change(path: 'packages')) | ||
: buildHandler(request); | ||
}; | ||
|
||
final server = await shelf.serve(handler, '127.0.0.1', port); | ||
|
||
printOutput( | ||
'Serving DevTools at http://${server.address.host}:${server.port}', | ||
{ | ||
'method': 'server.started', | ||
'params': {'host': server.address.host, 'port': server.port, 'pid': pid} | ||
}, | ||
machineMode: machineMode, | ||
); | ||
} | ||
|
||
Future<String> _getVersion() async { | ||
final Uri resourceUri = await Isolate.resolvePackageUri( | ||
Uri(scheme: 'package', path: 'devtools/devtools.dart')); | ||
final String packageDir = | ||
path.dirname(path.dirname(resourceUri.toFilePath())); | ||
final File pubspecFile = File(path.join(packageDir, 'pubspec.yaml')); | ||
final String versionLine = | ||
pubspecFile.readAsLinesSync().firstWhere((String line) { | ||
return line.startsWith('version: '); | ||
}, orElse: () => null); | ||
return versionLine == null | ||
? 'unknown' | ||
: versionLine.substring('version: '.length).trim(); | ||
} | ||
|
||
void printOutput( | ||
String message, | ||
Object json, { | ||
@required bool machineMode, | ||
}) { | ||
print(machineMode ? jsonEncode(json) : message); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: devtools_server | ||
description: A server for shared DevTools support. | ||
|
||
version: 0.0.1 | ||
|
||
author: Dart Team <[email protected]> | ||
homepage: https://github.com/flutter/devtools | ||
|
||
environment: | ||
sdk: '>=2.2.0-dev <3.0.0' | ||
|
||
dependencies: | ||
args: ^1.5.1 | ||
meta: ^1.1.0 | ||
path: ^1.6.0 | ||
pedantic: ^1.4.0 | ||
shelf: ^0.7.4 | ||
shelf_static: ^0.2.8 |
@devoncarew I think I got this wrong, this one should be
event
notmethod
? I'm gonna make VS Code tolerate both (((evt as any).method || evt.event)
), it might make sense for IntelliJ to do the same so we can fix it in future?