|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// IMPORTANT: to pick up changes made in this file, activate.dart must be |
| 6 | +// re-run to delete the old snapshot. |
| 7 | + |
| 8 | +import 'dart:async'; |
| 9 | +import 'dart:convert'; |
| 10 | +import 'dart:io'; |
| 11 | + |
| 12 | +import 'package:args/args.dart'; |
| 13 | +import 'package:args/command_runner.dart'; |
| 14 | +import 'package:browser_launcher/browser_launcher.dart'; |
| 15 | + |
| 16 | +class ObservatoryCommand extends CommandRunner<void> { |
| 17 | + ObservatoryCommand() |
| 18 | + : super('Observatory', 'Serves the Observatory developer tool.'); |
| 19 | + |
| 20 | + static const String kHelp = 'help'; |
| 21 | + static const String kDebug = 'debug'; |
| 22 | + static const String kLaunch = 'launch'; |
| 23 | + |
| 24 | + @override |
| 25 | + final ArgParser argParser = ArgParser() |
| 26 | + ..addFlag( |
| 27 | + kDebug, |
| 28 | + help: |
| 29 | + 'Run Observatory in debug mode. Useful when making changes to ' |
| 30 | + 'Observatory to automatically pick up code changes.', |
| 31 | + ) |
| 32 | + ..addFlag(kLaunch, help: 'Launch Observatory in Chrome.'); |
| 33 | + |
| 34 | + @override |
| 35 | + Future<void> runCommand(ArgResults results) async { |
| 36 | + if (results.flag(kHelp)) { |
| 37 | + printUsage(); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (!_checkForWebDev()) { |
| 41 | + print('Warning: webdev is not installed. Installing it now...'); |
| 42 | + _activateWebDev(); |
| 43 | + print('webdev installed successfully.'); |
| 44 | + } |
| 45 | + |
| 46 | + await _startWebDev( |
| 47 | + debug: results.flag(kDebug), |
| 48 | + launch: results.flag(kLaunch), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + bool _checkForWebDev() { |
| 53 | + final result = Process.runSync(Platform.resolvedExecutable, <String>[ |
| 54 | + 'pub', |
| 55 | + 'global', |
| 56 | + 'list', |
| 57 | + ]); |
| 58 | + return result.stdout.contains('webdev'); |
| 59 | + } |
| 60 | + |
| 61 | + void _activateWebDev() { |
| 62 | + final result = Process.runSync(Platform.resolvedExecutable, <String>[ |
| 63 | + 'pub', |
| 64 | + 'global', |
| 65 | + 'activate', |
| 66 | + 'webdev', |
| 67 | + ]); |
| 68 | + if (result.exitCode != 0) { |
| 69 | + throw StateError(''' |
| 70 | +Unexpected issue encountered while activating webdev.' |
| 71 | +
|
| 72 | +STDOUT: |
| 73 | +${result.stdout} |
| 74 | +
|
| 75 | +STDERR: |
| 76 | +${result.stderr} |
| 77 | +'''); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Future<void> _startWebDev({required bool debug, required bool launch}) async { |
| 82 | + Directory.current = _findObservatoryProjectRoot(); |
| 83 | + final process = await Process.start(Platform.resolvedExecutable, <String>[ |
| 84 | + 'pub', |
| 85 | + 'global', |
| 86 | + 'run', |
| 87 | + 'webdev', |
| 88 | + 'serve', |
| 89 | + if (!debug) '--release', |
| 90 | + ]); |
| 91 | + final uriCompleter = Completer<String>(); |
| 92 | + final uriRegExp = RegExp('Serving `web` on (http://.*)'); |
| 93 | + late StreamSubscription<String> sub; |
| 94 | + sub = process.stdout.transform(utf8.decoder).listen((e) { |
| 95 | + if (uriRegExp.hasMatch(e)) { |
| 96 | + uriCompleter.complete(uriRegExp.firstMatch(e)!.group(1)); |
| 97 | + sub.cancel(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + final observatoryUri = await uriCompleter.future; |
| 102 | + print('Observatory is available at: $observatoryUri'); |
| 103 | + |
| 104 | + if (launch) { |
| 105 | + Chrome.start(<String>[observatoryUri]); |
| 106 | + } |
| 107 | + await process.exitCode; |
| 108 | + } |
| 109 | + |
| 110 | + String _findObservatoryProjectRoot() { |
| 111 | + final uri = Platform.script; |
| 112 | + String relativePath = '..'; |
| 113 | + if (uri.path.endsWith('.snapshot')) { |
| 114 | + relativePath = '../../../..'; |
| 115 | + } |
| 116 | + return uri.resolve(relativePath).toFilePath(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +Future<void> main(List<String> args) async { |
| 121 | + await ObservatoryCommand().run(args); |
| 122 | +} |
0 commit comments