Skip to content

Commit

Permalink
Improve console output of dotnet watch ⌚ (#23318)
Browse files Browse the repository at this point in the history
* Requester

* Relative File Path

* Updated Rude Edit Dialog

* Emojify

* Tests

* Test fix 2 (ChangeExcludedFile still failing)

* Relative path robustness

* File changed test

* Arg based emojis

* Remove duplicate tip message
  • Loading branch information
TanayParikh authored Jan 12, 2022
1 parent 1ef0e81 commit 5ae5131
Show file tree
Hide file tree
Showing 26 changed files with 337 additions and 205 deletions.
4 changes: 2 additions & 2 deletions src/BuiltInTools/dotnet-watch/DotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task WatchAsync(DotNetWatchContext context, CancellationToken cance
var args = string.Join(" ", processSpec.Arguments);
_reporter.Verbose($"Running {processSpec.ShortDisplayName()} with the following arguments: {args}");

_reporter.Output("Started");
_reporter.Output("Started", emoji: "🚀");

Task<FileItem?> fileSetTask;
Task finishedTask;
Expand Down Expand Up @@ -141,7 +141,7 @@ await _staticFileHandler.TryHandleFileChange(context, fileItem, combinedCancella
// Process exited. Redo evaludation
context.RequiresMSBuildRevaluation = true;
// Now wait for a file to change before restarting process
context.ChangedFile = await fileSetWatcher.GetChangedFileAsync(cancellationToken, () => _reporter.Warn("Waiting for a file to change before restarting dotnet..."));
context.ChangedFile = await fileSetWatcher.GetChangedFileAsync(cancellationToken, () => _reporter.Warn("Waiting for a file to change before restarting dotnet...", emoji: ""));
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/BuiltInTools/dotnet-watch/Filters/DotNetBuildFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async ValueTask ProcessAsync(DotNetWatchContext context, CancellationToke
WorkingDirectory = context.ProcessSpec.WorkingDirectory,
};

_reporter.Output("Building...");
_reporter.Output("Building...", emoji: "🔧");
var exitCode = await _processRunner.RunAsync(processSpec, cancellationToken);
context.FileSet = await _fileSetFactory.CreateAsync(cancellationToken);
if (exitCode == 0)
Expand All @@ -47,7 +47,7 @@ public async ValueTask ProcessAsync(DotNetWatchContext context, CancellationToke

// If the build fails, we'll retry until we have a successful build.
using var fileSetWatcher = new FileSetWatcher(context.FileSet, _reporter);
await fileSetWatcher.GetChangedFileAsync(cancellationToken, () => _reporter.Warn("Waiting for a file to change before restarting dotnet..."));
await fileSetWatcher.GetChangedFileAsync(cancellationToken, () => _reporter.Warn("Waiting for a file to change before restarting dotnet...", emoji: ""));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
// 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}");
_reporter.Output($"Unable to launch the browser. Navigate to {launchUrl}", emoji: "🌐");
}
}
else if (_watchContext?.BrowserRefreshServer is { } browserRefresh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public async ValueTask<bool> TryHandleFileChange(DotNetWatchContext context, Fil
HotReloadEventSource.Log.HotReloadEnd(HotReloadEventSource.StartType.CompilationHandler);
if (applyState)
{
_reporter.Output($"Hot reload of changes succeeded.");
_reporter.Output($"Hot reload of changes succeeded.", emoji: "🔥");
}

return applyState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static async Task<ImmutableArray<string>> GetHotReloadCapabilitiesAsync(
try
{
var capabilities = await hotReloadCapabilitiesTask;
reporter.Verbose($"Hot reload capabilities: {string.Join(" ", capabilities)}.");
reporter.Verbose($"Hot reload capabilities: {string.Join(" ", capabilities)}.", emoji: "🔥");

return capabilities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public static HotReloadProfile InferHotReloadProfile(ProjectGraph projectGraph,
// We saw a previous project that was AspNetCore. This must he a blazor hosted app.
if (aspnetCoreProject is not null && aspnetCoreProject != currentNode.ProjectInstance)
{
reporter.Verbose($"HotReloadProfile: BlazorHosted. {aspnetCoreProject.FullPath} references BlazorWebAssembly project {currentNode.ProjectInstance.FullPath}.");
reporter.Verbose($"HotReloadProfile: BlazorHosted. {aspnetCoreProject.FullPath} references BlazorWebAssembly project {currentNode.ProjectInstance.FullPath}.", emoji: "🔥");
return HotReloadProfile.BlazorHosted;
}

reporter.Verbose("HotReloadProfile: BlazorWebAssembly.");
reporter.Verbose("HotReloadProfile: BlazorWebAssembly.", emoji: "🔥");
return HotReloadProfile.BlazorWebAssembly;
}
}
Expand All @@ -50,7 +50,7 @@ public static HotReloadProfile InferHotReloadProfile(ProjectGraph projectGraph,
}
}

reporter.Verbose("HotReloadProfile: Default.");
reporter.Verbose("HotReloadProfile: Default.", emoji: "🔥");
return HotReloadProfile.Default;
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/BuiltInTools/dotnet-watch/HotReload/RudeEditDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Tools.Internal;

namespace Microsoft.DotNet.Watcher.Tools
{
public class RudeEditDialog
{
private readonly IReporter _reporter;
private readonly IRequester _requester;
private readonly IConsole _console;
private bool? _restartImmediatelySessionPreference; // Session preference

public RudeEditDialog(IReporter reporter, IRequester requester, IConsole console)
{
_reporter = reporter;
_requester = requester;
_console = console;

var alwaysRestart = Environment.GetEnvironmentVariable("DOTNET_WATCH_RESTART_ON_RUDE_EDIT");

if (alwaysRestart == "1" || string.Equals(alwaysRestart, "true", StringComparison.OrdinalIgnoreCase))
{
_reporter.Verbose($"DOTNET_WATCH_RESTART_ON_RUDE_EDIT = '{alwaysRestart}'. Restarting without prompt.");
_restartImmediatelySessionPreference = true;
}
}

public async Task EvaluateAsync(CancellationToken cancellationToken)
{
if (_restartImmediatelySessionPreference.HasValue)
{
await GetRudeEditResult(_restartImmediatelySessionPreference.Value, cancellationToken);
return;
}

var key = await _requester.GetKeyAsync(
"Do you want to restart your app - Yes (y) / No (n) / Always (a) / Never (v)?",
KeyPressed,
cancellationToken);

switch (key)
{
case ConsoleKey.Escape:
case ConsoleKey.Y:
await GetRudeEditResult(restartImmediately: true, cancellationToken);
return;
case ConsoleKey.N:
await GetRudeEditResult(restartImmediately: false, cancellationToken);
return;
case ConsoleKey.A:
_restartImmediatelySessionPreference = true;
await GetRudeEditResult(restartImmediately: true, cancellationToken);
return;
case ConsoleKey.V:
_restartImmediatelySessionPreference = false;
await GetRudeEditResult(restartImmediately: false, cancellationToken);
return;
}

bool KeyPressed(ConsoleKey key)
{
return key is ConsoleKey.Y or ConsoleKey.N or ConsoleKey.A or ConsoleKey.V;
}
}

private Task GetRudeEditResult(bool restartImmediately, CancellationToken cancellationToken)
{
if (restartImmediately)
{
return Task.CompletedTask;
}

_reporter.Output("Hot reload suspended. To continue hot reload, press \"Ctrl + R\".", emoji: "🔥");

return Task.Delay(-1, cancellationToken);
}
}
}
95 changes: 0 additions & 95 deletions src/BuiltInTools/dotnet-watch/HotReload/RudeEditPreference.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async ValueTask<bool> TryHandleFileChange(DotNetWatchContext context, Fil
return false;
}
await HandleBrowserRefresh(context.BrowserRefreshServer, file, cancellationToken);
_reporter.Output("Hot reload of scoped css succeeded.");
_reporter.Output("Hot reload of scoped css succeeded.", emoji: "🔥");
HotReloadEventSource.Log.HotReloadEnd(HotReloadEventSource.StartType.ScopedCssHandler);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async ValueTask<bool> TryHandleFileChange(DotNetWatchContext context, Fil
_reporter.Verbose($"Handling file change event for static content {file.FilePath}.");
await HandleBrowserRefresh(context.BrowserRefreshServer, file, cancellationToken);
HotReloadEventSource.Log.HotReloadEnd(HotReloadEventSource.StartType.StaticHandler);
_reporter.Output("Hot reload of static file succeeded.");
_reporter.Output("Hot reload of static file succeeded.", emoji: "🔥");
return true;
}

Expand Down
Loading

0 comments on commit 5ae5131

Please sign in to comment.