Skip to content

Commit 3cc27b0

Browse files
authored
Remove explicit libraries (#157)
1 parent 38722e9 commit 3cc27b0

17 files changed

Lines changed: 60 additions & 63 deletions
-186 KB
Binary file not shown.
-2.85 MB
Binary file not shown.
-142 KB
Binary file not shown.
-246 KB
Binary file not shown.
-46.3 KB
Binary file not shown.

src/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net9.0</TargetFramework>
55
<LangVersion>latest</LangVersion>
6+
<UsingBrowserRuntimeWorkload>false</UsingBrowserRuntimeWorkload>
67
</PropertyGroup>
78

89
</Project>

src/Try.Core/CodeFile.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ public CodeFileType Type
1919
{
2020
get
2121
{
22-
if (!this.type.HasValue)
22+
if (!type.HasValue)
2323
{
24-
var extension = System.IO.Path.GetExtension(this.Path);
24+
var extension = System.IO.Path.GetExtension(Path);
2525

26-
this.type = extension switch
26+
type = extension switch
2727
{
2828
RazorFileExtension => CodeFileType.Razor,
2929
CsharpFileExtension => CodeFileType.CSharp,
3030
_ => throw new NotSupportedException($"Unsupported extension: {extension}"),
3131
};
3232
}
3333

34-
return this.type.Value;
34+
return type.Value;
3535
}
3636
}
3737
}

src/Try.Core/CompilationService.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.ComponentModel.DataAnnotations;
78
using System.IO;
89
using System.Linq;
@@ -18,7 +19,6 @@
1819
using Microsoft.CodeAnalysis.CSharp;
1920
using Microsoft.CodeAnalysis.Razor;
2021
using Microsoft.JSInterop;
21-
using Try;
2222

2323
public class CompilationService
2424
{
@@ -44,17 +44,18 @@ @using MudBlazor
4444

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

5050
private readonly RazorProjectFileSystem fileSystem = new VirtualRazorProjectFileSystem();
51-
private readonly RazorConfiguration configuration = RazorConfiguration.Create(
51+
private readonly RazorConfiguration configuration = new(
5252
RazorLanguageVersion.Latest,
53-
configurationName: "Blazor",
54-
extensions: Array.Empty<RazorExtension>());
53+
ConfigurationName: "Blazor",
54+
Extensions: ImmutableArray<RazorExtension>.Empty);
5555

5656
public static async Task InitAsync(HttpClient httpClient)
5757
{
58+
5859
var basicReferenceAssemblyRoots = new[]
5960
{
6061
typeof(Console).Assembly, // System.Console
@@ -89,7 +90,7 @@ public static async Task InitAsync(HttpClient httpClient)
8990
.Select(a => a.Value)
9091
.ToList();
9192

92-
baseCompilation = CSharpCompilation.Create(
93+
_baseCompilation = CSharpCompilation.Create(
9394
DefaultRootNamespace,
9495
Array.Empty<SyntaxTree>(),
9596
basicReferenceAssemblies,
@@ -104,7 +105,7 @@ public static async Task InitAsync(HttpClient httpClient)
104105
new KeyValuePair<string, ReportDiagnostic>("CS1702", ReportDiagnostic.Suppress),
105106
}));
106107

107-
cSharpParseOptions = new CSharpParseOptions(LanguageVersion.Preview);
108+
_cSharpParseOptions = new CSharpParseOptions(LanguageVersion.Preview);
108109
}
109110

110111
public async Task<CompileToAssemblyResult> CompileToAssemblyAsync(
@@ -154,10 +155,10 @@ private static CompileToAssemblyResult CompileToAssembly(IReadOnlyList<CompileTo
154155
for (var i = 0; i < cSharpResults.Count; i++)
155156
{
156157
var cSharpResult = cSharpResults[i];
157-
syntaxTrees[i] = CSharpSyntaxTree.ParseText(cSharpResult.Code, cSharpParseOptions, cSharpResult.FilePath);
158+
syntaxTrees[i] = CSharpSyntaxTree.ParseText(cSharpResult.Code, _cSharpParseOptions, cSharpResult.FilePath);
158159
}
159160

160-
var finalCompilation = baseCompilation.AddSyntaxTrees(syntaxTrees);
161+
var finalCompilation = _baseCompilation.AddSyntaxTrees(syntaxTrees);
161162

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

@@ -181,7 +182,7 @@ private static CompileToAssemblyResult CompileToAssembly(IReadOnlyList<CompileTo
181182
return result;
182183
}
183184

184-
private static RazorProjectItem CreateRazorProjectItem(string fileName, string fileContent)
185+
private static VirtualProjectItem CreateRazorProjectItem(string fileName, string fileContent)
185186
{
186187
var fullPath = WorkingDirectory + fileName;
187188

@@ -249,12 +250,12 @@ private async Task<IReadOnlyList<CompileToCSharpResult>> CompileToCSharpAsync(
249250
var tempAssembly = CompileToAssembly(declarations);
250251
if (tempAssembly.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
251252
{
252-
return new[] { new CompileToCSharpResult { Diagnostics = tempAssembly.Diagnostics } };
253+
return [new CompileToCSharpResult { Diagnostics = tempAssembly.Diagnostics }];
253254
}
254255

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

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

@@ -287,7 +288,7 @@ private async Task<IReadOnlyList<CompileToCSharpResult>> CompileToCSharpAsync(
287288
}
288289

289290
private RazorProjectEngine CreateRazorProjectEngine(IReadOnlyList<MetadataReference> references) =>
290-
RazorProjectEngine.Create(this.configuration, this.fileSystem, b =>
291+
RazorProjectEngine.Create(configuration, fileSystem, b =>
291292
{
292293
b.SetRootNamespace(DefaultRootNamespace);
293294
b.AddDefaultImports(DefaultImports);

src/Try.Core/CompileToAssemblyResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
namespace Try.Core
22
{
33
using System.Collections.Generic;
4-
using System.Linq;
54
using Microsoft.CodeAnalysis;
65

76
public class CompileToAssemblyResult
87
{
98
public Compilation Compilation { get; set; }
109

11-
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = Enumerable.Empty<CompilationDiagnostic>();
10+
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = [];
1211

1312
public byte[] AssemblyBytes { get; set; }
1413
}

src/Try.Core/CompileToCSharpResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Try.Core
22
{
33
using System.Collections.Generic;
4-
using System.Linq;
54
using Microsoft.AspNetCore.Razor.Language;
65

76
internal class CompileToCSharpResult
@@ -12,6 +11,6 @@ internal class CompileToCSharpResult
1211

1312
public string FilePath { get; set; }
1413

15-
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = Enumerable.Empty<CompilationDiagnostic>();
14+
public IEnumerable<CompilationDiagnostic> Diagnostics { get; set; } = [];
1615
}
1716
}

0 commit comments

Comments
 (0)