private async Task <int> MainInternalAsync( IReporter reporter, string project, IReadOnlyList <string> args, CancellationToken cancellationToken) { // TODO multiple projects should be easy enough to add here string projectFile; try { projectFile = MsBuildProjectFinder.FindMsBuildProject(_workingDirectory, project); } catch (FileNotFoundException ex) { reporter.Error(ex.Message); return(1); } var isDefaultRunCommand = false; if (args.Count == 1 && args[0] == "run") { isDefaultRunCommand = true; } else if (args.Count == 0) { isDefaultRunCommand = true; args = new[] { "run" }; } var watchOptions = DotNetWatchOptions.Default; var fileSetFactory = new MsBuildFileSetFactory(reporter, watchOptions, projectFile, waitOnError: true, trace: false); var processInfo = new ProcessSpec { Executable = DotnetMuxer.MuxerPath, WorkingDirectory = Path.GetDirectoryName(projectFile), Arguments = args, EnvironmentVariables = { ["DOTNET_WATCH"] = "1" }, }; if (CommandLineOptions.IsPollingEnabled) { _reporter.Output("Polling file watcher is enabled"); } var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(_workingDirectory, reporter); var context = new DotNetWatchContext { ProcessSpec = processInfo, Reporter = _reporter, SuppressMSBuildIncrementalism = watchOptions.SuppressMSBuildIncrementalism, DefaultLaunchSettingsProfile = defaultProfile, }; if (isDefaultRunCommand && !string.IsNullOrEmpty(defaultProfile?.HotReloadProfile)) { _reporter.Verbose($"Found HotReloadProfile={defaultProfile.HotReloadProfile}. Watching with hot-reload"); // We'll sue hot-reload based watching if // a) watch was invoked with no args or with exactly one arg - the run command e.g. `dotnet watch` or `dotnet watch run` // b) The launch profile supports hot-reload based watching. // The watcher will complain if users configure this for runtimes that would not support it. await using var watcher = new HotReloadDotNetWatcher(reporter, fileSetFactory, watchOptions, _console); await watcher.WatchAsync(context, cancellationToken); } else { _reporter.Verbose("Did not find a HotReloadProfile or running a non-default command. Watching with legacy behavior."); // We'll use the presence of a profile to decide if we're going to use the hot-reload based watching. // The watcher will complain if users configure this for runtimes that would not support it. await using var watcher = new DotNetWatcher(reporter, fileSetFactory, watchOptions); await watcher.WatchAsync(context, cancellationToken); } return(0); }
private async Task <int> MainInternalAsync(CommandLineOptions options, CancellationToken cancellationToken) { // TODO multiple projects should be easy enough to add here string projectFile; try { projectFile = MsBuildProjectFinder.FindMsBuildProject(_workingDirectory, options.Project); } catch (FileNotFoundException ex) { _reporter.Error(ex.Message); return(1); } var args = options.RemainingArguments; var isDefaultRunCommand = false; if (args.Count == 1 && args[0] == "run") { isDefaultRunCommand = true; } else if (args.Count == 0) { isDefaultRunCommand = true; args = new[] { "run" }; } var watchOptions = DotNetWatchOptions.Default; watchOptions.NonInteractive = options.NonInteractive; var fileSetFactory = new MsBuildFileSetFactory(_reporter, watchOptions, projectFile, waitOnError: true, trace: false); var processInfo = new ProcessSpec { Executable = DotnetMuxer.MuxerPath, WorkingDirectory = Path.GetDirectoryName(projectFile), Arguments = args, EnvironmentVariables = { ["DOTNET_WATCH"] = "1" }, }; if (CommandLineOptions.IsPollingEnabled) { _reporter.Output("Polling file watcher is enabled"); } var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(processInfo.WorkingDirectory, _reporter) ?? new(); var context = new DotNetWatchContext { ProcessSpec = processInfo, Reporter = _reporter, SuppressMSBuildIncrementalism = watchOptions.SuppressMSBuildIncrementalism, DefaultLaunchSettingsProfile = defaultProfile, }; context.ProjectGraph = TryReadProject(projectFile); if (!options.NoHotReload && isDefaultRunCommand && context.ProjectGraph is not null && IsHotReloadSupported(context.ProjectGraph)) { _reporter.Verbose($"Project supports hot reload and was configured to run with the default run-command. Watching with hot-reload"); // Use hot-reload based watching if // a) watch was invoked with no args or with exactly one arg - the run command e.g. `dotnet watch` or `dotnet watch run` // b) The launch profile supports hot-reload based watching. // The watcher will complain if users configure this for runtimes that would not support it. await using var watcher = new HotReloadDotNetWatcher(_reporter, _requester, fileSetFactory, watchOptions, _console, _workingDirectory); await watcher.WatchAsync(context, cancellationToken); }