openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
codegen/generator/src/Visitors/ModelDirectoryVisitor.cs
40lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.ClientModel; |
| 2 | using Microsoft.TypeSpec.Generator.Providers; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.IO; |
| 6 | |
| 7 | namespace OpenAILibraryPlugin.Visitors; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// A visitor that organizes model files into subdirectories based on their namespace. |
| 11 | /// </summary> |
| 12 | public class ModelDirectoryVisitor : ScmLibraryVisitor |
| 13 | { |
| 14 | private static readonly HashSet<string> _excludedNamespaces = new(StringComparer.OrdinalIgnoreCase) |
| 15 | { |
| 16 | }; |
| 17 | |
| 18 | protected override TypeProvider VisitType(TypeProvider type) |
| 19 | { |
| 20 | if (!_excludedNamespaces.Contains(type.Type.Namespace)) |
| 21 | { |
| 22 | // Only apply to types in the Models folder |
| 23 | if (type.RelativeFilePath.Contains("Models")) |
| 24 | { |
| 25 | var segments = type.Type.Namespace?.Split('.'); |
| 26 | |
| 27 | if (segments is { Length: >= 2 } && segments[0] == "OpenAI") |
| 28 | { |
| 29 | var folderName = segments[1]; // Use second segment, e.g., "Chat" from "OpenAI.Chat" |
| 30 | var fileName = Path.GetFileName(type.RelativeFilePath); |
| 31 | var newRelativePath = Path.Combine("src", "Generated", "Models", folderName, fileName); |
| 32 | |
| 33 | type.Update(relativeFilePath: newRelativePath); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return type; |
| 39 | } |
| 40 | } |
| 41 | |