mirror of
https://github.com/facundoolano/jorge.git
synced 2024-11-16 07:47:40 +01:00
initial blorg build implementation
This commit is contained in:
parent
16cbf1d10e
commit
971aed3e00
1 changed files with 56 additions and 18 deletions
74
main.go
74
main.go
|
@ -3,24 +3,24 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO consider using cobra or something else to make cli more declarative
|
|
||||||
// and get a better ux out of the box
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// TODO consider using cobra or something else to make cli more declarative
|
||||||
|
// and get a better ux out of the box
|
||||||
initCmd := flag.NewFlagSet("init", flag.ExitOnError)
|
initCmd := flag.NewFlagSet("init", flag.ExitOnError)
|
||||||
buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
|
|
||||||
newCmd := flag.NewFlagSet("new", flag.ExitOnError)
|
newCmd := flag.NewFlagSet("new", flag.ExitOnError)
|
||||||
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
|
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
printAndExit()
|
// TODO print usage
|
||||||
|
exit("expected subcommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
|
|
||||||
case "init":
|
case "init":
|
||||||
initCmd.Parse(os.Args[2:])
|
initCmd.Parse(os.Args[2:])
|
||||||
// get working directory
|
// get working directory
|
||||||
|
@ -29,14 +29,7 @@ func main() {
|
||||||
// copy over default files
|
// copy over default files
|
||||||
fmt.Println("not implemented yet")
|
fmt.Println("not implemented yet")
|
||||||
case "build":
|
case "build":
|
||||||
buildCmd.Parse(os.Args[2:])
|
build()
|
||||||
// delete target if exist
|
|
||||||
// create target dir
|
|
||||||
// walk through files in src dir
|
|
||||||
// copy them over to target
|
|
||||||
// (later render templates and org)
|
|
||||||
// (later minify)
|
|
||||||
fmt.Println("not implemented yet")
|
|
||||||
case "new":
|
case "new":
|
||||||
newCmd.Parse(os.Args[2:])
|
newCmd.Parse(os.Args[2:])
|
||||||
// prompt for title
|
// prompt for title
|
||||||
|
@ -52,12 +45,57 @@ func main() {
|
||||||
serveCmd.Parse(os.Args[2:])
|
serveCmd.Parse(os.Args[2:])
|
||||||
fmt.Println("not implemented yet")
|
fmt.Println("not implemented yet")
|
||||||
default:
|
default:
|
||||||
printAndExit()
|
// TODO print usage
|
||||||
|
exit("unknown subcommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printAndExit() {
|
// Read the files in src/ render them and copy the result to target/
|
||||||
// TODO print usage
|
func build() {
|
||||||
fmt.Println("expected a subcommand")
|
// fail if no src dir
|
||||||
|
_, err := os.ReadDir("src")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
exit("missing src/ directory")
|
||||||
|
} else if err != nil {
|
||||||
|
panic("couldn't read src")
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear previous target contents
|
||||||
|
const FILE_MODE = 0777
|
||||||
|
os.RemoveAll("target")
|
||||||
|
os.Mkdir("target", FILE_MODE)
|
||||||
|
|
||||||
|
// render each source file and copy it over to target
|
||||||
|
filepath.WalkDir("src", func(path string, entry fs.DirEntry, err error) error {
|
||||||
|
subpath, _ := filepath.Rel("src", path)
|
||||||
|
targetSubpath := filepath.Join("target", subpath)
|
||||||
|
|
||||||
|
if entry.IsDir() {
|
||||||
|
os.MkdirAll(targetSubpath, FILE_MODE)
|
||||||
|
} else {
|
||||||
|
// read file contents
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to load %s", targetSubpath))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO render templates and org
|
||||||
|
// TODO minify
|
||||||
|
|
||||||
|
// write the file contents over to target at the same location
|
||||||
|
err = os.WriteFile(targetSubpath, data, FILE_MODE)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to load %s", targetSubpath))
|
||||||
|
}
|
||||||
|
fmt.Printf("wrote %v", targetSubpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func exit(message string) {
|
||||||
|
fmt.Println(message)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue