Skip to content

Commit a7ea953

Browse files
First version
1 parent ad194ff commit a7ea953

File tree

6 files changed

+710
-10
lines changed

6 files changed

+710
-10
lines changed

Directory.Build.props

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

README.md

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,61 @@
44

55
The tool is mainly used in Dockerfiles to optimize the docker build caching for "dotnet restore" instructions.
66

7-
# Motivation
7+
## Motivation
88

99
To learn more about the motivation behind `dotnet-subset`, please read [the related blog post](https://blog.nimbleways.com/p/45d44a69-5460-4fb3-aacb-be7419b27aad/).
1010

11-
# Roadmap
12-
[ ] Add tests
13-
[ ] Refactor the codebase
14-
[ ] Add "build" algorithm
11+
## Installation
12+
### From NuGet
13+
```
14+
dotnet tool install --global dotnet-subset
15+
```
16+
### From source
17+
Prerequisite: .NET SDK 6
18+
19+
1. Clone this repository
20+
2. Open a terminal in the repository's root
21+
3. `dotnet pack --configuration Release`
22+
4. `dotnet tool install dotnet-subset --global --add-source ./artifacts/Release/nupkg/dotnet-subset/`
23+
24+
## Usage
25+
```
26+
Description:
27+
Create a subset for the restore operation
28+
29+
Usage:
30+
dotnet-subset restore <project> [options]
31+
32+
Arguments:
33+
<project> Project or solution to restore
34+
35+
Options:
36+
--root-directory <root-directory> (REQUIRED) Directory from where the files will be copied
37+
--output <output> (REQUIRED) Directory where the subset files will be copied, preserving the
38+
original structure.
39+
```
40+
41+
Example:
42+
```
43+
dotnet subset restore /source/complexapp/complexapp.csproj --root-directory /source/ --output /tmp/restore_subset/
44+
```
45+
46+
## dotnet-subset + docker
47+
Please check this pull request on how to use `dotnet-subset` in your `Dockerfile`: https://github.com/othmane-kinane-nw/dotnet-docker/pull/1/files?diff=split&w=0
48+
49+
## Roadmap
50+
- [ ] Add tests
51+
- [ ] Automate the versionning
52+
- [ ] Automate the deployment to NuGet
53+
- [ ] Refactor the codebase
54+
- [ ] Add "build" algorithm
55+
56+
## License
57+
58+
Copyright © [Nimbleways](https://www.nimbleways.com/), [Othmane Kinane](https://github.com/othmane-kinane-nw) and contributors.
59+
60+
`dotnet-subset` is provided as-is under the MIT license. For more information see [LICENSE](https://github.com/nimbleways/dotnet-subset/blob/main/LICENSE).
61+
62+
* For Microsoft.Build, see https://github.com/dotnet/msbuild/blob/main/LICENSE
63+
* For Microsoft.Build.Locator, see https://github.com/microsoft/MSBuildLocator/blob/master/LICENSE
64+
* For System.CommandLine, see https://github.com/dotnet/command-line-api/blob/main/LICENSE.md

src/dotnet-subset/Program.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
// See https://aka.ms/new-console-template for more information
2-
Console.WriteLine("Hello, World!");
1+
// https://www.daveaglick.com/posts/running-a-design-time-build-with-msbuild-apis
2+
// https://docs.microsoft.com/en-us/visualstudio/msbuild/updating-an-existing-application?view=vs-2022#use-microsoftbuildlocator
3+
4+
using System.CommandLine;
5+
6+
using Microsoft.Build.Locator;
7+
8+
using Nimbleways.Tools.Subset;
9+
10+
MSBuildLocator.RegisterDefaults();
11+
12+
var projectArgument = new Argument<FileInfo>(
13+
name: "project",
14+
description: "Project to restore.");
15+
16+
var rootDirectoryOption = new Option<DirectoryInfo>(
17+
name: "--root-directory",
18+
description: "Directory from where the files will be copied, usually the repository's root.")
19+
{ IsRequired = true };
20+
21+
var outputDirectoryOption = new Option<DirectoryInfo>(
22+
name: "--output",
23+
description: "Directory where the subset files will be copied, preserving the original hierarchy.")
24+
{ IsRequired = true };
25+
26+
var rootCommand = new RootCommand(".NET Tool to copy a subset of files from a repository to a directory.");
27+
28+
var readCommand = new Command("restore", "Create a subset for the restore operation.")
29+
{
30+
rootDirectoryOption,
31+
outputDirectoryOption,
32+
};
33+
readCommand.AddArgument(projectArgument);
34+
rootCommand.AddCommand(readCommand);
35+
36+
readCommand.SetHandler((project, rootDirectory, outputDirectory) => RestoreSubset.Execute(project.FullName, rootDirectory.FullName, outputDirectory.FullName),
37+
projectArgument, rootDirectoryOption, outputDirectoryOption);
38+
39+
return rootCommand.Invoke(args);

src/dotnet-subset/RestoreSubset.cs

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
using Microsoft.Build.Evaluation;
2+
3+
namespace Nimbleways.Tools.Subset;
4+
internal static class RestoreSubset
5+
{
6+
public static void Execute(string mainProjectPath, string rootFolder, string destinationFolder)
7+
{
8+
if (!IsSameOrUnder(rootFolder, mainProjectPath))
9+
{
10+
throw new ArgumentException($"Project '${mainProjectPath}' must be under the root folder '{rootFolder}'");
11+
}
12+
Directory.CreateDirectory(destinationFolder);
13+
14+
using var projectCollection = new ProjectCollection();
15+
var projectsByFullPath = new Dictionary<string, Project>();
16+
VisitAllProjects(projectCollection, rootFolder, mainProjectPath, projectsByFullPath);
17+
var projectListAsString = string.Join(Environment.NewLine + " - ", projectsByFullPath.Keys);
18+
Console.WriteLine($"Found {projectsByFullPath.Count} project(s) to copy:{Environment.NewLine + " - "}{projectListAsString}");
19+
var filesInvolvedInRestore = projectsByFullPath.Values.SelectMany(project => GetFilesInvolvedInRestore(rootFolder, project));
20+
var nugetConfigFiles = GetNugetConfigFiles(rootFolder, projectsByFullPath);
21+
var allFilesToCopy = filesInvolvedInRestore.Concat(nugetConfigFiles).Distinct().OrderBy(f => f).ToArray();
22+
23+
int copiedFilesCount = 0;
24+
foreach (var file in allFilesToCopy)
25+
{
26+
var destinationFile = Path.Combine(destinationFolder, Path.GetRelativePath(rootFolder, file));
27+
Directory.CreateDirectory(Path.GetDirectoryName(destinationFile)!);
28+
if (!File.Exists(destinationFile))
29+
{
30+
File.Copy(file, destinationFile);
31+
++copiedFilesCount;
32+
}
33+
else if (!AreFilesIdentical(file, destinationFile))
34+
{
35+
string errorMessage = $"ERROR: cannot copy '{file}' to '{destinationFile}': destination file already exist but have different content from source file";
36+
Console.WriteLine(errorMessage);
37+
throw new ArgumentException(errorMessage);
38+
}
39+
}
40+
Console.WriteLine($"Copied {copiedFilesCount} file(s) to '{destinationFolder}'. {allFilesToCopy.Length - copiedFilesCount} file(s) already exist in destination.");
41+
}
42+
43+
private static bool IsSameOrUnder(string rootFolder, string path)
44+
{
45+
var relativePath = Path.GetRelativePath(rootFolder, path);
46+
return relativePath != ".." && !relativePath.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) && !Path.IsPathRooted(relativePath);
47+
}
48+
49+
private static string GetFullPathWithOriginalCase(string path, string? basePath = null)
50+
{
51+
var fullFilePath = basePath != null ? Path.GetFullPath(path, basePath) : Path.GetFullPath(path);
52+
if (!File.Exists(fullFilePath))
53+
{
54+
return fullFilePath;
55+
}
56+
57+
var folder = Path.GetDirectoryName(fullFilePath) ?? throw new ArgumentException("GetDirectoryName returns null", nameof(path));
58+
return Directory.EnumerateFiles(folder, Path.GetFileName(fullFilePath)).First();
59+
}
60+
61+
private static void VisitAllProjects(ProjectCollection projectCollection, string rootFolder, string projectPath, Dictionary<string, Project> projects)
62+
{
63+
if (!Path.IsPathRooted(projectPath))
64+
{
65+
throw new ArgumentException($"projectPath must be absolute: '{projectPath}");
66+
}
67+
var fullPath = GetFullPathWithOriginalCase(projectPath);
68+
if (projects.ContainsKey(fullPath) || !IsSameOrUnder(rootFolder, fullPath))
69+
{
70+
return;
71+
}
72+
73+
var project = projectCollection.LoadProject(fullPath);
74+
projects.Add(fullPath, project);
75+
var r = project.GetItems("ProjectReference");
76+
foreach (var projectReference in r)
77+
{
78+
var path = Path.IsPathRooted(projectReference.EvaluatedInclude) ? projectReference.EvaluatedInclude : Path.Combine(Path.GetDirectoryName(fullPath)!, projectReference.EvaluatedInclude);
79+
VisitAllProjects(projectCollection, rootFolder, path, projects);
80+
}
81+
}
82+
83+
private static string? GetPackagesLockFile(string projectFolder, Project project)
84+
{
85+
var candidateFilenames = new List<string>();
86+
var nuGetLockFilePathPropertyValue = project.GetPropertyValue("NuGetLockFilePath");
87+
if (!string.IsNullOrEmpty(nuGetLockFilePathPropertyValue))
88+
{
89+
candidateFilenames.Add(nuGetLockFilePathPropertyValue);
90+
}
91+
var projectNameWithoutSpaces = Path
92+
.GetFileNameWithoutExtension(project.FullPath)
93+
.Replace(" ", "_", StringComparison.OrdinalIgnoreCase);
94+
candidateFilenames.Add($"packages.{projectNameWithoutSpaces}.lock.json");
95+
candidateFilenames.Add("packages.lock.json");
96+
return candidateFilenames
97+
.Select(name => GetFullPathWithOriginalCase(name, projectFolder))
98+
.FirstOrDefault(File.Exists);
99+
}
100+
private static IEnumerable<string> GetFilesInvolvedInRestore(string rootFolder, Project project)
101+
{
102+
yield return project.FullPath;
103+
104+
var projectFolder = Path.GetDirectoryName(project.FullPath);
105+
if (projectFolder is null || !Directory.Exists(projectFolder))
106+
{
107+
throw new ArgumentException($"'{rootFolder}' doesn't exist");
108+
}
109+
110+
var packagesLockFile = GetPackagesLockFile(projectFolder, project);
111+
if (packagesLockFile is not null)
112+
{
113+
yield return packagesLockFile;
114+
}
115+
116+
foreach (var import in project.Imports)
117+
{
118+
if (IsSameOrUnder(rootFolder, import.ImportedProject.FullPath))
119+
{
120+
yield return import.ImportedProject.FullPath;
121+
}
122+
}
123+
}
124+
private static IEnumerable<string> GetNugetConfigFiles(string rootFolder, Dictionary<string, Project> projects)
125+
{
126+
static void GetNugetConfigFiles(string rootFolder, DirectoryInfo folder, IDictionary<string, string> nugetConfigFiles)
127+
{
128+
if (nugetConfigFiles.ContainsKey(folder.FullName) || !IsSameOrUnder(rootFolder, folder.FullName))
129+
{
130+
return;
131+
}
132+
133+
var f = new[] { "nuget.config", "NuGet.config", "NuGet.Config" }
134+
.Select(name => GetFullPathWithOriginalCase(name, folder.FullName))
135+
.FirstOrDefault(File.Exists);
136+
if (f is not null)
137+
{
138+
nugetConfigFiles.Add(folder.FullName, f);
139+
}
140+
141+
GetNugetConfigFiles(rootFolder, folder.Parent!, nugetConfigFiles);
142+
}
143+
var nugetConfigFilesByFolder = new Dictionary<string, string>();
144+
var csprojDefinedNugetConfigFiles = new List<string>();
145+
foreach (var (projectPath, project) in projects)
146+
{
147+
var restoreConfigFilePropertyValue = project.GetPropertyValue("RestoreConfigFile");
148+
if (!string.IsNullOrEmpty(restoreConfigFilePropertyValue))
149+
{
150+
var fullPath = GetFullPathWithOriginalCase(restoreConfigFilePropertyValue);
151+
if (IsSameOrUnder(rootFolder, fullPath))
152+
{
153+
if (File.Exists(fullPath))
154+
{
155+
csprojDefinedNugetConfigFiles.Add(fullPath);
156+
}
157+
else
158+
{
159+
string errorMessage = $"ERROR: cannot find the file '{restoreConfigFilePropertyValue}' defined in the property 'RestoreConfigFile' of the project '{projectPath}'";
160+
errorMessage += $"{Environment.NewLine}If the path is relative, it is resolved by NuGet against the current working directy, not the project directory";
161+
Console.WriteLine(errorMessage);
162+
throw new ArgumentException(errorMessage);
163+
}
164+
}
165+
}
166+
else
167+
{
168+
GetNugetConfigFiles(rootFolder, Directory.GetParent(projectPath)!, nugetConfigFilesByFolder);
169+
}
170+
}
171+
return nugetConfigFilesByFolder.Values.Concat(csprojDefinedNugetConfigFiles);
172+
}
173+
private static bool AreFilesIdentical(string file1, string file2)
174+
{
175+
const int bytesToRead = 1024 * 10;
176+
177+
using FileStream fs1 = File.Open(file1, FileMode.Open);
178+
using FileStream fs2 = File.Open(file2, FileMode.Open);
179+
180+
if (fs1.Length != fs2.Length)
181+
{
182+
return false;
183+
}
184+
185+
var buffer1 = new byte[bytesToRead];
186+
var buffer2 = new byte[bytesToRead];
187+
var buffer1ReadOnlySpan = (ReadOnlySpan<byte>)buffer1;
188+
var buffer2ReadOnlySpan = (ReadOnlySpan<byte>)buffer2;
189+
int len1, len2;
190+
do
191+
{
192+
len1 = fs1.Read(buffer1, 0, bytesToRead);
193+
len2 = fs2.Read(buffer2, 0, bytesToRead);
194+
195+
if (!buffer1ReadOnlySpan.SequenceEqual(buffer2ReadOnlySpan))
196+
{
197+
return false;
198+
}
199+
}
200+
while (len1 == 0 || len2 == 0);
201+
202+
return true;
203+
}
204+
}

src/dotnet-subset/dotnet-subset.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ The tool is mainly used in Dockerfiles to optimize the docker build caching for
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<None Include="$(RepositoryRoot)README.md" Pack="true" PackagePath="$(PackageReadmeFile)"/>
30+
<None Include="$(RepositoryRoot)README.md" Pack="true" PackagePath="$(PackageReadmeFile)" />
3131
</ItemGroup>
3232

33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.Build" Version="17.2.0" ExcludeAssets="runtime" />
35+
<PackageReference Include="Microsoft.Build.Framework" Version="17.2.0" ExcludeAssets="runtime" />
36+
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
37+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.2.0" ExcludeAssets="runtime" />
38+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.2.0" ExcludeAssets="runtime" />
39+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
40+
</ItemGroup>
41+
3342
</Project>

0 commit comments

Comments
 (0)