Skip to content

Commit

Permalink
WIP: #19 NuGet Packages
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Anderson <jamsidedown@users.noreply.github.com>
Co-authored-by: Daniel P. Doyle <3020488+TheDanielDoyle@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 28, 2023
1 parent 4c03e96 commit 1c77554
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/NUGET.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# NuGet

- [NuGetThosePackages.csx](../src/scripts/NuGetThosePackages.csx)

Running `dotnet add package`

```dotnetcli
info : GET https://api.nuget.org/v3/registration5-gz-semver2/ironpython/index.json
info : OK https://api.nuget.org/v3/registration5-gz-semver2/ironpython/index.json 165ms
info : Restoring packages for /Users/robert.anderson/Documents/code/playground/nuget_testing/nuget_testing.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/ironpython/index.json
info : OK https://api.nuget.org/v3-flatcontainer/ironpython/index.json 177ms
info : GET https://api.nuget.org/v3-flatcontainer/ironpython/3.4.1/ironpython.3.4.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/ironpython/3.4.1/ironpython.3.4.1.nupkg 445ms
info : GET https://api.nuget.org/v3-flatcontainer/dynamiclanguageruntime/index.json
info : GET https://api.nuget.org/v3-flatcontainer/mono.unix/index.json
info : OK https://api.nuget.org/v3-flatcontainer/dynamiclanguageruntime/index.json 158ms
info : GET https://api.nuget.org/v3-flatcontainer/dynamiclanguageruntime/1.3.4/dynamiclanguageruntime.1.3.4.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/mono.unix/index.json 166ms
info : GET https://api.nuget.org/v3-flatcontainer/mono.unix/7.1.0-final.1.21458.1/mono.unix.7.1.0-final.1.21458.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/dynamiclanguageruntime/1.3.4/dynamiclanguageruntime.1.3.4.nupkg 86ms
info : OK https://api.nuget.org/v3-flatcontainer/mono.unix/7.1.0-final.1.21458.1/mono.unix.7.1.0-final.1.21458.1.nupkg 92ms
```
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- [Acknowledgements](ACKNOWLEDGEMENTS.md)
- [Changelog](CHANGELOG.md)

- [NuGet](NUGET.md)

