From a04c62b92eb6e1498b9c3e728c9e01386bd4a043 Mon Sep 17 00:00:00 2001 From: Tareq Imbasher Date: Thu, 7 Dec 2023 22:31:05 +0200 Subject: [PATCH] WIP ASP.NET --- .../editor/providers/snippets/csharp.ts | 9 +++++++++ src/Core/NetPad.Compilation/SystemAssemblies.cs | 3 +++ .../NetPad.Domain/DotNet/DotNetCSharpProject.cs | 10 ++++++++-- src/Core/NetPad.Domain/DotNet/ProjectOutputType.cs | 6 ++++++ src/Core/NetPad.Domain/Scripts/ScriptConfig.cs | 13 ++++++++++++- .../ExternalProcessScriptRuntime.Setup.cs | 2 +- .../EntityFrameworkDatabaseScaffolder.cs | 2 +- .../NetPad.Plugins.OmniSharp/ScriptProject.cs | 7 ++----- .../Services/AppOmniSharpServer.cs | 1 + 9 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Apps/NetPad.Apps.App/App/src/core/@application/editor/providers/snippets/csharp.ts b/src/Apps/NetPad.Apps.App/App/src/core/@application/editor/providers/snippets/csharp.ts index 12a7cfa3..b82dc024 100644 --- a/src/Apps/NetPad.Apps.App/App/src/core/@application/editor/providers/snippets/csharp.ts +++ b/src/Apps/NetPad.Apps.App/App/src/core/@application/editor/providers/snippets/csharp.ts @@ -506,5 +506,14 @@ export const snippets = { "}$0" ], description: "create xunit test method" + }, + "Web App (ASP.NET)": { + prefix: "webapp", + body: [ + "var app = WebApplication.CreateBuilder().Build();", + "app.UseWelcomePage();", + "await app.RunAsync(\"http://localhost:${0:5678}\");", + ], + description: "create xunit test method" } } diff --git a/src/Core/NetPad.Compilation/SystemAssemblies.cs b/src/Core/NetPad.Compilation/SystemAssemblies.cs index fb48271e..daf1270d 100644 --- a/src/Core/NetPad.Compilation/SystemAssemblies.cs +++ b/src/Core/NetPad.Compilation/SystemAssemblies.cs @@ -63,6 +63,9 @@ private static HashSet GetReferenceAssemblyLocationsFromDotNetRoot(IDotN } return Directory.GetFiles(assembliesDirectory, "*.dll") + .Union( + Directory.GetFiles("/opt/dotnet/packs/Microsoft.AspNetCore.App.Ref/7.0.10/ref/net7.0", "*.dll") + ) .Where(a => !a.Contains("VisualBasic")) .ToHashSet(); } diff --git a/src/Core/NetPad.Domain/DotNet/DotNetCSharpProject.cs b/src/Core/NetPad.Domain/DotNet/DotNetCSharpProject.cs index ec2e0b0b..0f1de1a3 100644 --- a/src/Core/NetPad.Domain/DotNet/DotNetCSharpProject.cs +++ b/src/Core/NetPad.Domain/DotNet/DotNetCSharpProject.cs @@ -73,7 +73,11 @@ public DotNetCSharpProject( /// The .NET framework to target. /// The output type of the project. /// If true, will delete the project directory if it already exists on disk. - public virtual async Task CreateAsync(DotNetFrameworkVersion targetDotNetFrameworkVersion, ProjectOutputType outputType, bool deleteExisting = false) + public virtual async Task CreateAsync( + DotNetFrameworkVersion targetDotNetFrameworkVersion, + ProjectOutputType outputType, + ProjectSdkType sdkType = ProjectSdkType.NetCoreApp, + bool deleteExisting = false) { if (deleteExisting) { @@ -84,7 +88,9 @@ public virtual async Task CreateAsync(DotNetFrameworkVersion targetDotNetFramewo string dotnetOutputType = outputType == ProjectOutputType.Executable ? "Exe" : "Library"; - string xml = $@" + var sdk = sdkType == ProjectSdkType.NetCoreApp ? "Microsoft.NET.Sdk" : "Microsoft.NET.Sdk.Web"; + + string xml = $@" {dotnetOutputType} diff --git a/src/Core/NetPad.Domain/DotNet/ProjectOutputType.cs b/src/Core/NetPad.Domain/DotNet/ProjectOutputType.cs index 728dea7a..c7fea23d 100644 --- a/src/Core/NetPad.Domain/DotNet/ProjectOutputType.cs +++ b/src/Core/NetPad.Domain/DotNet/ProjectOutputType.cs @@ -5,3 +5,9 @@ public enum ProjectOutputType Library, Executable } + +public enum ProjectSdkType +{ + NetCoreApp, + AspNetCoreApp +} diff --git a/src/Core/NetPad.Domain/Scripts/ScriptConfig.cs b/src/Core/NetPad.Domain/Scripts/ScriptConfig.cs index 4a6f4fca..57509bba 100644 --- a/src/Core/NetPad.Domain/Scripts/ScriptConfig.cs +++ b/src/Core/NetPad.Domain/Scripts/ScriptConfig.cs @@ -120,6 +120,17 @@ public static class ScriptConfigDefaults //"System.Transactions", "System.Xml", "System.Xml.Linq", - "System.Xml.XPath" + "System.Xml.XPath", + + + "System.Net.Http.Json", + "Microsoft.AspNetCore.Builder", + "Microsoft.AspNetCore.Hosting", + "Microsoft.AspNetCore.Http", + "Microsoft.AspNetCore.Routing", + "Microsoft.Extensions.Configuration", + "Microsoft.Extensions.DependencyInjection", + "Microsoft.Extensions.Hosting", + "Microsoft.Extensions.Logging", }; } diff --git a/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.Setup.cs b/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.Setup.cs index b5e01c85..801f56b3 100644 --- a/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.Setup.cs +++ b/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.Setup.cs @@ -283,7 +283,7 @@ private string GenerateRuntimeConfigFileContents(RunDependencies runDependencies ""runtimeOptions"": {{ ""tfm"": ""{tfm}"", ""framework"": {{ - ""name"": ""Microsoft.NETCore.App"", + ""name"": ""Microsoft.AspNetCore.App"", ""version"": ""{runtimeVersion}"" }}, ""rollForward"": ""Minor"", diff --git a/src/Infrastructure/NetPad.Infrastructure/Data/EntityFrameworkCore/Scaffolding/EntityFrameworkDatabaseScaffolder.cs b/src/Infrastructure/NetPad.Infrastructure/Data/EntityFrameworkCore/Scaffolding/EntityFrameworkDatabaseScaffolder.cs index 25eaf716..8147d283 100644 --- a/src/Infrastructure/NetPad.Infrastructure/Data/EntityFrameworkCore/Scaffolding/EntityFrameworkDatabaseScaffolder.cs +++ b/src/Infrastructure/NetPad.Infrastructure/Data/EntityFrameworkCore/Scaffolding/EntityFrameworkDatabaseScaffolder.cs @@ -51,7 +51,7 @@ public EntityFrameworkDatabaseScaffolder( public async Task ScaffoldAsync() { - await _project.CreateAsync(_targetFrameworkVersion, ProjectOutputType.Library, true); + await _project.CreateAsync(_targetFrameworkVersion, ProjectOutputType.Library, ProjectSdkType.NetCoreApp, true); try { diff --git a/src/Plugins/NetPad.Plugins.OmniSharp/ScriptProject.cs b/src/Plugins/NetPad.Plugins.OmniSharp/ScriptProject.cs index 4c6544b7..931d9e8d 100644 --- a/src/Plugins/NetPad.Plugins.OmniSharp/ScriptProject.cs +++ b/src/Plugins/NetPad.Plugins.OmniSharp/ScriptProject.cs @@ -40,12 +40,9 @@ public ScriptProject( public string UserProgramFilePath { get; } public string DataConnectionProgramFilePath { get; } - public override async Task CreateAsync(DotNetFrameworkVersion targetDotNetFrameworkVersion, ProjectOutputType outputType, bool deleteExisting = false) + public override async Task CreateAsync(DotNetFrameworkVersion targetDotNetFrameworkVersion, ProjectOutputType outputType, ProjectSdkType sdkType, bool deleteExisting = false) { - await base.CreateAsync(targetDotNetFrameworkVersion, outputType, deleteExisting); - - var domainAssembly = typeof(IOutputWriter<>).Assembly; - await AddAssemblyFileReferenceAsync(new AssemblyFileReference(domainAssembly.Location)); + await base.CreateAsync(targetDotNetFrameworkVersion, outputType, sdkType, deleteExisting); await AddReferencesAsync(Script.Config.References); } diff --git a/src/Plugins/NetPad.Plugins.OmniSharp/Services/AppOmniSharpServer.cs b/src/Plugins/NetPad.Plugins.OmniSharp/Services/AppOmniSharpServer.cs index f6f56c8f..87ef6bcb 100644 --- a/src/Plugins/NetPad.Plugins.OmniSharp/Services/AppOmniSharpServer.cs +++ b/src/Plugins/NetPad.Plugins.OmniSharp/Services/AppOmniSharpServer.cs @@ -80,6 +80,7 @@ public async Task StartAsync() await Project.CreateAsync( _environment.Script.Config.TargetFrameworkVersion, ProjectOutputType.Executable, + ProjectSdkType.AspNetCoreApp, true); await Project.SetProjectPropertyAsync("AllowUnsafeBlocks", "true");