LCOV - code coverage report
Current view: top level - cmd - gencomponents.go Coverage Total Hit
Test: coverage.lcov Lines: 70.0 % 20 14
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package cmd
       2              : 
       3              : import (
       4              :         "bytes"
       5              :         "context"
       6              :         "fmt"
       7              :         "go/format"
       8              :         "io"
       9              :         "os"
      10              :         "text/template"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/components/genspec"
      13              :         "github.com/urfave/cli/v3"
      14              : )
      15              : 
      16              : // CmdGenComponents は genspec.Registry から components パッケージの
      17              : // EntitySpec / Components / InitializeComponents / AddEntity を生成するサブコマンド。
      18              : // 通常はトップの //go:generate から呼ばれる。
      19              : var CmdGenComponents = &cli.Command{
      20              :         Name:  "gencomponents",
      21              :         Usage: "generate component definitions such as EntitySpec and Components from the registry",
      22              :         Flags: []cli.Flag{
      23              :                 &cli.StringFlag{
      24              :                         Name:  outFlag,
      25              :                         Value: "internal/components/components_gen.go",
      26              :                         Usage: "output file path",
      27              :                 },
      28              :         },
      29              :         Action: runGenComponents,
      30              : }
      31              : 
      32              : func runGenComponents(_ context.Context, cmd *cli.Command) error {
      33            1 :         return genComponents(cmd.Writer, cmd.String(outFlag))
      34            1 : }
      35              : 
      36              : // genComponents は登録表から生成したコードを outPath へ書き、完了を out へ書く。
      37              : func genComponents(out io.Writer, outPath string) error {
      38            1 :         formatted, err := generateComponents()
      39            1 :         if err != nil {
      40            0 :                 return err
      41            0 :         }
      42            1 :         if err := os.WriteFile(outPath, formatted, 0o644); err != nil {
      43            1 :                 return fmt.Errorf("failed to write generated code: %w", err)
      44            1 :         }
      45            1 :         _, _ = fmt.Fprintf(out, "Generated %s\n", outPath)
      46            1 :         return nil
      47              : }
      48              : 
      49              : // generateComponents は登録表から整形済みの生成コードを返す。
      50              : // format.Source を通すため、返り値がGoとして妥当でなければエラーになる。
      51              : func generateComponents() ([]byte, error) {
      52            1 :         var buf bytes.Buffer
      53            1 :         if err := componentsTmpl.Execute(&buf, genspec.Registry); err != nil {
      54            0 :                 return nil, fmt.Errorf("failed to expand template: %w", err)
      55            0 :         }
      56              : 
      57            1 :         formatted, err := format.Source(buf.Bytes())
      58            1 :         if err != nil {
      59            0 :                 return nil, fmt.Errorf("failed to format generated code: %w\n%s", err, buf.String())
      60            0 :         }
      61            1 :         return formatted, nil
      62              : }
      63              : 
      64              : var componentsTmpl = template.Must(template.New("gen").Parse(`// Code generated by "ruins gencomponents". DO NOT EDIT.
      65              : 
      66              : package components
      67              : 
      68              : import "github.com/mlange-42/ark/ecs"
      69              : 
      70              : // EntitySpec is the specification used to create an entity.
      71              : // It defines the set of components to attach, which AddEntity converts into an ECS entity.
      72              : type EntitySpec struct {
      73              : {{- range .}}
      74              :         {{.Field}} *{{.Field}}
      75              : {{- end}}
      76              : }
      77              : 
      78              : // Components is the bundle of ECS component handles.
      79              : // It holds a typed *ecs.Map[T] for each component type, used by Add/Has/Get and queries.
      80              : type Components struct {
      81              : {{- range .}}
      82              :         {{.Field}} *ecs.Map[{{.Field}}]
      83              : {{- end}}
      84              : }
      85              : 
      86              : // InitializeComponents registers every component type with the Ark world
      87              : // and assigns a typed Map handle to each field.
      88              : func (c *Components) InitializeComponents(world *ecs.World) error {
      89              : {{- range .}}
      90              :         c.{{.Field}} = ecs.NewMap[{{.Field}}](world)
      91              : {{- end}}
      92              :         return nil
      93              : }
      94              : 
      95              : // AddEntity creates a new entity from an EntitySpec and attaches each non-nil field
      96              : // as its corresponding component.
      97              : func (c *Components) AddEntity(world *ecs.World, spec *EntitySpec) ecs.Entity {
      98              :         entity := world.NewEntity()
      99              : {{- range .}}
     100              :         addComp(c.{{.Field}}, entity, spec.{{.Field}})
     101              : {{- end}}
     102              :         return entity
     103              : }
     104              : `))
        

Generated by: LCOV version 2.0-1