2020-09-22 22:42:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-11 22:00:29 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-04-11 23:50:18 +02:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2020-09-22 22:42:14 +02:00
|
|
|
"log"
|
2021-04-11 23:50:18 +02:00
|
|
|
"os"
|
2021-04-20 22:00:39 +02:00
|
|
|
"path"
|
2021-04-11 23:50:18 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-09-29 01:54:48 +02:00
|
|
|
)
|
|
|
|
|
2021-04-12 06:31:55 +02:00
|
|
|
var (
|
|
|
|
Version string
|
|
|
|
)
|
|
|
|
|
2020-09-22 22:42:14 +02:00
|
|
|
func main() {
|
2021-04-20 22:00:39 +02:00
|
|
|
if len(os.Args) >= 2 {
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "build":
|
|
|
|
build()
|
|
|
|
return
|
|
|
|
case "new":
|
|
|
|
if len(os.Args) < 3 {
|
|
|
|
fmt.Println("usage: kiln new <path>")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
newSite(os.Args[2])
|
|
|
|
return
|
|
|
|
case "version":
|
|
|
|
fmt.Println("kiln", Version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("usage: kiln <build | new | version> args...")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func build() {
|
2021-04-11 22:00:29 +02:00
|
|
|
var (
|
2021-04-20 22:00:39 +02:00
|
|
|
task string
|
|
|
|
config string
|
2021-04-11 22:00:29 +02:00
|
|
|
)
|
|
|
|
|
2021-04-20 22:12:52 +02:00
|
|
|
flags := flag.NewFlagSet("kiln build", flag.ExitOnError)
|
|
|
|
flags.StringVar(&task, "t", "", "the task to run")
|
|
|
|
flags.StringVar(&config, "c", "config.toml", "the configuration file to use")
|
|
|
|
flags.Parse(os.Args[2:])
|
2020-09-22 22:42:14 +02:00
|
|
|
|
2020-11-20 18:07:38 +01:00
|
|
|
// Load config
|
2021-04-11 22:00:29 +02:00
|
|
|
cfg, err := LoadConfig(config)
|
2021-03-20 07:02:36 +01:00
|
|
|
if err != nil {
|
2021-04-11 22:00:29 +02:00
|
|
|
log.Fatal(err)
|
2020-11-20 18:07:38 +01:00
|
|
|
}
|
2021-03-20 07:18:15 +01:00
|
|
|
if err := cfg.LoadTemplates("templates"); err != nil {
|
2021-04-11 22:00:29 +02:00
|
|
|
log.Fatal(err)
|
2020-10-01 04:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:00:29 +02:00
|
|
|
if err := run(cfg, task); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cfg *Config, taskName string) error {
|
2021-04-20 22:12:52 +02:00
|
|
|
if taskName == "" {
|
2021-04-11 22:00:29 +02:00
|
|
|
return runAll(cfg)
|
|
|
|
}
|
2021-04-20 22:12:52 +02:00
|
|
|
task, ok := cfg.Tasks[taskName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("run task %q: no such task", taskName)
|
|
|
|
}
|
|
|
|
return runTask(cfg, task)
|
2021-04-11 22:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runAll(cfg *Config) error {
|
|
|
|
for _, task := range cfg.Tasks {
|
|
|
|
err := runTask(cfg, task)
|
|
|
|
if err != nil {
|
2021-03-21 04:17:58 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-02-28 03:53:16 +01:00
|
|
|
}
|
2021-04-11 22:00:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-28 03:53:16 +01:00
|
|
|
|
2021-04-11 22:00:29 +02:00
|
|
|
func runTask(cfg *Config, task *Task) error {
|
|
|
|
// Load content
|
|
|
|
dir := NewDir("")
|
2021-04-12 00:42:55 +02:00
|
|
|
dir.inputExt = task.InputExt
|
|
|
|
dir.outputExt = task.OutputExt
|
|
|
|
dir.templateExt = task.TemplateExt
|
2021-04-11 22:00:29 +02:00
|
|
|
if err := dir.read("content", ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir.sort()
|
|
|
|
// Manipulate content
|
|
|
|
if err := dir.manipulate(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Write content
|
2021-04-12 00:42:55 +02:00
|
|
|
if err := dir.write(task.OutputDir, task); err != nil {
|
2021-04-11 22:00:29 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-04-11 23:50:18 +02:00
|
|
|
// Copy static files
|
2021-04-12 00:42:55 +02:00
|
|
|
if task.StaticDir != "" {
|
|
|
|
err := copyAll(task.StaticDir, task.OutputDir)
|
2021-04-11 23:50:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 22:42:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-11 23:50:18 +02:00
|
|
|
|
|
|
|
func copyAll(srcDir, dstDir string) error {
|
|
|
|
return filepath.Walk(srcDir, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
// Do nothing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
dstPath := filepath.Join(dstDir, strings.TrimPrefix(path, srcDir))
|
|
|
|
dst, err := os.Create(dstPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-04-20 22:00:39 +02:00
|
|
|
|
|
|
|
func newSite(name string) {
|
|
|
|
name = path.Clean(name)
|
|
|
|
os.Mkdir(name, 0755)
|
|
|
|
os.Mkdir(path.Join(name, "content"), 0755)
|
|
|
|
os.Mkdir(path.Join(name, "templates"), 0755)
|
|
|
|
os.Mkdir(path.Join(name, "static"), 0755)
|
|
|
|
|
|
|
|
config, _ := builtin.ReadFile("config.toml")
|
|
|
|
os.WriteFile(path.Join(name, "config.toml"), config, 0644)
|
|
|
|
|
|
|
|
index := []byte("# Hello, world!\n")
|
|
|
|
os.WriteFile(path.Join(name, "content/index.gmi"), index, 0644)
|
|
|
|
}
|