|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/jodi-ivan/numbered-notation-xml/adapter" |
| 13 | + "github.com/jodi-ivan/numbered-notation-xml/internal/renderer" |
| 14 | + "github.com/jodi-ivan/numbered-notation-xml/svc/repository" |
| 15 | + "github.com/jodi-ivan/numbered-notation-xml/svc/usecase" |
| 16 | + "github.com/jodi-ivan/numbered-notation-xml/utils/config" |
| 17 | + "github.com/jodi-ivan/numbered-notation-xml/utils/storage" |
| 18 | +) |
| 19 | + |
| 20 | +// TODO: the snapshot testing |
| 21 | +/* |
| 22 | + - generate golden file |
| 23 | + - generate the svg |
| 24 | + - sampling |
| 25 | + - assert |
| 26 | +*/ |
| 27 | + |
| 28 | +var defaultxml = "~/go/src/github.com/jodi-ivan/numbered-notation-xml/files/scores/musicxml/" |
| 29 | +var defaultdb = "~/go/src/github.com/jodi-ivan/numbered-notation-xml/files/database/kidung-jemaat.db" |
| 30 | + |
| 31 | +func main() { |
| 32 | + env := os.Environ() |
| 33 | + |
| 34 | + // Define a string flag |
| 35 | + method := flag.String("snapshot", "gen", "Operation, 'gen' generate golden. 'assert' assert snapshot") |
| 36 | + goldenPath := flag.String("path", "", "Path of the golden files to be generated, only when snapshot=gen") |
| 37 | + |
| 38 | + // Parse the command-line arguments into the defined flags |
| 39 | + flag.Parse() |
| 40 | + |
| 41 | + log.Println("method:", *method) |
| 42 | + xmlFlag := defaultxml |
| 43 | + dbPath := defaultdb |
| 44 | + |
| 45 | + for _, e := range env { |
| 46 | + keyval := strings.Split(e, "=") |
| 47 | + switch keyval[0] { |
| 48 | + case "KJ_TEST_MUSICXML_PATH": |
| 49 | + xmlFlag = keyval[1] |
| 50 | + case "KJ_TEST_DB_PATH": |
| 51 | + dbPath = keyval[1] |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Parse the flags from the command line |
| 56 | + |
| 57 | + cfg := config.Config{ |
| 58 | + MusicXML: config.MusicXMLConfig{ |
| 59 | + Path: xmlFlag, |
| 60 | + FilePrefix: "kj", |
| 61 | + }, |
| 62 | + SQLite: config.SQLiteConfig{ |
| 63 | + DBPath: dbPath, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + db, err := storage.NewStorage(context.Background(), cfg.SQLite.DBPath) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Failed to connect to storage: %s", err.Error()) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + defer db.Close() |
| 74 | + |
| 75 | + repo := repository.New(context.Background(), db) |
| 76 | + usecaseMod := usecase.New(cfg, repo, renderer.NewRenderer()) |
| 77 | + stringRender := adapter.NewRenderString(usecaseMod) |
| 78 | + |
| 79 | + switch *method { |
| 80 | + case "gen": |
| 81 | + if *goldenPath == "" { |
| 82 | + fmt.Printf("Goldenpath -path cannot be empty") |
| 83 | + os.Exit(2) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("Read the musicxml files on %s.\n", defaultxml) |
| 88 | + fmt.Printf("Generate golden files. Generated on %s. \n\n", *goldenPath) |
| 89 | + |
| 90 | + err = GenerateGolden(context.Background(), stringRender, *goldenPath) |
| 91 | + if err != nil { |
| 92 | + fmt.Printf("Failed to Generate Snapshot: %s\n", err.Error()) |
| 93 | + os.Exit(2) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + os.Exit(0) |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +func GenerateGolden(ctx context.Context, stringRenderer *adapter.RenderString, path string) error { |
| 104 | + numFiles := 22 |
| 105 | + for i := 1; i <= numFiles; i++ { |
| 106 | + buff := bytes.NewBuffer(nil) |
| 107 | + content, err := stringRenderer.RenderHymn(context.Background(), buff, i) |
| 108 | + if err != nil { |
| 109 | + log.Fatalf("Problem creating file: %v", err) |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + fileName := fmt.Sprintf("%s/kj-%03d.svg", path, i) |
| 114 | + fmt.Println("Creating golden for", fileName) |
| 115 | + file, err := os.Create(fileName) |
| 116 | + if err != nil { |
| 117 | + log.Fatalf("Problem creating file: %v", err) |
| 118 | + return err |
| 119 | + } |
| 120 | + fmt.Fprintf(file, "%s", content) |
| 121 | + file.Close() |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
0 commit comments