Skip to content

Commit 509db15

Browse files
authored
sln-add: --include-references when adding a project (#48815)
1 parent 07620ea commit 509db15

24 files changed

+179
-4
lines changed

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2483,4 +2483,7 @@ To display a value, specify the corresponding command-line option without provid
24832483
<data name="ZeroTestsRan" xml:space="preserve">
24842484
<value>Zero tests ran</value>
24852485
</data>
2486-
</root>
2486+
<data name="SolutionAddReferencedProjectsOptionDescription" xml:space="preserve">
2487+
<value>Recursively add projects' ReferencedProjects to solution</value>
2488+
</data>
2489+
</root>

src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommand.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal class SolutionAddCommand : CommandBase
2222
private readonly IReadOnlyCollection<string> _projects;
2323
private readonly string? _solutionFolderPath;
2424
private string _solutionFileFullPath = string.Empty;
25+
private bool _includeReferences;
2526

2627
private static string GetSolutionFolderPathWithForwardSlashes(string path)
2728
{
@@ -43,6 +44,7 @@ public SolutionAddCommand(ParseResult parseResult) : base(parseResult)
4344
_projects = (IReadOnlyCollection<string>)(parseResult.GetValue(SolutionAddCommandParser.ProjectPathArgument) ?? []);
4445
_inRoot = parseResult.GetValue(SolutionAddCommandParser.InRootOption);
4546
_solutionFolderPath = parseResult.GetValue(SolutionAddCommandParser.SolutionFolderOption);
47+
_includeReferences = parseResult.GetValue(SolutionAddCommandParser.IncludeReferencesOption);
4648
SolutionArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _projects, SolutionArgumentValidator.CommandType.Add, _inRoot, _solutionFolderPath);
4749
_solutionFileFullPath = SlnFileFactory.GetSolutionFileFullPath(_fileOrDirectory);
4850
}
@@ -138,7 +140,7 @@ private async Task AddProjectsToSolutionAsync(IEnumerable<string> projectPaths,
138140
await serializer.SaveAsync(_solutionFileFullPath, solution, cancellationToken);
139141
}
140142

141-
private void AddProject(SolutionModel solution, string fullProjectPath, ISolutionSerializer serializer = null)
143+
private void AddProject(SolutionModel solution, string fullProjectPath, ISolutionSerializer serializer = null, bool showMessageOnDuplicate = true)
142144
{
143145
string solutionRelativeProjectPath = Path.GetRelativePath(Path.GetDirectoryName(_solutionFileFullPath), fullProjectPath);
144146

@@ -175,7 +177,10 @@ private void AddProject(SolutionModel solution, string fullProjectPath, ISolutio
175177
}
176178
catch (SolutionArgumentException ex) when (ex.Type == SolutionErrorType.DuplicateProjectName || solution.FindProject(solutionRelativeProjectPath) is not null)
177179
{
178-
Reporter.Output.WriteLine(CliStrings.SolutionAlreadyContainsProject, _solutionFileFullPath, solutionRelativeProjectPath);
180+
if (showMessageOnDuplicate)
181+
{
182+
Reporter.Output.WriteLine(CliStrings.SolutionAlreadyContainsProject, _solutionFileFullPath, solutionRelativeProjectPath);
183+
}
179184
return;
180185
}
181186

@@ -205,5 +210,17 @@ private void AddProject(SolutionModel solution, string fullProjectPath, ISolutio
205210
}
206211

207212
Reporter.Output.WriteLine(CliStrings.ProjectAddedToTheSolution, solutionRelativeProjectPath);
213+
214+
// Get referencedprojects from the project instance
215+
var referencedProjectsFullPaths = projectInstance.GetItems("ProjectReference")
216+
.Select(item => Path.GetFullPath(item.EvaluatedInclude, Path.GetDirectoryName(fullProjectPath)));
217+
218+
if (_includeReferences)
219+
{
220+
foreach (var referencedProjectFullPath in referencedProjectsFullPaths)
221+
{
222+
AddProject(solution, referencedProjectFullPath, serializer, showMessageOnDuplicate: false);
223+
}
224+
}
208225
}
209226
}

src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommandParser.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.CommandLine;
7+
using System.CommandLine.Parsing;
78

89
namespace Microsoft.DotNet.Cli.Commands.Solution.Add;
910

@@ -26,6 +27,12 @@ public static class SolutionAddCommandParser
2627
Description = CliCommandStrings.AddProjectSolutionFolderArgumentDescription
2728
};
2829

30+
public static readonly Option<bool> IncludeReferencesOption = new("--include-references")
31+
{
32+
Description = CliCommandStrings.SolutionAddReferencedProjectsOptionDescription,
33+
DefaultValueFactory = (_) => true,
34+
};
35+
2936
private static readonly Command Command = ConstructCommand();
3037

3138
public static Command GetCommand()
@@ -40,6 +47,7 @@ private static Command ConstructCommand()
4047
command.Arguments.Add(ProjectPathArgument);
4148
command.Options.Add(InRootOption);
4249
command.Options.Add(SolutionFolderOption);
50+
command.Options.Add(IncludeReferencesOption);
4351

4452
command.SetAction((parseResult) => new SolutionAddCommand(parseResult).Execute());
4553

src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)