private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
        {
            if (string.IsNullOrEmpty(eventArgs.Data))
            {
                return;
            }

            var match = NowListeningRegex.Match(eventArgs.Data);

            if (match.Success)
            {
                var launchUrl = match.Groups["url"].Value;

                var process = (Process)sender;
                process.OutputDataReceived -= OnOutput;

                if (!_attemptedBrowserLaunch)
                {
                    _attemptedBrowserLaunch = true;

                    _reporter.Verbose("Launching browser.");

                    try
                    {
                        LaunchBrowser(launchUrl);
                    }
                    catch (Exception ex)
                    {
                        _reporter.Verbose($"An exception occurred when attempting to launch a browser: {ex}");
                        _browserProcess = null;
                    }

                    if (_browserProcess is null || _browserProcess.HasExited)
                    {
                        // dotnet-watch, by default, relies on URL file association to launch browsers. On Windows and MacOS, this works fairly well
                        // where URLs are associated with the default browser. On Linux, this is a bit murky.
                        // From emperical observation, it's noted that failing to launch a browser results in either Process.Start returning a null-value
                        // or for the process to have immediately exited.
                        // We can use this to provide a helpful message.
                        _reporter.Output($"Unable to launch the browser. Navigate to {launchUrl}");
                    }
                }
                else
                {
                    _reporter.Verbose("Reloading browser.");
                    _ = _refreshServer?.ReloadAsync(_cancellationToken);
                }
            }
        }
Ejemplo n.º 2
0
        private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
        {
            if (string.IsNullOrEmpty(eventArgs.Data))
            {
                return;
            }

            var match = NowListeningRegex.Match(eventArgs.Data);

            if (match.Success)
            {
                var launchUrl = match.Groups["url"].Value;

                var process = (Process)sender;
                process.OutputDataReceived -= OnOutput;

                if (!_browserLaunched)
                {
                    _reporter.Verbose("Launching browser.");
                    try
                    {
                        LaunchBrowser(launchUrl);
                        _browserLaunched = true;
                    }
                    catch (Exception ex)
                    {
                        _reporter.Output($"Unable to launch browser: {ex}");
                        _canLaunchBrowser = false;
                    }
                }
                else
                {
                    _reporter.Verbose("Reloading browser.");
                    _ = _refreshServer?.ReloadAsync(_cancellationToken);
                }
            }
        }