Skip to content

Commit

Permalink
WIP add compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 11, 2024
1 parent 5f42a1c commit e68c97b
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ jobs:
run: docker build . -t anemon

- name: Run Docker container
run: docker run --rm -v ${{ github.workspace }}/assets/latex/output:/output anemon
run: docker run --rm -v ${{ github.workspace }}/assets/latex/output:/assets/latex/output anemon

- name: LSINFO
run: ls -al && tree

- name: Upload compiled PDF
uses: actions/upload-artifact@v4
Expand Down
18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
FROM golang:1.23 as build
FROM golang:1.23

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .
RUN make build
RUN ./anemon generate

FROM debian:latest
COPY --from=build /app/assets/latex/output/ /internal_output
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
RUN apt-get update && \
apt-get install -y \
texlive \
texlive-latex-extra \
texlive-fonts-extra \
texlive-xetex \
texlive-font-utils \
fonts-font-awesome \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
fonts-font-awesome && \
apt-get clean && rm -rf /var/lib/apt/lists/*

RUN make build

CMD mkdir -p /tmp_output && cd /internal_output && for i in *.tex; do pdflatex $i -output-directory=/tmp_output || true; done && ls && pwd && ls /tmp_output && cp /internal_output/*.pdf /output/
CMD ["./anemon", "generate"]
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
run:
echo "Not yet complete"

clean:
rm ./assets/latex/output/*

build:
go build

Expand All @@ -12,4 +15,4 @@ tidy:
go mod tidy

test:
go test ./...
go test -v ./...
3 changes: 2 additions & 1 deletion internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func GenerateCVFromMarkDownToLatex(root string)error{
var paramsSource core.SourceParams = &YamlSource{}
var templateReader core.TemplateReader = &output.LatexReader{}
var templateProccesor core.TemplateProcessor = &output.LatexProccesor{}
var compiler core.Compiler = &output.LatexCompiler{}
service := &core.CVService{}
return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor)
return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor,compiler)
}
45 changes: 45 additions & 0 deletions internal/adapters/output/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package output

import (
"fmt"
"log/slog"
"os"
"os/exec"
"strings"
)


const COMPILER = "pdflatex"
type LatexCompiler struct{}

//Compile the template into PDF
func (*LatexCompiler) CompileTemplate(root string)error{
templates,err := getListOfTemplate(root);if err != nil {
return err
}
for _,template := range templates{
cmd := exec.Command(COMPILER,"-interaction=nonstopmode",
"-output-directory="+root+"/assets/latex/output", template )
log, err := cmd.Output(); if err != nil {
slog.Info("---FROM pdflatex ---\n"+string(log))
slog.Error("failed to compile file:"+template)
}
}
return nil
}

//Return the path of latex file inside the template directory
func getListOfTemplate(root string)([]string, error){
var res []string
templates, err := os.ReadDir(root + "/assets/latex/output"); if err != nil {
slog.Error("failed to read directory because: "+ err.Error())
return res,err
}
for _, template := range templates {
if strings.HasSuffix(template.Name(),".tex"){
res = append(res, root+"/assets/latex/output/"+template.Name())
}
}
fmt.Println(res)
return res,nil
}
36 changes: 36 additions & 0 deletions internal/adapters/output/latex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package output

import (
"anemon/internal/core"
"path/filepath"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -290,3 +292,37 @@ func TestApplyInfoToTemplate(t *testing.T) {
}
}


func TestGetListOfTemplate(t *testing.T) {
root := "testdata"
templateDir := filepath.Join(root, "assets", "latex", "output")
err := os.MkdirAll(templateDir, os.ModePerm)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
defer os.RemoveAll(root)

files := []string{"foo.tex", "bar.tex","garbage"}
for _, file := range files {
f, err := os.Create(filepath.Join(templateDir, file))
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
f.Close()
}

got, err := getListOfTemplate(root)
if err != nil {
t.Fatalf("getListOfTemplate returned an error: %v", err)
}
expected := files
if len(got) != len(expected)-1{//Should ommit garbage
t.Errorf("expected %d files, got %d", len(expected), len(got))
}
for _, filePath := range got {
_, err := os.Stat(filePath)
if err != nil {
t.Errorf("expected path %s not found: %v", filePath, err)
}
}
}
6 changes: 4 additions & 2 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (

type CVService struct{}

//TODO apply information in header of CV
//generate the template for the cvs defined in the assets directory
func (g *CVService) GenerateTemplates(root string, source Source, paramsSource SourceParams,
templateReader TemplateReader, templateProcessor TemplateProcessor)error{
templateReader TemplateReader, templateProcessor TemplateProcessor, compiler Compiler)error{
slog.Info("--Generating CVs--")
cvs,err := source.GetCVsFrom(root);if err != nil{ return err }
params,err := paramsSource.GetParamsFrom(root);if err != nil{ return err }
Expand Down Expand Up @@ -40,10 +39,13 @@ func (g *CVService) GenerateTemplates(root string, source Source, paramsSource S
}
}
}
err = compiler.CompileTemplate(root); if err != nil{ return err }
return nil
}

//Sorte a slice of items by the number of keyword
//
//The sort is done in ascending order as the section append work like a stack(Lifo)
func sortByScore(items []string, keywords []string){
sort.Slice(items, func(i, j int) bool {
return getScore(items[i],keywords)<getScore(items[j],keywords)
Expand Down
9 changes: 7 additions & 2 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func (s *MockParamsSource) GetParamsFrom(root string) (Params, error) {
return s.Params, nil
}

type MockCompiler struct {}
func (c *MockCompiler) CompileTemplate(root string) error { return nil }

type MockSource struct {
CVs []CV
Err error
Expand Down Expand Up @@ -106,8 +109,9 @@ func TestGenerateTemplates(t *testing.T) {
paramsSource := &MockParamsSource{ Params: params, }
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor)
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler )

if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 2 { t.Fatalf("expected 2 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand All @@ -126,8 +130,9 @@ func TestGenerateTemplates(t *testing.T) {
paramsSource := &MockParamsSource{ Params: Params{}, }
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor)
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler)

if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 1 { t.Fatalf("expected 1 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand Down
4 changes: 4 additions & 0 deletions internal/core/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type ICVservice interface {
generateTemplates(root string, source Source, templateReader TemplateReader, templateProcessor TemplateProcessor)error
}

type Compiler interface {
CompileTemplate(root string) error
}

type SourceParams interface {
GetParamsFrom(root string) (Params, error)
}
Expand Down

0 comments on commit e68c97b

Please sign in to comment.