Skip to content

Commit

Permalink
refactor parameter compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 8, 2024
1 parent ed21584 commit f1ceb46
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f1ceb46

Please sign in to comment.