Skip to content

Commit 44f15a1

Browse files
committed
Fix links to nuget.org
1 parent 62d36da commit 44f15a1

File tree

4 files changed

+74
-41
lines changed

4 files changed

+74
-41
lines changed

src/Shared/Util.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ public static async IAsyncEnumerable<T> Concat<T>(this IAsyncEnumerable<T> a, IE
120120
return null;
121121
}
122122

123+
public static async Task<T?> FirstOrNullAsync<T>(this IAsyncEnumerable<T> source, Func<T, bool> predicate) where T : struct
124+
{
125+
await foreach (var item in source)
126+
{
127+
if (predicate(item))
128+
{
129+
return item;
130+
}
131+
}
132+
133+
return null;
134+
}
135+
136+
public static async Task<T?> FirstOrNullAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task<bool>> predicate) where T : struct
137+
{
138+
await foreach (var item in source)
139+
{
140+
if (await predicate(item))
141+
{
142+
return item;
143+
}
144+
}
145+
146+
return null;
147+
}
148+
123149
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
124150
{
125151
foreach (var item in source)

src/Worker/Lab/CompilerDependencyProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static CompilerDependency createOne(CompilerKind compilerKind)
133133
return new()
134134
{
135135
Info = () => Task.FromResult(new CompilerDependencyInfo(assemblyName: info.AssemblyNames[0],
136-
versionLink: (d) => SimpleNuGetUtil.GetPackageDetailUrl(packageId: info.PackageId, version: d.Version))
136+
versionLink: (d) => SimpleNuGetUtil.GetPackageDetailUrl(packageId: info.PackageId, version: d.Version, fromNuGetOrg: false))
137137
{
138138
VersionSpecifier = specifier,
139139
Configuration = BuildConfiguration.Release,

src/Worker/Lab/NuGetDownloader.cs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal sealed class NuGetDownloaderPlugin(
6767
internal sealed class NuGetDownloader : ICompilerDependencyResolver
6868
{
6969
private readonly SourceCacheContext cacheContext;
70-
private readonly ImmutableArray<AsyncLazy<FindPackageByIdResource>> findPackageByIds;
70+
private readonly ImmutableArray<(AsyncLazy<FindPackageByIdResource> Resource, bool NuGetOrg)> findPackageByIds;
7171

7272
public NuGetDownloader(
7373
IOptions<NuGetDownloaderOptions> options,
@@ -84,17 +84,16 @@ public NuGetDownloader(
8484
new(() => new ServiceIndexResourceV3Provider()),
8585
new(() => new RemoteV3FindPackageByIdResourceProvider()),
8686
];
87-
IEnumerable<string> sourceUrls =
87+
IEnumerable<(string Url, bool NuGetOrg)> sources =
8888
[
89-
"https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json",
90-
"https://api.nuget.org/v3/index.json",
89+
("https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json", false),
90+
("https://api.nuget.org/v3/index.json", true),
9191
];
92-
var repositories = sourceUrls.Select(url => Repository.CreateSource(
93-
providers,
94-
url));
92+
var repositories = sources.Select(t =>
93+
(Source: Repository.CreateSource(providers, t.Url), t.NuGetOrg));
9594
cacheContext = new SourceCacheContext();
96-
findPackageByIds = repositories.SelectAsArray(repository =>
97-
new AsyncLazy<FindPackageByIdResource>(() => repository.GetResourceAsync<FindPackageByIdResource>()));
95+
findPackageByIds = repositories.SelectAsArray(t =>
96+
(new AsyncLazy<FindPackageByIdResource>(() => t.Source.GetResourceAsync<FindPackageByIdResource>()), t.NuGetOrg));
9897
}
9998

10099
public NuGetDownloaderOptions Options { get; }
@@ -105,18 +104,18 @@ public NuGetDownloader(
105104
CompilerVersionSpecifier specifier,
106105
BuildConfiguration configuration)
107106
{
108-
(FindPackageByIdResource? findPackageById, NuGetVersion version) result;
107+
((FindPackageByIdResource Resource, bool NuGetOrg)? Finder, NuGetVersion version) result;
109108
if (specifier is CompilerVersionSpecifier.NuGetLatest)
110109
{
111110
var versions = findPackageByIds.ToAsyncEnumerable()
112-
.SelectAsync(static async lazy => await lazy)
113-
.SelectManyAsync(findPackageById =>
114-
findPackageById.GetAllVersionsAsync(
111+
.SelectAsync(static async t => (Resource: await t.Resource, t.NuGetOrg))
112+
.SelectManyAsync(t =>
113+
t.Resource.GetAllVersionsAsync(
115114
info.PackageId,
116115
cacheContext,
117116
NullLogger.Instance,
118117
CancellationToken.None),
119-
(findPackageById, version) => (findPackageById, version));
118+
(t, version) => (t, version));
120119
result = await versions.FirstOrNullAsync() ??
121120
throw new InvalidOperationException($"Package '{info.PackageId}' not found.");
122121
}
@@ -131,35 +130,38 @@ public NuGetDownloader(
131130

132131
var package = new NuGetDownloadablePackage(specifier, info.PackageFolder, async () =>
133132
{
134-
var (findPackageById, version) = result;
133+
var (finder, version) = result;
135134

136-
var finders = findPackageById != null
137-
? AsyncEnumerable.Create(findPackageById)
138-
: findPackageByIds.ToAsyncEnumerable().SelectAsync(async lazy => await lazy);
135+
var finders = finder is { } f
136+
? AsyncEnumerable.Create(f)
137+
: findPackageByIds.ToAsyncEnumerable().SelectAsync(async t => (Resource: await t.Resource, t.NuGetOrg));
139138

140139
var stream = new MemoryStream();
141-
var success = await finders.AnyAsync(async (findPackageById, cancellationToken) =>
140+
var success = await finders.SelectAsync(async (f) =>
142141
{
142+
bool success;
143143
try
144144
{
145-
return await findPackageById.CopyNupkgToStreamAsync(
145+
success = await f.Resource.CopyNupkgToStreamAsync(
146146
info.PackageId,
147147
version,
148148
stream,
149149
cacheContext,
150150
NullLogger.Instance,
151-
cancellationToken);
151+
CancellationToken.None);
152152
}
153-
catch (Newtonsoft.Json.JsonReaderException) { return false; }
154-
});
153+
catch (Newtonsoft.Json.JsonReaderException) { success = false; }
154+
return (Success: success, f.NuGetOrg);
155+
})
156+
.FirstOrNullAsync(static t => t.Success);
155157

156-
if (!success)
158+
if (success is not { } s)
157159
{
158160
throw new InvalidOperationException(
159161
$"Failed to download '{info.PackageId}' version '{version}'.");
160162
}
161163

162-
return stream;
164+
return new() { Stream = stream, FromNuGetOrg = s.NuGetOrg };
163165
});
164166

165167
return new()
@@ -170,28 +172,30 @@ public NuGetDownloader(
170172
}
171173
}
172174

175+
internal readonly struct NuGetDownloadablePackageResult
176+
{
177+
public required MemoryStream Stream { get; init; }
178+
public required bool FromNuGetOrg { get; init; }
179+
}
180+
173181
internal sealed class NuGetDownloadablePackage(
174182
CompilerVersionSpecifier specifier,
175183
string folder,
176-
Func<Task<MemoryStream>> streamFactory)
184+
Func<Task<NuGetDownloadablePackageResult>> resultFactory)
177185
{
178-
private readonly AsyncLazy<MemoryStream> _stream = new(streamFactory);
186+
private readonly AsyncLazy<NuGetDownloadablePackageResult> _result = new(resultFactory);
179187

180-
private async Task<Stream> GetStreamAsync()
188+
private async Task<NuGetDownloadablePackageResult> GetResultAsync()
181189
{
182-
var result = await _stream;
183-
result.Position = 0;
190+
var result = await _result;
191+
result.Stream.Position = 0;
184192
return result;
185193
}
186194

187-
private async Task<PackageArchiveReader> GetReaderAsync()
188-
{
189-
return new(await GetStreamAsync(), leaveStreamOpen: true);
190-
}
191-
192195
public async Task<CompilerDependencyInfo> GetInfoAsync()
193196
{
194-
using var reader = await GetReaderAsync();
197+
var result = await GetResultAsync();
198+
using var reader = new PackageArchiveReader(result.Stream, leaveStreamOpen: true);
195199
var metadata = reader.NuspecReader.GetRepositoryMetadata();
196200
var identity = reader.GetIdentity();
197201
var version = identity.Version.ToString();
@@ -200,15 +204,16 @@ public async Task<CompilerDependencyInfo> GetInfoAsync()
200204
commitHash: metadata.Commit,
201205
repoUrl: metadata.Url)
202206
{
203-
VersionLink = SimpleNuGetUtil.GetPackageDetailUrl(packageId: identity.Id, version: version),
207+
VersionLink = SimpleNuGetUtil.GetPackageDetailUrl(packageId: identity.Id, version: version, fromNuGetOrg: result.FromNuGetOrg),
204208
VersionSpecifier = specifier,
205209
Configuration = BuildConfiguration.Release,
206210
};
207211
}
208212

209213
public async Task<ImmutableArray<LoadedAssembly>> GetAssembliesAsync()
210214
{
211-
return await NuGetUtil.GetAssembliesFromNupkgAsync(await GetStreamAsync(), folder: folder);
215+
var result = await GetResultAsync();
216+
return await NuGetUtil.GetAssembliesFromNupkgAsync(result.Stream, folder: folder);
212217
}
213218
}
214219

src/WorkerApi/Utils/SimpleNuGetUtil.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ public static string GetPackageVersionListUrl(string packageId)
99
return $"{BaseAddress}{packageId}/versions";
1010
}
1111

12-
public static string GetPackageDetailUrl(string packageId, string version)
12+
public static string GetPackageDetailUrl(string packageId, string version, bool fromNuGetOrg)
1313
{
14-
return $"{BaseAddress}{packageId}/overview/{version}";
14+
return fromNuGetOrg
15+
? $"https://www.nuget.org/packages/{packageId}/{version}"
16+
: $"{BaseAddress}{packageId}/overview/{version}";
1517
}
1618
}

0 commit comments

Comments
 (0)