LCOV - code coverage report
Current view: top level - cmd - genreadme.go Coverage Total Hit
Test: coverage.lcov Lines: 95.0 % 80 76
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package cmd
       2              : 
       3              : import (
       4              :         "context"
       5              :         "fmt"
       6              :         "io"
       7              :         "os"
       8              :         "path/filepath"
       9              :         "slices"
      10              :         "strings"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/designdoc"
      13              :         "github.com/urfave/cli/v3"
      14              : )
      15              : 
      16              : // CmdGenReadme はREADME.mdを生成するサブコマンド
      17              : var CmdGenReadme = &cli.Command{
      18              :         Name:   "genreadme",
      19              :         Usage:  "generate README.md from README.tmpl.md",
      20              :         Action: runGenReadme,
      21              : }
      22              : 
      23              : const (
      24              :         templateFile          = "README.tmpl.md"
      25              :         outputFile            = "README.md"
      26              :         imageDir              = "internal/states/testdata"
      27              :         placeholder           = "<!-- VRT_IMAGES -->"
      28              :         designStatusPlacehldr = "<!-- DESIGN_STATUS -->"
      29              :         columns               = 4
      30              : )
      31              : 
      32              : func runGenReadme(_ context.Context, cmd *cli.Command) error {
      33            0 :         return genReadme(cmd.Writer, templateFile, outputFile, imageDir, designdoc.DefaultDir)
      34            0 : }
      35              : 
      36              : // genReadme はテンプレートの各プレースホルダを画像テーブルと設計ドキュメント状態で置換し、
      37              : // outputPath へ書き出す。読み書きするパスはすべて引数で受け、生成の完了を out へ書く。
      38              : func genReadme(out io.Writer, templatePath, outputPath, imageDirPath, designDir string) error {
      39            1 :         tmpl, err := os.ReadFile(templatePath)
      40            1 :         if err != nil {
      41            1 :                 return fmt.Errorf("failed to read template: %w", err)
      42            1 :         }
      43              : 
      44            1 :         table, err := buildImageTableFrom(imageDirPath)
      45            1 :         if err != nil {
      46            1 :                 return fmt.Errorf("failed to build image table: %w", err)
      47            1 :         }
      48              : 
      49            1 :         docs, err := designdoc.LoadDir(designDir)
      50            1 :         if err != nil {
      51            1 :                 return fmt.Errorf("failed to read design documents: %w", err)
      52            1 :         }
      53            1 :         statusTable := designdoc.RenderStatusSection(docs)
      54            1 : 
      55            1 :         result := strings.Replace(string(tmpl), placeholder, table, 1)
      56            1 :         result = strings.Replace(result, designStatusPlacehldr, statusTable, 1)
      57            1 :         if err := os.WriteFile(outputPath, []byte(result), 0o644); err != nil {
      58            0 :                 return fmt.Errorf("failed to write README.md: %w", err)
      59            0 :         }
      60              : 
      61            1 :         _, _ = fmt.Fprintf(out, "Generated %s from %s (%s)\n", outputPath, templatePath, imageDirPath)
      62            1 :         return nil
      63              : }
      64              : 
      65              : // imageEntry はテーブルに載せる画像1枚。README からの相対パスと見出しを持つ。
      66              : type imageEntry struct {
      67              :         path  string
      68              :         label string
      69              : }
      70              : 
      71              : // buildImageTableFrom は指定ディレクトリ直下の TestGolden_*.png から4列のMarkdownテーブルを生成する。
      72              : func buildImageTableFrom(dir string) (string, error) {
      73            1 :         entries, err := collectTopImages(dir)
      74            1 :         if err != nil {
      75            1 :                 return "", err
      76            1 :         }
      77            1 :         if len(entries) == 0 {
      78            1 :                 return "*no images*", nil
      79            1 :         }
      80            1 :         return renderImageTable(entries), nil
      81              : }
      82              : 
      83              : // pngNames はディレクトリ直下のPNGファイル名をソートして返す。
      84              : func pngNames(dir string) ([]string, error) {
      85            1 :         files, err := os.ReadDir(dir)
      86            1 :         if err != nil {
      87            1 :                 return nil, err
      88            1 :         }
      89            1 :         var names []string
      90            1 :         for _, e := range files {
      91            1 :                 if !e.IsDir() && strings.HasSuffix(e.Name(), ".png") {
      92            1 :                         names = append(names, e.Name())
      93            1 :                 }
      94              :         }
      95            1 :         slices.Sort(names)
      96            1 :         return names, nil
      97              : }
      98              : 
      99              : // collectTopImages はディレクトリ直下の TestGolden_*.png を集める。見出しは TestGolden_ 接頭辞を外した名前。
     100              : func collectTopImages(dir string) ([]imageEntry, error) {
     101            1 :         names, err := pngNames(dir)
     102            1 :         if err != nil {
     103            1 :                 return nil, fmt.Errorf("failed to read %s: %w", dir, err)
     104            1 :         }
     105            1 :         entries := make([]imageEntry, 0, len(names))
     106            1 :         for _, name := range names {
     107            1 :                 entries = append(entries, imageEntry{
     108            1 :                         path:  filepath.Join(dir, name),
     109            1 :                         label: strings.TrimSuffix(strings.TrimPrefix(name, "TestGolden_"), ".png"),
     110            1 :                 })
     111            1 :         }
     112            1 :         return entries, nil
     113              : }
     114              : 
     115              : // renderImageTable は画像エントリを4列のMarkdownテーブルにする。
     116              : func renderImageTable(entries []imageEntry) string {
     117            1 :         var sb strings.Builder
     118            1 : 
     119              :         // Markdownテーブルのヘッダー
     120            1 :         sb.WriteString("|")
     121            1 :         for range columns {
     122            1 :                 sb.WriteString(" |")
     123            1 :         }
     124            1 :         sb.WriteString("\n|")
     125            1 :         for range columns {
     126            1 :                 sb.WriteString("---|")
     127            1 :         }
     128            1 :         sb.WriteString("\n")
     129            1 : 
     130            1 :         for i, e := range entries {
     131            1 :                 if i%columns == 0 {
     132            1 :                         if i > 0 {
     133            1 :                                 sb.WriteString("\n")
     134            1 :                         }
     135            1 :                         sb.WriteString("|")
     136              :                 }
     137            1 :                 fmt.Fprintf(&sb, " <img src=\"%s\" width=\"200\" /><br>%s |", e.path, e.label)
     138              :         }
     139              :         // 最終行の残りセルを埋める
     140            1 :         if rem := len(entries) % columns; rem != 0 {
     141            1 :                 for range columns - rem {
     142            1 :                         sb.WriteString(" |")
     143            1 :                 }
     144              :         }
     145            1 :         sb.WriteString("\n")
     146            1 : 
     147            1 :         return sb.String()
     148              : }
        

Generated by: LCOV version 2.0-1