Skip to content

Commit

Permalink
feat(): pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Feser <[email protected]>
  • Loading branch information
joefeser committed Aug 10, 2024
1 parent 7abde69 commit 384648d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/ApiGenerator/ApiGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpier.Core" Version="0.28.2" />
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSwag.Core" Version="14.1.0" />
<PackageReference Include="Proc" Version="0.8.1" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
<PackageReference Include="SemanticVersioning" Version="2.0.2" />
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
Expand Down
2 changes: 2 additions & 0 deletions src/ApiGenerator/Configuration/GeneratorLocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static class GeneratorLocations

public static string LowLevel(params string[] paths) => LowLevelGeneratedFolder + string.Join("/", paths);

public static string SolutionFolder { get; } = $"{Root}../../";

public static readonly Assembly Assembly = typeof(Generator.ApiGenerator).Assembly;

private static string _root;
Expand Down
1 change: 0 additions & 1 deletion src/ApiGenerator/Generator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace ApiGenerator.Generator
public class ApiGenerator
{
public static List<string> Warnings { get; private set; } = new();
public static HashSet<string> GeneratedFilePaths = [];

public static async Task Generate(bool lowLevelOnly, RestApiSpec spec, CancellationToken token)
{
Expand Down
34 changes: 22 additions & 12 deletions src/ApiGenerator/Generator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ public static class CodeGenerator
{
public static string CatFormatPropertyGenerator(string type, string name, string key, string setter) =>
$"public {type} {name} {{ "
+ $" get => Q<{type}>(\"{key}\");"
+ $" get => Q<{type}>(\"{key}\");{Environment.NewLine}"
+ $" set {{ Q(\"{key}\", {setter}); SetAcceptHeader({setter}); }}"
+ $"}}";

public static string PropertyGenerator(string type, string name, string key, string setter) =>
$"public {type} {name} {{ get => Q<{type}>(\"{key}\"); set => Q(\"{key}\", {setter}); }}";
$"public {type} {name} {{ get => Q<{type}>(\"{key}\");{Environment.NewLine} set => Q(\"{key}\", {setter}); }}";

public static string Property(string @namespace, string type, string name, string key, string setter, string obsolete, Version versionAdded, params string[] doc)
{
var components = new List<string>();
foreach (var d in RenderDocumentation(doc)) A(d);
if (versionAdded != null) A($"/// <remarks>Supported by OpenSearch servers of version {versionAdded} or greater.</remarks>");
if (!string.IsNullOrWhiteSpace(obsolete)) A($"[Obsolete(\"{obsolete}\")]");
foreach (var d in RenderDocumentation(doc))
A(d);
if (versionAdded != null)
A($"/// <remarks>Supported by OpenSearch servers of version {versionAdded} or greater.</remarks>");
if (!string.IsNullOrWhiteSpace(obsolete))
A($"[Obsolete(\"{obsolete}\")]");

var generated = @namespace != null && @namespace == "Cat" && name == "Format"
? CatFormatPropertyGenerator(type, name, key, setter)
Expand All @@ -69,12 +72,16 @@ void A(string s)
public static string Constructor(Constructor c)
{
var components = new List<string>();
if (!c.Description.IsNullOrEmpty()) A(c.Description);
if (!c.Description.IsNullOrEmpty())
A(c.Description);
var generated = c.Generated;
if (c.Body.IsNullOrEmpty()) generated += "{}";
if (c.Body.IsNullOrEmpty())
generated += "{}";
A(generated);
if (!c.Body.IsNullOrEmpty()) A(c.Body);
if (!c.AdditionalCode.IsNullOrEmpty()) A(c.AdditionalCode);
if (!c.Body.IsNullOrEmpty())
A(c.Body);
if (!c.AdditionalCode.IsNullOrEmpty())
A(c.AdditionalCode);
return string.Join($"{Environment.NewLine}\t\t", components);

void A(string s)
Expand All @@ -88,15 +95,17 @@ private static IEnumerable<string> RenderDocumentation(params string[] doc)
doc = (doc?.SelectMany(WrapDocumentation) ?? Enumerable.Empty<string>()).ToArray();
switch (doc.Length)
{
case 0: yield break;
case 0:
yield break;
case 1:
yield return $"/// <summary>{doc[0]}</summary>";

yield break;
default:
yield return "/// <summary>";

foreach (var d in doc) yield return $"/// {d}";
foreach (var d in doc)
yield return $"/// {d}";

yield return "/// </summary>";

Expand All @@ -106,7 +115,8 @@ private static IEnumerable<string> RenderDocumentation(params string[] doc)

private static string[] WrapDocumentation(string documentation)
{
if (string.IsNullOrWhiteSpace(documentation)) return Array.Empty<string>();
if (string.IsNullOrWhiteSpace(documentation))
return Array.Empty<string>();
const int max = 140;
var lines = documentation.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var charCount = 0;
Expand Down
3 changes: 0 additions & 3 deletions src/ApiGenerator/Generator/Razor/RazorGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ private static async Task WriteFormattedCsharpFile(string path, string contents)
if (Directory.GetParent(path) is { Exists: false } dir)
dir.Create();

var directory = Directory.GetParent(path);
ApiGenerator.GeneratedFilePaths.Add($"{directory.FullName}\\"); //we must have a trailing slash

await File.WriteAllTextAsync(path, contents);
}

Expand Down
42 changes: 4 additions & 38 deletions src/ApiGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System.Threading;
using System.Threading.Tasks;
using ApiGenerator.Configuration;
using ProcNet;
using Spectre.Console;

#pragma warning disable 162
Expand Down Expand Up @@ -175,45 +176,10 @@ private static void RunDotNetFormat()
AnsiConsole.Write(new Rule("[b white on chartreuse4] Formatting Code using dotnet format [/]").LeftJustified());
Console.WriteLine();

var rootPath = GetRootPath(typeof(Program).Assembly.Location);

var relativeFolderList = new List<string>();

foreach (var item in Generator.ApiGenerator.GeneratedFilePaths)
{
var relativePath = item.Replace(rootPath.FullName, string.Empty);
relativeFolderList.Add($".{relativePath.Replace("\\", "/")}");
}

var si = new System.Diagnostics.ProcessStartInfo
Proc.Exec(new ExecArguments("dotnet", "format", "--verbosity", "normal", "--include", "./src/*/_Generated/")
{
WorkingDirectory = rootPath.FullName,
FileName = "dotnet",
Arguments = "format -v diag --include " + string.Join(' ', relativeFolderList.Select(item => $"{item}")),
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};

var process = System.Diagnostics.Process.Start(si);

Console.WriteLine();
Console.WriteLine($"Running dotnet format --include {string.Join(' ', relativeFolderList.Select(item => $"{item}"))} -v diag");

// hookup the eventhandlers to capture the data that is received
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);

process.Start();

// start our event pumps
process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();
Console.WriteLine();
WorkingDirectory = GeneratorLocations.SolutionFolder
});
}

private static bool Ask(string question, bool defaultAnswer = true)
Expand Down

0 comments on commit 384648d

Please sign in to comment.