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