Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 3563b24

Browse files
authored
Merge pull request #79 from mathworks/2.0.0-Qualification
2.0.0 qualification changes merged to master
2 parents be8286e + 02d33cf commit 3563b24

File tree

9 files changed

+318
-149
lines changed

9 files changed

+318
-149
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
<tag>HEAD</tag>
5454
</scm>
5555

56+
<dependencies>
57+
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
58+
<dependency>
59+
<groupId>org.jenkins-ci.plugins</groupId>
60+
<artifactId>matrix-project</artifactId>
61+
<version>1.14</version>
62+
</dependency>
63+
</dependencies>
64+
5665
<build>
5766
<plugins>
5867
<!-- Plugin to download the matlab run scripts and keep it under class

src/main/java/com/mathworks/ci/MatlabBuild.java

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
/**
33
* Copyright 2019-2020 The MathWorks, Inc.
44
*
5-
* Build Interface has two default methods. MATLAB builders can override the
6-
* default behavior.
5+
* Build Interface has two default methods. MATLAB builders can override the default behavior.
76
*
87
*/
98

109
import java.io.IOException;
1110
import java.io.InputStream;
11+
import java.util.UUID;
1212
import hudson.EnvVars;
1313
import hudson.FilePath;
1414
import hudson.Launcher;
@@ -17,65 +17,78 @@
1717
import hudson.model.TaskListener;
1818

1919
public interface MatlabBuild {
20-
20+
2121
/**
22-
* This Method decorates the launcher with MATLAB command provided and returns the Process
22+
* This Method decorates the launcher with MATLAB command provided and returns the Process
2323
* object to launch MATLAB with appropriate startup options like -r or -batch
24-
* @param workspace Current build workspace
24+
*
25+
* @param workspace Current build workspace
2526
* @param launcher Current build launcher
26-
* @param listener Current build listener
27+
* @param listener Current build listener
2728
* @param envVars Environment variables of the current build
28-
* @param matlabCommand MATLAB command to execute on shell
29+
* @param matlabCommand MATLAB command to execute on shell
2930
* @return matlabLauncher returns the process launcher to run MATLAB commands
3031
*/
31-
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException {
32-
//Get node specific tmp directory to copy matlab runner script
33-
String tmpDir = getNodeSpecificTmpFolderPath();
32+
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,
33+
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
34+
throws IOException, InterruptedException {
35+
// Get node specific tmp directory to copy matlab runner script
36+
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
3437
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
3538
ProcStarter matlabLauncher;
36-
if(launcher.isUnix()) {
37-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/run_matlab_command.sh",matlabCommand).stdout(listener);
38-
39-
//Copy runner .sh for linux platform in workspace.
40-
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
41-
}else {
42-
launcher = launcher.decorateByPrefix("cmd.exe","/C");
43-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+"run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener);
44-
//Copy runner.bat for Windows platform in workspace.
45-
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
39+
if (launcher.isUnix()) {
40+
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
41+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
42+
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
43+
44+
// Copy runner .sh for linux platform in workspace.
45+
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
46+
targetWorkspace);
47+
} else {
48+
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
49+
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
50+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
51+
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
52+
.stdout(listener);
53+
// Copy runner.bat for Windows platform in workspace.
54+
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,
55+
targetWorkspace);
4656
}
4757
return matlabLauncher;
4858
}
4959

50-
/**
60+
/*
5161
* Method to copy given file from source to target node specific workspace.
5262
*/
53-
default void copyFileInWorkspace(String sourceFile, String targetFile,
54-
FilePath targetWorkspace) throws IOException, InterruptedException {
63+
default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace)
64+
throws IOException, InterruptedException {
5565
final ClassLoader classLoader = getClass().getClassLoader();
56-
FilePath targetFilePath = new FilePath(targetWorkspace, Message.getValue(targetFile));
66+
FilePath targetFilePath = new FilePath(targetWorkspace, targetFile);
5767
InputStream in = classLoader.getResourceAsStream(sourceFile);
5868
targetFilePath.copyFrom(in);
5969
// set executable permission
6070
targetFilePath.chmod(0755);
71+
}
72+
73+
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName, FilePath workspace)
74+
throws IOException, InterruptedException {
75+
/*Use of Computer is not recommended as jenkins hygeine for pipeline support
76+
* https://javadoc.jenkins-ci.org/jenkins/tasks/SimpleBuildStep.html */
6177

78+
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
79+
return new FilePath(launcher.getChannel(), tmpDir+"/"+uniqueName);
6280
}
63-
64-
default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher) throws IOException, InterruptedException {
65-
Computer cmp = Computer.currentComputer();
66-
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
67-
if(launcher.isUnix()) {
68-
tmpDir = tmpDir+"/run_matlab_command.sh";
69-
}else {
70-
tmpDir = tmpDir+"\\"+"run_matlab_command.bat";
81+
82+
default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOException, InterruptedException {
83+
Computer cmp = workspace.toComputer();
84+
if (cmp == null) {
85+
throw new IOException(Message.getValue("build.workspace.computer.not.found"));
7186
}
72-
return new FilePath(launcher.getChannel(), tmpDir);
73-
}
74-
75-
default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException {
76-
Computer cmp = Computer.currentComputer();
7787
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
7888
return tmpDir;
7989
}
8090

91+
default String getUniqueNameForRunnerFile() {
92+
return UUID.randomUUID().toString();
93+
}
8194
}

src/main/java/com/mathworks/ci/MatlabBuilderConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class MatlabBuilderConstants {
1212
static final double BASE_MATLAB_VERSION_EXPORTSTMRESULTS_SUPPORT = 9.6;
1313

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

src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,39 @@ public boolean isApplicable(
9595
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
9696
@Nonnull Launcher launcher, @Nonnull TaskListener listener)
9797
throws InterruptedException, IOException {
98-
99-
try {
100-
// Set the environment variable specific to the this build
101-
setEnv(build.getEnvironment(listener));
10298

103-
// Invoke MATLAB command and transfer output to standard
104-
// Output Console
99+
// Set the environment variable specific to the this build
100+
setEnv(build.getEnvironment(listener));
105101

106-
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
102+
// Invoke MATLAB command and transfer output to standard
103+
// Output Console
107104

108-
if (buildResult != 0) {
109-
build.setResult(Result.FAILURE);
110-
}
111-
} finally {
112-
// Cleanup the runner File from tmp directory
113-
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher);
114-
if(matlabRunnerScript.exists()) {
115-
matlabRunnerScript.delete();
116-
}
105+
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
106+
107+
if (buildResult != 0) {
108+
build.setResult(Result.FAILURE);
117109
}
118110
}
119111

120112
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
121113
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
114+
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
122115
ProcStarter matlabLauncher;
123116
try {
124-
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand());
117+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
118+
getCommand(), uniqueTmpFldrName);
119+
return matlabLauncher.join();
125120
} catch (Exception e) {
126121
listener.getLogger().println(e.getMessage());
127122
return 1;
123+
} finally {
124+
// Cleanup the runner File from tmp directory
125+
FilePath matlabRunnerScript =
126+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
127+
if (matlabRunnerScript.exists()) {
128+
matlabRunnerScript.deleteRecursive();
129+
}
128130
}
129-
return matlabLauncher.join();
130-
}
131+
132+
}
131133
}

0 commit comments

Comments
 (0)