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

Commit ff3f51e

Browse files
committed
Adopted run_matlab_runner script file to construct matlab command.
1 parent 95f7742 commit ff3f51e

File tree

7 files changed

+128
-117
lines changed

7 files changed

+128
-117
lines changed

pom.xml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,31 @@
5252
<url>http://github.com/jenkinsci/matlab-plugin</url>
5353
<tag>HEAD</tag>
5454
</scm>
55-
55+
5656
<build>
57+
<plugins>
58+
<!-- Plugin to download the matlab run scripts and keep it under class
59+
path -->
60+
<plugin>
61+
<groupId>com.googlecode.maven-download-plugin</groupId>
62+
<artifactId>download-maven-plugin</artifactId>
63+
<version>1.5.0</version>
64+
<executions>
65+
<execution>
66+
<id>get-matlab-runner-scripts</id>
67+
<phase>validate</phase>
68+
<goals>
69+
<goal>wget</goal>
70+
</goals>
71+
<configuration>
72+
<url>https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v0/run-matlab-command.zip</url>
73+
<unpack>true</unpack>
74+
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
75+
</configuration>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
</plugins>
5780
<pluginManagement>
5881
<plugins>
5982
<!--This plugin's configuration is used to store Eclipse m2e settings
@@ -92,6 +115,33 @@
92115
<artifactId>maven-site-plugin</artifactId>
93116
<version>3.7.1</version>
94117
</plugin>
118+
<!-- Delete the runner files during clean operation -->
119+
<plugin>
120+
<artifactId>maven-clean-plugin</artifactId>
121+
<version>3.0.0</version>
122+
<executions>
123+
<execution>
124+
<id>default-clean</id>
125+
<phase>clean</phase>
126+
<goals>
127+
<goal>clean</goal>
128+
</goals>
129+
<configuration>
130+
<filesets>
131+
<fileset>
132+
<directory>${basedir}/src/main/resources</directory>
133+
<includes>
134+
<include>**/*.bat</include>
135+
<include>**/*.sh</include>
136+
<include>**/*.txt</include>
137+
</includes>
138+
<followSymlinks>false</followSymlinks>
139+
</fileset>
140+
</filesets>
141+
</configuration>
142+
</execution>
143+
</executions>
144+
</plugin>
95145
</plugins>
96146
</pluginManagement>
97147
</build>

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

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
* builders. Author : Nikhil Bhoski email : [email protected] Date : 11/02/2020
77
*/
88

9-
import java.util.ArrayList;
10-
import java.util.Arrays;
11-
import java.util.Collections;
12-
import java.util.List;
9+
import java.io.IOException;
10+
import java.io.InputStream;
1311
import org.apache.commons.io.FilenameUtils;
14-
import org.apache.commons.lang.ArrayUtils;
1512
import hudson.FilePath;
1613
import hudson.Launcher;
1714

