From 8bd064901c64c0ab6ad7921d356f833b5b02b788 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Thu, 29 Apr 2021 21:45:25 -0400 Subject: [PATCH] Refactoring, technical debt: export more of trigger attributes into triggers.txt file #2077 moving methods --- .../src/com/rusefi/ConfigDefinition.java | 77 +------------------ .../src/com/rusefi/util/CachingStrategy.java | 76 ++++++++++++++++++ 2 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 java_tools/configuration_definition/src/com/rusefi/util/CachingStrategy.java diff --git a/java_tools/configuration_definition/src/com/rusefi/ConfigDefinition.java b/java_tools/configuration_definition/src/com/rusefi/ConfigDefinition.java index 224917dce9..5d49bdbc07 100644 --- a/java_tools/configuration_definition/src/com/rusefi/ConfigDefinition.java +++ b/java_tools/configuration_definition/src/com/rusefi/ConfigDefinition.java @@ -1,11 +1,8 @@ package com.rusefi; import com.rusefi.output.*; -import com.rusefi.util.IoUtils; -import com.rusefi.util.LazyFile; -import com.rusefi.util.SystemOut; +import com.rusefi.util.*; import com.rusefi.enum_reader.Value; -import com.rusefi.util.ZipUtil; import org.yaml.snakeyaml.Yaml; import java.io.*; @@ -203,7 +200,7 @@ public class ConfigDefinition { boolean needToUpdateTsFiles = isNeedToUpdateTsFiles(tsPath, cachePath, cacheZipFile, inputAllFiles); - boolean needToUpdateOtherFiles = checkIfOutputFilesAreOutdated(inputFiles, cachePath, cacheZipFile); + boolean needToUpdateOtherFiles = CachingStrategy.checkIfOutputFilesAreOutdated(inputFiles, cachePath, cacheZipFile); if (!needToUpdateTsFiles && !needToUpdateOtherFiles) { SystemOut.println("All output files are up-to-date, nothing to do here!"); return; @@ -266,7 +263,7 @@ public class ConfigDefinition { processTextTemplate(romRaiderInputFile, romRaiderDestination); } - saveCachedInputFiles(inputAllFiles, cachePath, cacheZipFile); + CachingStrategy.saveCachedInputFiles(inputAllFiles, cachePath, cacheZipFile); } private static void handleFiringOrder(String firingEnumFileName) throws IOException { @@ -295,7 +292,7 @@ public class ConfigDefinition { boolean needToUpdateTsFiles = false; if (tsPath != null) { SystemOut.println("Check the input/output TS files:"); - needToUpdateTsFiles = checkIfOutputFilesAreOutdated(inputAllFiles, cachePath, cacheZipFile); + needToUpdateTsFiles = CachingStrategy.checkIfOutputFilesAreOutdated(inputAllFiles, cachePath, cacheZipFile); } return needToUpdateTsFiles; } @@ -512,72 +509,6 @@ public class ConfigDefinition { } } - private static boolean checkIfOutputFilesAreOutdated(List inputFileNames, String cachePath, String cacheZipFile) { - if (cachePath == null) - return true; - SystemOut.println("Check the input/output other files:"); - // find if any input file was changed from the cached version - for (String inputFileName : inputFileNames) { - File inputFile = new File(inputFileName); - try { - byte[] inputFileContent = Files.readAllBytes(inputFile.toPath()); - byte[] f2; - if (cacheZipFile != null) { - f2 = ZipUtil.unzipFileContents(cacheZipFile, cachePath + File.separator + inputFileName); - } else { - String cachedFileName = getCachedInputFileName(cachePath, inputFile.getName()); - SystemOut.println("* cache ZIP file not specified, reading " + cachedFileName + " vs " + inputFileName); - /** - * todo: do we have a bug in this branch? how often do we simply read same 'inputFile'? - */ - File cachedFile = new File(cachedFileName); - f2 = Files.readAllBytes(cachedFile.toPath()); - } - boolean isEqual = Arrays.equals(inputFileContent, f2); - if (!isEqual) { - SystemOut.println("* the file " + inputFileName + " is changed!"); - return true; - } else { - SystemOut.println("* the file " + inputFileName + " is NOT changed!"); - } - } catch (IOException e) { - SystemOut.println("* cannot validate the file " + inputFileName + ", so assuming it's changed."); - return true; - } - } - SystemOut.println("* all the files are up-to-date!"); - return false; - } - - private static boolean saveCachedInputFiles(List inputFiles, String cachePath, String cacheZipFile) throws IOException { - if (cachePath == null) { - SystemOut.println("* cache storage is disabled."); - return false; - } - // copy all input files to the cache - if (cacheZipFile != null) { - ZipUtil.zipAddFiles(cacheZipFile, inputFiles, cachePath); - } else { - for (String iFile : inputFiles) { - File newFile = new File(iFile); - File cachedFile = new File(getCachedInputFileName(cachePath, newFile.getName())); - cachedFile.mkdirs(); - try { - Files.copy(newFile.toPath(), cachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - SystemOut.println("* cannot store the cached file for " + iFile); - throw e; - } - } - } - SystemOut.println("* input files copied to the cached folder"); - return true; - } - - private static String getCachedInputFileName(String cachePath, String inputFile) { - return cachePath + File.separator + inputFile; - } - private static long getCrc32(String fileName) throws IOException { File file = new File(fileName); byte[] fileContent = Files.readAllBytes(file.toPath()); diff --git a/java_tools/configuration_definition/src/com/rusefi/util/CachingStrategy.java b/java_tools/configuration_definition/src/com/rusefi/util/CachingStrategy.java new file mode 100644 index 0000000000..70b4e0c06f --- /dev/null +++ b/java_tools/configuration_definition/src/com/rusefi/util/CachingStrategy.java @@ -0,0 +1,76 @@ +package com.rusefi.util; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.Arrays; +import java.util.List; + +public class CachingStrategy { + public static boolean checkIfOutputFilesAreOutdated(List inputFileNames, String cachePath, String cacheZipFile) { + if (cachePath == null) + return true; + SystemOut.println("Check the input/output other files:"); + // find if any input file was changed from the cached version + for (String inputFileName : inputFileNames) { + File inputFile = new File(inputFileName); + try { + byte[] inputFileContent = Files.readAllBytes(inputFile.toPath()); + byte[] f2; + if (cacheZipFile != null) { + f2 = ZipUtil.unzipFileContents(cacheZipFile, cachePath + File.separator + inputFileName); + } else { + String cachedFileName = getCachedInputFileName(cachePath, inputFile.getName()); + SystemOut.println("* cache ZIP file not specified, reading " + cachedFileName + " vs " + inputFileName); + /** + * todo: do we have a bug in this branch? how often do we simply read same 'inputFile'? + */ + File cachedFile = new File(cachedFileName); + f2 = Files.readAllBytes(cachedFile.toPath()); + } + boolean isEqual = Arrays.equals(inputFileContent, f2); + if (!isEqual) { + SystemOut.println("* the file " + inputFileName + " is changed!"); + return true; + } else { + SystemOut.println("* the file " + inputFileName + " is NOT changed!"); + } + } catch (IOException e) { + SystemOut.println("* cannot validate the file " + inputFileName + ", so assuming it's changed."); + return true; + } + } + SystemOut.println("* all the files are up-to-date!"); + return false; + } + + public static boolean saveCachedInputFiles(List inputFiles, String cachePath, String cacheZipFile) throws IOException { + if (cachePath == null) { + SystemOut.println("* cache storage is disabled."); + return false; + } + // copy all input files to the cache + if (cacheZipFile != null) { + ZipUtil.zipAddFiles(cacheZipFile, inputFiles, cachePath); + } else { + for (String iFile : inputFiles) { + File newFile = new File(iFile); + File cachedFile = new File(getCachedInputFileName(cachePath, newFile.getName())); + cachedFile.mkdirs(); + try { + Files.copy(newFile.toPath(), cachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + SystemOut.println("* cannot store the cached file for " + iFile); + throw e; + } + } + } + SystemOut.println("* input files copied to the cached folder"); + return true; + } + + private static String getCachedInputFileName(String cachePath, String inputFile) { + return cachePath + File.separator + inputFile; + } +}