From 159c58373e0ac8dd8ed7da15a322441507d71c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Thu, 7 Mar 2024 10:53:09 -0300 Subject: [PATCH] main cli done; add todo's; fix getOutArtifact --- .../github/clagomess/pirilampo/cli/Main.java | 90 +++++++---------- .../clagomess/pirilampo/cli/MainOptions.java | 5 +- .../clagomess/pirilampo/cli/MainTest.java | 91 +++++++----------- .../pirilampo/cli/feature/html_embed.html | 1 + .../pirilampo/cli/feature/smallest.png | Bin 0 -> 67 bytes .../pirilampo/cli/feature/xxx.Feature | 27 ++++++ .../clagomess/pirilampo/cli/feature/xxx.png | Bin 0 -> 796 bytes .../pirilampo/cli/master/xxx.Feature | 28 ++++++ .../pirilampo/core/compilers/Compiler.java | 12 ++- .../core/compilers/FeatureToHTMLCompiler.java | 2 + .../core/compilers/FeatureToPDFCompiler.java | 1 + .../core/compilers/FolderToHTMLCompiler.java | 2 + .../core/compilers/FolderToPDFCompiler.java | 1 + 13 files changed, 145 insertions(+), 115 deletions(-) create mode 100644 cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/html_embed.html create mode 100644 cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/smallest.png create mode 100644 cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/xxx.Feature create mode 100644 cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/xxx.png create mode 100644 cli/src/test/resources/com/github/clagomess/pirilampo/cli/master/xxx.Feature diff --git a/cli/src/main/java/com/github/clagomess/pirilampo/cli/Main.java b/cli/src/main/java/com/github/clagomess/pirilampo/cli/Main.java index 37f9b92..48b5dba 100644 --- a/cli/src/main/java/com/github/clagomess/pirilampo/cli/Main.java +++ b/cli/src/main/java/com/github/clagomess/pirilampo/cli/Main.java @@ -1,72 +1,56 @@ package com.github.clagomess.pirilampo.cli; +import com.github.clagomess.pirilampo.core.compilers.FeatureToHTMLCompiler; +import com.github.clagomess.pirilampo.core.compilers.FeatureToPDFCompiler; +import com.github.clagomess.pirilampo.core.compilers.FolderToHTMLCompiler; +import com.github.clagomess.pirilampo.core.compilers.FolderToPDFCompiler; +import com.github.clagomess.pirilampo.core.dto.ParametersDto; import lombok.extern.slf4j.Slf4j; +import java.util.Arrays; + +import static com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum.HTML; +import static com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum.PDF; +import static com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum.*; + @Slf4j public class Main { - /* - public static void main(String[] args) throws Exception { - Main main = new Main(); - log.info("Pirilampo - Ver.: {}", main.getVersion()); + private static final MainOptions mainOptions = new MainOptions(); - if(args.length > 0){ - CommandLine cmd = consoleOptions(args); - Compilador compilador = new Compilador(); + public static void main(String[] args) { + log.info("Pirilampo - Ver.: {}", Main.class.getPackage().getImplementationVersion()); - if(cmd.getOptionValue("feature") == null && cmd.getOptionValue("feature_path") == null){ - log.warn("É necessário informar {feature} ou {feature_path}"); - System.exit(1); - } + try { + ParametersDto parameters = mainOptions.getArgs(args); - if(cmd.getOptionValue("feature") != null){ - compilador.compilarFeature(new Parametro(cmd)); - System.exit(0); + if (parameters.getCompilationType() == FEATURE && + parameters.getCompilationArtifact() == HTML + ) { + new FeatureToHTMLCompiler(parameters).build(); } - if(cmd.getOptionValue("feature_path") != null){ - compilador.compilarPasta(new Parametro(cmd)); - System.exit(0); + if (parameters.getCompilationType() == FEATURE && + parameters.getCompilationArtifact() == PDF + ) { + new FeatureToPDFCompiler(parameters).build(); } - }else{ - MainUi.launch(MainUi.class); - } - } - - private static CommandLine consoleOptions(String[] args){ - Options options = new Options(); - Option option; - - options.addOption(new Option("feature", true, "Arquivo *.feature")); - options.addOption(new Option("feature_path", true, "Diretório contendo arquivos *.feature")); - options.addOption(new Option("feature_path_master", true, "Diretório contendo arquivos *.feature master")); - options.addOption(new Option("output", true, "Diretório de saída")); - option = new Option("name", true, "Nome do projeto"); - option.setRequired(true); - options.addOption(option); - - option = new Option("version", true, "Versão"); - option.setRequired(true); - options.addOption(option); - - CommandLineParser parser = new DefaultParser(); - HelpFormatter formatter = new HelpFormatter(); - CommandLine cmd = null; + if (Arrays.asList(FOLDER, FOLDER_DIFF).contains(parameters.getCompilationType()) && + parameters.getCompilationArtifact() == HTML + ) { + new FolderToHTMLCompiler(parameters).build(); + } - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - log.info(e.getMessage()); - formatter.printHelp("Pirilampo", options); + if (parameters.getCompilationType() == FOLDER && + parameters.getCompilationArtifact() == PDF + ) { + new FolderToPDFCompiler(parameters).build(); + } + System.exit(0); + } catch (Throwable e) { + log.error(log.getName(), e); System.exit(1); } - - return cmd; - } - - private synchronized String getVersion(){ - return getClass().getPackage().getImplementationVersion(); } - */ } diff --git a/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java b/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java index 84ff224..e85f9e8 100644 --- a/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java +++ b/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java @@ -183,12 +183,11 @@ public ParametersDto getArgs(String[] argv){ parameters.validate(); return parameters; - } catch (ParseException | ParametersException e ) { + } catch (ParseException | ParametersException e) { log.error(e.getMessage()); + new HelpFormatter().printHelp("Pirilampo", options); } catch (Throwable e) { log.error(log.getName(), e); - } finally { - new HelpFormatter().printHelp("Pirilampo", options); } System.exit(1); diff --git a/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainTest.java b/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainTest.java index 334827b..2515bf3 100644 --- a/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainTest.java +++ b/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainTest.java @@ -1,69 +1,52 @@ package com.github.clagomess.pirilampo.cli; -import com.github.clagomess.pirilampo.cli.Main; +import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; -import java.io.File; -import java.util.Calendar; +import java.util.LinkedList; +import java.util.List; @Slf4j public class MainTest { - /* - @Rule - public final ExpectedSystemExit exit = ExpectedSystemExit.none(); - - private File criarPasta(){ - String dir = System.getProperty("java.io.tmpdir"); - dir += File.separator; - dir += "pirilampo_test"; - - File f = new File(dir); - - if (f.isDirectory() || f.mkdir()) { - dir += File.separator; - dir += (new Long(Calendar.getInstance().getTime().getTime())).toString(); - - f = new File(dir); - if(f.mkdir()){ - pastas.add(f); - } + @Test + @ExpectSystemExitWithStatus(1) + public void main_options_validate(){ + Main.main(new String[]{"-projectSource", "aaa"}); + } - log.info("Pasta de teste: {}", f.getAbsolutePath()); + @ParameterizedTest + @CsvSource(value = { + "FEATURE,HTML,feature/xxx.Feature,", + "FEATURE,PDF,feature/xxx.Feature,", + "FOLDER,HTML,feature,", + "FOLDER_DIFF,HTML,feature,master", + "FOLDER,PDF,feature,", + }) + @ExpectSystemExitWithStatus(0) + public void main_ok( + String compilationType, + String compilationArtifact, + String projectSource, + String projectMasterSource + ){ + List argv = new LinkedList<>(); + argv.add("-projectSource"); + argv.add(getClass().getResource(projectSource).getFile()); + + if(projectMasterSource != null) { + argv.add("-projectMasterSource"); + argv.add(getClass().getResource(projectMasterSource).getFile()); } - return f; - } - - @Test - public void testMain() throws Exception { - exit.expectSystemExit(); + argv.add("-compilationType"); + argv.add(compilationType); - String outDir = criarPasta().getAbsolutePath(); - Main.main(new String[]{ - "-feature_path", - resourcePath + File.separator + "feature", - "-name", - "XXX", - "-version", - "1.2.3", - "-output", - outDir, - }); - Assert.assertTrue((new File(outDir + File.separator + "index.html")).isFile()); + argv.add("-compilationArtifact"); + argv.add(compilationArtifact); - outDir = criarPasta().getAbsolutePath(); - Main.main(new String[]{ - "-feature", - resourcePath + File.separator + "feature/xxx.Feature", - "-name", - "XXX", - "-version", - "1.2.3", - "-output", - criarPasta().getAbsolutePath(), - }); - Assert.assertTrue((new File(outDir + File.separator + "xxx.html")).isFile()); + Main.main(argv.toArray(new String[0])); } - */ } diff --git a/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/html_embed.html b/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/html_embed.html new file mode 100644 index 0000000..77889c8 --- /dev/null +++ b/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/html_embed.html @@ -0,0 +1 @@ +html_embed_txt \ No newline at end of file diff --git a/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/smallest.png b/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/smallest.png new file mode 100644 index 0000000000000000000000000000000000000000..252d9502d8573d033e633f5e377d81bebf8afd36 GIT binary patch literal 67 zcmeAS@N?(olHy`uVBq!ia0vp^j35jm7|ip2ssJf2PZ!6K3dZCFAe)JSvAC2`0?1 | +| | +| ![Image](https://pt.wikipedia.org/static/images/project-logos/ptwiki.png) | +| Link Html Embeded: [Link Embeded](html_embed.html) | +| Link Google: [Google](https://www.google.com.br) | +| strike | +| strike
strike
| + + +Esquema do Cenário: JJJ +Quando xxx +E YYY +Exemplos: +| a | b | +| c | d | \ No newline at end of file diff --git a/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/xxx.png b/cli/src/test/resources/com/github/clagomess/pirilampo/cli/feature/xxx.png new file mode 100644 index 0000000000000000000000000000000000000000..d61f866a9201adf76cc4d7a8cbd477d310c3f22c GIT binary patch literal 796 zcmV+%1LOROP)#2Rtl5?WeD{_BtfYYA2FIRjSF4O!ksh2(m%l%H!jR< z+_}>*i^)tB_x;Yf=fP1*@vYC- z-(Ls7&Y;ZC%}Ix8_-~od1cSpo&p+ch?j^$$mirGCQc4dHjYjF|KtSvs7n}7iHAT-} zxTT?&89eh`rd+p-VUK%lRL4UQP0*wq3{rwYQHi>yLMh}>=B-TU669_G?t>Lrn!VF?0u?AJ8wiqX3&`zk zAg`2=Psb5onyb{yyvTIcX7A4cS}P6ueZKYWjZaTCog;dQps4E5Bn{a_3=$isH`k!# z`FKX*+UqT5Tx@5b zq*qodTxNM5JO9N | +| | +| ![Image](https://pt.wikipedia.org/static/images/project-logos/ptwiki.png) | +| Link Html Embeded: [Link Embeded](html_embed.html) | +| Link Google: [Google](https://www.google.com.br) | +| strike | +| strike
strike
| + + +Esquema do Cenário: JJJ +Quando xxx +E YYY +Exemplos: +| a | b | +| c | d | \ No newline at end of file diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java index 3a1dc5a..a214e7e 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java @@ -2,8 +2,6 @@ import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; -import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; -import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; import org.apache.commons.io.input.BOMInputStream; import java.io.*; @@ -13,6 +11,10 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum.HTML; +import static com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum.FOLDER; +import static com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum.FOLDER_DIFF; + public abstract class Compiler { public String getFeatureExtension(File f){ Matcher matcher = Pattern.compile("\\.feature$", Pattern.CASE_INSENSITIVE) @@ -64,7 +66,7 @@ protected FeatureMetadataDto getFeatureMetadata(ParametersDto parameters, File f } protected File getOutArtifact(ParametersDto parameters){ - if(parameters.getCompilationType() == CompilationTypeEnum.FOLDER){ + if(Arrays.asList(FOLDER, FOLDER_DIFF).contains(parameters.getCompilationType())){ File targetDir = parameters.getProjectTarget() != null ? new File(parameters.getProjectTarget(), "html") : new File(parameters.getProjectSource().getParent(), "html"); @@ -73,12 +75,12 @@ protected File getOutArtifact(ParametersDto parameters){ throw new RuntimeException(String.format("Failed to create dir: %s", targetDir.getAbsolutePath())); } - return new File(targetDir, parameters.getCompilationArtifact() == CompilationArtifactEnum.HTML ? "index.html" : "index.pdf"); + return new File(targetDir, parameters.getCompilationArtifact() == HTML ? "index.html" : "index.pdf"); }else{ String filename = String.format( "%s.%s", getFeatureMetadata(parameters, parameters.getProjectSource()).getName(), - parameters.getCompilationArtifact() == CompilationArtifactEnum.HTML ? "html" : "pdf" + parameters.getCompilationArtifact() == HTML ? "html" : "pdf" ); File targetDir = parameters.getProjectTarget() != null ? diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java index feb55ec..8f36692 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java @@ -47,5 +47,7 @@ public void build() throws Exception { out.print(""); } + + // @TODO: add done and took } } 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 9c0970a..242f65c 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 @@ -62,5 +62,6 @@ public void build() throws Exception { } // @TODO: remove buffer file + // @TODO: add done and took } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java index ac5332d..7b3c759 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java @@ -210,5 +210,7 @@ public void build() throws Exception { out.print("\n"); out.print(""); } + + // @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 d099fd4..8e741c6 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 @@ -63,5 +63,6 @@ public void build() throws Exception { // @TODO: remove buffer file // @TODO: impl. PDF Index + // @TODO: add done and took } }