From a5db572ed9052ecd461ca54bea018ce246cc6d45 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Nov 2019 10:03:37 +0100 Subject: [PATCH 1/2] swap deprecated dependency `vm_service_lib` for `vm_service` --- lib/src/jaguar_hotreload_base.dart | 4 ++-- pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/jaguar_hotreload_base.dart b/lib/src/jaguar_hotreload_base.dart index 75e56d0..25a3414 100644 --- a/lib/src/jaguar_hotreload_base.dart +++ b/lib/src/jaguar_hotreload_base.dart @@ -5,8 +5,8 @@ import 'dart:async'; import 'dart:io'; import 'dart:isolate'; import 'package:glob/glob.dart'; -import 'package:vm_service_lib/vm_service_lib_io.dart' show vmServiceConnectUri; -import 'package:vm_service_lib/vm_service_lib.dart' show VmService; +import 'package:vm_service/vm_service_io.dart' show vmServiceConnectUri; +import 'package:vm_service/vm_service.dart' show VmService; import 'package:watcher/watcher.dart'; const String _kVmServiceUrl = 'ws://localhost:8181/ws'; diff --git a/pubspec.yaml b/pubspec.yaml index b47213b..037f42a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ environment: sdk: '>=2.0.0 <3.0.0' dependencies: - vm_service_lib: ^0.3.5 + vm_service: ^2.1.1 watcher: ^0.9.7 glob: ^1.1.3 From 62eb26fc1832e350b8b5c70dc3a0cf5f9e515847 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Nov 2019 11:05:19 +0100 Subject: [PATCH 2/2] auto-detect local service URL by default --- lib/src/jaguar_hotreload_base.dart | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/src/jaguar_hotreload_base.dart b/lib/src/jaguar_hotreload_base.dart index 25a3414..cff50a7 100644 --- a/lib/src/jaguar_hotreload_base.dart +++ b/lib/src/jaguar_hotreload_base.dart @@ -2,6 +2,7 @@ // is governed by a BSD-style license that can be found in the LICENSE file. import 'dart:async'; +import 'dart:developer' as dev; import 'dart:io'; import 'dart:isolate'; import 'package:glob/glob.dart'; @@ -9,8 +10,6 @@ import 'package:vm_service/vm_service_io.dart' show vmServiceConnectUri; import 'package:vm_service/vm_service.dart' show VmService; import 'package:watcher/watcher.dart'; -const String _kVmServiceUrl = 'ws://localhost:8181/ws'; - /// Encapsulates Hot reloader path class HotReloaderPath { /// [HotReloader] the path belongs to @@ -67,7 +66,7 @@ class HotReloader { /// /// More information can be found at: /// https://www.dartlang.org/dart-vm/tools/dart-vm - final String vmServiceUrl; + final FutureOr vmServiceUrl; /// Debounce interval for [onChange] event final Duration debounceInterval; @@ -107,10 +106,11 @@ class HotReloader { /// Creates a [HotReloader] with given [vmServiceUrl] /// - /// By default, [vmServiceUrl] uses `ws://localhost:8181/ws` + /// By default, [vmServiceUrl] will be auto-detected locally. HotReloader( - {this.vmServiceUrl = _kVmServiceUrl, - this.debounceInterval = const Duration(seconds: 1)}) { + {FutureOr vmServiceUrl, + this.debounceInterval = const Duration(seconds: 1)}) + : this.vmServiceUrl = vmServiceUrl ?? _detectServiceUrl() { if (!isHotReloadable) throw notHotReloadable; _onChangeSub = onChange @@ -347,7 +347,7 @@ More information can be found at: https://www.dartlang.org/dart-vm/tools/dart-vm print('Reloading the application...'); // Get vm service - _client ??= await vmServiceConnectUri(vmServiceUrl); + _client ??= await vmServiceConnectUri(await vmServiceUrl); // Find main isolate id to reload it final vm = await _client.getVM(); @@ -397,3 +397,18 @@ class _FoldedDebounce }); } } + +Future _detectServiceUrl() async { + var info = await dev.Service.getInfo(); + var uri = info.serverUri; + + uri = uri.replace(path: (uri.path.endsWith('/') ? uri.path : uri.path + '/') + 'ws'); + + if (uri.scheme == 'https') { + uri = uri.replace(scheme: 'wss'); + } else { + uri = uri.replace(scheme: 'ws'); + } + + return uri.toString(); +}