Updated hot reloader

This commit is contained in:
thomashii 2021-07-18 15:18:31 +08:00
parent 50bc5d2c51
commit a02e8025bd
10 changed files with 84 additions and 100 deletions

View file

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
</component>
</module>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hot.iml" filepath="$PROJECT_DIR$/.idea/hot.iml" />
</modules>
</component>
</project>

View file

@ -1,8 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main.dart" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true" nameIsGenerated="true">
<option name="VMOptions" value="--observe" />
<option name="filePath" value="$PROJECT_DIR$/example/basic/main.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/example/basic" />
<method />
</configuration>
</component>

View file

@ -1,7 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main.dart (No VM service)" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
<option name="filePath" value="$PROJECT_DIR$/example/basic/main.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/example/basic" />
<method />
</configuration>
</component>

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -1,41 +1,60 @@
# 4.1.0 # Change Log
## 4.1.1
* Fixed NNBD issues
* Updated README
## 4.1.0
* Updated `vm_service` to 7.1.x * Updated `vm_service` to 7.1.x
# 4.0.0 ## 4.0.0
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD
# 3.0.0 ## 3.0.0
* Migrated to work with Dart SDK 2.12.x Non NNBD * Migrated to work with Dart SDK 2.12.x Non NNBD
# 2.0.6 ## 2.0.6
* Support `--observe=*`, `--enable-vm-service=*` (`startsWith`, instead of `==`). * Support `--observe=*`, `--enable-vm-service=*` (`startsWith`, instead of `==`).
# 2.0.5 ## 2.0.5
* Use `dart:developer` to find the Observatory URI. * Use `dart:developer` to find the Observatory URI.
* Use the app's logger when necessary. * Use the app's logger when necessary.
* Apply `package:pedantic`. * Apply `package:pedantic`.
# 2.0.4 ## 2.0.4
* Forcibly close app loggers on shutdown. * Forcibly close app loggers on shutdown.
# 2.0.3 ## 2.0.3
* Fixed up manual restart. * Fixed up manual restart.
* Remove stutter on hotkey press. * Remove stutter on hotkey press.
# 2.0.2 ## 2.0.2
* Fixed for compatibility with `package:angel_websocket@^2.0.0-alpha.5`. * Fixed for compatibility with `package:angel_websocket@^2.0.0-alpha.5`.
# 2.0.1 ## 2.0.1
* Add import of `package:angel_framework/http.dart`
* https://github.com/angel-dart/hot/pull/7 * Add import of `package:angel_framework/http.dart`
* <https://github.com/angel-dart/hot/pull/7>
## 2.0.0
# 2.0.0
* Update for Dart 2 + Angel 2. * Update for Dart 2 + Angel 2.
# 1.1.1+1 ## 1.1.1+1
* Fix a bug that threw when `--observe` was not present. * Fix a bug that threw when `--observe` was not present.
# 1.1.1 ## 1.1.1
* Disable the observatory from pausing the isolate * Disable the observatory from pausing the isolate
on exceptions, because Angel already handles on exceptions, because Angel already handles
all exceptions by itself. all exceptions by itself.

View file

