From 2d6d11df42d37f4cb8be26ec882599c7182b194a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Thu, 7 Mar 2024 15:51:24 -0300 Subject: [PATCH] some refactor --- .../core/compilers/FeatureToPDFCompiler.java | 64 +++++++++++-------- .../core/compilers/FolderToPDFCompiler.java | 13 +++- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java index 3fe23ff..4e0f34a 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java @@ -28,40 +28,54 @@ public FeatureToPDFCompiler(ParametersDto parameters) { public void build() throws Exception { File bufferHtml = File.createTempFile("pirilampo-buffer-", ".html"); log.info("Created buffer file: {}", bufferHtml); + File outArtifact = getOutArtifact(parameters); try ( - FileOutputStream fos = new FileOutputStream(bufferHtml); - BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)); - PrintWriter out = new PrintWriter(bw); + FileOutputStream fosPDF = new FileOutputStream(outArtifact); + InputStream css = Objects.requireNonNull(Thread.currentThread().getContextClassLoader() + .getResource("htmlTemplate/dist/feature-pdf.min.css")) + .openStream(); ){ - out.print(""); - out.print("

"); - out.print(String.format( - "%s %s %s", - parameters.getProjectName(), - featureMetadataDto.getName(), - parameters.getProjectVersion() - )); - out.print("

"); + parameters.setEmbedImages(false); - new GherkinDocumentParser(parameters, feature).build(out); + PdfParser pdfParser = new PdfParser(parameters, css); + pdfParser.initDocument(fosPDF); - out.print(""); - } + try ( + FileOutputStream fos = new FileOutputStream(bufferHtml); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)); + PrintWriter out = new PrintWriter(bw); + ){ + out.print(""); + out.print("

"); + out.print(String.format( + "%s %s %s", + parameters.getProjectName(), + featureMetadataDto.getName(), + parameters.getProjectVersion() + )); + out.print("

"); - // @TODO: maibe a pipe with these streams? + new GherkinDocumentParser(parameters, feature).build(out); - try ( - FileOutputStream fos = new FileOutputStream(getOutArtifact(parameters)); - InputStream html = Files.newInputStream(bufferHtml.toPath()); - InputStream css = Objects.requireNonNull(Thread.currentThread().getContextClassLoader() - .getResource("htmlTemplate/dist/feature-pdf.min.css")) - .openStream(); - ){ - // new PdfParser().build(fos, html, css, parameters.getLayoutPdf()); @TODO: fix + out.print(""); + } catch (Throwable e){ + bufferHtml.delete(); + throw e; + } finally { + if(bufferHtml.exists()){ + pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); + } + + bufferHtml.delete(); + } + + pdfParser.closeDocument(); + } catch (Throwable e){ + outArtifact.delete(); + throw e; } - // @TODO: remove buffer file // @TODO: add done and took } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java index 17d50ac..eecbe13 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java @@ -54,9 +54,20 @@ public void build() throws Exception { )); out.print(""); - new GherkinDocumentParser(parameters, feature).build(out); + GherkinDocumentParser parser = new GherkinDocumentParser(parameters, feature); + parser.build(out); out.print(""); + + if(!parser.getPaginaHtmlAnexo().isEmpty()){ + for(File html : parser.getPaginaHtmlAnexo()){ + log.info("- appending: {}", html); + out.print(String.format("

%s

", html.getName())); + writeFileToOut(html, out); + out.print(""); + } + } + out.print(""); } catch (Throwable e){ bufferHtml.delete();