Skip to content

Commit

Permalink
DXSL
Browse files Browse the repository at this point in the history
  • Loading branch information
byt3n33dl3 committed Nov 25, 2024
1 parent 4934cce commit 0a7bb3c
Show file tree
Hide file tree
Showing 636 changed files with 69,336 additions and 0 deletions.
13 changes: 13 additions & 0 deletions logger/autorest.codemodel/AutoRest.CodeModel.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="10.0.23" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions logger/autorest.codemodel/CustomEnumNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NJsonSchema;
using NJsonSchema.CodeGeneration;

namespace AutoRest.CodeModel
{
internal class CustomEnumNameGenerator : IEnumNameGenerator
{
private readonly DefaultEnumNameGenerator _defaultEnumNameGenerator = new DefaultEnumNameGenerator();

public string Generate(int index, string name, object value, JsonSchema schema) =>
_defaultEnumNameGenerator.Generate(
index,
// Fixes + and - enum values that cannot be generated into C# enum names
name.Equals("+") ? "plus" : name.Equals("-") ? "minus" : name,
value,
schema);
}
}
27 changes: 27 additions & 0 deletions logger/autorest.codemodel/CustomPropertyNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NJsonSchema;
using NJsonSchema.CodeGeneration.CSharp;

namespace AutoRest.CodeModel
{
internal class CustomPropertyNameGenerator : CSharpPropertyNameGenerator
{
public override string Generate(JsonSchemaProperty property)
{
// Makes array type properties initialized with empty collections
if (property.IsArray)
{
property.IsRequired = true;
}

// Cases CSharp properly
if (property.Name == "csharp")
{
return "CSharp";
}
return base.Generate(property);
}
}
}
30 changes: 30 additions & 0 deletions logger/autorest.codemodel/CustomTypeNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using NJsonSchema;

namespace AutoRest.CodeModel
{
internal class CustomTypeNameGenerator : DefaultTypeNameGenerator
{
// Class names that conflict with project class names
private static readonly Dictionary<string, string> RenameMap = new Dictionary<string, string>
{
{ "HttpHeader", "HttpResponseHeader" },
{ "Parameter", "RequestParameter" },
{ "Request", "ServiceRequest" },
{ "Response", "ServiceResponse" },
{ "SerializationFormat", "SerializationFormatMetadata" }
};

public override string Generate(JsonSchema schema, string typeNameHint, IEnumerable<string> reservedTypeNames)
{
if (RenameMap.ContainsKey(typeNameHint))
{
typeNameHint = RenameMap[typeNameHint];
}
return base.Generate(schema, typeNameHint, reservedTypeNames);
}
}
}
69 changes: 69 additions & 0 deletions logger/autorest.codemodel/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NJsonSchema;
using NJsonSchema.CodeGeneration.CSharp;

namespace AutoRest.CodeModel
{
internal static class Program
{
private const string Path = "AutoRest.CSharp/Common/Input/Generated";
private static readonly string Namespace = "AutoRest.CSharp.Input";

private static async Task Main()
{
using var webClient = new HttpClient();
var schemaJson = await webClient.GetStringAsync(@"https://raw.githubusercontent.com/Azure/autorest/master/packages/libs/codemodel/.resources/all-in-one/json/code-model.json");
schemaJson = schemaJson
// Makes Choices only have string values
.Replace(" \"type\": [\n \"string\",\n \"number\",\n \"boolean\"\n ]\n", $" \"type\": \"string\"\n");

var schema = JsonSchema.FromJsonAsync(schemaJson).GetAwaiter().GetResult();
var settings = new CSharpGeneratorSettings
{
Namespace = Namespace,
HandleReferences = true,
TypeAccessModifier = "internal",
TypeNameGenerator = new CustomTypeNameGenerator(),
PropertyNameGenerator = new CustomPropertyNameGenerator(),
EnumNameGenerator = new CustomEnumNameGenerator(),
ExcludedTypeNames = new[]
{
"GroupSchema"
}
};
var rawFile = new CSharpGenerator(schema, settings).GenerateFile();
var cleanFile = String.Join(Environment.NewLine, rawFile.ToLines()
// Converts Newtonsoft attributes to YamlDotNet attributes
.Where(l => !l.Contains("Newtonsoft.Json.JsonConverter")
&& !l.Contains("Newtonsoft.Json.JsonExtensionData"))
.Select(l => Regex.Replace(l, @"(.*\[)Newtonsoft\.Json\.JsonProperty\((.*""),?.*(\)\])",
"$1YamlDotNet.Serialization.YamlMember(Alias = $2$3", RegexOptions.Singleline).TrimEnd()))
// Removes AdditionalProperties property from types as they are required to derive from IDictionary<string, object> for deserialization to work properly
.Replace($" private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();{Environment.NewLine}{Environment.NewLine} public System.Collections.Generic.IDictionary<string, object> AdditionalProperties{Environment.NewLine} {{{Environment.NewLine} get {{ return _additionalProperties; }}{Environment.NewLine} set {{ _additionalProperties = value; }}{Environment.NewLine} }}{Environment.NewLine}", String.Empty)
// Fixes stray blank lines from the C# generator
.Replace($"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}", Environment.NewLine)
.Replace($"{Environment.NewLine}{Environment.NewLine} }}", $"{Environment.NewLine} }}")
// Weird generation issue workaround
.Replace($"{Namespace}.bool.True", "true");

var lines = cleanFile.ToLines().ToArray();
var fileWithNullable = String.Join(Environment.NewLine, lines.Zip(lines.Skip(1).Append(String.Empty))
.Select(ll =>
{
var isNullableProperty = !ll.First.Contains("System.ComponentModel.DataAnnotations.Required") && ll.Second.Contains("{ get; set; }");
return isNullableProperty ? Regex.Replace(ll.Second, @"( \w+ { get; set; })", "?$1") : ll.Second;
})
.SkipLast(1)
.Prepend(lines.First()));
File.WriteAllText($"../../src/{Path}/CodeModel.cs", fileWithNullable);
}
}
}
8 changes: 8 additions & 0 deletions logger/autorest.codemodel/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"AutoRest.CodeModel": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)"
}
}
}
25 changes: 25 additions & 0 deletions logger/autorest.codemodel/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;

namespace AutoRest.CodeModel
{
internal static class StringExtensions
{
//https://stackoverflow.com/a/41176852/294804
public static IEnumerable<string> ToLines(this string value, bool removeEmptyLines = false)
{
using var sr = new StringReader(value);
string? line;
while ((line = sr.ReadLine()) != null)
{
if (removeEmptyLines && String.IsNullOrWhiteSpace(line))
continue;
yield return line;
}
}
}
}
Loading

0 comments on commit 0a7bb3c

Please sign in to comment.