| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | package codegen | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/codejen" | 
					
						
							| 
									
										
										
										
											2024-03-13 19:30:05 +08:00
										 |  |  | 	"github.com/grafana/cuetsy/ts" | 
					
						
							|  |  |  | 	"github.com/grafana/cuetsy/ts/ast" | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 04:32:38 +08:00
										 |  |  | // LatestMajorsOrXJenny returns a jenny that repeats the input for the latest in each major version.
 | 
					
						
							| 
									
										
										
										
											2024-03-13 19:30:05 +08:00
										 |  |  | func LatestMajorsOrXJenny(parentdir string) OneToMany { | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 	return &lmox{ | 
					
						
							| 
									
										
										
										
											2024-03-11 19:51:44 +08:00
										 |  |  | 		parentdir: parentdir, | 
					
						
							| 
									
										
										
										
											2024-03-13 19:30:05 +08:00
										 |  |  | 		inner:     TSTypesJenny{ApplyFuncs: []ApplyFunc{renameSpecNode}}, | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type lmox struct { | 
					
						
							| 
									
										
										
										
											2024-03-11 19:51:44 +08:00
										 |  |  | 	parentdir string | 
					
						
							|  |  |  | 	inner     codejen.OneToOne[SchemaForGen] | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (j *lmox) JennyName() string { | 
					
						
							|  |  |  | 	return "LatestMajorsOrXJenny" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | func (j *lmox) Generate(sfg SchemaForGen) (codejen.Files, error) { | 
					
						
							|  |  |  | 	sfg.IsGroup = true | 
					
						
							| 
									
										
										
										
											2024-03-11 19:51:44 +08:00
										 |  |  | 	f, err := j.inner.Generate(sfg) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | 		return nil, fmt.Errorf("%s jenny failed for %s: %w", j.inner.JennyName(), sfg.Name, err) | 
					
						
							| 
									
										
										
										
											2023-04-28 04:32:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-11 19:51:44 +08:00
										 |  |  | 	if f == nil || !f.Exists() { | 
					
						
							|  |  |  | 		return nil, nil | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | 	f.RelativePath = filepath.Join(j.parentdir, sfg.OutputName, "x", f.RelativePath) | 
					
						
							| 
									
										
										
										
											2024-03-11 19:51:44 +08:00
										 |  |  | 	f.From = append(f.From, j) | 
					
						
							|  |  |  | 	return codejen.Files{*f}, nil | 
					
						
							| 
									
										
										
										
											2022-11-22 22:00:29 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-13 19:30:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // renameSpecNode rename spec node from the TS file result
 | 
					
						
							|  |  |  | func renameSpecNode(sfg SchemaForGen, tf *ast.File) { | 
					
						
							|  |  |  | 	specidx, specdefidx := -1, -1 | 
					
						
							|  |  |  | 	for idx, def := range tf.Nodes { | 
					
						
							|  |  |  | 		// Peer through export keywords
 | 
					
						
							|  |  |  | 		if ex, is := def.(ast.ExportKeyword); is { | 
					
						
							|  |  |  | 			def = ex.Decl | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch x := def.(type) { | 
					
						
							|  |  |  | 		case ast.TypeDecl: | 
					
						
							|  |  |  | 			if x.Name.Name == "spec" { | 
					
						
							|  |  |  | 				specidx = idx | 
					
						
							|  |  |  | 				x.Name.Name = sfg.Name | 
					
						
							|  |  |  | 				tf.Nodes[idx] = x | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		case ast.VarDecl: | 
					
						
							|  |  |  | 			// Before:
 | 
					
						
							|  |  |  | 			//   export const defaultspec: Partial<spec> = {
 | 
					
						
							|  |  |  | 			// After:
 | 
					
						
							|  |  |  | 			// /  export const defaultPlaylist: Partial<Playlist> = {
 | 
					
						
							|  |  |  | 			if x.Names.Idents[0].Name == "defaultspec" { | 
					
						
							|  |  |  | 				specdefidx = idx | 
					
						
							|  |  |  | 				x.Names.Idents[0].Name = "default" + sfg.Name | 
					
						
							|  |  |  | 				tt := x.Type.(ast.TypeTransformExpr) | 
					
						
							|  |  |  | 				tt.Expr = ts.Ident(sfg.Name) | 
					
						
							|  |  |  | 				x.Type = tt | 
					
						
							|  |  |  | 				tf.Nodes[idx] = x | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if specidx != -1 { | 
					
						
							|  |  |  | 		decl := tf.Nodes[specidx] | 
					
						
							|  |  |  | 		tf.Nodes = append(append(tf.Nodes[:specidx], tf.Nodes[specidx+1:]...), decl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if specdefidx != -1 { | 
					
						
							|  |  |  | 		if specdefidx > specidx { | 
					
						
							|  |  |  | 			specdefidx-- | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		decl := tf.Nodes[specdefidx] | 
					
						
							|  |  |  | 		tf.Nodes = append(append(tf.Nodes[:specdefidx], tf.Nodes[specdefidx+1:]...), decl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |