Skip to content

Commit

Permalink
TS-40724 Implemented fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Klein committed Nov 11, 2024
1 parent ac38a63 commit 8c8b7fa
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
20 changes: 16 additions & 4 deletions src/main/java/com/teamscale/upload/utils/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static void enableStackTracePrintingForKnownErrors() {
*/
public static void fail(String message, SafeResponse response) {
String url = response.unsafeResponse.request().url().toString();
fail("Upload to Teamscale failed:\n\n" + message + "\n\nTeamscale's response:\n" + url + "\n"
+ response.body);
fail("Upload to Teamscale failed:\n\n" + message + "\n\nTeamscale's response:\n" + url + "\n" + response.body);
}

/**
Expand Down Expand Up @@ -51,8 +50,7 @@ public static void failWithoutStackTrace(String message, Throwable throwable) {
throwable.printStackTrace();
} else {
System.err.println("ERROR: " + throwable.getClass().getSimpleName() + ": " + throwable.getMessage());
System.err.println(
"Stack trace suppressed. Rerun this command with --stacktrace to see the stack trace.");
System.err.println("Stack trace suppressed. Rerun this command with --stacktrace to see the stack trace.");
}
fail(message);
}
Expand Down Expand Up @@ -108,6 +106,20 @@ public static void debug(String message) {
}
}

/**
* Print a debug message to stdout and log the given throwable.
* <p>
* Use to log information that is not useful during normal operations but
* helpful when something goes wrong.
*/
public static void debug(String message, Throwable throwable) {
if (!debugLogEnabled) {
return;
}
debug(message);
throwable.printStackTrace();
}

/**
* Enables debug logging and all stack traces.
*/
Expand Down
35 changes: 29 additions & 6 deletions src/main/java/com/teamscale/upload/xcode/XCResultConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
Expand Down Expand Up @@ -109,9 +110,9 @@ private static List<String> getSourceFiles(File reportDirectory) throws Conversi
ProcessResult result = ProcessUtils.run("xcrun", "xccov", "view", "--archive", "--file-list",
reportDirectory.getAbsolutePath());
if (!result.wasSuccessful()) {
throw new ConversionException(
throw ConversionException.withProcessResult(
"Error while obtaining file list from XCResult archive " + reportDirectory.getAbsolutePath(),
result.exception);
result);
}
return result.output.lines().sorted().collect(toList());
}
Expand Down Expand Up @@ -209,11 +210,21 @@ private File getReportDirectory(File report) throws IOException, ConversionExcep

private ActionsInvocationRecord getActionsInvocationRecord(File reportDirectory)
throws InterruptedException, ConversionException {
ProcessResult result = ProcessUtils.run("xcrun", "xcresulttool", "get", "--path",
reportDirectory.getAbsolutePath(), "--format", "json");
List<String> command = new ArrayList<>();
Collections.addAll(command, "xcrun", "xcresulttool", "get", "--path", reportDirectory.getAbsolutePath(),
"--format", "json");
if (XcodeVersion.determine().major() >= 16) {
// Starting with Xcode 16 this command is marked as deprecated and will fail if
// ran without the legacy flag
// see TS-40724 for more information
command.add("--legacy");
}

ProcessResult result = ProcessUtils.run(command.toArray(new String[0]));
if (!result.wasSuccessful()) {
throw new ConversionException("Error while obtaining ActionInvocationsRecord from XCResult archive "
+ reportDirectory.getAbsolutePath(), result.exception);
throw ConversionException
.withProcessResult("Error while obtaining ActionInvocationsRecord from XCResult archive "
+ reportDirectory.getAbsolutePath(), result);
}
String actionsInvocationRecordJson = result.output;
return new Gson().fromJson(actionsInvocationRecordJson, ActionsInvocationRecord.class);
Expand Down Expand Up @@ -305,5 +316,17 @@ public ConversionException(String message) {
public ConversionException(String message, Exception e) {
super(message, e);
}

/**
* Creates a {@link ConversionException} with the given message and the
* {@linkplain ProcessResult#errorOutput error output of the command}.
*/
public static ConversionException withProcessResult(String message, ProcessResult processResult) {
String messageIncludingErrorOutput = message;
if (processResult.errorOutput != null) {
messageIncludingErrorOutput += " (command output: " + processResult.errorOutput + ")";
}
return new ConversionException(messageIncludingErrorOutput, processResult.exception);
}
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/teamscale/upload/xcode/XcodeVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.teamscale.upload.xcode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.teamscale.upload.autodetect_revision.ProcessUtils;
import com.teamscale.upload.utils.LogUtils;

/**
* Represents an Xcode version.
*
* @param major
* The minor version number (e.g., {@code 16} for version
* {@code 16.1} or {@link Integer#MAX_VALUE} for latest version)
* @param minor
* The minor version number (e.g., {@code 1} for version {@code 16.1}
* or {@link Integer#MAX_VALUE} for latest version)
*/
public record XcodeVersion(int major, int minor) {

/**
* Pattern that matches the output of an {@code xcodebuild -version} command to
* determine the installed version.
*/
private static final Pattern XCODE_BUILD_VERSION_PATTERN = Pattern
.compile("^Xcode (?<major>\\d+)\\.(?<minor>\\d+)");

/** Determines the version installed on this machine. */
public static XcodeVersion determine() {
ProcessUtils.ProcessResult result = ProcessUtils.run("xcodebuild", "-version");
if (!result.wasSuccessful()) {
LogUtils.warn("Could not determine installed Xcode version. Assuming latest Xcode version is installed.");
LogUtils.debug("Error whilst running 'xcodebuild -version' command: ", result.exception);
return latestVersion();
}

String xcodeBuildVersionCommandOutput = result.output;
Matcher m = XCODE_BUILD_VERSION_PATTERN.matcher(xcodeBuildVersionCommandOutput);
if (!m.find()) {
LogUtils.warn("Could not determine installed Xcode version. Assuming latest Xcode version is installed.");
LogUtils.debug("Output of 'xcodebuild -version' command:\n" + xcodeBuildVersionCommandOutput);
return latestVersion();
}

int major = Integer.parseInt(m.group("major"));
int minor = Integer.parseInt(m.group("minor"));
return new XcodeVersion(major, minor);
}

/**
* Returns a {@link XcodeVersion} that represents the latest version.
* <p>
* Instead of determining the version via the web, {@link #major} and
* {@link #minor} will be simply set to {@link Integer#MAX_VALUE}.
*/
private static XcodeVersion latestVersion() {
return new XcodeVersion(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
}

0 comments on commit 8c8b7fa

Please sign in to comment.