diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java index 58fbbdf..c1329fe 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java @@ -1,73 +1,106 @@ package com.github.clagomess.pirilampo.core.compilers; - 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 com.github.clagomess.pirilampo.core.enums.HtmlPanelToggleEnum; +import com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.util.Properties; @Slf4j -public class PropertiesCompiler { - private static final String FILENAME = "/../html/config.properties"; - - public static ParametersDto getData(String sourcePath){ - Properties prop = new Properties(); - InputStream input = null; - - if((new File(sourcePath + FILENAME)).isFile()) { - try { - input = new FileInputStream(sourcePath + FILENAME); - - prop.load(input); - } catch (IOException ex) { - log.warn(log.getName(), ex); - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - log.warn(log.getName(), e); - } - } - } - } +public class PropertiesCompiler extends Compiler { + protected static final String FILENAME = "config.properties"; - return null; //new ParametersDto(prop); @TODO: check this + protected File getSourceDir(ParametersDto parameters){ + if(parameters.getProjectSource().isDirectory()){ + return parameters.getProjectSource(); + } else { + return parameters.getProjectSource().getParentFile(); + } } - public static void setData(ParametersDto parameters){ - File file = new File(parameters.getProjectSource() + FILENAME); + public void loadData(ParametersDto parameters){ + File file = new File(getSourceDir(parameters), FILENAME); + if(!file.isFile()) return; + + try (InputStream input = Files.newInputStream(file.toPath())){ + Properties prop = new Properties(); + prop.load(input); - if(!file.isFile()){ - try { - if(file.createNewFile()){ - log.info("Arquivo de configuraĆ§Ć£o criado em: {}", file.getAbsolutePath()); - } - } catch (IOException e) { - log.error(e.getMessage()); + parameters.setProjectName(prop.getProperty("projectName", parameters.getProjectName())); + parameters.setProjectVersion(prop.getProperty("projectVersion", parameters.getProjectVersion())); + + if(prop.containsKey("projectLogo")){ + parameters.setProjectLogo(StringUtils.isNotBlank(prop.getProperty("projectLogo")) ? + new File(prop.getProperty("projectLogo")) : + null + ); } + + parameters.setLayoutPdf(LayoutPdfEnum.valueOf(prop.getProperty( + "layoutPdf", + parameters.getLayoutPdf().name() + ))); + + parameters.setHtmlPanelToggle(HtmlPanelToggleEnum.valueOf(prop.getProperty( + "htmlPanelToggle", + parameters.getHtmlPanelToggle().name() + ))); + + parameters.setMenuColor(prop.getProperty("menuColor", parameters.getMenuColor())); + parameters.setMenuTextColor(prop.getProperty("menuTextColor", parameters.getMenuTextColor())); + + parameters.setEmbedImages(Boolean.parseBoolean(prop.getProperty( + "embedImages", + String.valueOf(parameters.isEmbedImages()) + ))); + + parameters.setCompilationType(CompilationTypeEnum.valueOf(prop.getProperty( + "compilationType", + parameters.getCompilationType().name() + ))); + + parameters.setCompilationArtifact(CompilationArtifactEnum.valueOf(prop.getProperty( + "compilationArtifact", + parameters.getCompilationArtifact().name() + ))); + + } catch (IOException e) { + log.error(log.getName(), e); } + } + + public void setData(ParametersDto parameters){ + File file = new File(getSourceDir(parameters), FILENAME); + + try (OutputStream output = Files.newOutputStream(file.toPath())){ + Properties prop = new Properties(); + prop.setProperty("projectName", parameters.getProjectName()); + prop.setProperty("projectVersion", parameters.getProjectVersion()); + prop.setProperty("projectLogo", + parameters.getProjectLogo() != null ? + parameters.getProjectLogo().getAbsolutePath() : + "" + ); + prop.setProperty("layoutPdf", parameters.getLayoutPdf().name()); + prop.setProperty("htmlPanelToggle", parameters.getHtmlPanelToggle().name()); + prop.setProperty("menuColor", parameters.getMenuColor()); + prop.setProperty("menuTextColor", parameters.getMenuTextColor()); + prop.setProperty("embedImages", String.valueOf(parameters.isEmbedImages())); + prop.setProperty("compilationType", parameters.getCompilationType().name()); + prop.setProperty("compilationArtifact", parameters.getCompilationArtifact().name()); - try (OutputStream output = new FileOutputStream(parameters.getProjectSource() + FILENAME)){ - Properties prop = parametroToProperties(parameters); prop.store(output, null); } catch (IOException ex) { log.error(log.getName(), ex); } } - - public static Properties parametroToProperties(ParametersDto parameters){ - Properties prop = new Properties(); - /* @TODO: check - prop.setProperty("txtNome", parameters.getTxtNome()); - prop.setProperty("txtVersao", parameters.getTxtVersao()); - prop.setProperty("txtLogoSrc", parameters.getTxtLogoSrc() != null ? parameters.getTxtLogoSrc() : ""); - prop.setProperty("clrMenu", parameters.getClrMenu()); - prop.setProperty("clrTextoMenu", parameters.getClrTextoMenu()); - prop.setProperty("sitEmbedarImagens", parameters.getSitEmbedarImagens().toString()); - prop.setProperty("tipPainelFechado", parameters.getTipPainel().toString()); - */ - return prop; - } } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java deleted file mode 100644 index df0387e..0000000 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompiler.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.clagomess.pirilampo.core.compilers; - -import com.github.clagomess.pirilampo.core.dto.ParametersDto; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.Test; - -import java.util.Properties; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -@Slf4j -public class PropertiesCompiler { - @Test - public void parametroToProperties(){ - Properties prop = PropertiesCompiler.parametroToProperties(new ParametersDto()); - - assertNotNull(prop.getProperty("txtNome")); - } -} diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest.java new file mode 100644 index 0000000..8be8733 --- /dev/null +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest.java @@ -0,0 +1,92 @@ +package com.github.clagomess.pirilampo.core.compilers; + +import com.github.clagomess.pirilampo.core.Common; +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 lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FileUtils; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Slf4j +public class PropertiesCompilerTest extends Common { + private final PropertiesCompiler propertiesCompiler = new PropertiesCompiler(); + private final File source = new File("target/PropertiesCompilerTest"); + + @BeforeEach + public void setup(){ + if(!source.isDirectory()){ + assertTrue(source.mkdir()); + }else{ + Arrays.stream(source.listFiles()).forEach(File::delete); + } + } + + @Test + public void getSourceDir_source_file(){ + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureFile); + parameters.setCompilationType(CompilationTypeEnum.FEATURE); + parameters.setCompilationArtifact(CompilationArtifactEnum.HTML); + + assertTrue(propertiesCompiler.getSourceDir(parameters).isDirectory()); + } + + @Test + public void getSourceDir_source_dir(){ + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureFolder); + parameters.setCompilationType(CompilationTypeEnum.FOLDER); + parameters.setCompilationArtifact(CompilationArtifactEnum.HTML); + + assertTrue(propertiesCompiler.getSourceDir(parameters).isDirectory()); + } + + @Test + public void loadData(){ + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(new File(getClass() + .getResource("PropertiesCompilerTest") + .getFile()) + ); + + propertiesCompiler.loadData(parameters); + assertEquals("foo-bar", parameters.getProjectName()); + assertTrue(parameters.isEmbedImages()); + } + + @Test + public void setData() throws IOException { + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(source); + + propertiesCompiler.setData(parameters); + + File result = new File(source, PropertiesCompiler.FILENAME); + assertTrue(result.isFile()); + + Assertions.assertThat(FileUtils.readFileToString(result)) + .contains("projectName") + .contains("projectVersion") + .contains("projectLogo") + .contains("layoutPdf") + .contains("htmlPanelToggle") + .contains("menuColor") + .contains("menuTextColor") + .contains("embedImages") + .contains("compilationType") + .contains("compilationArtifact") + .doesNotContain("projectSource") + .doesNotContain("projectMasterSource") + .doesNotContain("projectTarget"); + } +} diff --git a/core/src/test/resources/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest/config.properties b/core/src/test/resources/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest/config.properties new file mode 100644 index 0000000..cfc24c1 --- /dev/null +++ b/core/src/test/resources/com/github/clagomess/pirilampo/core/compilers/PropertiesCompilerTest/config.properties @@ -0,0 +1,12 @@ +#Fri Mar 08 09:50:14 BRT 2024 +compilationType=FOLDER +htmlPanelToggle=OPENED +projectLogo= +compilationArtifact=HTML +projectVersion=1.0 +projectTarget=foo/bar +embedImages=true +menuColor=\#14171A +layoutPdf=PORTRAIT +menuTextColor=\#DDDDDD +projectName=foo-bar