| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | package codegen | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/codejen" | 
					
						
							| 
									
										
										
										
											2023-02-01 08:40:15 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/kindsys" | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // LatestJenny returns a jenny that runs another jenny for only the latest
 | 
					
						
							| 
									
										
										
										
											2023-01-31 17:50:08 +08:00
										 |  |  | // schema in a DefForGen, and prefixes the resulting file with the provided
 | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | // parentdir (e.g. "pkg/kinds/") and with a directory based on the kind's
 | 
					
						
							|  |  |  | // machine name (e.g. "dashboard/").
 | 
					
						
							|  |  |  | func LatestJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) OneToOne { | 
					
						
							|  |  |  | 	if inner == nil { | 
					
						
							|  |  |  | 		panic("inner jenny must not be nil") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &latestj{ | 
					
						
							|  |  |  | 		parentdir: parentdir, | 
					
						
							|  |  |  | 		inner:     inner, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type latestj struct { | 
					
						
							|  |  |  | 	parentdir string | 
					
						
							|  |  |  | 	inner     codejen.OneToOne[SchemaForGen] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (j *latestj) JennyName() string { | 
					
						
							|  |  |  | 	return "LatestJenny" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 08:40:15 +08:00
										 |  |  | func (j *latestj) Generate(kind kindsys.Kind) (*codejen.File, error) { | 
					
						
							|  |  |  | 	comm := kind.Props().Common() | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 	sfg := SchemaForGen{ | 
					
						
							|  |  |  | 		Name:    comm.Name, | 
					
						
							| 
									
										
										
										
											2023-02-01 08:40:15 +08:00
										 |  |  | 		Schema:  kind.Lineage().Latest(), | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 		IsGroup: comm.LineageIsGroup, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f, err := j.inner.Generate(sfg) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-01 08:40:15 +08:00
										 |  |  | 		return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), kind.Props().Common().Name, err) | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if f == nil || !f.Exists() { | 
					
						
							|  |  |  | 		return nil, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f.RelativePath = filepath.Join(j.parentdir, comm.MachineName, f.RelativePath) | 
					
						
							|  |  |  | 	f.From = append(f.From, j) | 
					
						
							|  |  |  | 	return f, nil | 
					
						
							|  |  |  | } |