@@ -31,7 +28,7 @@ public void setLauncher(Launcher launcher) {
3128
public String getMatlabRoot() {
3229
return matlabRoot;
3330
}
34-
31+
3532

3633
/*
3734
* Constructor to accepts the current launcher instance of the build with two parameters
@@ -43,90 +40,28 @@ public CommandConstructUtil(Launcher launcher, String matlabRoot) {
4340
this.launcher = launcher;
4441
this.matlabRoot = matlabRoot;
4542
}
43+
44+
/*
45+
* New set of methods for new changes with run matlab command script
46+
*/
4647

47-
public List<String> constructBatchCommandForTestRun(String inputArguments) {
48-
final String runCommand;
48+
public String constructCommandForTest(String inputArguments) {
49+
String runCommand;
4950
String matlabFunctionName = FilenameUtils.removeExtension(
5051
Message.getValue(MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE));
5152
runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
52-
return getPlatformSpecificBatchCommand(runCommand);
53-
}
54-
55-
public List<String> constructBatchCommandForScriptRun(String customCommand) {
56-
return getPlatformSpecificBatchCommand(customCommand);
57-
}
58-
59-
private List<String> getPlatformSpecificBatchCommand(String command) {
60-
final List<String> matlabDefaultArgs;
61-
final String nodeSpecificFileSep = getNodeSpecificFileSeperator();
62-
matlabDefaultArgs = Arrays.asList(
63-
getMatlabRoot() + nodeSpecificFileSep + "bin" + nodeSpecificFileSep + "matlab", "-batch",
64-
command);
65-
return matlabDefaultArgs;
66-
}
67-
68-
public List<String> constructDefaultCommandForTestRun(String inputArguments)
69-
throws MatlabVersionNotFoundException {
70-
final List<String> matlabDefaultArgs = new ArrayList<String>();
71-
Collections.addAll(matlabDefaultArgs, getPreRunnerSwitches());
72-
Collections.addAll(matlabDefaultArgs, getRunnerSwitch(inputArguments));
73-
Collections.addAll(matlabDefaultArgs, getPostRunnerSwitches());
74-
return matlabDefaultArgs;
75-
}
76-
77-
public List<String> constructDefaultCommandForScriptRun(String command)
78-
throws MatlabVersionNotFoundException {
79-
final List<String> matlabDefaultArgs = new ArrayList<String>();
80-
Collections.addAll(matlabDefaultArgs, getPreRunnerSwitches());
81-
Collections.addAll(matlabDefaultArgs, getRunnerForScriptRun(command));
82-
Collections.addAll(matlabDefaultArgs, getPostRunnerSwitches());
83-
return matlabDefaultArgs;
84-
}
85-
86-
87-
private String[] getPreRunnerSwitches() throws MatlabVersionNotFoundException {
88-
String nodeSpecificFileSep = getNodeSpecificFileSeperator();
89-
FilePath nodeSpecificMatlabRoot = new FilePath(getLauncher().getChannel(), getMatlabRoot());
90-
MatlabReleaseInfo matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot);
91-
String[] preRunnerSwitches =
92-
{getMatlabRoot() + nodeSpecificFileSep + "bin" + nodeSpecificFileSep + "matlab",
93-
"-nosplash", "-nodesktop"};
94-
95-
if (!isUnix()) {
96-
preRunnerSwitches = (String[]) ArrayUtils.add(preRunnerSwitches, "-noDisplayDesktop");
53+
if(isUnix()) {
54+
runCommand = getBashCompatibleCommandString(runCommand);
9755
}
98-
99-
if (!matlabRel
100-
.verLessThan(MatlabBuilderConstants.BASE_MATLAB_VERSION_NO_APP_ICON_SUPPORT)) {
101-
preRunnerSwitches = (String[]) ArrayUtils.add(preRunnerSwitches, "-noAppIcon");
102-
}
103-
return preRunnerSwitches;
56+
return runCommand;
10457
}
105-
106-
private String[] getPostRunnerSwitches() {
107-
String[] postRunnerSwitch = {"-log"};
108-
if (!isUnix()) {
109-
postRunnerSwitch = (String[]) ArrayUtils.add(postRunnerSwitch, "-wait");
58+
59+
public String constructCommandForRunCommand(String command) {
60+
String runCommand = command;
61+
if(isUnix()) {
62+
runCommand = getBashCompatibleCommandString(command);
11063
}
111-
return postRunnerSwitch;
112-
}
113-
114-
private String[] getRunnerSwitch(String inputArguments) throws MatlabVersionNotFoundException {
115-
final String runCommand;
116-
String matlabFunctionName = FilenameUtils.removeExtension(
117-
Message.getValue(MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE));
118-
runCommand = "try,exit(" + matlabFunctionName + "(" + inputArguments
119-
+ ")),catch e,disp(getReport(e,'extended')),exit(1),end";
120-
final String[] runnerSwitch = {"-r", runCommand};
121-
return runnerSwitch;
122-
}
123-
124-
private String[] getRunnerForScriptRun(String command) {
125-
final String runCommand;
126-
runCommand = "try,eval('" + command.replaceAll("'", "''")
127-
+ "'),catch e,disp(getReport(e,'extended')),exit(1),end,exit";
128-
final String[] runnerSwitch = {"-r", runCommand};
129-
return runnerSwitch;
64+
return runCommand;
13065
}
13166

13267
public String getNodeSpecificFileSeperator() {
@@ -140,4 +75,28 @@ public String getNodeSpecificFileSeperator() {
14075
public boolean isUnix() {
14176
return this.launcher.isUnix();
14277
}
78+
79+
/*
80+
* This Method is to escape all open and closing brackets and single quotes
81+
* to make it compatible with /bin/bash -c command on linux
82+
*
83+
*/
84+
private String getBashCompatibleCommandString(String command) {
85+
return command.replaceAll("'","\\\\'").replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)");
86+
}
87+
88+
public void copyMatlabScratchFileInWorkspace(String matlabRunnerResourcePath,
89+
String matlabRunnerTarget, FilePath targetWorkspace)
90+
throws IOException, InterruptedException {
91+
final ClassLoader classLoader = getClass().getClassLoader();
92+
FilePath targetFile =
93+
new FilePath(targetWorkspace, Message.getValue(matlabRunnerTarget));
94+
InputStream in = classLoader.getResourceAsStream(matlabRunnerResourcePath);
95+
targetFile.copyFrom(in);
96+
//set executable permission to the file on Unix.
97+
if(isUnix()) {
98+
targetFile.chmod(0777);
99+
}
100+
101+
}
143102
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public class MatlabBuilderConstants {
2222
static final String STM_RESULTS = "'SimulinkTestResults'";
2323
static final String COBERTURA_CODE_COVERAGE = "'CoberturaCodeCoverage'";
2424
static final String COBERTURA_MODEL_COVERAGE = "'CoberturaModelCoverage'";
25+
26+
// Matlab Runner files
27+
static final String BAT_RUNNER_SCRIPT = "run_matlab_command.bat";
28+
static final String SHELL_RUNNER_SCRIPT = "run_matlab_command.sh";
2529
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,18 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
116116
// Get mMatlabroot set in wrapper.
117117
String matlabRoot = envVars.get("matlabroot");
118118
CommandConstructUtil cmdUtils = new CommandConstructUtil(launcher, matlabRoot);
119-
120-
// Get node specific matlabroot to get matlab version information
121-
FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot);
122-
MatlabReleaseInfo matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot);
123119
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
124-
if (matlabRel.verLessThan(MatlabBuilderConstants.BASE_MATLAB_VERSION_BATCH_SUPPORT)) {
125-
ListenerLogDecorator outStream = new ListenerLogDecorator(listener);
126-
matlabLauncher = matlabLauncher
127-
.cmds(cmdUtils.constructDefaultCommandForScriptRun(getCommand()))
128-
.stderr(outStream);
129-
} else {
130-
matlabLauncher = matlabLauncher
131-
.cmds(cmdUtils.constructBatchCommandForScriptRun(getCommand()))
132-
.stdout(listener);
120+
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
121+
if(launcher.isUnix()) {
122+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("/bin/bash","-c","./run_matlab_command.sh "+cmdUtils.constructCommandForRunCommand(getCommand())).stdout(listener);
123+
//Copy runner .sh for linux platform in workspace.
124+
cmdUtils.copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
125+
}else {
126+
launcher = launcher.decorateByPrefix("cmd.exe","/C");
127+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\""+cmdUtils.constructCommandForRunCommand(getCommand())+"\"").stdout(listener);
128+
//Copy runner.bat for Windows platform in workspace.
129+
cmdUtils.copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
133130
}
134-
135131
} catch (Exception e) {
136132
listener.getLogger().println(e.getMessage());
137133
return 1;

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.function.Function;
1717
import javax.annotation.Nonnull;
18+
import org.apache.commons.io.FilenameUtils;
1819
import org.jenkinsci.Symbol;
1920
import org.kohsuke.stapler.DataBoundConstructor;
2021
import org.kohsuke.stapler.DataBoundSetter;
@@ -288,38 +289,39 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
288289
// Get matlabroot set in wrapper class.
289290
String matlabRoot = envVars.get("matlabroot");
290291
CommandConstructUtil cmdUtils = new CommandConstructUtil(launcher, matlabRoot);
291-
292-
// Get node specific matlabroot to get matlab version information
293-
FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot);
294-
MatlabReleaseInfo matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot);
295292
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
296-
if (matlabRel.verLessThan(MatlabBuilderConstants.BASE_MATLAB_VERSION_BATCH_SUPPORT)) {
297-
ListenerLogDecorator outStream = new ListenerLogDecorator(listener);
298-
matlabLauncher = matlabLauncher.cmds(cmdUtils.constructDefaultCommandForTestRun(getInputArguments())).stderr(outStream);
299-
} else {
300-
matlabLauncher = matlabLauncher.cmds(cmdUtils.constructBatchCommandForTestRun(getInputArguments())).stdout(listener);
293+
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
294+
if(launcher.isUnix()) {
295+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("/bin/bash","-c","./run_matlab_command.sh "+cmdUtils.constructCommandForTest(getInputArguments())).stdout(listener);
296+
//Copy runner .sh for linux platform in workspace.
297+
cmdUtils.copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
298+
}else {
299+
launcher = launcher.decorateByPrefix("cmd.exe","/C");
300+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\""+cmdUtils.constructCommandForTest(getInputArguments())+"\"").stdout(listener);
301+
//Copy runner.bat for Windows platform in workspace.
302+
cmdUtils.copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
301303
}
302-
304+
303305
// Copy MATLAB scratch file into the workspace.
304-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
305-
copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.MATLAB_RUNNER_RESOURCE, MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE, targetWorkspace);
306+
cmdUtils.copyMatlabScratchFileInWorkspace(MatlabBuilderConstants.MATLAB_RUNNER_RESOURCE, MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE, targetWorkspace);
306307
} catch (Exception e) {
307308
listener.getLogger().println(e.getMessage());
308309
return 1;
309310
}
310311
return matlabLauncher.join();
311312
}
312313

