Skip to content

Commit

Permalink
Infer OTP_RELEASE & ERLANG_SDK_HOME if no environment variable is set (
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuataylor authored Jul 14, 2024
1 parent 73ad91b commit 9875d18
Showing 1 changed file with 80 additions and 3 deletions.
83 changes: 80 additions & 3 deletions jps-builder/tests/org/elixir_lang/jps/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,62 @@

import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;


/**
* Created by zyuyou on 15/7/17.
*/
public class BuilderTest extends JpsBuildTestCase {
private static final String TEST_MODULE_NAME = "m";
private JpsSdk<SdkProperties> elixirSdk;

private static String otpReleaseVersion = null;

/**
* Retrieves the OTP Release version, eg 24.3.4.6
* This method first checks for an environment variable OTP_RELEASE (used in CI).
* If not set, it attempts to infer the version path from the erl command.
*
* @return A String representing the OTP Release version.
* @throws AssertionError if ERLANG_SDK_HOME is not set and cannot be inferred from PATH.
*/
private static String otpRelease() {
if (otpReleaseVersion != null) {
return otpReleaseVersion;
}

String otpRelease = System.getenv("OTP_RELEASE");
if (otpRelease != null && !otpRelease.isEmpty()) {
otpReleaseVersion = otpRelease;
return otpRelease;
}

assertNotNull("OTP_RELEASE is not set", otpRelease);
try {
Process process = Runtime.getRuntime().exec(new String[]{
"erl",
"-eval",
"{ok, Version} = file:read_file(filename:join([code:root_dir(), \"releases\", erlang:system_info(otp_release), \"OTP_VERSION\"])), io:fwrite(Version), halt().",
"-noshell"
});

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
otpRelease = reader.readLine();
}

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Failed to get OTP release. Exit code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed to get OTP release", e);
}

if (otpRelease == null || otpRelease.isEmpty()) {
throw new RuntimeException("Failed to get OTP release");
}

otpReleaseVersion = otpRelease;
return otpRelease;
}

Expand All @@ -42,11 +85,45 @@ private static String elixirSdkHome() {
return sdkHomeFromEbinDirectory(ebinDirectory());
}

/**
* Retrieves the Erlang SDK home directory.
* This method first checks for an environment variable ERLANG_SDK_HOME.
* If not set, it attempts to infer the SDK home from the erl command.
*
* @return A String representing the path to the Erlang SDK home directory.
* @throws AssertionError if ERLANG_SDK_HOME is not set and cannot be retrieved from the erl command.
*/
@NotNull
private static String erlangSdkHome() {
String erlangSdkHome = System.getenv("ERLANG_SDK_HOME");

assertNotNull("ERLANG_SDK_HOME is not set", erlangSdkHome);
if (erlangSdkHome != null && !erlangSdkHome.isEmpty()) {
return erlangSdkHome;
}

try {
Process process = Runtime.getRuntime().exec(new String[]{
"erl",
"-eval",
"io:format(\"~s~n\", [code:root_dir()]), halt().",
"-noshell"
});

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
erlangSdkHome = reader.readLine();
}

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Failed to get Erlang SDK home. Exit code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed to get Erlang SDK home", e);
}

if (erlangSdkHome == null || erlangSdkHome.isEmpty()) {
throw new RuntimeException("Failed to get Erlang SDK home");
}

return erlangSdkHome;
}
Expand Down

0 comments on commit 9875d18

Please sign in to comment.