Skip to content

Commit 3704366

Browse files
Copilotbaronfel
andcommitted
Refactor to extract common command execution logic
Extracted the common code from ExecuteWithProjectProfile and ExecuteWithExecutableProfile into a new ExecuteCommand method. This method handles: - Applying launch settings to the command - Applying environment variables from command line - Sending telemetry - Setting up Ctrl-C handler - Executing the command This reduces code duplication and makes the execution flow clearer. Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent b41ab20 commit 3704366

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,7 @@ private int ExecuteWithProjectProfile(ProjectLaunchSettingsModel? launchSettings
178178
try
179179
{
180180
ICommand targetCommand = GetTargetCommand(projectFactory, cachedRunProperties);
181-
ApplyLaunchSettingsProfileToCommand(targetCommand, launchSettings);
182-
183-
// Env variables specified on command line override those specified in launch profile:
184-
foreach (var (name, value) in EnvironmentVariables)
185-
{
186-
targetCommand.EnvironmentVariable(name, value);
187-
}
188-
189-
// Send telemetry about the run operation
190-
SendRunTelemetry(launchSettings, virtualCommand);
191-
192-
// Ignore Ctrl-C for the remainder of the command's execution
193-
Console.CancelKeyPress += (sender, e) => { e.Cancel = true; };
194-
195-
return targetCommand.Execute().ExitCode;
181+
return ExecuteCommand(targetCommand, launchSettings, virtualCommand);
196182
}
197183
catch (InvalidProjectFileException e)
198184
{
@@ -249,6 +235,11 @@ private int ExecuteWithExecutableProfile(ExecutableLaunchSettingsModel launchSet
249235
var command = CommandFactoryUsingResolver.Create(commandSpec)
250236
.WorkingDirectory(workingDirectory);
251237

238+
return ExecuteCommand(command, launchSettings, virtualCommand: null);
239+
}
240+
241+
private int ExecuteCommand(ICommand command, LaunchSettingsModel? launchSettings, VirtualProjectBuildingCommand? virtualCommand)
242+
{
252243
// Apply environment variables from launch profile
253244
ApplyLaunchSettingsProfileToCommand(command, launchSettings);
254245

@@ -259,7 +250,7 @@ private int ExecuteWithExecutableProfile(ExecutableLaunchSettingsModel launchSet
259250
}
260251

261252
// Send telemetry about the run operation
262-
SendRunTelemetry(launchSettings, virtualCommand: null);
253+
SendRunTelemetry(launchSettings, virtualCommand);
263254

264255
// Ignore Ctrl-C for the remainder of the command's execution
265256
Console.CancelKeyPress += (sender, e) => { e.Cancel = true; };

0 commit comments

Comments
 (0)