diff --git a/Makefile b/Makefile index ad5baed..f39e93e 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ run-docker: clean: rm ./assets/latex/output/* + rm ./assets/latex/output/* build: go build diff --git a/internal/adapters/input/cli.go b/internal/adapters/input/cli.go index 1c024d0..34ce5f9 100644 --- a/internal/adapters/input/cli.go +++ b/internal/adapters/input/cli.go @@ -7,11 +7,13 @@ import ( //Use the implementation for markdown and latex to generate latex CV from a tree dir of mardown document func GenerateCVFromMarkDownToLatex(root string)error{ - var source core.Source = &MarkdownSource{} - 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,compiler) + var builder core.BuilderService = core.BuilderService{} + builder.SetRoot(root) + builder.SetSource(&MarkdownSource{}) + builder.SetParamsSource(&YamlSource{}) + builder.SetTemplateReader(&output.LatexReader{}) + builder.SetTemplateProcessor(&output.LatexProccesor{}) + builder.SetCompiler(&output.LatexCompiler{}) + service := builder.GetService() + return service.GenerateTemplates() } diff --git a/internal/core/generate.go b/internal/core/generate.go index f33e928..896a717 100644 --- a/internal/core/generate.go +++ b/internal/core/generate.go @@ -6,26 +6,77 @@ import ( "strings" ) -type CVService struct{} +type CVService struct{ + root string + source Source + paramsSource SourceParams + templateReader TemplateReader + templateProcessor TemplateProcessor + compiler Compiler +} + +type BuilderService struct{ + root string + source Source + paramsSource SourceParams + templateReader TemplateReader + templateProcessor TemplateProcessor + compiler Compiler +} + +func (cv *BuilderService) SetRoot(root string) { + cv.root = root +} + +func (cv *BuilderService) SetSource(source Source) { + cv.source = source +} + +func (cv *BuilderService) SetParamsSource(paramsSource SourceParams) { + cv.paramsSource = paramsSource +} + +func (cv *BuilderService) SetTemplateReader(templateReader TemplateReader) { + cv.templateReader = templateReader +} + +func (cv *BuilderService) SetTemplateProcessor(templateProcessor TemplateProcessor) { + cv.templateProcessor = templateProcessor +} + +func (cv *BuilderService) SetCompiler(compiler Compiler) { + cv.compiler = compiler +} + +func (s *BuilderService) GetService() CVService { + return CVService{ + root: s.root, + source: s.source, + paramsSource: s.paramsSource, + templateReader: s.templateReader, + templateProcessor: s.templateProcessor, + compiler: s.compiler, + } +} + //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, compiler Compiler)error{ +func (s *CVService) GenerateTemplates()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 } - generiqueTemplate,err := templateReader.ReadCVTemplate(root,params); if err != nil{ + cvs,err := s.source.GetCVsFrom(s.root);if err != nil{ return err } + params,err := s.paramsSource.GetParamsFrom(s.root);if err != nil{ return err } + generiqueTemplate,err := s.templateReader.ReadCVTemplate(s.root,params); if err != nil{ return err } for _, cv := range cvs { - err = generateCVFrom(cv,params,root,generiqueTemplate,templateProcessor) + err = generateCVFrom(cv,params,s.root,generiqueTemplate,s.templateProcessor) if err != nil{ return err } } - err = compiler.CompileTemplate(root); if err != nil{ return err } + err = s.compiler.CompileTemplate(s.root); if err != nil{ return err } return nil } diff --git a/internal/core/generate_test.go b/internal/core/generate_test.go index 4bc2d1c..a856311 100644 --- a/internal/core/generate_test.go +++ b/internal/core/generate_test.go @@ -110,8 +110,19 @@ func TestGenerateTemplates(t *testing.T) { templateReader := &MockTemplateReader{ Template: baseTemplate } templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) } compiler := &MockCompiler{} - service := CVService{} - err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler ) + var builder BuilderService + + builder.SetRoot(root) + builder.SetSource(source) + builder.SetParamsSource(paramsSource) + builder.SetTemplateReader(templateReader) + builder.SetTemplateProcessor(templateProcessor) + builder.SetCompiler(compiler) + + service := builder.GetService() + + err := service.GenerateTemplates() + 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)) } @@ -131,8 +142,17 @@ func TestGenerateTemplates(t *testing.T) { templateReader := &MockTemplateReader{ Template: baseTemplate } templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) } compiler := &MockCompiler{} - service := CVService{} - err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler) + var builder BuilderService + + builder.SetRoot(root) + builder.SetSource(source) + builder.SetParamsSource(paramsSource) + builder.SetTemplateReader(templateReader) + builder.SetTemplateProcessor(templateProcessor) + builder.SetCompiler(compiler) + + service := builder.GetService() + err := service.GenerateTemplates() 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)) }