@ -1,18 +1,20 @@
# angel3_hot # Angel3 Hot Reloader
[![version](https://img.shields.io/badge/pub-v4.1.0-brightgreen)](https://pub.dartlang.org/packages/angel3_hot)
[![version](https://img.shields.io/badge/pub-v4.1.1-brightgreen)](https://pub.dartlang.org/packages/angel3_hot)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/hot/LICENSE) [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/hot/LICENSE)
![Screenshot of terminal](screenshots/screenshot.png) ![Screenshot of terminal](screenshots/angel3-screenshot.png)
Supports *hot reloading* of Angel servers on file changes. This is faster and Supports *hot reloading* of Angel3 servers on file changes. This is faster and more reliable than merely reactively restarting a `Process`.
more reliable than merely reactively restarting a `Process`. This package only works with the [Angel3 framework](https://github.com/dukefirehawk/angel).
This package only works with the [Angel framework](https://github.com/dukefirehawk/angel/tree/angel3). **Not recommended to use in production, unless you are specifically intending for a "hot code push" in production..**
## Installation
# Installation
In your `pubspec.yaml`: In your `pubspec.yaml`:
```yaml ```yaml
@ -21,32 +23,28 @@ dependencies:
angel3_hot: ^4.0.0 angel3_hot: ^4.0.0
``` ```
# Usage ## Usage
This package is dependent on the Dart VM service, so you *must* run
Dart with the `--observe` (or `--enable-vm-service`) argument!!!
Usage is fairly simple. Pass a function that creates an `Angel` server, along with a collection of paths This package is dependent on the Dart VM service, so you *must* run Dart with the `--observe` (or `--enable-vm-service`) argument!!!
to watch, to the `HotReloader` constructor. The rest is history!!!
The recommended pattern is to only use hot-reloading in your application entry point. Create your `Angel` instance Usage is fairly simple. Pass a function that creates an `Angel` server, along with a collection of paths to watch, to the `HotReloader` constructor. The rest is history!!!
within a separate function, conventionally named `createServer`.
**Using this in production mode is not recommended, unless you are The recommended pattern is to only use hot-reloading in your application entry point. Create your `Angel` instance within a separate function, conventionally named `createServer`.
specifically intending for a "hot code push" in production..**
You can watch: You can watch:
* Files
* Directories * Files
* Globs * Directories
* URI's * Globs
* `package:` URI's * URI's
* `package:` URI's
```dart ```dart
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel_hot/angel_hot.dart'; import 'package:angel3_hot/angel_hot.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'src/foo.dart'; import 'src/foo.dart';
@ -55,26 +53,26 @@ main() async {
new Directory('src'), new Directory('src'),
new Directory('src'), new Directory('src'),
'main.dart', 'main.dart',
Uri.parse('package:angel_hot/angel_hot.dart') Uri.parse('package:angel3_hot/angel3_hot.dart')
]); ]);
await hot.startServer('127.0.0.1', 3000); await hot.startServer('127.0.0.1', 3000);
} }
Future<Angel> createServer() async { Future<Angel> createServer() async {
var app = new Angel()..serializer = json.encode; var app = Angel()..serializer = json.encode;
// Edit this line, and then refresh the page in your browser! // Edit this line, and then refresh the page in your browser!
app.get('/', (req, res) => {'hello': 'hot world!'}); app.get('/', (req, res) => {'hello': 'hot world!'});
app.get('/foo', (req, res) => new Foo(bar: 'baz')); app.get('/foo', (req, res) => Foo(bar: 'baz'));
app.fallback((req, res) => throw new AngelHttpException.notFound()); app.fallback((req, res) => throw AngelHttpException.notFound());
app.encoders.addAll({ app.encoders.addAll({
'gzip': gzip.encoder, 'gzip': gzip.encoder,
'deflate': zlib.encoder, 'deflate': zlib.encoder,
}); });
app.logger = new Logger('angel') app.logger = Logger('angel')
..onRecord.listen((rec) { ..onRecord.listen((rec) {
print(rec); print(rec);
if (rec.error != null) { if (rec.error != null) {

View file

@ -140,7 +140,7 @@ class HotReloader {
void _logWarning(String msg) { void _logWarning(String msg) {
if (_server?.app.logger != null) { if (_server?.app.logger != null) {
_server!.app.logger!.warning(msg); _server?.app.logger?.warning(msg);
} else { } else {
print(yellow.wrap('WARNING: $msg')); print(yellow.wrap('WARNING: $msg'));
} }
@ -148,7 +148,7 @@ class HotReloader {
void _logInfo(String msg) { void _logInfo(String msg) {
if (_server?.app.logger != null) { if (_server?.app.logger != null) {
_server!.app.logger!.info(msg); _server?.app.logger?.info(msg);
} else { } else {
print(lightGray.wrap(msg)); print(lightGray.wrap(msg));
} }
@ -183,11 +183,15 @@ class HotReloader {
} }
_client = await vm.vmServiceConnectUri(uri.toString()); _client = await vm.vmServiceConnectUri(uri.toString());
_vmachine ??= await _client.getVM(); _vmachine ??= await _client.getVM();
_mainIsolate ??= _vmachine!.isolates!.first; _mainIsolate ??= _vmachine?.isolates?.first;
for (var isolate in _vmachine!.isolates!) { if (_vmachine != null) {
for (var isolate in _vmachine!.isolates ?? <vm.IsolateRef>[]) {
if (isolate.id != null) {
await _client.setExceptionPauseMode(isolate.id!, 'None'); await _client.setExceptionPauseMode(isolate.id!, 'None');
} }
}
}
await _listenToFilesystem(); await _listenToFilesystem();
} }
@ -206,16 +210,25 @@ class HotReloader {
if (enableHotkeys) { if (enableHotkeys) {
var serverUri = var serverUri =
Uri(scheme: 'http', host: server.address.address, port: server.port); Uri(scheme: 'http', host: server.address.address, port: server.port);
var observatoryUri =
await dev.Service.getInfo().then((i) => i.serverUri!); var observatoryUri;
if (isHot) {
observatoryUri = await dev.Service.getInfo().then((i) => i.serverUri!);
}
print(styleBold.wrap( print(styleBold.wrap(
'\n🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".')); '\n🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".'));
stdout.write('Your Angel server is listening at: '); stdout.write('Your server is listening at: ');
print(wrapWith('$serverUri', [cyan, styleUnderlined])); print(wrapWith('$serverUri', [cyan, styleUnderlined]));
if (isHot) {
stdout.write( stdout.write(
'An Observatory debugger and profiler on ${Platform.operatingSystem} is available at: '); 'An Observatory debugger and profiler on ${Platform.operatingSystem} is available at: ');
print(wrapWith('$observatoryUri', [cyan, styleUnderlined])); print(wrapWith('$observatoryUri', [cyan, styleUnderlined]));
} else {
stdout.write(
'The observatory debugger and profiler are not available.\n');
}
print( print(
'For a more detailed help message, press "h". To quit, press "q".\n'); 'For a more detailed help message, press "h". To quit, press "q".\n');

View file

@ -1,6 +1,6 @@
name: angel3_hot name: angel3_hot
description: Supports hot reloading/hot code push of Angel servers on file changes. description: Supports hot reloading/hot code push of Angel servers on file changes.
version: 4.1.0 version: 4.1.1
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/hot homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/hot
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB