From a5a44d47e8e2c0165b889336997547757db59cce Mon Sep 17 00:00:00 2001 From: Artem Eroshenko Date: Mon, 21 Oct 2024 11:52:44 +0300 Subject: [PATCH] cleanup code --- .gitignore | 2 + .../xcresults/export/ExportProcessor.java | 113 ++++-------------- .../xcresults/util/ProcessUtil.java | 47 ++++++++ 3 files changed, 74 insertions(+), 88 deletions(-) create mode 100644 src/main/java/io/eroshenkoam/xcresults/util/ProcessUtil.java diff --git a/.gitignore b/.gitignore index 6c1db33..0dde9d1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ .gradle build +# Project files +CommandTest.java diff --git a/src/main/java/io/eroshenkoam/xcresults/export/ExportProcessor.java b/src/main/java/io/eroshenkoam/xcresults/export/ExportProcessor.java index 98fd842..3ec2f9e 100644 --- a/src/main/java/io/eroshenkoam/xcresults/export/ExportProcessor.java +++ b/src/main/java/io/eroshenkoam/xcresults/export/ExportProcessor.java @@ -3,16 +3,14 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import freemarker.template.Version; import io.eroshenkoam.xcresults.carousel.CarouselPostProcessor; import io.qameta.allure.model.ExecutableItem; import io.qameta.allure.model.TestResult; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -22,6 +20,7 @@ import static io.eroshenkoam.xcresults.util.FormatUtil.getResultFilePath; import static io.eroshenkoam.xcresults.util.FormatUtil.parseDate; +import static io.eroshenkoam.xcresults.util.ProcessUtil.*; public class ExportProcessor { @@ -61,7 +60,6 @@ public class ExportProcessor { private static final String TARGET_NAME = "targetName"; private static final String TEST_REF = "testsRef"; - private static String XCODE_VERSION = null; private final ObjectMapper mapper = new ObjectMapper() .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); @@ -225,54 +223,16 @@ private List getAttributeValues(final JsonNode node, final String attr } return result; } - - private static String getXcodeVersion() { - if (XCODE_VERSION == null) { - try { - ProcessBuilder processBuilder = new ProcessBuilder("xcodebuild", "-version"); - Process process = processBuilder.start(); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - if (line.startsWith("Xcode")) { - // Extract the version number from the line - XCODE_VERSION = line.split(" ")[1]; - break; - } - } - reader.close(); - process.waitFor(); - } catch (IOException | InterruptedException e) { - throw new RuntimeException(e); - } - } - return XCODE_VERSION; - } - - private static int compareVersions(String version1, String version2) { - String[] parts1 = version1.split("\\."); - String[] parts2 = version2.split("\\."); - - int major1 = Integer.parseInt(parts1[0]); - int minor1 = Integer.parseInt(parts1.length > 1 ? parts1[1] : "0"); - int patch1 = Integer.parseInt(parts1.length > 2 ? parts1[2] : "0"); - - int major2 = Integer.parseInt(parts2[0]); - int minor2 = Integer.parseInt(parts2.length > 1 ? parts2[1] : "0"); - int patch2 = Integer.parseInt(parts2.length > 2 ? parts2[2] : "0"); - - if (major1 != major2) { - return Integer.compare(major1, major2); - } - if (minor1 != minor2) { - return Integer.compare(minor1, minor2); - } - return Integer.compare(patch1, patch2); - } private static boolean isLegacyMode() { - String version = getXcodeVersion(); - return compareVersions(version, "16.0") >= 0; + try { + final String output = readProcessOutputAsString(new ProcessBuilder("xcodebuild", "-version")); + final String versionLine = output.split("\n")[0]; + final Version version = new Version(versionLine.replaceFirst("Xcode ", "").trim()); + return version.getMajor() >= 16; + } catch (final Exception e) { + return false; + } } private ProcessBuilder processBuilderForXCResultToolCommand(String... command) { @@ -288,41 +248,33 @@ private ProcessBuilder processBuilderForXCResultToolCommand(String... command) { private JsonNode readSummary() { final ProcessBuilder builder = processBuilderForXCResultToolCommand( - "get", - "--format", "json", - "--path", inputPath.toAbsolutePath().toString() + "get", + "--format", "json", + "--path", inputPath.toAbsolutePath().toString() ); - return readProcessOutput(builder); + return readProcessOutputAsJson(builder, mapper); } private JsonNode getReference(final String id) { final ProcessBuilder builder = processBuilderForXCResultToolCommand( - "get", - "--format", "json", - "--path", inputPath.toAbsolutePath().toString(), - "--id", id + "get", + "--format", "json", + "--path", inputPath.toAbsolutePath().toString(), + "--id", id ); - return readProcessOutput(builder); + return readProcessOutputAsJson(builder, mapper); } private void exportReference(final String id, final Path output) { final ProcessBuilder exportBuilder = processBuilderForXCResultToolCommand( - "export", - "--type", "file", - "--path", inputPath.toAbsolutePath().toString(), - "--id", id, - "--output-path", output.toAbsolutePath().toString() + "export", + "--type", "file", + "--path", inputPath.toAbsolutePath().toString(), + "--id", id, + "--output-path", output.toAbsolutePath().toString() ); - if (isLegacyMode()) { - try { - exportBuilder.start(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } else { - readProcessOutput(exportBuilder); - } + readProcessOutput(exportBuilder, (i) -> null); if (FILE_EXTENSION_HEIC.equals(FilenameUtils.getExtension(output.toString()))) { convertHeicToJpeg(output); @@ -349,19 +301,4 @@ private void convertHeicToJpeg(Path heicPath) { } } - private JsonNode readProcessOutput(final ProcessBuilder builder) { - try { - final Process process = builder.start(); - try (InputStream input = process.getInputStream()) { - if (Objects.nonNull(input)) { - return mapper.readTree(input); - } else { - return null; - } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } diff --git a/src/main/java/io/eroshenkoam/xcresults/util/ProcessUtil.java b/src/main/java/io/eroshenkoam/xcresults/util/ProcessUtil.java new file mode 100644 index 0000000..fe461bd --- /dev/null +++ b/src/main/java/io/eroshenkoam/xcresults/util/ProcessUtil.java @@ -0,0 +1,47 @@ +package io.eroshenkoam.xcresults.util; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +public final class ProcessUtil { + + private ProcessUtil() { + } + + public static String readProcessOutputAsString(final ProcessBuilder builder) { + return readProcessOutput(builder, input -> IOUtils.toString(input, StandardCharsets.UTF_8)); + } + + public static JsonNode readProcessOutputAsJson(final ProcessBuilder builder, + final ObjectMapper mapper) { + return readProcessOutput(builder, mapper::readTree); + } + + public static T readProcessOutput(final ProcessBuilder builder, + final ThrowableFunction reader) { + try { + final Process process = builder.start(); + try (InputStream input = process.getInputStream()) { + if (Objects.nonNull(input)) { + return reader.apply(input); + } else { + return null; + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @FunctionalInterface + public interface ThrowableFunction { + R apply(T t) throws IOException; + } + +}