2024-02-10 15:37:38 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-12 19:59:18 +01:00
|
|
|
"github.com/facundoolano/blorg/site"
|
2024-02-11 17:16:10 +01:00
|
|
|
"io"
|
2024-02-10 15:37:38 +01:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-11 17:16:10 +01:00
|
|
|
"strings"
|
2024-02-10 15:37:38 +01:00
|
|
|
)
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
const SRC_DIR = "src"
|
|
|
|
const TARGET_DIR = "target"
|
2024-02-12 20:27:06 +01:00
|
|
|
const LAYOUTS_DIR = "layouts"
|
2024-02-12 19:16:56 +01:00
|
|
|
const FILE_RW_MODE = 0777
|
|
|
|
|
2024-02-10 15:37:38 +01:00
|
|
|
func Init() error {
|
|
|
|
// get working directory
|
|
|
|
// default to .
|
|
|
|
// if not exist, create directory
|
|
|
|
// copy over default files
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the files in src/ render them and copy the result to target/
|
2024-02-12 20:27:06 +01:00
|
|
|
// TODO add root dir override support
|
2024-02-10 15:37:38 +01:00
|
|
|
func Build() error {
|
2024-02-12 20:27:06 +01:00
|
|
|
site, err := site.Load(SRC_DIR, LAYOUTS_DIR)
|
2024-02-12 19:59:18 +01:00
|
|
|
if err != nil {
|
2024-02-12 19:16:56 +01:00
|
|
|
return err
|
2024-02-10 15:37:38 +01:00
|
|
|
}
|
2024-02-12 19:16:56 +01:00
|
|
|
|
|
|
|
// clear previous target contents
|
|
|
|
os.RemoveAll(TARGET_DIR)
|
|
|
|
os.Mkdir(TARGET_DIR, FILE_RW_MODE)
|
|
|
|
|
|
|
|
// walk the source directory, creating directories and files at the target dir
|
|
|
|
return filepath.WalkDir(SRC_DIR, func(path string, entry fs.DirEntry, err error) error {
|
|
|
|
subpath, _ := filepath.Rel(SRC_DIR, path)
|
|
|
|
targetPath := filepath.Join(TARGET_DIR, subpath)
|
2024-02-11 17:16:10 +01:00
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
if entry.IsDir() {
|
|
|
|
os.MkdirAll(targetPath, FILE_RW_MODE)
|
|
|
|
} else {
|
|
|
|
|
2024-02-14 01:32:04 +01:00
|
|
|
if templ, ok := site.Templates[path]; ok {
|
2024-02-12 19:16:56 +01:00
|
|
|
// if a template was found at source, render it
|
2024-02-12 19:59:18 +01:00
|
|
|
content, err := site.Render(templ)
|
2024-02-11 17:16:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the file contents over to target at the same location
|
2024-02-12 19:16:56 +01:00
|
|
|
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.Ext()
|
2024-02-12 20:27:06 +01:00
|
|
|
fmt.Println("writing", targetPath)
|
2024-02-12 19:16:56 +01:00
|
|
|
return os.WriteFile(targetPath, []byte(content), FILE_RW_MODE)
|
2024-02-11 17:16:10 +01:00
|
|
|
} else {
|
|
|
|
// if a non template was found, copy file as is
|
2024-02-12 20:27:06 +01:00
|
|
|
fmt.Println("writing", targetPath)
|
2024-02-11 17:16:10 +01:00
|
|
|
return copyFile(path, targetPath)
|
2024-02-10 15:37:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-11 17:16:10 +01:00
|
|
|
func copyFile(source string, target string) error {
|
|
|
|
// does this need to be so verbose?
|
|
|
|
srcFile, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
targetFile, _ := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer targetFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(targetFile, srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetFile.Sync()
|
|
|
|
}
|
|
|
|
|
2024-02-10 15:37:38 +01:00
|
|
|
func New() error {
|
|
|
|
// prompt for title
|
|
|
|
// slugify
|
|
|
|
// fail if file already exist
|
|
|
|
// create a new .org file with the slug
|
|
|
|
// add front matter and org options
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Serve() error {
|
|
|
|
// build
|
|
|
|
// serve target with file server
|
|
|
|
// (later watch and live reload)
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|