Skip to content

Commit

Permalink
Added 'locations:' prop in config'.
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-a-otc committed Nov 1, 2023
1 parent 18a1834 commit 5028130
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
*/
package org.otcframework.common.config;

import org.apache.commons.io.FileUtils;
import org.otcframework.common.config.exception.OtcConfigException;
import org.otcframework.common.exception.OtcException;
import org.otcframework.common.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -52,10 +50,10 @@ public enum OtcConfig {

private static final String OTC_UNITTEST_FOLDER = "otc-unittest" + File.separator;
private static final String OTC_LIB_FOLDER = "lib" + File.separator;
private static final String OTC_SRC_FOLDER = "src" + File.separator;
public static final String OTC_SRC_FOLDER = "src" + File.separator;
public static final String OTC_TMD_FOLDER = "tmd" + File.separator;
private static final String OTC_TARGET_FOLDER = "target" + File.separator;
private static final String OTC_CONFIG_FILE = "config" + File.separator + "otc.yaml";
public static final String OTC_TARGET_FOLDER = "target" + File.separator;
public static final String OTC_CONFIG_FILE = "config" + File.separator + "otc.yaml";
private static boolean isDefaultLocations = true;
private static String otcHome;

Expand Down Expand Up @@ -92,18 +90,18 @@ public enum OtcConfig {
throw new OtcConfigException(ex);
}
// -- load sourceCodeLocation and tmdLocation properties
if (YAML_CONFIG.compiler != null) {
sourceCodeLocation = YAML_CONFIG.compiler.sourceCodeLocation;
tmdLocation = YAML_CONFIG.compiler.tmdLocation;
targetLocation = YAML_CONFIG.compiler.targetLocation;
if (YAML_CONFIG.compiler != null && YAML_CONFIG.compiler.locations != null) {
sourceCodeLocation = YAML_CONFIG.compiler.locations.sourceCodeLocation;
tmdLocation = YAML_CONFIG.compiler.locations.tmdLocation;
targetLocation = YAML_CONFIG.compiler.locations.targetLocation;
boolean isSourceCodeLocationDefined = !CommonUtils.isTrimmedAndEmpty(sourceCodeLocation);
boolean isTmdLocationDefined = !CommonUtils.isTrimmedAndEmpty(tmdLocation);
boolean isTargetLocationDefined = !CommonUtils.isTrimmedAndEmpty(targetLocation);
if (!(isSourceCodeLocationDefined == isTmdLocationDefined &&
isSourceCodeLocationDefined == isTargetLocationDefined)) {
throw new OtcConfigException("", String.format("Either ALL or NONE of this set of 3 properties " +
"('compiler.sourceCodeLocation:', 'compiler.tmdLocation:', 'compiler.targetLocation:') " +
"should be defined in the '%s%s' file.", otcHome, OTC_CONFIG_FILE));
"('compiler.locations.sourceCodeLocation:', 'compiler.locations.tmdLocation:', " +
"'compiler.locations.targetLocation:') should be defined in the '%s%s' file.", otcHome, OTC_CONFIG_FILE));
}
if (isSourceCodeLocationDefined) {
isDefaultLocations = false;
Expand All @@ -112,20 +110,19 @@ public enum OtcConfig {
LOGGER.warn("You have set 'compiler.cleanupBeforeCompile' property to true. Updated " +
"source-code if any will be lost during clean-up.");
}
sourceCodeLocation = initFolder(sourceCodeLocation, OTC_SRC_FOLDER);
targetLocation = initFolder(targetLocation, OTC_TARGET_FOLDER);

if (!CommonUtils.isTrimmedAndEmpty(tmdLocation)) {
if (!tmdLocation.endsWith(File.separator)) {
tmdLocation += File.separator;
}
tmdLocation += OTC_TMD_FOLDER;
} else {
tmdLocation = otcHome + OTC_TMD_FOLDER;
}
sourceCodeLocation = initFolder(sourceCodeLocation, OTC_SRC_FOLDER);
targetLocation = initFolder(targetLocation, OTC_TARGET_FOLDER);
if (!CommonUtils.isTrimmedAndEmpty(tmdLocation)) {
if (!tmdLocation.endsWith(File.separator)) {
tmdLocation += File.separator;
}
deleteRecursive(tmdLocation);
OtcUtils.creteDirectory(tmdLocation);
tmdLocation += OTC_TMD_FOLDER;
} else {
tmdLocation = otcHome + OTC_TMD_FOLDER;
}
OtcUtils.deleteRecursive(tmdLocation);
OtcUtils.creteDirectory(tmdLocation);

try {
URL url = new File(targetLocation).toURI().toURL();
Expand All @@ -147,9 +144,7 @@ private static String initFolder(String configuredPath, String defaultPath) {
} else {
path = otcHome + defaultPath;
}
if (getCleanupBeforeCompile()) {
deleteRecursive(path);
}
OtcUtils.deleteRecursive(path);
OtcUtils.creteDirectory(path);
return path;
}
Expand All @@ -163,7 +158,7 @@ public static String getOtcHomeLocation() {
return otcHome;
}

public static Boolean isDefaultLocations() {
public static boolean isDefaultLocations() {
return isDefaultLocations;
}
/**
Expand All @@ -172,8 +167,8 @@ public static Boolean isDefaultLocations() {
* @return the otc lib location
*/
public static String getOtcLibLocation() {
if (YAML_CONFIG.compiler.libLocation != null) {
return YAML_CONFIG.compiler.libLocation;
if (YAML_CONFIG.compiler.locations != null && YAML_CONFIG.compiler.locations.libLocation != null) {
return YAML_CONFIG.compiler.locations.libLocation;
}
return otcHome + OTC_LIB_FOLDER;
}
Expand All @@ -190,7 +185,7 @@ public static boolean getCleanupBeforeCompile() {
*
* @return the otc source location
*/
public static String getOtcSourceLocation() {
public static String getUnitTestLocation() {
return otcHome + OTC_UNITTEST_FOLDER;
}

Expand Down Expand Up @@ -287,35 +282,23 @@ public static Map<Class<?>, String> getConcreteTypes() {
return null;
}

private static void deleteRecursive(String path) {
if (!isDefaultLocations || !OtcConfig.getCleanupBeforeCompile()) {
return;
}
File folder = new File(path);
if (!folder.isDirectory() || !folder.exists()) {
return;
}
try {
FileUtils.deleteDirectory(folder);
} catch (IOException e) {
throw new OtcConfigException("", "Could not clean up generated folders.", e);
}
}

public static final class YamlConfig {
public CompilerProps compiler;
public Map<String, String> concreteTypes;
public Set<String> filterPackages;

public static final class CompilerProps {
public Boolean failFast;
public String libLocation;
public Boolean cleanupBeforeCompile;
public String sourceCodeLocation;
public String tmdLocation;
public String targetLocation;
public Integer cyclicReferenceDepth;
}
public Locations locations;

public static final class Locations {
public String libLocation;
public String sourceCodeLocation;
public String tmdLocation;
public String targetLocation;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
*/
package org.otcframework.common.util;

import org.apache.commons.io.FileUtils;
import org.otcframework.common.OtcConstants;
import org.otcframework.common.config.OtcConfig;
import org.otcframework.common.config.exception.OtcConfigException;
import org.otcframework.common.dto.OtcCommandDto;
import org.otcframework.common.exception.OtcException;
import org.otcframework.common.exception.OtcUnsupportedJdkException;
Expand All @@ -32,6 +34,7 @@

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -332,4 +335,19 @@ public static void creteDirectory(String path) {
}
}

public static void deleteRecursive(String path) {
if (!OtcConfig.isDefaultLocations() || !OtcConfig.getCleanupBeforeCompile()) {
return;
}
File folder = new File(path);
if (!folder.isDirectory() || !folder.exists()) {
return;
}
try {
FileUtils.deleteDirectory(folder);
} catch (IOException e) {
throw new OtcConfigException("", "Could not clean up generated folders.", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final class OtcsCompilerImpl implements OtcsCompiler {
private static final OtcCodeGenerator otcCodeGenerator = OtcCodeGeneratorImpl.getInstance();

/** The Constant otcSrcDir. */
private static final String OTCS_SOURCE_LOCATION = OtcConfig.getOtcSourceLocation();
private static final String UNIT_TEST_LOCATION = OtcConfig.getUnitTestLocation();

/** The Constant srcDir. */
private static final String SOURCE_CODE_LOCATION = OtcConfig.getSourceCodeLocation();
Expand Down Expand Up @@ -127,11 +127,14 @@ public static OtcsCompiler getInstance() {
@Override
public List<CompilationReport> compileOtcsFiles() {
long startTime = System.nanoTime();
LOGGER.info("Initiating OTCS file compilations in {}", OTCS_SOURCE_LOCATION);
File otcSourceDirectory = new File(OTCS_SOURCE_LOCATION);
LOGGER.info("Initiating OTCS file compilations in {}", UNIT_TEST_LOCATION);
File otcSourceDirectory = new File(UNIT_TEST_LOCATION);
if (!otcSourceDirectory.exists()) {
throw new OtcCompilerException("", String.format("Missing '%s' folder.", UNIT_TEST_LOCATION));
}
List<CompilationReport> compilationReports = compileOtc(otcSourceDirectory, null);
if (compilationReports == null) {
LOGGER.info("No OTCS files to compile in '{}'", OTCS_SOURCE_LOCATION);
LOGGER.info("No OTCS files to compile in '{}'", UNIT_TEST_LOCATION);
return null;
}
int successful = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class OtcCommand {
private static final String TARGET_LOCATION = OtcConfig.getTargetLocation();

/** The Constant otcSourceDir. */
private static final String OTC_SOURCE_DIR = OtcConfig.getOtcSourceLocation();
private static final String UNIT_TEST_LOCATION = OtcConfig.getUnitTestLocation();

/** The Constant sourceFileLocation. */
private static final String SOURCE_FILE_LOCATION = OtcConfig.getSourceCodeLocation();
Expand Down Expand Up @@ -329,7 +329,7 @@ private void appendBeginClass(TargetOtcCommandContext targetOCC, SourceOtcComman
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
file = new File(OTC_SOURCE_DIR + fileName + ".java");
file = new File(UNIT_TEST_LOCATION + fileName + ".java");
try {
Files.delete(file.toPath());
} catch (IOException e) {
Expand Down

0 comments on commit 5028130

Please sign in to comment.