Skip to content

Commit 2f429ad

Browse files
committed
sln-add: Add --include-references option
Update option name
1 parent 94310a7 commit 2f429ad

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal class SolutionAddCommand : CommandBase
2020
private readonly IReadOnlyCollection<string> _projects;
2121
private readonly string? _solutionFolderPath;
2222
private string _solutionFileFullPath = string.Empty;
23+
private bool _addReferencedProjects;
2324

2425
private static string GetSolutionFolderPathWithForwardSlashes(string path)
2526
{
@@ -41,6 +42,7 @@ public SolutionAddCommand(ParseResult parseResult) : base(parseResult)
4142
_projects = (IReadOnlyCollection<string>)(parseResult.GetValue(SolutionAddCommandParser.ProjectPathArgument) ?? []);
4243
_inRoot = parseResult.GetValue(SolutionAddCommandParser.InRootOption);
4344
_solutionFolderPath = parseResult.GetValue(SolutionAddCommandParser.SolutionFolderOption);
45+
_addReferencedProjects = parseResult.GetValue(SolutionAddCommandParser.AddReferencedProjectsOption);
4446
SolutionArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _projects, SolutionArgumentValidator.CommandType.Add, _inRoot, _solutionFolderPath);
4547
_solutionFileFullPath = SlnFileFactory.GetSolutionFileFullPath(_fileOrDirectory);
4648
}
@@ -195,6 +197,21 @@ private void AddProject(SolutionModel solution, string fullProjectPath, ISolutio
195197
project.AddProjectConfigurationRule(new ConfigurationRule(BuildDimension.BuildType, solutionBuildType, "*", projectBuildType));
196198
}
197199

200+
// Get referencedprojects from the project instance
201+
var referencedProjectsFullPaths = projectInstance.EvaluatedItemElements
202+
.Where(item => item.ItemType == "ProjectReference")
203+
.Select(item => item.Include)
204+
.Select(item => Path.GetFullPath(item, Path.GetDirectoryName(fullProjectPath)))
205+
.ToList();
206+
198207
Reporter.Output.WriteLine(CliStrings.ProjectAddedToTheSolution, solutionRelativeProjectPath);
208+
209+
if (_addReferencedProjects)
210+
{
211+
foreach (var referencedProjectFullPath in referencedProjectsFullPaths)
212+
{
213+
AddProject(solution, referencedProjectFullPath, serializer);
214+
}
215+
}
199216
}
200217
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5+
using System.CommandLine.Parsing;
56

67
namespace Microsoft.DotNet.Cli.Commands.Solution.Add;
78

@@ -24,6 +25,12 @@ public static class SolutionAddCommandParser
2425
Description = CliCommandStrings.AddProjectSolutionFolderArgumentDescription
2526
};
2627

28+
public static readonly Option<bool> AddReferencedProjectsOption = new("--include-references")
29+
{
30+
Description = CliCommandStrings.SolutionAddReferencedProjectsOptionDescription,
31+
DefaultValueFactory = (_) => true,
32+
};
33+
2734
private static readonly Command Command = ConstructCommand();
2835

2936
public static Command GetCommand()
@@ -38,6 +45,7 @@ private static Command ConstructCommand()
3845
command.Arguments.Add(ProjectPathArgument);
3946
command.Options.Add(InRootOption);
4047
command.Options.Add(SolutionFolderOption);
48+
command.Options.Add(AddReferencedProjectsOption);
4149

4250
command.SetAction((parseResult) => new SolutionAddCommand(parseResult).Execute());
4351

0 commit comments

Comments
 (0)