Skip to content

Commit e89f88d

Browse files
committed
Add target pack support and scan project.assets.json files for package downloads
1 parent b3f4a20 commit e89f88d

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/SourceBuild/content/eng/Versions.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<!-- msbuild dependencies -->
3333
<MicrosoftBuildVersion>17.12.6</MicrosoftBuildVersion>
3434
<!-- nuget dependencies -->
35-
<NuGetProtocolVersion>6.12.1</NuGetProtocolVersion>
36-
<NuGetProjectModelVersion>6.12.1</NuGetProjectModelVersion>
35+
<NuGetProtocolVersion>6.13.1</NuGetProtocolVersion>
36+
<NuGetProjectModelVersion>6.13.1</NuGetProjectModelVersion>
3737
<!-- runtime dependencies -->
3838
<MicrosoftExtensionsFileSystemGlobbingVersion>9.0.0</MicrosoftExtensionsFileSystemGlobbingVersion>
3939
<MicrosoftExtensionsLoggingConsoleVersion>9.0.0</MicrosoftExtensionsLoggingConsoleVersion>

src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/WriteSbrpUsageReport.cs

+37-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Xml.Linq;
1010
using Microsoft.Build.Framework;
1111
using Microsoft.Build.Utilities;
12+
using NuGet.LibraryModel;
1213
using NuGet.ProjectModel;
1314

1415
namespace Microsoft.DotNet.UnifiedBuild.Tasks;
@@ -48,8 +49,9 @@ public override bool Execute()
4849
{
4950
Log.LogMessage($"Scanning for SBRP Package Usage...");
5051

51-
ReadSbrpPackages("referencePackages", trackTfms: true);
52-
ReadSbrpPackages("textOnlyPackages", trackTfms: false);
52+
ReadSbrpPackages(Path.Combine("referencePackages", "src"), trackTfms: true);
53+
ReadSbrpPackages(Path.Combine("targetPacks", "ILsrc"), trackTfms: false);
54+
ReadSbrpPackages(Path.Combine("textOnlyPackages", "src"), trackTfms: false);
5355

5456
ScanProjectReferences();
5557

@@ -110,11 +112,10 @@ private void PurgeNonReferencedReferences()
110112
private IEnumerable<PackageInfo> GetUnreferencedSbrps() =>
111113
_sbrpPackages.Values.Where(pkg => pkg.References.Count == 0);
112114

113-
private string GetSbrpPackagesPath(string packageType) => Path.Combine(SbrpRepoSrcPath, packageType, "src");
114-
115-
private void ReadSbrpPackages(string packageType, bool trackTfms)
115+
private void ReadSbrpPackages(string packageSrcRelativePath, bool trackTfms)
116116
{
117-
foreach (string projectPath in Directory.GetFiles(GetSbrpPackagesPath(packageType), "*.csproj", SearchOption.AllDirectories))
117+
string packageSrcPath = Path.Combine(SbrpRepoSrcPath, packageSrcRelativePath);
118+
foreach (string projectPath in Directory.GetFiles(packageSrcPath, "*.csproj", SearchOption.AllDirectories))
118119
{
119120
DirectoryInfo? directory = Directory.GetParent(projectPath);
120121
string version = directory!.Name;
@@ -163,24 +164,39 @@ private void ScanProjectReferences()
163164
LockFile lockFile = new LockFileFormat().Read(projectJsonFile);
164165
foreach (LockFileTargetLibrary lib in lockFile.Targets.SelectMany(t => t.Libraries))
165166
{
166-
if (!_sbrpPackages.TryGetValue(PackageInfo.GetId(lib.Name, lib.Version?.ToString()), out PackageInfo? info))
167-
{
168-
continue;
169-
}
170-
171-
if (!info.References.TryGetValue(lockFile.Path, out HashSet<string>? referencedTfms))
172-
{
173-
referencedTfms = [];
174-
info.References.Add(lockFile.Path, referencedTfms);
175-
}
176-
177167
IEnumerable<string> tfms = lib.CompileTimeAssemblies
178168
.Where(asm => asm.Path.StartsWith("lib") || asm.Path.StartsWith("ref"))
179169
.Select(asm => asm.Path.Split('/')[1]);
180-
foreach (string tfm in tfms)
181-
{
182-
referencedTfms.Add(tfm);
183-
}
170+
171+
TrackPackageReference(lockFile.Path, lib.Name, lib.Version?.ToString(), tfms);
172+
}
173+
174+
foreach (DownloadDependency downloadDep in lockFile.PackageSpec.TargetFrameworks.SelectMany(fx => fx.DownloadDependencies))
175+
{
176+
TrackPackageReference(lockFile.Path, downloadDep.Name, downloadDep.VersionRange.MinVersion?.ToString(), null);
177+
}
178+
}
179+
}
180+
181+
private void TrackPackageReference(string lockFilePath, string? name, string? version, IEnumerable<string>? tfms)
182+
{
183+
string id = PackageInfo.GetId(name, version);
184+
if (!_sbrpPackages.TryGetValue(id, out PackageInfo? info))
185+
{
186+
return;
187+
}
188+
189+
if (!info.References.TryGetValue(lockFilePath, out HashSet<string>? referencedTfms))
190+
{
191+
referencedTfms = [];
192+
info.References.Add(lockFilePath, referencedTfms);
193+
}
194+
195+
if (tfms != null)
196+
{
197+
foreach (string tfm in tfms)
198+
{
199+
referencedTfms!.Add(tfm);
184200
}
185201
}
186202
}

0 commit comments

Comments
 (0)