Skip to content

Commit

Permalink
refactor to use FileINfo to reduce bugs froim using just String
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Jul 19, 2024
1 parent 0ea8eb9 commit 0f4a7fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
10 changes: 9 additions & 1 deletion src/Cli/dotnet/commands/dotnet-run/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ public static RunCommand FromParseResult(ParseResult parseResult)
if (parseResult.UsingRunCommandShorthandProjectOption())
{
Reporter.Output.WriteLine(LocalizableStrings.RunCommandProjectAbbreviationDeprecated.Yellow());
project = parseResult.GetRunCommandShorthandProjectValues().FirstOrDefault();
var possibleProject = parseResult.GetRunCommandShorthandProjectValues().FirstOrDefault();
if (Directory.Exists(possibleProject))
{
project = RunCommandParser.FindSingleProjectInDirectory(possibleProject);
}
else
{
project = new FileInfo(possibleProject);
}
}

var command = new RunCommand(
Expand Down
24 changes: 12 additions & 12 deletions src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class RunCommand
private record RunProperties(string? RunCommand, string? RunArguments, string? RunWorkingDirectory);

public bool NoBuild { get; private set; }
public string Project { get; private set; }
public FileInfo ProjectFile { get; private set; }
public string[] Args { get; set; }
public bool NoRestore { get; private set; }
public bool Interactive { get; private set; }
Expand All @@ -35,7 +35,7 @@ private record RunProperties(string? RunCommand, string? RunArguments, string? R

public RunCommand(
bool noBuild,
string project,
FileInfo project,
string launchProfile,
bool noLaunchProfile,
bool noRestore,
Expand All @@ -44,7 +44,7 @@ public RunCommand(
string[] args)
{
NoBuild = noBuild;
Project = project;
ProjectFile = project;
LaunchProfile = launchProfile;
NoLaunchProfile = noLaunchProfile;
Args = args;
Expand Down Expand Up @@ -81,7 +81,7 @@ public int Execute()
catch (InvalidProjectFileException e)
{
throw new GracefulException(
string.Format(LocalizableStrings.RunCommandSpecifiecFileIsNotAValidProject, Project),
string.Format(LocalizableStrings.RunCommandSpecifiecFileIsNotAValidProject, ProjectFile.FullName),
e);
}
}
Expand Down Expand Up @@ -119,7 +119,7 @@ private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel?
return true;
}

var launchSettingsPath = TryFindLaunchSettings(Project);
var launchSettingsPath = TryFindLaunchSettings(ProjectFile);
if (!File.Exists(launchSettingsPath))
{
if (!string.IsNullOrEmpty(LaunchProfile))
Expand Down Expand Up @@ -158,9 +158,9 @@ private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel?

return true;

static string? TryFindLaunchSettings(string projectFilePath)
static string? TryFindLaunchSettings(FileInfo projectFile)
{
var buildPathContainer = File.Exists(projectFilePath) ? Path.GetDirectoryName(projectFilePath) : projectFilePath;
var buildPathContainer = projectFile.Directory?.FullName;
if (buildPathContainer is null)
{
return null;
Expand All @@ -171,7 +171,7 @@ private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel?
// VB.NET projects store the launch settings file in the
// "My Project" directory instead of a "Properties" directory.
// TODO: use the `AppDesignerFolder` MSBuild property instead, which captures this logic already
if (string.Equals(Path.GetExtension(projectFilePath), ".vbproj", StringComparison.OrdinalIgnoreCase))
if (string.Equals(projectFile.Extension, ".vbproj", StringComparison.OrdinalIgnoreCase))
{
propsDirectory = "My Project";
}
Expand All @@ -189,7 +189,7 @@ private void EnsureProjectIsBuilt()
{
var buildResult =
new RestoringCommand(
RestoreArgs.Prepend(Project),
new string[] { ProjectFile.FullName }.Concat(RestoreArgs),
NoRestore,
advertiseWorkloadUpdates: false
).Execute();
Expand Down Expand Up @@ -224,13 +224,13 @@ private string[] GetRestoreArguments(IEnumerable<string> cliRestoreArgs)
private ICommand GetTargetCommand()
{
// TODO for MSBuild usage here: need to sync loggers (primarily binlog) used with this evaluation
var project = EvaluateProject(Project, RestoreArgs);
var project = EvaluateProject(ProjectFile, RestoreArgs);
InvokeRunArgumentsTarget(project);
var runProperties = ReadRunPropertiesFromProject(project, Args);
var command = CreateCommandFromRunProperties(project, runProperties);
return command;

static ProjectInstance EvaluateProject(string projectFilePath, string[] restoreArgs)
static ProjectInstance EvaluateProject(FileInfo projectFile, string[] restoreArgs)
{
var globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
Expand All @@ -248,7 +248,7 @@ static ProjectInstance EvaluateProject(string projectFilePath, string[] restoreA
globalProperties[key] = string.Join(";", values);
}
}
var project = new ProjectInstance(projectFilePath, globalProperties, null);
var project = new ProjectInstance(projectFile.FullName, globalProperties, null);
return project;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ internal static class RunCommandParser

public static readonly CliOption<string> RuntimeOption = CommonOptions.RuntimeOption;

public static readonly CliOption<string> ProjectOption = new("--project")
public static readonly CliOption<FileInfo> ProjectOption = new("--project")
{
Description = LocalizableStrings.CommandOptionProjectDescription,
DefaultValueFactory = (System.CommandLine.Parsing.ArgumentResult inputArg) =>
CustomParser = (System.CommandLine.Parsing.ArgumentResult inputArg) =>
{
return inputArg.Tokens switch
{
[] => FindSingleProjectInDirectory(Environment.CurrentDirectory),
[var dirOrFile] => Directory.Exists(dirOrFile.Value) ? FindSingleProjectInDirectory(dirOrFile.Value) : dirOrFile.Value,
[var dirOrFile] => Directory.Exists(dirOrFile.Value) ? FindSingleProjectInDirectory(dirOrFile.Value) : new(dirOrFile.Value),
_ => throw new System.InvalidOperationException("Impossible, System.CommandLine parser prevents this due to arity constraints.") //
};
},
};

private static string FindSingleProjectInDirectory(string directory)
internal static FileInfo FindSingleProjectInDirectory(string directory)
{
string[] projectFiles = Directory.GetFiles(directory, "*.*proj");

Expand All @@ -44,7 +44,7 @@ private static string FindSingleProjectInDirectory(string directory)
throw new Utils.GracefulException(LocalizableStrings.RunCommandExceptionMultipleProjects, directory);
}

return projectFiles[0];
return new FileInfo(projectFiles[0]);
}

public static readonly CliOption<string[]> PropertyOption = CommonOptions.PropertiesOption;
Expand Down

0 comments on commit 0f4a7fd

Please sign in to comment.