-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4934cce
commit 0a7bb3c
Showing
636 changed files
with
69,336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"profiles": { | ||
"AutoRest.CodeModel": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.