1package main
2
3import (
4 "errors"
5 "fmt"
6 "io"
7 "os"
8 "path"
9 "path/filepath"
10 "sort"
11
12 "github.com/jphastings/dotpostcard/formats"
13 "github.com/jphastings/dotpostcard/formats/component"
14 "github.com/jphastings/dotpostcard/formats/usdz"
15 "github.com/jphastings/dotpostcard/formats/web"
16 "github.com/jphastings/dotpostcard/types"
17 "github.com/jphastings/shutup.jp/build/mapping"
18)
19
20const outDir = "dist/"
21
22func check(e error) {
23 if e != nil {
24 panic(e)
25 }
26}
27
28type tmplVars struct {
29 Postcards []types.Postcard
30 Sizes map[string]int64
31 Countries mapping.Countries
32}
33
34func main() {
35 files, err := filepath.Glob("postcards/*.postcard.webp")
36 check(err)
37 doHeavyLifting := true
38
39 vars := tmplVars{
40 Sizes: make(map[string]int64),
41 Countries: make(mapping.Countries),
42 }
43 toCopy := []string{
44 "static/ar.svg",
45 "static/postcard.css",
46 "static/shutup.css",
47 "static/bg-light.png",
48 "static/bg-dark.png",
49 "static/og-image.jpg",
50 }
51 check(os.MkdirAll(outDir, 0755))
52
53 if doHeavyLifting {
54 fmt.Println("Finding postcards & extracting front/back images & 3D models")
55 for _, file := range files {
56 f, err := os.Open(file)
57 check(err)
58 defer f.Close()
59
60 b := web.BundleFromReader(f, file)
61 pc, err := b.Decode(formats.DecodeOptions{})
62 check(err)
63
64 pcImgName := "postcards/" + pc.Name + ".postcard.webp"
65 vars.Postcards = append(vars.Postcards, pc)
66 toCopy = append(toCopy, pcImgName)
67 vars.Countries.Add(pc.Meta.Location)
68
69 // Make front & back covers
70 compFWs, err := component.Codec().Encode(pc, &formats.EncodeOptions{MaxDimension: 800})
71 check(err)
72
73 // Make 3D models
74 usdFWs, err := usdz.Codec().Encode(pc, &formats.EncodeOptions{})
75 check(err)
76
77 fws := append(compFWs, usdFWs...)
78
79 for _, fw := range fws {
80 fname, err := fw.WriteFile(outDir, false)
81 if !errors.Is(err, os.ErrExist) {
82 check(err)
83 }
84 fs, err := os.Stat(path.Join(outDir, fname))
85 check(err)
86 vars.Sizes[fname] = fs.Size()
87 }
88 }
89 sort.Sort(types.BySentOn(vars.Postcards))
90 }
91
92 fmt.Println("Creating index.html")
93 indexF, err := os.OpenFile(path.Join(outDir, "index.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
94 check(err)
95 check(indexTmpl.Execute(indexF, vars))
96
97 fmt.Println("Creating feed.xml")
98 feedF, err := os.OpenFile(path.Join(outDir, "feed.xml"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
99 check(err)
100 check(feedTmpl.Execute(feedF, vars))
101
102 fmt.Printf("Copying files for distribution:\n")
103 for _, tc := range toCopy {
104 fmt.Printf(" %s\n", tc)
105 src, err := os.Open(tc)
106 check(err)
107
108 dst, err := os.OpenFile(path.Join(outDir, filepath.Base(tc)), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
109 check(err)
110
111 _, err = io.Copy(dst, src)
112 check(err)
113 }
114}