Skip to content

Commit

Permalink
- CLI: webapi sample update
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-shilo committed Jan 14, 2023
1 parent 85edca3 commit 0ebdce3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/cscs/CodeDom/Proxies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ internal static string CreateProject(CompilerParameters options, string[] fileNa
.ToList();

void setTargetFremeworkWin() => project_element.Element("PropertyGroup")
.SetElementValue("TargetFramework", "net6.0-windows");
.SetElementValue("TargetFramework", "net7.0-windows");

bool refWinForms = ref_assemblies.Any(x => x.EndsWith("System.Windows.Forms") ||
x.EndsWith("System.Windows.Forms.dll"));
Expand Down
43 changes: 43 additions & 0 deletions src/cscs/HelpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,8 @@ public SampleInfo(string code, string fileExtension)
{ "winform", CSharp_winforms_Sample},
{ "cmd", CSharp_command_Sample},
{ "winform-vb", DefaultVbDesktopSample},
{ "webapi", context => CSharp_webipi_Sample(context, addOpenApi:false) },
{ "webapi-openapi", context => CSharp_webipi_Sample(context, addOpenApi:true) },
{ "wpf", CSharp_wpf_Sample },
{ "wpf-cm", CSharp_wpf_ss_Sample },
};
Expand Down Expand Up @@ -1323,6 +1325,47 @@ static SampleInfo[] CSharp_command_Sample(string context)
return new[] { new SampleInfo(cs.NormalizeNewLines(), ".cs") };
}

static SampleInfo[] CSharp_webipi_Sample(string context, bool addOpenApi)
{
var cs =
@"//css_webapp
$extrapackages$//css_nuget Swashbuckle.AspNetCore
//css_inc global-usings
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger()
.UseSwaggerUI()
.UseHttpsRedirection()
.UseStaticFiles();
app.MapGet(""/test"", (HttpRequest request) => new { Name = ""Test Response"" })$extracode$;
app.Run();
";
if (addOpenApi)
{
cs = cs.Replace("$extracode$", $"{Environment.NewLine} .WithOpenApi()")
.Replace("$extrapackages$", $"//css_nuget Microsoft.AspNetCore.OpenApi -ver:7.0.0 -rt:net7.0{Environment.NewLine}" +
$"//css_nuget Microsoft.OpenApi{Environment.NewLine}");
}
else
{
cs = cs.Replace("$extracode$", "")
.Replace("$extrapackages$", "");
}

return new[] { new SampleInfo(cs.NormalizeNewLines(), ".cs") };
}

static SampleInfo[] CSharp_winforms_Sample(string context)
{
var cs =
Expand Down
24 changes: 12 additions & 12 deletions src/cscs/NuGet.Core.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using CSScripting;
using CSScripting.CodeDom;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using CSScripting;
using CSScripting.CodeDom;

namespace csscript
{
Expand Down Expand Up @@ -54,8 +54,8 @@ class NuGetCore
//https://docs.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
// .NET Mono, .NET Core
static public string NuGetCache => Runtime.IsWin ?
Environment.ExpandEnvironmentVariables(@"%userprofile%\.nuget\packages") :
"~/.nuget/packages";
Environment.ExpandEnvironmentVariables(@"%userprofile%\.nuget\packages") :
"~/.nuget/packages";

static public string NuGetExe => "dotnet";

Expand Down Expand Up @@ -86,8 +86,8 @@ static public void InstallPackage(string packageNameMask, string version = null,

// C:\Users\user\AppData\Local\Temp\csscript.core\.nuget\333
var nuget_dir = Runtime.GetScriptTempDir()
.PathJoin(".nuget", Process.GetCurrentProcess().Id)
.EnsureDir();
.PathJoin(".nuget", Process.GetCurrentProcess().Id)
.EnsureDir();

try
{
Expand Down Expand Up @@ -277,7 +277,7 @@ static string GetPackageCompatibleLib(PackageInfo package)
else // host runtime
{
if (Runtime.IsCore)
return (frameworks.FirstOrDefault(x => x.Runtime.StartsWith("netcore", ignoreCase: true))
return (frameworks.FirstOrDefault(x => x.Runtime.StartsWith("net", ignoreCase: true))
?? frameworks.FirstOrDefault(x => x.Runtime.StartsWith("netstandard", ignoreCase: true)))?.Path;
else
return frameworks.FirstOrDefault(x => x.Runtime.StartsWith("net", ignoreCase: true)
Expand Down Expand Up @@ -323,7 +323,7 @@ static PackageInfo FindPackage(string name, string version)
Directory.CreateDirectory(NuGetCache);

var packages = Directory.GetDirectories(NuGetCache, name, SearchOption.TopDirectoryOnly)
.SelectMany(x =>
.SelectMany(x =>
{
return Directory.GetFiles(x, "*.nuspec", SearchOption.AllDirectories)
.Select(spec =>
Expand All @@ -341,12 +341,12 @@ static PackageInfo FindPackage(string name, string version)
return null;
});
})
.OrderByDescending(x => x.Version)
.Where(x => x != null)
.ToArray();
.OrderByDescending(x => x.Version)
.Where(x => x != null)
.ToArray();

return packages.FirstOrDefault(x => string.Compare(x.Name, name, StringComparison.OrdinalIgnoreCase) == 0 &&
(version.IsEmpty() || version == x.Version));
(version.IsEmpty() || version == x.Version));
}

static public string[] Resolve(string[] packages, bool suppressDownloading, string script)
Expand Down

0 comments on commit 0ebdce3

Please sign in to comment.