Skip to content

Reorg #49524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Reorg #49524

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BuiltInTools/DotNetWatchTasks/DotNetWatchTasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\dotnet-watch\Internal\MSBuildFileSetResult.cs" />
<Compile Include="..\dotnet-watch\Build\MSBuildFileSetResult.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class MSBuildFileSetFactory(
reporter.Output($"MSBuild output from target '{TargetName}':");
}

BuildUtilities.ReportBuildOutput(reporter, capturedOutput, success, projectDisplay: null);
BuildOutput.ReportBuildOutput(reporter, capturedOutput, success, projectDisplay: null);
if (!success)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ internal sealed class DotNetWatchContext
public required ProcessRunner ProcessRunner { get; init; }

public required ProjectOptions RootProjectOptions { get; init; }

public MSBuildFileSetFactory CreateMSBuildFileSetFactory()
=> new(
RootProjectOptions.ProjectPath,
RootProjectOptions.BuildArguments,
EnvironmentOptions,
ProcessRunner,
Reporter);
}
}

Large diffs are not rendered by default.

27 changes: 0 additions & 27 deletions src/BuiltInTools/dotnet-watch/Internal/Ensure.cs

This file was deleted.

53 changes: 0 additions & 53 deletions src/BuiltInTools/dotnet-watch/Internal/MsBuildProjectFinder.cs

This file was deleted.

21 changes: 0 additions & 21 deletions src/BuiltInTools/dotnet-watch/Internal/ReporterTraceListener.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ private sealed class ProcessState
/// <param name="isUserApplication">True if the process is a user application, false if it is a helper process (e.g. msbuild).</param>
public async Task<int> RunAsync(ProcessSpec processSpec, IReporter reporter, bool isUserApplication, ProcessLaunchResult? launchResult, CancellationToken processTerminationToken)
{
Ensure.NotNull(processSpec, nameof(processSpec));

var state = new ProcessState();
var stopwatch = new Stopwatch();

Expand Down
123 changes: 81 additions & 42 deletions src/BuiltInTools/dotnet-watch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Loader;
using Microsoft.Build.Locator;

Expand Down Expand Up @@ -76,14 +78,8 @@ public static async Task<int> Main(string[] args)
reporter.Verbose($"Test flags: {environmentOptions.TestFlags}");
}

string projectPath;
try
{
projectPath = MsBuildProjectFinder.FindMsBuildProject(workingDirectory, options.ProjectPath);
}
catch (FileNotFoundException ex)
if (!TryFindProject(workingDirectory, options, reporter, out var projectPath))
{
reporter.Error(ex.Message);
errorCode = 1;
return null;
}
Expand All @@ -93,6 +89,51 @@ public static async Task<int> Main(string[] args)
return new Program(console, reporter, rootProjectOptions, options, environmentOptions);
}

/// <summary>
/// Finds a compatible MSBuild project.
/// <param name="searchBase">The base directory to search</param>
/// <param name="project">The filename of the project. Can be null.</param>
/// </summary>
private static bool TryFindProject(string searchBase, CommandLineOptions options, IReporter reporter, [NotNullWhen(true)] out string? projectPath)
{
projectPath = options.ProjectPath ?? searchBase;

if (!Path.IsPathRooted(projectPath))
{
projectPath = Path.Combine(searchBase, projectPath);
}

if (Directory.Exists(projectPath))
{
var projects = Directory.EnumerateFileSystemEntries(projectPath, "*.*proj", SearchOption.TopDirectoryOnly)
.Where(f => !".xproj".Equals(Path.GetExtension(f), StringComparison.OrdinalIgnoreCase))
.ToList();

if (projects.Count > 1)
{
reporter.Error(string.Format(CultureInfo.CurrentCulture, Resources.Error_MultipleProjectsFound, projectPath));
return false;
}

if (projects.Count == 0)
{
reporter.Error(string.Format(CultureInfo.CurrentCulture, Resources.Error_NoProjectsFound, projectPath));
return false;
}

projectPath = projects[0];
return true;
}

if (!File.Exists(projectPath))
{
reporter.Error(string.Format(CultureInfo.CurrentCulture, Resources.Error_ProjectPath_NotFound, projectPath));
return false;
}

return true;
}

// internal for testing
internal async Task<int> RunAsync()
{
Expand Down Expand Up @@ -132,8 +173,23 @@ internal async Task<int> RunAsync()
return await ListFilesAsync(processRunner, shutdownCancellationToken);
}

var watcher = CreateWatcher(processRunner, runtimeProcessLauncherFactory: null);
await watcher.WatchAsync(shutdownCancellationToken);
if (environmentOptions.IsPollingEnabled)
{
reporter.Output("Polling file watcher is enabled");
}

var context = CreateContext(processRunner);

if (IsHotReoadEnabled())
{
var watcher = new HotReloadDotNetWatcher(context, console, runtimeProcessLauncherFactory: null);
await watcher.WatchAsync(shutdownCancellationToken);
}
else
{
await DotNetWatcher.WatchAsync(context, shutdownCancellationToken);
}

return 0;
}
catch (OperationCanceledException) when (shutdownCancellationToken.IsCancellationRequested)
Expand All @@ -155,49 +211,32 @@ internal async Task<int> RunAsync()
}

// internal for testing
internal Watcher CreateWatcher(ProcessRunner processRunner, IRuntimeProcessLauncherFactory? runtimeProcessLauncherFactory)
{
if (environmentOptions.IsPollingEnabled)
internal DotNetWatchContext CreateContext(ProcessRunner processRunner)
=> new()
{
reporter.Output("Polling file watcher is enabled");
}

var fileSetFactory = new MSBuildFileSetFactory(
rootProjectOptions.ProjectPath,
rootProjectOptions.BuildArguments,
environmentOptions,
processRunner,
reporter);
Reporter = reporter,
ProcessRunner = processRunner,
Options = options.GlobalOptions,
EnvironmentOptions = environmentOptions,
RootProjectOptions = rootProjectOptions,
};

bool enableHotReload;
private bool IsHotReoadEnabled()
{
if (rootProjectOptions.Command != "run")
{
reporter.Verbose($"Command '{rootProjectOptions.Command}' does not support Hot Reload.");
enableHotReload = false;
return false;
}
else if (options.GlobalOptions.NoHotReload)

if (options.GlobalOptions.NoHotReload)
{
reporter.Verbose("Hot Reload disabled by command line switch.");
enableHotReload = false;
return false;
}
else
{
reporter.Report(MessageDescriptor.WatchingWithHotReload);
enableHotReload = true;
}

var context = new DotNetWatchContext
{
Reporter = reporter,
ProcessRunner = processRunner,
Options = options.GlobalOptions,
EnvironmentOptions = environmentOptions,
RootProjectOptions = rootProjectOptions,
};

return enableHotReload
? new HotReloadDotNetWatcher(context, console, fileSetFactory, runtimeProcessLauncherFactory)
: new DotNetWatcher(context, fileSetFactory);
reporter.Report(MessageDescriptor.WatchingWithHotReload);
return true;
}

private async Task<int> ListFilesAsync(ProcessRunner processRunner, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.DotNet.Watch;

internal static partial class BuildUtilities
internal static partial class BuildOutput
{
private const string BuildEmoji = "🔨";
private static readonly Regex s_buildDiagnosticRegex = GetBuildDiagnosticRegex();
Expand Down
Loading
Loading