Skip to content

Commit

Permalink
Add NuGet downloader project
Browse files Browse the repository at this point in the history
  • Loading branch information
acidicMercury8 committed Jan 4, 2024
1 parent b1a60cf commit 29ce396
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
6 changes: 3 additions & 3 deletions repository-utilities/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Prefer "var" everywhere
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = false
csharp_style_var_elsewhere = false

# Space preferences
csharp_space_after_cast = true
Expand Down
8 changes: 7 additions & 1 deletion repository-utilities/RepositoryUtilities.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitBatchUpdate", "src\GitBatchUpdate\GitBatchUpdate.csproj", "{9120DF65-ABDC-4AF2-8734-402E0CD1362B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitBatchUpdate", "src\GitBatchUpdate\GitBatchUpdate.csproj", "{9120DF65-ABDC-4AF2-8734-402E0CD1362B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NuGetDownloader", "src\NuGetDownloader\NuGetDownloader.csproj", "{714CD295-FE7D-45A8-9DC9-9A0C6D8D8847}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,6 +29,10 @@ Global
{9120DF65-ABDC-4AF2-8734-402E0CD1362B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9120DF65-ABDC-4AF2-8734-402E0CD1362B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9120DF65-ABDC-4AF2-8734-402E0CD1362B}.Release|Any CPU.Build.0 = Release|Any CPU
{714CD295-FE7D-45A8-9DC9-9A0C6D8D8847}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{714CD295-FE7D-45A8-9DC9-9A0C6D8D8847}.Debug|Any CPU.Build.0 = Debug|Any CPU
{714CD295-FE7D-45A8-9DC9-9A0C6D8D8847}.Release|Any CPU.ActiveCfg = Release|Any CPU
{714CD295-FE7D-45A8-9DC9-9A0C6D8D8847}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 14 additions & 0 deletions repository-utilities/src/NuGetDownloader/NuGetDownloader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<!--
<ItemGroup>
<PackageReference Include="NuGet.Common" Version="6.8.0" />
<PackageReference Include="NuGet.Protocol" Version="6.8.0" />
</ItemGroup>
-->
</Project>
89 changes: 89 additions & 0 deletions repository-utilities/src/NuGetDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace NuGetDownloader;

internal record class PackageVersions(
string[] Versions);

internal class Program {
public static async Task Main(string[] args) {
string packageId = args[0];

//ILogger logger = NullLogger.Instance;
//CancellationToken cancellationToken = CancellationToken.None;

//SourceCacheContext cache = new();
//SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
//FindPackageByIdResource packageResource = await repository.GetResourceAsync<FindPackageByIdResource>();

//IEnumerable<NuGetVersion> versions = await packageResource.GetAllVersionsAsync(packageId,
// cache, logger, cancellationToken);
//Console.WriteLine($"Versions:");
//foreach (NuGetVersion version in versions) {
// Console.WriteLine($"{version}");
//}

//Console.WriteLine();

//string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

//Console.WriteLine("Downloaded:");
//foreach (NuGetVersion version in versions) {
// using MemoryStream packageStream = new();
// await packageResource.CopyNupkgToStreamAsync(packageId, version, packageStream,
// cache, logger, cancellationToken);
// Console.WriteLine($"{packageId} {version}");

// string packageFolderPath = Path.Combine(currentDirectory, "pkg", packageId);
// if (!Directory.Exists(packageFolderPath)) {
// Directory.CreateDirectory(packageFolderPath.ToLower());
// }

// string packageFilePath = Path.Combine(packageFolderPath,
// packageId.ToLower() + "." + version.ToString().ToLower() + ".nupkg");

// packageStream.Seek(0, SeekOrigin.Begin);
// using FileStream fileStream = new(packageFilePath, FileMode.Create, FileAccess.Write);
// packageStream.CopyTo(fileStream);
//}

HttpClient nugetV3Client = new() {
BaseAddress = new Uri("https://api.nuget.org/v3-flatcontainer"),
};
HttpClient nugetV2Client = new() {
BaseAddress = new Uri("https://www.nuget.org/api/v2"),
};

Console.WriteLine($"{packageId.ToLower()}:");

string packageVersionsRequestUri = nugetV3Client.BaseAddress.ToString() + $"/{packageId.ToLower()}/index.json";
PackageVersions? packageVersions = await nugetV3Client.GetFromJsonAsync<PackageVersions?>(packageVersionsRequestUri);

string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
string packageFolderPath = Path.Combine(currentDirectory, "pkg", packageId.ToLower());
if (!Directory.Exists(packageFolderPath)) {
Directory.CreateDirectory(packageFolderPath.ToLower());
}

if (packageVersions is not null) {
foreach (string packageVersion in packageVersions.Versions) {
using Stream packageStream = await nugetV2Client.GetStreamAsync(nugetV2Client.BaseAddress.ToString() + $"/package/{packageId.ToLower()}/{packageVersion.ToLower()}");
using FileStream packageFileStream = new(Path.Combine($"{packageFolderPath}", $"{packageId.ToLower()}.{packageVersion.ToLower()}.nupkg"), FileMode.Create, FileAccess.Write);
await packageStream.CopyToAsync(packageFileStream);
Console.WriteLine($"{packageId.ToLower()}.{packageVersion.ToLower()}.nupkg");

using HttpResponseMessage symbolPackageResponce = await nugetV2Client.GetAsync(nugetV2Client.BaseAddress.ToString() + $"/symbolpackage/{packageId.ToLower()}/{packageVersion.ToLower()}");
if (symbolPackageResponce.IsSuccessStatusCode) {
using Stream symbolPackageStream = await nugetV2Client.GetStreamAsync(nugetV2Client.BaseAddress.ToString() + $"/symbolpackage/{packageId.ToLower()}/{packageVersion.ToLower()}");
using FileStream symbolPackageFileStream = new(Path.Combine($"{packageFolderPath}", $"{packageId.ToLower()}.{packageVersion.ToLower()}.snupkg"), FileMode.Create, FileAccess.Write);
await symbolPackageStream.CopyToAsync(symbolPackageFileStream);
Console.WriteLine($"{packageId.ToLower()}.{packageVersion.ToLower()}.snupkg");
}
}
}
}
}

0 comments on commit 29ce396

Please sign in to comment.