automated snapshot
This commit is contained in:
92
storage.go
92
storage.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/d2fn/sumi/internal/ids"
|
||||
"github.com/d2fn/sumi/internal/ora"
|
||||
"github.com/go-git/go-git/v6"
|
||||
"log"
|
||||
//"github.com/go-git/go-git/v5/plumbing"
|
||||
@@ -86,48 +87,83 @@ func initSchema(db *sql.DB) error {
|
||||
}
|
||||
|
||||
func (s *Storage) Save(capture *SketchCapture) (string, error) {
|
||||
|
||||
id, _ := s.gen.Next()
|
||||
kid := ids.Base62Encode(id)
|
||||
path := filepath.Join(s.snapshotsDir, kid)
|
||||
flakeId := ids.Base62Encode(id)
|
||||
path := filepath.Join(s.snapshotsDir, flakeId)
|
||||
os.MkdirAll(path, 0755)
|
||||
|
||||
// wysiwyg at screen res
|
||||
img := rl.LoadImageFromScreen()
|
||||
defer rl.UnloadImage(img)
|
||||
|
||||
snapshotPng := filepath.Join(path, fmt.Sprintf("%s.png", kid))
|
||||
rl.ExportImage(*img, snapshotPng)
|
||||
|
||||
// capture full res compsite
|
||||
compositePng := filepath.Join(path, fmt.Sprintf("%s-composite.png", kid))
|
||||
rl.ExportImage(*capture.compositeImage, compositePng)
|
||||
|
||||
// capture full res layer
|
||||
for i, layerImage := range capture.layerImages {
|
||||
layerPng := filepath.Join(path, fmt.Sprintf("%s-%03d.png", kid, i))
|
||||
rl.ExportImage(*layerImage, layerPng)
|
||||
}
|
||||
|
||||
s.log.Printf("Checking git status...\n")
|
||||
|
||||
hash, branch, committed, err := CommitAllIfDirty(s.repoRoot, "automated snapshot", s.log)
|
||||
hash, branch, committed, err := s.SaveToGit(flakeId)
|
||||
|
||||
if err != nil {
|
||||
s.log.Printf("Error getting working tree in a known clean state: %v", err)
|
||||
} else {
|
||||
if committed {
|
||||
s.log.Printf("Created commit %s on %s for snapshot %s", hash, branch, kid)
|
||||
s.log.Printf("Created commit %s on %s for snapshot %s", hash, branch, flakeId)
|
||||
} else {
|
||||
s.log.Printf("Referencing commit %s on %s for snapshot %s", hash, branch, kid)
|
||||
s.log.Printf("Referencing commit %s on %s for snapshot %s", hash, branch, flakeId)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = s.db.Exec(`
|
||||
err = s.SaveToDb(id, flakeId, branch, hash, path, committed)
|
||||
if err != nil {
|
||||
s.log.Printf("Error writing to db: %v\n", err)
|
||||
}
|
||||
|
||||
// wysiwyg at screen res
|
||||
img := rl.LoadImageFromScreen()
|
||||
defer rl.UnloadImage(img)
|
||||
|
||||
snapshotPng := filepath.Join(path, fmt.Sprintf("%s-screen.png", flakeId))
|
||||
rl.ExportImage(*img, snapshotPng)
|
||||
|
||||
// capture full res compsite
|
||||
compositePng := filepath.Join(path, fmt.Sprintf("%s-final.png", flakeId))
|
||||
rl.ExportImage(*capture.compositeImage, compositePng)
|
||||
|
||||
// capture full res layer
|
||||
oraLayers := make([]ora.ORALayer, len(capture.layerToolsOrdered))
|
||||
for i, layerTools := range capture.layerToolsOrdered {
|
||||
filename := fmt.Sprintf("%s-%03d.png", flakeId, i)
|
||||
layerPng := filepath.Join(path, "data", filename)
|
||||
rl.ExportImage(*layerTools.capture, layerPng)
|
||||
opacity := float32(layerTools.config.a) / 255.0
|
||||
oraLayers[i] =
|
||||
ora.ORALayer{
|
||||
Name: layerTools.name,
|
||||
Filename: filename,
|
||||
Visible: layerTools.config.visible,
|
||||
Opacity: opacity,
|
||||
Blend: "svg:src-over",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
oraPath := filepath.Join(path, fmt.Sprintf("%s-layers.ora", flakeId))
|
||||
|
||||
ora.WriteORA(oraPath, int(capture.width), int(capture.height), oraLayers,
|
||||
func(name string) ([]byte, error) {
|
||||
return os.ReadFile(filepath.Join(path, "data", name))
|
||||
})
|
||||
|
||||
s.log.Printf("Saved snapshot to %s\n", path)
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (s *Storage) SaveToGit(flakeId string) (string, string, bool, error) {
|
||||
s.log.Printf("Checking git status...\n")
|
||||
return CommitAllIfDirty(s.repoRoot, "automated snapshot", s.log)
|
||||
}
|
||||
|
||||
func (s *Storage) SaveToDb(id uint64, flakeId string, branch string, hash string, path string, committed bool) error {
|
||||
|
||||
_, err := s.db.Exec(`
|
||||
INSERT INTO snapshots (id, sid, created_at, branch, git_hash, committed, path)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
id,
|
||||
kid,
|
||||
flakeId,
|
||||
time.Now().UnixMilli(),
|
||||
branch,
|
||||
hash,
|
||||
@@ -139,9 +175,7 @@ func (s *Storage) Save(capture *SketchCapture) (string, error) {
|
||||
s.log.Printf("Error inserting snapshot row into db: %v\n", err)
|
||||
}
|
||||
|
||||
s.log.Printf("Saved snapshot to %s\n", path)
|
||||
|
||||
return path, nil
|
||||
return err
|
||||
}
|
||||
|
||||
func HeadHash(repoPath string) (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user