Skip to content

Commit

Permalink
feat: WIP: change output directory structure to follow Vercel Build O…
Browse files Browse the repository at this point in the history
…utput API
  • Loading branch information
macrat committed Jan 26, 2024
1 parent 77ab134 commit 114e2df
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
dist
.vercel
.cache
pages/photos
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build:
cd builder && go run .

rebuild:
-rm -rf ./dist
-rm -rf ./.vercel/output
cd builder && go run .

serve:
Expand All @@ -17,5 +17,5 @@ prepare:
git worktree add pages/photos photos

clean:
-rm -rf ./dist ./.cache
-rm -rf ./.vercel/output ./.vercel/cache
git worktree remove pages/photos
8 changes: 4 additions & 4 deletions builder/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ func (c *ArticleConverter) Convert(dst fs.Writable, src Source, conf Config) (Ar
}

func (c *ArticleConverter) convertHTML(dst fs.Writable, src Source, externalPath string, input []byte) (Article, error) {
destPath := path.Join(externalPath, "index.html")
destPath := path.Join("static", externalPath, "index.html")
if path.Base(externalPath) == "index" {
destPath = externalPath + ".html"
destPath = path.Join("static", externalPath+".html")
}

article, err := c.article.Load("/"+externalPath, input)
Expand Down Expand Up @@ -223,7 +223,7 @@ func (c *ArticleConverter) convertImage(dst fs.Writable, src Source, externalPat
externalPath = "index"
}

outputPath := path.Join("images", externalPath+".png")
outputPath := path.Join("static", "images", externalPath+".png")

asset := Asset{
name: outputPath,
Expand All @@ -249,7 +249,7 @@ func (c *ArticleConverter) convertQR(dst fs.Writable, src Source, externalPath s
externalPath = "index"
}

outputPath := path.Join("images", externalPath+".qr.png")
outputPath := path.Join("static", "images", externalPath+".qr.png")

asset := Asset{
name: outputPath,
Expand Down
41 changes: 32 additions & 9 deletions builder/autoindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"encoding/json"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (g IndexGenerator) Generate(dst fs.Writable, artifacts ArtifactList, conf C
for _, artifact := range artifacts {
if article, ok := artifact.(Article); ok {
articles = append(articles, article)
if strings.HasPrefix(article.Name(), "blog/") {
if strings.HasPrefix(article.Name(), "static/blog/") {
posts.Add(article)
}
}
Expand Down Expand Up @@ -134,6 +135,11 @@ func (g IndexGenerator) Generate(dst fs.Writable, artifacts ArtifactList, conf C
} else {
return nil, err
}
if as, err := g.generateConfig(dst, articles, conf); err == nil {
result = append(result, as...)
} else {
return nil, err
}
return result, nil
}

Expand Down Expand Up @@ -164,9 +170,9 @@ func (g *IndexGenerator) generateOrderedIndex(dst fs.Writable, articles ArticleL
}

for page := 0; page < totalPages; page++ {
targetPath := fmt.Sprintf("blog/%d/index.html", page+1)
targetPath := fmt.Sprintf("static/blog/%d/index.html", page+1)
if page == 0 {
targetPath = "blog/index.html"
targetPath = "static/blog/index.html"
}

start := page * conf.PostsPerPage
Expand Down Expand Up @@ -244,7 +250,7 @@ func (g *IndexGenerator) generateYearlyIndex(dst fs.Writable, articles ArticleLi
}

err = articles.GeneratePerPath(func(a Article) string {
return a.Published.Format("blog/2006/index.html")
return a.Published.Format("static/blog/2006/index.html")
}, func(targetPath string, articles ArticleList) error {
posts := make([]ArticleList, 12)
for _, p := range articles {
Expand Down Expand Up @@ -299,7 +305,7 @@ func (g *IndexGenerator) generateMonthlyIndex(dst fs.Writable, articles ArticleL
}

articles.GeneratePerPath(func(a Article) string {
return a.Published.Format("blog/2006/01/index.html")
return a.Published.Format("static/blog/2006/01/index.html")
}, func(targetPath string, articles ArticleList) error {
result = append(result, Index{
name: targetPath,
Expand Down Expand Up @@ -392,7 +398,7 @@ func (g *IndexGenerator) generateTagsIndex(dst fs.Writable, articles ArticleList
}

for tag, posts := range tags {
targetPath := fmt.Sprintf("blog/tags/%s/index.html", EscapeTag(tag))
targetPath := fmt.Sprintf("static/blog/tags/%s/index.html", EscapeTag(tag))

tagPageContext := TagPageContext{
URL: fmt.Sprintf("https://blanktar.jp/blog/tags/%s", EscapeTag(tag)),
Expand Down Expand Up @@ -427,7 +433,7 @@ func (g *IndexGenerator) generateTagsIndex(dst fs.Writable, articles ArticleList
}
}

targetPath := "blog/tags/index.html"
targetPath := "static/blog/tags/index.html"

result = append(result, Index{
name: targetPath,
Expand Down Expand Up @@ -473,7 +479,7 @@ func (g *IndexGenerator) generateFeed(dst fs.Writable, articles ArticleList, con
}
articles = reversed

targetPath := "blog/feed.xml"
targetPath := "static/blog/feed.xml"
result := ArtifactList{Index{
name: targetPath,
sources: articles.Sources(),
Expand Down Expand Up @@ -520,7 +526,7 @@ func (g *IndexGenerator) generateSitemap(dst fs.Writable, articles ArticleList,
}
}

targetPath := "sitemap.xml"
targetPath := "static/sitemap.xml"
result := ArtifactList{Index{
name: targetPath,
sources: as.Sources(),
Expand Down Expand Up @@ -551,3 +557,20 @@ func (g *IndexGenerator) generateSitemap(dst fs.Writable, articles ArticleList,

return result, output.Close()
}

func (g *IndexGenerator) generateConfig(dst fs.Writable, articles ArticleList, conf Config) (ArtifactList, error) {
output, err := CreateOutput(dst, "config.json", "application/json")
if err != nil {
return nil, err
}
defer output.Close()

err = json.NewEncoder(output).Encode(map[string]any{
"version": 3,
})
as := ArtifactList{Index{
name: "config.json",
sources: articles.Sources(),
}}
return as, err
}
8 changes: 5 additions & 3 deletions builder/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"io"
"path"
"strings"

"github.com/macrat/blanktar/builder/fs"
Expand Down Expand Up @@ -58,12 +59,13 @@ func (c MinifyConverter) Convert(dst fs.Writable, src Source, conf Config) (Arti
}

func Copy(dst fs.Writable, src Source, mimeType string) (ArtifactList, error) {
dstPath := path.Join("static", src.Name())
as := ArtifactList{Asset{
name: src.Name(),
name: dstPath,
source: src,
}}

if fs.ModTime(dst, src.Name()).After(src.ModTime()) {
if fs.ModTime(dst, dstPath).After(src.ModTime()) {
return as, nil
}

Expand All @@ -73,7 +75,7 @@ func Copy(dst fs.Writable, src Source, mimeType string) (ArtifactList, error) {
}
defer input.Close()

output, err := CreateOutput(dst, src.Name(), mimeType)
output, err := CreateOutput(dst, dstPath, mimeType)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions builder/errorpages.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ func (e ErrorGenerator) Generate(dst fs.Writable, as ArtifactList, conf Config)
return nil, err
}

targetPath := "static/404.html"
assets := ArtifactList{
Asset{
name: "/404.html",
name: targetPath,
},
}

if _, err := fs.Stat(dst, "/404.html"); err == nil {
if _, err := fs.Stat(dst, targetPath); err == nil {
return assets, nil
}

w, err := CreateOutput(dst, "/404.html", "text/html")
w, err := CreateOutput(dst, targetPath, "text/html")
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ func main() {
preview := len(os.Args) > 1 && os.Args[1] == "preview"

if len(os.Args) > 1 && os.Args[1] == "serve" {
serve("../dist")
serve("../.vercel/output/static")
}

sourceDir := "../pages"
src := fs.NewOnDisk(sourceDir)
var dst fs.Writable = fs.NewOnDisk("../dist")
var dst fs.Writable = fs.NewOnDisk("../.vercel/output")

if preview {
dst = fs.NewInMemory()
Expand All @@ -272,7 +272,7 @@ func main() {
log.Fatal(err)
}

cache, err := NewFileAssetCache("../.cache")
cache, err := NewFileAssetCache("../.vercel/cache")
if err != nil {
log.Fatal(err)
}
Expand Down
36 changes: 21 additions & 15 deletions builder/photos.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c PhotoConverter) Convert(dst fs.Writable, src Source, conf Config) (Artif
return nil, err
}

targetName := fmt.Sprintf("photos/%d/%s", meta.DateTime.Year(), path.Base(src.Name()))
externalName := fmt.Sprintf("photos/%d/%s", meta.DateTime.Year(), path.Base(src.Name()))
variants := make(map[int]string)
thumbnails := make(map[int]string)

Expand All @@ -75,7 +75,7 @@ func (c PhotoConverter) Convert(dst fs.Writable, src Source, conf Config) (Artif
artifacts = append(artifacts, Photo{
name: name,
source: src,
OriginalPath: targetName,
OriginalPath: externalName,
VariantPathes: variants,
ThumbnailPaths: thumbnails,
Size: size,
Expand All @@ -85,25 +85,25 @@ func (c PhotoConverter) Convert(dst fs.Writable, src Source, conf Config) (Artif

srcModTime := src.ModTime()

addArtifact(targetName, 0)
if err := c.saveImage(dst, targetName, img, srcModTime); err != nil {
addArtifact(externalName, 0)
if err := c.saveImage(dst, externalName, img, srcModTime); err != nil {
return nil, err
}

for _, size := range IMAGE_SIZES {
targetName := fmt.Sprintf("photos/%d/%s-%d.jpg", meta.DateTime.Year(), path.Base(src.Name())[0:len(path.Base(src.Name()))-4], size)
variants[size] = targetName
addArtifact(targetName, size)
if err := c.saveCompactImage(dst, targetName, img, size, srcModTime); err != nil {
externalName := fmt.Sprintf("photos/%d/%s-%d.jpg", meta.DateTime.Year(), path.Base(src.Name())[0:len(path.Base(src.Name()))-4], size)
variants[size] = externalName
addArtifact(externalName, size)
if err := c.saveCompactImage(dst, externalName, img, size, srcModTime); err != nil {
return nil, err
}
}

for _, size := range THUMBNAIL_SIZES {
targetName := fmt.Sprintf("photos/%d/%s-s%d.jpg", meta.DateTime.Year(), path.Base(src.Name())[0:len(path.Base(src.Name()))-4], size)
thumbnails[size] = targetName
addArtifact(targetName, size)
if err := c.saveThumbnail(dst, targetName, img, size, srcModTime); err != nil {
externalName := fmt.Sprintf("photos/%d/%s-s%d.jpg", meta.DateTime.Year(), path.Base(src.Name())[0:len(path.Base(src.Name()))-4], size)
thumbnails[size] = externalName
addArtifact(externalName, size)
if err := c.saveThumbnail(dst, externalName, img, size, srcModTime); err != nil {
return nil, err
}
}
Expand All @@ -127,6 +127,8 @@ func (c PhotoConverter) loadImage(src Source) (*image.Image, error) {
}

func (c PhotoConverter) saveImage(dst fs.Writable, name string, img *image.Image, srcModTime time.Time) error {
name = path.Join("static", name)

if fs.ModTime(dst, name).After(srcModTime) {
return nil
}
Expand All @@ -143,6 +145,8 @@ func (c PhotoConverter) saveImage(dst fs.Writable, name string, img *image.Image
}

func (c PhotoConverter) saveCompactImage(dst fs.Writable, name string, img *image.Image, size int, srcModTime time.Time) error {
name = path.Join("static", name)

if fs.ModTime(dst, name).After(srcModTime) {
return nil
}
Expand All @@ -159,6 +163,8 @@ func (c PhotoConverter) saveCompactImage(dst fs.Writable, name string, img *imag
}

func (c PhotoConverter) saveThumbnail(dst fs.Writable, name string, img *image.Image, size int, srcModTime time.Time) error {
name = path.Join("static", name)

if fs.ModTime(dst, name).After(srcModTime) {
return nil
}
Expand Down Expand Up @@ -266,7 +272,7 @@ func (g PhotoGenerator) generateDetailPages(dst fs.Writable, photos PhotoList, c
externalPath := fmt.Sprintf("photos/%d/%s", p.Metadata.DateTime.Year(), path.Base(p.Name())[0:len(path.Base(p.Name()))-4])

contexts = append(contexts, PhotoPageContext{
targetPath: externalPath + "/index.html",
targetPath: path.Join("static", externalPath, "index.html"),
source: p.source,

URL: fmt.Sprintf("https://blanktar.jp/%s", externalPath),
Expand Down Expand Up @@ -341,7 +347,7 @@ func (c PhotoIndexPageContext) Sources() SourceList {
func (g PhotoGenerator) generateIndexPages(dst fs.Writable, pages PhotoPageContextList, conf Config) (ArtifactList, error) {
contexts := map[string]*PhotoIndexPageContext{
"photos": {
targetPath: "photos/index.html",
targetPath: "static/photos/index.html",

PageName: "photos",
URL: "https://blanktar.jp/photos",
Expand All @@ -354,7 +360,7 @@ func (g PhotoGenerator) generateIndexPages(dst fs.Writable, pages PhotoPageConte

if _, ok := contexts[externalPath]; !ok {
contexts[externalPath] = &PhotoIndexPageContext{
targetPath: externalPath + "/index.html",
targetPath: path.Join("static", externalPath, "index.html"),

PageName: fmt.Sprintf("%d年の写真", p.Metadata.DateTime.Year()),
URL: fmt.Sprintf("https://blanktar.jp/%s", externalPath),
Expand Down
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"outputDirectory": "dist",
"buildCommand": "make build",
"trailingSlash": false,
"headers": [
{
Expand Down

0 comments on commit 114e2df

Please sign in to comment.