- [Tests](TESTS.md)
- [Errors](ERRORS.md)
- [Hack](HACKS.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorInteractive.DependencyManagement\BlazorInteractive.DependencyManagement.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using OneOf.Types;

namespace BlazorInteractive.DependencyManagement.Tests;

public class NuGetPackageGetterTest
{
private readonly Mock<ILogger<NuGetPackageGetter>> _logger;
// private readonly CancellationToken _defaultCancellationToken;

public NuGetPackageGetterTest()
{
_logger = new Mock<ILogger<NuGetPackageGetter>>();
// _defaultCancellationToken = CancellationToken.None;
}

[Fact]
public async Task GetPackage_WithValidName_ReturnsSuccess()
{
var packageName = "Package";
var nugetPackageGetter = new NuGetPackageGetter(_logger.Object);

var result = await nugetPackageGetter.GetPackage(packageName);
result.Value.Should().BeOfType<Success>();
}

[Fact]
public async Task GetPackage_WithInvalidName_ReturnsFailure()
{
var packageName = "Package";
var nugetPackageGetter = new NuGetPackageGetter(_logger.Object);

var result = await nugetPackageGetter.GetPackage(packageName);
result.Value.Should().BeOfType<Failure>();
}

}
1 change: 1 addition & 0 deletions src/BlazorInteractive.DependencyManagement.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="OneOf" Version="3.0.255" />
<PackageReference Include="OneOf.SourceGenerator" Version="3.0.255" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/BlazorInteractive.DependencyManagement/IPackageGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BlazorInteractive.DependencyManagement;

public interface IPackageGetter
{
// GetVersions()
Task<PackageResult> GetPackage(string name);
}
18 changes: 18 additions & 0 deletions src/BlazorInteractive.DependencyManagement/NuGetPackageGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.Logging;

namespace BlazorInteractive.DependencyManagement;

public class NuGetPackageGetter : IPackageGetter
{
private readonly ILogger _logger;

public NuGetPackageGetter(ILogger<NuGetPackageGetter> logger)
{
_logger = logger;
}

public Task<PackageResult> GetPackage(string name)
{
throw new NotImplementedException();
}
}
11 changes: 11 additions & 0 deletions src/BlazorInteractive.DependencyManagement/Results/Failure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BlazorInteractive.DependencyManagement;

public sealed record Failure(string errorMessage) {

public Failure(Exception ex, string errorMessage) : this(errorMessage)
{
Exception = ex;
}

public Exception? Exception { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using OneOf;
using OneOf.Types;

namespace BlazorInteractive.DependencyManagement;

[GenerateOneOf]
public partial class PackageResult : OneOfBase<Success, Failure> { }
12 changes: 12 additions & 0 deletions src/BlazorInteractive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.Compilati
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.Compilation.Tests", "BlazorInteractive.Compilation.Tests\BlazorInteractive.Compilation.Tests.csproj", "{E7183565-C108-4BFC-B5FF-638F7DB16A48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.DependencyManagement", "BlazorInteractive.DependencyManagement\BlazorInteractive.DependencyManagement.csproj", "{21C38526-0C2F-41FD-BA5B-6194A273F47F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.DependencyManagement.Tests", "BlazorInteractive.DependencyManagement.Tests\BlazorInteractive.DependencyManagement.Tests.csproj", "{D9397485-D136-4BBB-9B47-80D3AE8AAC69}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,5 +35,13 @@ Global
{E7183565-C108-4BFC-B5FF-638F7DB16A48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7183565-C108-4BFC-B5FF-638F7DB16A48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7183565-C108-4BFC-B5FF-638F7DB16A48}.Release|Any CPU.Build.0 = Release|Any CPU
{21C38526-0C2F-41FD-BA5B-6194A273F47F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21C38526-0C2F-41FD-BA5B-6194A273F47F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21C38526-0C2F-41FD-BA5B-6194A273F47F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21C38526-0C2F-41FD-BA5B-6194A273F47F}.Release|Any CPU.Build.0 = Release|Any CPU
{D9397485-D136-4BBB-9B47-80D3AE8AAC69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9397485-D136-4BBB-9B47-80D3AE8AAC69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9397485-D136-4BBB-9B47-80D3AE8AAC69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9397485-D136-4BBB-9B47-80D3AE8AAC69}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# src

- [data](data/)
- [Scripts](scripts/README.md)
75 changes: 75 additions & 0 deletions src/scripts/NuGetThosePackages.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.IO.Compression;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;

record IndexResource([property:JsonPropertyName("@Id")] string Id, [property:JsonPropertyName("@Type")] string Type, string Comment);
record IndexResponse(string Version, List<IndexResource> Resources);

record PackageVersions(List<string> Versions);

var client = new HttpClient();

async Task<T> GetAndCache<T>(string url, string file)
{
string json;
if (!File.Exists(file))
{
json = await client.GetStringAsync(url);
await File.WriteAllTextAsync(file, json);
}
else
{
json = await File.ReadAllTextAsync(file);
}

return JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions {PropertyNameCaseInsensitive = true});
}

var nugetIndexResponse = await GetAndCache<IndexResponse>("https://api.nuget.org/v3/index.json", "nuget/index.json");
var packageVersionsBaseUrl = nugetIndexResponse.Resources.First(resource => resource.Type == "PackageBaseAddress/3.0.0").Id;

var testUrl = $"{packageVersionsBaseUrl}ironpython/index.json";
Console.WriteLine(testUrl);
var versionsResponse = await GetAndCache<PackageVersions>($"{packageVersionsBaseUrl}ironpython/index.json", "nuget/ironpythonversions.json");
var latestVersion = versionsResponse.Versions.Last();

var packageUrl = $"{packageVersionsBaseUrl}ironpython/{latestVersion}/ironpython.{latestVersion}.nupkg";

if (!File.Exists($"nuget/ironpython.{latestVersion}.nupkg"))
{
using (var localFile = File.OpenWrite($"nuget/ironpython.{latestVersion}.nupkg"))
{
var packageZip = await client.GetStreamAsync(packageUrl);
await packageZip.CopyToAsync(localFile);
}
}

var extractDir = $"nuget/ironpython.{latestVersion}";
if (!Directory.Exists(extractDir))
{
Directory.CreateDirectory(extractDir);
}

using (var zip = ZipFile.OpenRead($"nuget/ironpython.{latestVersion}.nupkg"))
{
foreach (var file in zip.Entries)
{
var target = $"{extractDir}/{file.Name}";
if (File.Exists(target))
{
continue;
}

if (file.Name.EndsWith(".dll") && file.FullName.Contains("net6"))
{
file.ExtractToFile(target);
Console.WriteLine($"Extracted {file.FullName}");
}
else if (file.Name.EndsWith("nuspec"))
{
file.ExtractToFile(target);
Console.WriteLine($"Extracted {file.FullName}");
}
}
}
3 changes: 3 additions & 0 deletions src/scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Scripts

- [NuGetThosePackages.csx](NuGetThosePackages.csx)

0 comments on commit 1c77554

Please sign in to comment.