Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
<tag>HEAD</tag>
</scm>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.14</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Plugin to download the matlab run scripts and keep it under class
Expand Down
87 changes: 50 additions & 37 deletions src/main/java/com/mathworks/ci/MatlabBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
/**
* Copyright 2019-2020 The MathWorks, Inc.
*
* Build Interface has two default methods. MATLAB builders can override the
* default behavior.
* Build Interface has two default methods. MATLAB builders can override the default behavior.
*
*/

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -17,65 +17,78 @@
import hudson.model.TaskListener;

public interface MatlabBuild {

/**
* This Method decorates the launcher with MATLAB command provided and returns the Process
* This Method decorates the launcher with MATLAB command provided and returns the Process
* object to launch MATLAB with appropriate startup options like -r or -batch
* @param workspace Current build workspace
*
* @param workspace Current build workspace
* @param launcher Current build launcher
* @param listener Current build listener
* @param listener Current build listener
* @param envVars Environment variables of the current build
* @param matlabCommand MATLAB command to execute on shell
* @param matlabCommand MATLAB command to execute on shell
* @return matlabLauncher returns the process launcher to run MATLAB commands
*/
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException {
//Get node specific tmp directory to copy matlab runner script
String tmpDir = getNodeSpecificTmpFolderPath();
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
throws IOException, InterruptedException {
// Get node specific tmp directory to copy matlab runner script
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
ProcStarter matlabLauncher;
if(launcher.isUnix()) {
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/run_matlab_command.sh",matlabCommand).stdout(listener);

//Copy runner .sh for linux platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
}else {
launcher = launcher.decorateByPrefix("cmd.exe","/C");
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+"run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener);
//Copy runner.bat for Windows platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
if (launcher.isUnix()) {
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);

// Copy runner .sh for linux platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
targetWorkspace);
} else {
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
.stdout(listener);
// Copy runner.bat for Windows platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,
targetWorkspace);
}
return matlabLauncher;
}

/**
/*
* Method to copy given file from source to target node specific workspace.
*/
default void copyFileInWorkspace(String sourceFile, String targetFile,
FilePath targetWorkspace) throws IOException, InterruptedException {
default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace)
throws IOException, InterruptedException {
final ClassLoader classLoader = getClass().getClassLoader();
FilePath targetFilePath = new FilePath(targetWorkspace, Message.getValue(targetFile));
FilePath targetFilePath = new FilePath(targetWorkspace, targetFile);
InputStream in = classLoader.getResourceAsStream(sourceFile);
targetFilePath.copyFrom(in);
// set executable permission
targetFilePath.chmod(0755);
}

default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName, FilePath workspace)
throws IOException, InterruptedException {
/*Use of Computer is not recommended as jenkins hygeine for pipeline support
* https://javadoc.jenkins-ci.org/jenkins/tasks/SimpleBuildStep.html */

String tmpDir = getNodeSpecificTmpFolderPath(workspace);
return new FilePath(launcher.getChannel(), tmpDir+"/"+uniqueName);
}

default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher) throws IOException, InterruptedException {
Computer cmp = Computer.currentComputer();
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
if(launcher.isUnix()) {
tmpDir = tmpDir+"/run_matlab_command.sh";
}else {
tmpDir = tmpDir+"\\"+"run_matlab_command.bat";

default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOException, InterruptedException {
Computer cmp = workspace.toComputer();
if (cmp == null) {
throw new IOException(Message.getValue("build.workspace.computer.not.found"));
}
return new FilePath(launcher.getChannel(), tmpDir);
}

default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException {
Computer cmp = Computer.currentComputer();
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
return tmpDir;
}

default String getUniqueNameForRunnerFile() {
return UUID.randomUUID().toString();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/mathworks/ci/MatlabBuilderConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MatlabBuilderConstants {
static final double BASE_MATLAB_VERSION_EXPORTSTMRESULTS_SUPPORT = 9.6;

static final String MATLAB_RUNNER_TARGET_FILE = "Builder.matlab.runner.target.file.name";
static final String MATLAB_TESTS_RUNNER_TARGET_FILE = "runMatlabTests.m";
static final String MATLAB_RUNNER_RESOURCE = "com/mathworks/ci/MatlabBuilder/runMatlabTests.m";
static final String AUTOMATIC_OPTION = "RunTestsAutomaticallyOption";

Expand Down
40 changes: 21 additions & 19 deletions src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,39 @@ public boolean isApplicable(
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
@Nonnull Launcher launcher, @Nonnull TaskListener listener)
throws InterruptedException, IOException {

try {
// Set the environment variable specific to the this build
setEnv(build.getEnvironment(listener));

// Invoke MATLAB command and transfer output to standard
// Output Console
// Set the environment variable specific to the this build
setEnv(build.getEnvironment(listener));

buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
// Invoke MATLAB command and transfer output to standard
// Output Console

if (buildResult != 0) {
build.setResult(Result.FAILURE);
}
} finally {
// Cleanup the runner File from tmp directory
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher);
if(matlabRunnerScript.exists()) {
matlabRunnerScript.delete();
}
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());

if (buildResult != 0) {
build.setResult(Result.FAILURE);
}
}

private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
ProcStarter matlabLauncher;
try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand());
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
getCommand(), uniqueTmpFldrName);
return matlabLauncher.join();
} catch (Exception e) {
listener.getLogger().println(e.getMessage());
return 1;
} finally {
// Cleanup the runner File from tmp directory
FilePath matlabRunnerScript =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
if (matlabRunnerScript.exists()) {
matlabRunnerScript.deleteRecursive();
}
}
return matlabLauncher.join();
}

}
}
Loading