Skip to content

Commit f1b7545

Browse files
support sln file as entrypoint
1 parent e20e1b4 commit f1b7545

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<PropertyGroup>
5050
<Product>dotnet-subset</Product>
5151
<Description>.NET Tool to copy a subset of files from a repository to a directory</Description>
52-
<VersionPrefix>0.2.0</VersionPrefix>
52+
<VersionPrefix>0.3.0</VersionPrefix>
5353
<Company>Nimbleways</Company>
5454
<Authors>$(Company.ToLower())</Authors>
5555
<Copyright>© $(Company). All rights reserved.</Copyright>

src/dotnet-subset/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
MSBuildLocator.RegisterDefaults();
1111

12-
var projectArgument = new Argument<FileInfo>(
13-
name: "project",
14-
description: "Project to restore.");
12+
var projectOrSolutionArgument = new Argument<FileInfo>(
13+
name: "projectOrSolution",
14+
description: "Project or solution to restore.");
1515

1616
var rootDirectoryOption = new Option<DirectoryInfo>(
1717
name: "--root-directory",
@@ -30,10 +30,10 @@
3030
rootDirectoryOption,
3131
outputDirectoryOption,
3232
};
33-
readCommand.AddArgument(projectArgument);
33+
readCommand.AddArgument(projectOrSolutionArgument);
3434
rootCommand.AddCommand(readCommand);
3535

36-
readCommand.SetHandler((project, rootDirectory, outputDirectory) => RestoreSubset.Execute(project.FullName, rootDirectory.FullName, outputDirectory.FullName),
37-
projectArgument, rootDirectoryOption, outputDirectoryOption);
36+
readCommand.SetHandler((projectOrSolution, rootDirectory, outputDirectory) => RestoreSubset.Execute(projectOrSolution.FullName, rootDirectory.FullName, outputDirectory.FullName),
37+
projectOrSolutionArgument, rootDirectoryOption, outputDirectoryOption);
3838

3939
return rootCommand.Invoke(args);

src/dotnet-subset/RestoreSubset.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
1+
using Microsoft.Build.Construction;
12
using Microsoft.Build.Evaluation;
23

34
namespace Nimbleways.Tools.Subset;
45
internal static class RestoreSubset
56
{
6-
public static void Execute(string mainProjectPath, string rootFolder, string destinationFolder)
7+
public static void Execute(string projectOrSolution, string rootFolder, string destinationFolder)
78
{
8-
if (!IsSameOrUnder(rootFolder, mainProjectPath))
9+
if (!IsSameOrUnder(rootFolder, projectOrSolution))
910
{
10-
throw new ArgumentException($"Project '${mainProjectPath}' must be under the root folder '{rootFolder}'");
11+
throw new ArgumentException($"Project or solution '${projectOrSolution}' must be under the root folder '{rootFolder}'");
1112
}
1213
Directory.CreateDirectory(destinationFolder);
1314

1415
using var projectCollection = new ProjectCollection();
1516
var projectsByFullPath = new Dictionary<string, Project>();
16-
VisitAllProjects(projectCollection, rootFolder, mainProjectPath, projectsByFullPath);
17+
foreach (var project in GetRootProjects(projectOrSolution))
18+
{
19+
VisitAllProjects(projectCollection, rootFolder, project, projectsByFullPath);
20+
}
1721
var projectListAsString = string.Join(Environment.NewLine + " - ", projectsByFullPath.Keys.OrderBy(f => f));
1822
Console.WriteLine($"Found {projectsByFullPath.Count} project(s) to copy:{Environment.NewLine + " - "}{projectListAsString}");
1923
var nugetConfigFiles = GetNugetConfigFiles(rootFolder, projectsByFullPath);
2024
var extraFilesInvolvedInRestore = projectsByFullPath.Values
2125
.SelectMany(project => GetExtraFilesInvolvedInRestore(rootFolder, project))
2226
.Concat(nugetConfigFiles)
2327
.Distinct()
24-
.ToArray();
25-
if (extraFilesInvolvedInRestore.Length > 0)
28+
.ToList();
29+
if (IsSolutionFile(projectOrSolution))
30+
{
31+
extraFilesInvolvedInRestore.Add(projectOrSolution);
32+
}
33+
if (extraFilesInvolvedInRestore.Count > 0)
2634
{
2735
var extraFilesInvolvedInRestoreAsString = string.Join(Environment.NewLine + " - ", extraFilesInvolvedInRestore.OrderBy(f => f));
28-
Console.WriteLine($"Found {extraFilesInvolvedInRestore.Length} extra file(s) to copy:{Environment.NewLine + " - "}{extraFilesInvolvedInRestoreAsString}");
36+
Console.WriteLine($"Found {extraFilesInvolvedInRestore.Count} extra file(s) to copy:{Environment.NewLine + " - "}{extraFilesInvolvedInRestoreAsString}");
2937
}
3038
var allFilesToCopy = projectsByFullPath.Keys.Concat(extraFilesInvolvedInRestore).Distinct();
3139

@@ -50,6 +58,22 @@ public static void Execute(string mainProjectPath, string rootFolder, string des
5058
}
5159
Console.WriteLine($"Copied {copiedFilesCount} file(s) to '{destinationFolder}'. {allFilesCount - copiedFilesCount} file(s) already exist in destination.");
5260
}
61+
private static IEnumerable<string> GetRootProjects(string projectOrSolution)
62+
{
63+
if (IsSolutionFile(projectOrSolution))
64+
{
65+
var solution = SolutionFile.Parse(projectOrSolution);
66+
return solution.ProjectsInOrder
67+
.Where(p => p.ProjectType != SolutionProjectType.SolutionFolder)
68+
.Select(p => p.AbsolutePath);
69+
}
70+
return new[] { projectOrSolution };
71+
}
72+
73+
private static bool IsSolutionFile(string projectOrSolution)
74+
{
75+
return ".sln".Equals(Path.GetExtension(projectOrSolution), StringComparison.OrdinalIgnoreCase);
76+
}
5377

5478
private static bool IsSameOrUnder(string rootFolder, string path)
5579
{

0 commit comments

Comments
 (0)