313-
private void copyMatlabScratchFileInWorkspace(String matlabRunnerResourcePath,
314+
/* private void copyMatlabScratchFileInWorkspace(String matlabRunnerResourcePath,
314315
String matlabRunnerTarget, FilePath targetWorkspace)
315316
throws IOException, InterruptedException {
316317
final ClassLoader classLoader = getClass().getClassLoader();
317318
FilePath targetFile =
318319
new FilePath(targetWorkspace, Message.getValue(matlabRunnerTarget));
319320
InputStream in = classLoader.getResourceAsStream(matlabRunnerResourcePath);
320-
321321
targetFile.copyFrom(in);
322-
}
322+
//set executable permission to the file.
323+
targetFile.chmod(0777);
324+
}*/
323325

324326
// Concatenate the input arguments
325327
private String getInputArguments() {
@@ -335,6 +337,4 @@ private String getInputArguments() {
335337

336338
return inputArgsToMatlabFcn;
337339
}
338-
339-
340340
}

src/main/resources/com/mathworks/ci/RunMatlabCommandBuilder/help-matlabCommand.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div>
22
Enter the command you want to run in MATLAB. If the command represents a MATLAB function or script, do not specify the file extension.<br>
33
<b>Examples:</b><br>
4-
<b>To run MATLAB commands :</b> cd c:\MATLAB\tests; runtests('IncludingSubfolder','true')<br>
5-
<b>To run MATLAB function or a script file :</b> cd c:\MATLAB\myscripts; runMyScript(arg1, arg2...)<br>
4+
<b>Commands:</b> <br>cd c:\MATLAB\tests; runtests('IncludingSubfolder','true')<br>
5+
<b>Script or functions:</b><br>cd c:\MATLAB\myscripts; runMyScript(arg1, arg2...)<br>
66
<br>&nbsp;</br>
77
<b>Recommendation:</b>If you require a number of MATLAB commands to execute your build, consider writing a MATLAB script and executing the script file instead.<br>
88
<b>Note:</b> The build will fail if the execution of any MATLAB command causes an error.

src/main/resources/config.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ builder.matlab.customcommandoption.display.name = Custom
1414
Releaseinfo.matlab.version.not.found.error = Error finding MATLAB release for given MATLAB root. Verify MATLAB root path.
1515
Builder.matlab.modelcoverage.support.warning = To generate a Cobertura model coverage report, use MATLAB R2018b or a newer release.
1616
Builder.matlab.exportstmresults.support.warning = To export Simulink Test Manager results, use MATLAB R2019a or a newer release.
17+
Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh
18+
Builder.matlab.runner.script.target.file.windows.name = run_matlab_command.bat
1719

0 commit comments

Comments
 (0)