Skip to content

Commit

Permalink
#19 WIP nuget support
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 Nov 13, 2023
1 parent 9e8d535 commit 4467adb
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,5 @@ MigrationBackup/

**/coveragereport/**/**
src/**/coverage.json

src/scripts/nuget/**
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
[![Blazor](https://img.shields.io/badge/blazor-5C2D91.svg?style=for-the-badge&logo=blazor&logoColor=white)](https://dotnet.microsoft.com/en-us/apps/aspnet/web-apps/blazor)
[![C#](https://img.shields.io/badge/c%23-239120.svg?style=for-the-badge&logo=c-sharp&logoColor=white)](https://learn.microsoft.com/en-us/dotnet/csharp/)

<!-- [![VB.NET](https://img.shields.io/badge/VB.NET-512BD4.svg?style=for-the-badge&logo=visualbasic&logoColor=white)](https://learn.microsoft.com/en-us/dotnet/csharp/) -->
<!-- [![F#](https://img.shields.io/badge/F%23-3498DB?style=for-the-badge&logo=f-sharp&logoColor=white)](https://fsharp.org) -->

[![License: MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg?style=for-the-badge)](LICENSE) <!-- https://opensource.org/licenses/MIT -->

[![Build / Test (with Reports)](https://github.com/alex-hedley/BlazorInteractive/actions/workflows/build-test.yml/badge.svg)](https://github.com/alex-hedley/BlazorInteractive/actions/workflows/build-test.yml)
Expand All @@ -14,6 +16,7 @@

[![BlazorInteractive Tests](https://gist.githubusercontent.com/alex-hedley/0f6142396b2a1bb0231906adcd51bdcd/raw/0365da5b9d3b05984b8099051b6f5582eff2878b/blazor_interactive_tests.md_badge.svg "BlazorInteractive Tests")](https://gist.github.com/alex-hedley/0f6142396b2a1bb0231906adcd51bdcd)
[![BlazorInteractive Compilation Tests](https://gist.githubusercontent.com/alex-hedley/1a73c5dd2824cc80bfd2c145308dbcf9/raw/c0c284fcc207940aa665cd816a991d92bf94df26/blazor_interactive_compilation_tests.md_badge.svg "BlazorInteractive Compilation Tests")](https://gist.github.com/alex-hedley/1a73c5dd2824cc80bfd2c145308dbcf9)

<!-- [![BlazorInteractive Dependency Management Tests](https://gist.githubusercontent.com/alex-hedley/###/raw/###/blazor_interactive_compilation_tests.md_badge.svg "BlazorInteractive Dependency Management Tests")](https://gist.github.com/alex-hedley/###) -->

## Demo
Expand All @@ -29,6 +32,7 @@
| BlazorInteractive | BlazorWebAssembly | |
| BlazorInteractive.Compilation | | |
| BlazorInteractive.Compilation.Tests | | |
| BlazorInteractive.ConsoleApp | Microsoft.NET.Sdk | |
| BlazorInteractive.DependencyManagement | | |
| BlazorInteractive.DependencyManagement.Tests | | |
| BlazorInteractive.Tests | | |
Expand Down
23 changes: 23 additions & 0 deletions src/.run/BlazorInteractive.ConsoleApp.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BlazorInteractive.ConsoleApp" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/BlazorInteractive.ConsoleApp/bin/Debug/net7.0/BlazorInteractive.ConsoleApp" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/BlazorInteractive.ConsoleApp/bin/Debug/net7.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<envs>
<env name="DOTNET_EnableAVX" value="0" />
</envs>
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/BlazorInteractive.ConsoleApp/BlazorInteractive.ConsoleApp.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net7.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
17 changes: 17 additions & 0 deletions src/.run/BlazorInteractive_ http.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BlazorInteractive: http" type="LaunchSettings" factoryName=".NET Launch Settings Profile">
<option name="LAUNCH_PROFILE_PROJECT_FILE_PATH" value="$PROJECT_DIR$/BlazorInteractive/BlazorInteractive.csproj" />
<option name="LAUNCH_PROFILE_TFM" value="net7.0" />
<option name="LAUNCH_PROFILE_NAME" value="http" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="GENERATE_APPLICATIONHOST_CONFIG" value="1" />
<option name="SHOW_IIS_EXPRESS_OUTPUT" value="0" />
<option name="SEND_DEBUG_REQUEST" value="1" />
<option name="ADDITIONAL_IIS_EXPRESS_ARGUMENTS" value="" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
17 changes: 17 additions & 0 deletions src/.run/BlazorInteractive_ https.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BlazorInteractive: https" type="LaunchSettings" factoryName=".NET Launch Settings Profile">
<option name="LAUNCH_PROFILE_PROJECT_FILE_PATH" value="$PROJECT_DIR$/BlazorInteractive/BlazorInteractive.csproj" />
<option name="LAUNCH_PROFILE_TFM" value="net7.0" />
<option name="LAUNCH_PROFILE_NAME" value="https" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="GENERATE_APPLICATIONHOST_CONFIG" value="1" />
<option name="SHOW_IIS_EXPRESS_OUTPUT" value="0" />
<option name="SEND_DEBUG_REQUEST" value="1" />
<option name="ADDITIONAL_IIS_EXPRESS_ARGUMENTS" value="" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>
26 changes: 26 additions & 0 deletions src/BlazorInteractive.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using BlazorInteractive.DependencyManagement;
using Microsoft.Extensions.Logging;
using OneOf.Types;

namespace BlazorInteractive.ConsoleApp;

class Program
{
private static readonly ILogger _logger;

async static Task Main(string[] args)
{
var nugetPackageGetter = new NuGetPackageGetter(_logger);
var package = await nugetPackageGetter.GetPackage("ironpython");
package.Switch(
success =>
{
Console.WriteLine("Success");
},
failure =>
{
Console.WriteLine("Failure");
}
);
}
}
87 changes: 84 additions & 3 deletions src/BlazorInteractive.DependencyManagement/NuGetPackageGetter.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
using Microsoft.Extensions.Logging;
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Serialization;
using OneOf.Types;

namespace BlazorInteractive.DependencyManagement;

public class NuGetPackageGetter : IPackageGetter
{
private readonly ILogger _logger;

public NuGetPackageGetter(ILogger<NuGetPackageGetter> logger)
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);

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

public Task<PackageResult> GetPackage(string name)
public async Task<PackageResult> GetPackage(string name)
{
throw new NotImplementedException();
Directory.CreateDirectory("nuget");

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}{name}/index.json";
Console.WriteLine(testUrl);
var versionsResponse = await GetAndCache<PackageVersions>($"{packageVersionsBaseUrl}{name}/index.json", $"nuget/{name}versions.json");
var latestVersion = versionsResponse.Versions.Last();

// https://github.com/dotnet/runtime/issues/78991
// DOTNET_EnableAVX=0
var packageUrl = $"{packageVersionsBaseUrl}{name}/{latestVersion}/{name}.{latestVersion}.nupkg";
// var packageUrl = $"{packageVersionsBaseUrl.ToString()}{name.ToString()}/{latestVersion.ToString()}/{name.ToString()}.{latestVersion.ToString()}.nupkg";

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

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

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

// Check folders

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}");
}
}
}

return new PackageResult(new Success());
}

HttpClient client = new();

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});
}
}
6 changes: 6 additions & 0 deletions src/BlazorInteractive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.Dependenc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.DependencyManagement.Tests", "BlazorInteractive.DependencyManagement.Tests\BlazorInteractive.DependencyManagement.Tests.csproj", "{D9397485-D136-4BBB-9B47-80D3AE8AAC69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorInteractive.ConsoleApp", "BlazorInteractive.ConsoleApp\BlazorInteractive.ConsoleApp.csproj", "{5F3F79DB-B425-484E-85F1-5D9F016CF3DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -43,5 +45,9 @@ Global
{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
{5F3F79DB-B425-484E-85F1-5D9F016CF3DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F3F79DB-B425-484E-85F1-5D9F016CF3DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F3F79DB-B425-484E-85F1-5D9F016CF3DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F3F79DB-B425-484E-85F1-5D9F016CF3DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions src/scripts/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Scripts

- [NuGetThosePackages.csx](NuGetThosePackages.csx)

`dotnet script NuGetThosePackages.csx`

> System.IO.DirectoryNotFoundException: Could not find a part of the path '../BlazorInteractive/src/scripts/nuget/index.json'.
`mkdir nuget`
Empty file added src/scripts/nuget/.keep
Empty file.

0 comments on commit 4467adb

Please sign in to comment.