diff --git a/repository-utilities/.editorconfig b/repository-utilities/.editorconfig index c9aa455..4c47a25 100644 --- a/repository-utilities/.editorconfig +++ b/repository-utilities/.editorconfig @@ -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 diff --git a/repository-utilities/RepositoryUtilities.sln b/repository-utilities/RepositoryUtilities.sln index a8c3fdd..ef0b8c2 100644 --- a/repository-utilities/RepositoryUtilities.sln +++ b/repository-utilities/RepositoryUtilities.sln @@ -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 @@ -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 diff --git a/repository-utilities/src/NuGetDownloader/NuGetDownloader.csproj b/repository-utilities/src/NuGetDownloader/NuGetDownloader.csproj new file mode 100644 index 0000000..5e6020a --- /dev/null +++ b/repository-utilities/src/NuGetDownloader/NuGetDownloader.csproj @@ -0,0 +1,14 @@ + + + Exe + net6.0 + enable + + + + diff --git a/repository-utilities/src/NuGetDownloader/Program.cs b/repository-utilities/src/NuGetDownloader/Program.cs new file mode 100644 index 0000000..3046fc5 --- /dev/null +++ b/repository-utilities/src/NuGetDownloader/Program.cs @@ -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(); + + //IEnumerable 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(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"); + } + } + } + } +}