2020-08-09 05:08:30 +01:00
|
|
|
// +build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/magefile/mage/mg"
|
|
|
|
"github.com/magefile/mage/sh"
|
2020-09-12 14:06:28 +01:00
|
|
|
"gitlab.com/dananglin/cv/helpers"
|
2020-08-09 05:08:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
aspellLang string = "en_GB"
|
|
|
|
aspellPersonalWordlist string = "./.aspell/.aspell.en.pws"
|
|
|
|
cvJsonDataFile string = "./data/cv.json"
|
2020-09-12 14:06:28 +01:00
|
|
|
cvOutputDir string = "./__output"
|
|
|
|
cvTemplateDir string = "./template/tex/"
|
|
|
|
cvOutputFileName string = "cv.tex"
|
2020-08-09 05:08:30 +01:00
|
|
|
dockerfile string = "./docker/Dockerfile"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Default sets the default target.
|
|
|
|
var Default = Pdf
|
|
|
|
|
|
|
|
// SpellCheck checks the CV JSON file for spelling errors. If there are any
|
|
|
|
// misspelt words found the function returns a list of those words in an error.
|
|
|
|
// Any error found while gathering the list is returned.
|
|
|
|
// This function depends on the aspell program.
|
|
|
|
func SpellCheck() error {
|
|
|
|
fmt.Printf("Reading data from %s...\n", cvJsonDataFile)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(cvJsonDataFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read data from %s, %s", cvJsonDataFile, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// declare the aspell command and its standard input pipe.
|
|
|
|
cmd := exec.Command("aspell", "-d", aspellLang, "-p", aspellPersonalWordlist, "list")
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the CV data to standard input for piping.
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.WriteString(stdin, string(data))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// run aspell and get the list of mispelt words (if any).
|
|
|
|
// (the output is a string.)
|
|
|
|
fmt.Println("Running aspell...")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list := strings.Split(string(out), "\n")
|
|
|
|
if list[len(list)-1] == "" {
|
|
|
|
list = list[:len(list)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) > 0 {
|
|
|
|
var b strings.Builder
|
|
|
|
errMsg := fmt.Sprintf("the following spelling errors were found in %s:", cvJsonDataFile)
|
|
|
|
b.WriteString(errMsg)
|
|
|
|
|
|
|
|
for _, v := range list {
|
|
|
|
s := "\n- " + v
|
|
|
|
b.WriteString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(b.String())
|
|
|
|
} else {
|
|
|
|
fmt.Println("No spelling errors were found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tex takes the CV data file and generates the output tex file from
|
|
|
|
// template.
|
|
|
|
func Tex() error {
|
|
|
|
fmt.Println("Generating the tex file...")
|
2020-09-12 14:06:28 +01:00
|
|
|
|
2020-08-09 05:08:30 +01:00
|
|
|
mg.Deps(Clean)
|
|
|
|
|
2020-09-12 14:06:28 +01:00
|
|
|
return helpers.CreateCVTex(cvJsonDataFile, cvTemplateDir, cvOutputDir, cvOutputFileName)
|
2020-08-09 05:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pdf takes the output tex file generated by the Tex target and
|
|
|
|
// generates the output PDF file.
|
|
|
|
func Pdf() error {
|
|
|
|
mg.Deps(Tex)
|
|
|
|
|
2020-09-12 14:06:28 +01:00
|
|
|
pathArg := "--path=" + cvOutputDir
|
2020-08-09 05:08:30 +01:00
|
|
|
|
|
|
|
fmt.Println("Generating the PDF file...")
|
|
|
|
return sh.Run("mtxrun", pathArg, "--script", "context", "cv.tex")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image builds the CV builder docker image.
|
|
|
|
func Image() error {
|
2020-09-12 14:06:28 +01:00
|
|
|
image := helpers.ImageName()
|
2020-08-09 05:08:30 +01:00
|
|
|
|
|
|
|
return sh.Run("docker", "build", "-f", dockerfile, "-t", image, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishImage publishes the CV builder docker image
|
|
|
|
// to the docker registry.
|
|
|
|
func PublishImage() error {
|
|
|
|
mg.Deps(Image)
|
|
|
|
|
2020-09-12 14:06:28 +01:00
|
|
|
image := helpers.ImageName()
|
2020-08-09 05:08:30 +01:00
|
|
|
return sh.Run("docker", "push", image)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean removes the directory where the output files
|
|
|
|
// are generated.
|
|
|
|
func Clean() error {
|
2020-09-12 14:06:28 +01:00
|
|
|
if err := sh.Rm(cvOutputDir); err != nil {
|
2020-08-09 05:08:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|