Skip to content

Commit

Permalink
Remove explicit libraries (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro authored Nov 15, 2024
1 parent 38722e9 commit 3cc27b0
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 63 deletions.
Binary file removed libs/Microsoft.AspNetCore.Mvc.Razor.Extensions.dll
Binary file not shown.
Binary file removed libs/Microsoft.AspNetCore.Razor.Language.dll
Binary file not shown.
Binary file not shown.
Binary file removed libs/Microsoft.CodeAnalysis.Razor.dll
Binary file not shown.
Binary file removed libs/Microsoft.Extensions.ObjectPool.dll
Binary file not shown.
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<UsingBrowserRuntimeWorkload>false</UsingBrowserRuntimeWorkload>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions src/Try.Core/CodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public CodeFileType Type
{
get
{
if (!this.type.HasValue)
if (!type.HasValue)
{
var extension = System.IO.Path.GetExtension(this.Path);
var extension = System.IO.Path.GetExtension(Path);

this.type = extension switch
type = extension switch
{
RazorFileExtension => CodeFileType.Razor,
CsharpFileExtension => CodeFileType.CSharp,
_ => throw new NotSupportedException($"Unsupported extension: {extension}"),
};
}

return this.type.Value;
return type.Value;
}
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/Try.Core/CompilationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
Expand All @@ -18,7 +19,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.JSInterop;
using Try;

public class CompilationService
{
Expand All @@ -44,17 +44,18 @@ @using MudBlazor

// Creating the initial compilation + reading references is on the order of 250ms without caching
// so making sure it doesn't happen for each run.
private static CSharpCompilation baseCompilation;
private static CSharpParseOptions cSharpParseOptions;
private static CSharpCompilation _baseCompilation;
private static CSharpParseOptions _cSharpParseOptions;

private readonly RazorProjectFileSystem fileSystem = new VirtualRazorProjectFileSystem();
private readonly RazorConfiguration configuration = RazorConfiguration.Create(
private readonly RazorConfiguration configuration = new(
RazorLanguageVersion.Latest,
configurationName: "Blazor",
extensions: Array.Empty<RazorExtension>());
ConfigurationName: "Blazor",
Extensions: ImmutableArray<RazorExtension>.Empty);

public static async Task InitAsync(HttpClient httpClient)
{

var basicReferenceAssemblyRoots = new[]
{
typeof(Console).Assembly, // System.Console
Expand Down Expand Up @@ -89,7 +90,7 @@ public static async Task InitAsync(HttpClient httpClient)
.Select(a => a.Value)
.ToList();

baseCompilation = CSharpCompilation.Create(
_baseCompilation = CSharpCompilation.Create(
DefaultRootNamespace,
Array.Empty<SyntaxTree>(),
basicReferenceAssemblies,
Expand All @@ -104,7 +105,7 @@ public static async Task InitAsync(HttpClient httpClient)
new KeyValuePair<string, ReportDiagnostic>("CS1702", ReportDiagnostic.Suppress),
}));

cSharpParseOptions = new CSharpParseOptions(LanguageVersion.Preview);
_cSharpParseOptions = new CSharpParseOptions(LanguageVersion.Preview);
}

public async Task<CompileToAssemblyResult> CompileToAssemblyAsync(
Expand Down Expand Up @@ -154,10 +155,10 @@ private static CompileToAssemblyResult CompileToAssembly(IReadOnlyList<CompileTo
for (var i = 0; i < cSharpResults.Count; i++)
{
var cSharpResult = cSharpResults[i];
syntaxTrees[i] = CSharpSyntaxTree.ParseText(cSharpResult.Code, cSharpParseOptions, cSharpResult.FilePath);
syntaxTrees[i] = CSharpSyntaxTree.ParseText(cSharpResult.Code, _cSharpParseOptions, cSharpResult.FilePath);
}

var finalCompilation = baseCompilation.AddSyntaxTrees(syntaxTrees);
var finalCompilation = _baseCompilation.AddSyntaxTrees(syntaxTrees);

var compilationDiagnostics = finalCompilation.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Info);

Expand All @@ -181,7 +182,7 @@ private static CompileToAssemblyResult CompileToAssembly(IReadOnlyList<CompileTo
return result;
}

private static RazorProjectItem CreateRazorProjectItem(string fileName, string fileContent)
private static VirtualProjectItem CreateRazorProjectItem(string fileName, string fileContent)
{
var fullPath = WorkingDirectory + fileName;

Expand Down Expand Up @@ -249,12 +250,12 @@ private async Task<IReadOnlyList<CompileToCSharpResult>> CompileToCSharpAsync(
var tempAssembly = CompileToAssembly(declarations);
if (tempAssembly.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
{
return new[] { new CompileToCSharpResult { Diagnostics = tempAssembly.Diagnostics } };
return [new CompileToCSharpResult { Diagnostics = tempAssembly.Diagnostics }];
}

// Add the 'temp' compilation as a metadata reference
var references = new List<MetadataReference>(baseCompilation.References) { tempAssembly.Compilation.ToMetadataReference() };
projectEngine = this.CreateRazorProjectEngine(references);
var references = new List<MetadataReference>(_baseCompilation.References) { tempAssembly.Compilation.ToMetadataReference() };
projectEngine = CreateRazorProjectEngine(references);

await (updateStatusFunc?.Invoke("Preparing Project") ?? Task.CompletedTask);

Expand Down Expand Up @@ -287,7 +288,7 @@ private async Task<IReadOnlyList<CompileToCSharpResult>> CompileToCSharpAsync(
}

private RazorProjectEngine CreateRazorProjectEngine(IReadOnlyList<MetadataReference> references) =>
RazorProjectEngine.Create(this.configuration, this.fileSystem, b =>
RazorProjectEngine.Create(configuration, fileSystem, b =>
{
b.SetRootNamespace(DefaultRootNamespace);
b.AddDefaultImports(DefaultImports);
Expand Down
3 changes: 1 addition & 2 deletions src/Try.Core/CompileToAssemblyResult.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace Try.Core
{
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;

public class CompileToAssemblyResult
{
public Compilation Compilation { get; set; }

public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = Enumerable.Empty<CompilationDiagnostic>();
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = [];

public byte[] AssemblyBytes { get; set; }
}
Expand Down
3 changes: 1 addition & 2 deletions src/Try.Core/CompileToCSharpResult.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Try.Core
{
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;

internal class CompileToCSharpResult
Expand All @@ -12,6 +11,6 @@ internal class CompileToCSharpResult

public string FilePath { get; set; }

public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = Enumerable.Empty<CompilationDiagnostic>();
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = [];
}
}
4 changes: 2 additions & 2 deletions src/Try.Core/NotFoundProjectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal class NotFoundProjectItem : RazorProjectItem
{
public NotFoundProjectItem(string basePath, string path)
{
this.BasePath = basePath;
this.FilePath = path;
BasePath = basePath;
FilePath = path;
}

public override string BasePath { get; }
Expand Down
33 changes: 13 additions & 20 deletions src/Try.Core/Try.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>Microsoft.CodeAnalysis.Razor.Test</AssemblyName>
<KeyOriginatorFile>$(PkgMicrosoft_DotNet_Arcade_Sdk)\tools\snk\AspNetCore.snk</KeyOriginatorFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="System.Net.Http.Json" Version="9.0.0" />
<PackageReference Include="MudBlazor" Version="*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0-2.24555.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="4.13.0-2.24555.1" />
<PackageReference Include="Microsoft.DotNet.Arcade.Sdk" Version="9.0.0-beta.24372.7" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.Net.Compilers.Razor.Toolset" Version="9.0.0-preview.24555.4" PrivateAssets="all" IncludeAssets="none" GeneratePathProperty="true" />
</ItemGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<Reference Include="$(PkgMicrosoft_Net_Compilers_Razor_Toolset)\source-generators\Microsoft.CodeAnalysis.Razor.Compiler.dll" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UserComponents\Try.UserComponents.csproj" />
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<!-- https://github.com/dotnet/razor/issues/9494#issuecomment-2438228960 -->
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions">
<HintPath>..\..\libs\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Razor.Language">
<HintPath>..\..\libs\Microsoft.AspNetCore.Razor.Language.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Razor.Utilities.Shared">
<HintPath>..\..\libs\Microsoft.AspNetCore.Razor.Utilities.Shared.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis.Razor">
<HintPath>..\..\libs\Microsoft.CodeAnalysis.Razor.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.ObjectPool">
<HintPath>..\..\libs\Microsoft.Extensions.ObjectPool.dll</HintPath>
</Reference>
<ProjectReference Include="..\UserComponents\Try.UserComponents.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/Try.Core/VirtualProjectFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Try.Core
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
Expand All @@ -9,15 +8,15 @@ internal class VirtualRazorProjectFileSystem : RazorProjectFileSystem
{
public override IEnumerable<RazorProjectItem> EnumerateItems(string basePath)
{
this.NormalizeAndEnsureValidPath(basePath);
NormalizeAndEnsureValidPath(basePath);
return Enumerable.Empty<RazorProjectItem>();
}

public override RazorProjectItem GetItem(string path) => this.GetItem(path, fileKind: null);
public override RazorProjectItem GetItem(string path) => GetItem(path, fileKind: null);

public override RazorProjectItem GetItem(string path, string fileKind)
{
this.NormalizeAndEnsureValidPath(path);
NormalizeAndEnsureValidPath(path);
return new NotFoundProjectItem(string.Empty, path);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Try.Core/VirtualProjectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Try.Core

internal class VirtualProjectItem : RazorProjectItem
{
private readonly byte[] content;
private readonly byte[] _content;

public VirtualProjectItem(
string basePath,
Expand All @@ -15,14 +15,14 @@ public VirtualProjectItem(
string fileKind,
byte[] content)
{
this.BasePath = basePath;
this.FilePath = filePath;
this.PhysicalPath = physicalPath;
this.RelativePhysicalPath = relativePhysicalPath;
this.content = content;
BasePath = basePath;
FilePath = filePath;
PhysicalPath = physicalPath;
RelativePhysicalPath = relativePhysicalPath;
_content = content;

// Base class will detect based on file-extension.
this.FileKind = fileKind ?? base.FileKind;
FileKind = fileKind ?? base.FileKind;
}

public override string BasePath { get; }
Expand All @@ -37,6 +37,6 @@ public VirtualProjectItem(

public override bool Exists => true;

public override Stream Read() => new MemoryStream(this.content);
public override Stream Read() => new MemoryStream(this._content);
}
}
2 changes: 0 additions & 2 deletions src/TryMudBlazor.Client/TryMudBlazor.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.0" />
<PackageReference Include="MudBlazor" Version="*" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 0 additions & 4 deletions src/TryMudBlazor.Server/TryMudBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@
<ProjectReference Include="..\MudBlazor.Examples.Data\MudBlazor.Examples.Data.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\PublishProfiles\" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
</packageSources>
</configuration>

0 comments on commit 3cc27b0

Please sign in to comment.