| 
									
										
										
										
											2024-03-14 00:05:21 +08:00
										 |  |  | package codegen | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"go/format" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/codejen" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var registryPath = filepath.Join("pkg", "registry", "schemas") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CoreRegistryJenny generates a registry with all core kinds.
 | 
					
						
							|  |  |  | type CoreRegistryJenny struct { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (jenny *CoreRegistryJenny) JennyName() string { | 
					
						
							|  |  |  | 	return "CoreRegistryJenny" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | func (jenny *CoreRegistryJenny) Generate(cueFiles ...SchemaForGen) (codejen.Files, error) { | 
					
						
							| 
									
										
										
										
											2024-03-14 00:05:21 +08:00
										 |  |  | 	schemas := make([]Schema, len(cueFiles)) | 
					
						
							|  |  |  | 	for i, v := range cueFiles { | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | 		name, err := getSchemaName(v.Name) | 
					
						
							| 
									
										
										
										
											2024-03-14 00:05:21 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		schemas[i] = Schema{ | 
					
						
							|  |  |  | 			Name:     name, | 
					
						
							|  |  |  | 			FilePath: v.FilePath, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf := new(bytes.Buffer) | 
					
						
							|  |  |  | 	if err := tmpls.Lookup("core_registry.tmpl").Execute(buf, tvars_registry{ | 
					
						
							|  |  |  | 		Schemas: schemas, | 
					
						
							|  |  |  | 	}); err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("failed executing kind registry template: %w", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b, err := format.Source(buf.Bytes()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file := codejen.NewFile(filepath.Join(registryPath, "core_kind.go"), b, jenny) | 
					
						
							|  |  |  | 	return codejen.Files{*file}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | func getSchemaName(pkg string) (string, error) { | 
					
						
							| 
									
										
										
										
											2025-04-10 20:42:23 +08:00
										 |  |  | 	pkg = strings.ReplaceAll(pkg, "-", "_") | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | 	return strings.ToLower(pkg), nil | 
					
						
							| 
									
										
										
										
											2024-03-14 00:05:21 +08:00
										 |  |  | } |