|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using Microsoft.DotNet.HotReload; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | +namespace Microsoft.DotNet.Watch; |
| 9 | + |
| 10 | +internal delegate ValueTask ProcessExitAction(int processId, int? exitCode); |
| 11 | + |
| 12 | +internal sealed class ProjectLauncher( |
| 13 | + DotNetWatchContext context, |
| 14 | + ProjectNodeMap projectMap, |
| 15 | + CompilationHandler compilationHandler, |
| 16 | + int iteration) |
| 17 | +{ |
| 18 | + public int Iteration = iteration; |
| 19 | + |
| 20 | + public ILogger Logger |
| 21 | + => context.Logger; |
| 22 | + |
| 23 | + public ILoggerFactory LoggerFactory |
| 24 | + => context.LoggerFactory; |
| 25 | + |
| 26 | + public EnvironmentOptions EnvironmentOptions |
| 27 | + => context.EnvironmentOptions; |
| 28 | + |
| 29 | + public async ValueTask<RunningProject?> TryLaunchProcessAsync( |
| 30 | + ProjectOptions projectOptions, |
| 31 | + CancellationTokenSource processTerminationSource, |
| 32 | + Action<OutputLine>? onOutput, |
| 33 | + RestartOperation restartOperation, |
| 34 | + CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + var projectNode = projectMap.TryGetProjectNode(projectOptions.ProjectPath, projectOptions.TargetFramework); |
| 37 | + if (projectNode == null) |
| 38 | + { |
| 39 | + // error already reported |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + if (!projectNode.IsNetCoreApp(Versions.Version6_0)) |
| 44 | + { |
| 45 | + Logger.LogError($"Hot Reload based watching is only supported in .NET 6.0 or newer apps. Use --no-hot-reload switch or update the project's launchSettings.json to disable this feature."); |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + var appModel = HotReloadAppModel.InferFromProject(context, projectNode); |
| 50 | + |
| 51 | + // create loggers that include project name in messages: |
| 52 | + var projectDisplayName = projectNode.GetDisplayName(); |
| 53 | + var clientLogger = context.LoggerFactory.CreateLogger(HotReloadDotNetWatcher.ClientLogComponentName, projectDisplayName); |
| 54 | + var agentLogger = context.LoggerFactory.CreateLogger(HotReloadDotNetWatcher.AgentLogComponentName, projectDisplayName); |
| 55 | + |
| 56 | + var clients = await appModel.TryCreateClientsAsync(clientLogger, agentLogger, cancellationToken); |
| 57 | + if (clients == null) |
| 58 | + { |
| 59 | + // error already reported |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + var processSpec = new ProcessSpec |
| 64 | + { |
| 65 | + Executable = EnvironmentOptions.MuxerPath, |
| 66 | + IsUserApplication = true, |
| 67 | + WorkingDirectory = projectOptions.WorkingDirectory, |
| 68 | + OnOutput = onOutput, |
| 69 | + }; |
| 70 | + |
| 71 | + // Stream output lines to the process output reporter. |
| 72 | + // The reporter synchronizes the output of the process with the logger output, |
| 73 | + // so that the printed lines don't interleave. |
| 74 | + processSpec.OnOutput += line => |
| 75 | + { |
| 76 | + context.ProcessOutputReporter.ReportOutput(context.ProcessOutputReporter.PrefixProcessOutput ? line with { Content = $"[{projectDisplayName}] {line.Content}" } : line); |
| 77 | + }; |
| 78 | + |
| 79 | + var environmentBuilder = new Dictionary<string, string>(); |
| 80 | + |
| 81 | + // initialize with project settings: |
| 82 | + foreach (var (name, value) in projectOptions.LaunchEnvironmentVariables) |
| 83 | + { |
| 84 | + environmentBuilder[name] = value; |
| 85 | + } |
| 86 | + |
| 87 | + // override any project settings: |
| 88 | + environmentBuilder[EnvironmentVariables.Names.DotnetWatch] = "1"; |
| 89 | + environmentBuilder[EnvironmentVariables.Names.DotnetWatchIteration] = (Iteration + 1).ToString(CultureInfo.InvariantCulture); |
| 90 | + |
| 91 | + if (Logger.IsEnabled(LogLevel.Trace)) |
| 92 | + { |
| 93 | + environmentBuilder[EnvironmentVariables.Names.HotReloadDeltaClientLogMessages] = |
| 94 | + (EnvironmentOptions.SuppressEmojis ? Emoji.Default : Emoji.Agent).GetLogMessagePrefix() + $"[{projectDisplayName}]"; |
| 95 | + } |
| 96 | + |
| 97 | + clients.ConfigureLaunchEnvironment(environmentBuilder); |
| 98 | + |
| 99 | + processSpec.Arguments = GetProcessArguments(projectOptions, environmentBuilder); |
| 100 | + |
| 101 | + // Attach trigger to the process that detects when the web server reports to the output that it's listening. |
| 102 | + // Launches browser on the URL found in the process output for root projects. |
| 103 | + context.BrowserLauncher.InstallBrowserLaunchTrigger(processSpec, projectNode, projectOptions, clients.BrowserRefreshServer, cancellationToken); |
| 104 | + |
| 105 | + return await compilationHandler.TrackRunningProjectAsync( |
| 106 | + projectNode, |
| 107 | + projectOptions, |
| 108 | + clients, |
| 109 | + processSpec, |
| 110 | + restartOperation, |
| 111 | + processTerminationSource, |
| 112 | + cancellationToken); |
| 113 | + } |
| 114 | + |
| 115 | + private static IReadOnlyList<string> GetProcessArguments(ProjectOptions projectOptions, IDictionary<string, string> environmentBuilder) |
| 116 | + { |
| 117 | + var arguments = new List<string>() |
| 118 | + { |
| 119 | + projectOptions.Command, |
| 120 | + "--no-build" |
| 121 | + }; |
| 122 | + |
| 123 | + foreach (var (name, value) in environmentBuilder) |
| 124 | + { |
| 125 | + arguments.Add("-e"); |
| 126 | + arguments.Add($"{name}={value}"); |
| 127 | + } |
| 128 | + |
| 129 | + arguments.AddRange(projectOptions.CommandArguments); |
| 130 | + return arguments; |
| 131 | + } |
| 132 | + |
| 133 | + public ValueTask<int> TerminateProcessAsync(RunningProject project, CancellationToken cancellationToken) |
| 134 | + => compilationHandler.TerminateNonRootProcessAsync(project, cancellationToken); |
| 135 | +} |
0 commit comments