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

Commit 2c46140

Browse files
authored
Merge pull request #112 from mathworks/matlab_cmd_as_script
Matlab cmd as script
2 parents 33bca55 + 3e7f59e commit 2c46140

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ public interface MatlabBuild {
2929
* @param matlabCommand MATLAB command to execute on shell
3030
* @return matlabLauncher returns the process launcher to run MATLAB commands
3131
*/
32-
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,
33-
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
32+
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
33+
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
3434
throws IOException, InterruptedException {
3535
// Get node specific tmp directory to copy matlab runner script
3636
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
3737
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
3838
ProcStarter matlabLauncher;
3939
if (launcher.isUnix()) {
4040
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
41-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
42-
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
41+
matlabLauncher = launcher.launch().envs(envVars);
42+
matlabLauncher.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
4343

4444
// Copy runner .sh for linux platform in workspace.
4545
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
4646
targetWorkspace);
4747
} else {
4848
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
4949
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
50-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
51-
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
50+
matlabLauncher = launcher.launch().envs(envVars);
51+
matlabLauncher.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
5252
.stdout(listener);
5353
// Copy runner.bat for Windows platform in workspace.
5454
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
101101

102102
// Invoke MATLAB command and transfer output to standard
103103
// Output Console
104+
104105

105106
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
106107

@@ -112,22 +113,46 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
112113
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
113114
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
114115
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
116+
final String uniqueCommandFile =
117+
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
118+
final FilePath uniqeTmpFolderPath =
119+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
120+
121+
// Create MATLAB script
122+
createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener);
115123
ProcStarter matlabLauncher;
124+
116125
try {
117126
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
118-
getCommand(), uniqueTmpFldrName);
119-
return matlabLauncher.join();
127+
uniqueCommandFile, uniqueTmpFldrName);
128+
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
129+
listener.getLogger()
130+
.println("#################### Starting command output ####################");
131+
return matlabLauncher.pwd(uniqeTmpFolderPath).join();
132+
120133
} catch (Exception e) {
121134
listener.getLogger().println(e.getMessage());
122135
return 1;
123136
} finally {
124-
// Cleanup the runner File from tmp directory
125-
FilePath matlabRunnerScript =
126-
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
127-
if (matlabRunnerScript.exists()) {
128-
matlabRunnerScript.deleteRecursive();
137+
// Cleanup the tmp directory
138+
if (uniqeTmpFolderPath.exists()) {
139+
uniqeTmpFolderPath.deleteRecursive();
129140
}
130141
}
131-
142+
}
143+
144+
private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
145+
146+
// Create a new command runner script in the temp folder.
147+
final FilePath matlabCommandFile =
148+
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
149+
final String matlabCommandFileContent =
150+
"cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + getCommand();
151+
152+
// Display the commands on console output for users reference
153+
listener.getLogger()
154+
.println("Generating MATLAB script with content:\n" + getCommand() + "\n");
155+
156+
matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
132157
}
133158
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
240240
try {
241241
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
242242
constructCommandForTest(getInputArguments()), uniqueTmpFldrName);
243-
243+
244244
// Copy MATLAB scratch file into the workspace.
245245
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
246246
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
247247
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
248-
return matlabLauncher.join();
248+
249+
return matlabLauncher.pwd(workspace).join();
249250
} catch (Exception e) {
250251
listener.getLogger().println(e.getMessage());
251252
return 1;
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<div>
2-
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>
3-
<b>Examples:</b><br>
4-
<b>Run commands:</b> results = runtests(‘IncludingSubfolders’,true); assert(all(~[results.Failed]))<br>
5-
<b>Run a script:</b> runMyScript<br>
2+
Insert the MATLAB commands you want to execute in the <b>Command</b> box. If you need to specify more than one command, use a comma or semicolon to separate the commands.<br>
3+
<b>Example: </b>results = runtests, assertSuccess(results);<br><br>
4+
If you need to specify several MATLAB commands, consider writing a MATLAB script or function and executing this script or function instead. (To run a script or function, do not specify the file extension.)<br>
5+
<b>Example: </b>myscript<br>
66
<br>&nbsp;</br>
7-
<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>
8-
<b>Note:</b> The build will fail if the execution of any MATLAB command causes an error.
9-
<br>
7+
<b>Note:</b><ul><li>The build fails if the execution of any command results in an error.</li>
8+
<li>If the build uses a MATLAB version prior to R2020a, MATLAB might display non-ASCII characters, specified in the <b>Command</b> box, as garbled text. If you specify non-ASCII characters in your commands, consider running your commands as a .m or .mlx file created in the same locale that MATLAB uses on the build agent</li>
9+
</ul>
1010
</div>

src/test/java/com/mathworks/ci/RunMatlabCommandBuilderTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public void verifyMATLABlaunchedWithDefaultArguments() throws Exception {
134134
project.getBuildersList().add(this.scriptBuilder);
135135
FreeStyleBuild build = project.scheduleBuild2(0).get();
136136
jenkins.assertLogContains("run_matlab_command", build);
137-
jenkins.assertLogContains("pwd", build);
138137
}
139138

140139
/*
@@ -185,6 +184,7 @@ public void verifyBuildFailureWhenMatlabCommandPasses() throws Exception {
185184

186185
/*
187186
* Test to verify Builder picks the exact command that user entered.
187+
*
188188
*/
189189

190190
@Test
@@ -195,6 +195,7 @@ public void verifyBuildPicksTheCorretCommandBatch() throws Exception {
195195
project.getBuildersList().add(this.scriptBuilder);
196196
FreeStyleBuild build = project.scheduleBuild2(0).get();
197197
jenkins.assertLogContains("run_matlab_command", build);
198+
jenkins.assertLogContains("Generating MATLAB script with content", build);
198199
jenkins.assertLogContains("pwd", build);
199200
}
200201

@@ -214,6 +215,7 @@ public void verifyMATLABscratchFileNotGenerated() throws Exception {
214215

215216
/*
216217
* Test to verify command supports resolving environment variable (For MATRIX builds).
218+
*
217219
*/
218220
@Test
219221
public void verifyCommandSupportsEnvVar() throws Exception {
@@ -291,4 +293,26 @@ public void verifyMatrixBuildPasses() throws Exception {
291293
jenkins.assertLogContains("R2018b completed", build);
292294
jenkins.assertBuildStatus(Result.SUCCESS, build);
293295
}
296+
297+
/*
298+
* Test to verify if command parses succesfully when multiple combinations of
299+
* characters are passed. (candidate for integ-tests once integrated)
300+
*/
301+
302+
303+
public void verifyMultispecialChar() throws Exception {
304+
final String actualCommand =
305+
"!\"\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
306+
final String expectedCommand =
307+
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
308+
this.buildWrapper.setMatlabRootFolder(getMatlabroot("R2018b"));
309+
310+
project.getBuildWrappersList().add(this.buildWrapper);
311+
scriptBuilder.setMatlabCommand("disp(" + actualCommand + ")");
312+
project.getBuildersList().add(this.scriptBuilder);
313+
FreeStyleBuild build = project.scheduleBuild2(0).get();
314+
315+
jenkins.assertLogContains("Generating MATLAB script with content", build);
316+
jenkins.assertLogContains(expectedCommand, build);
317+
}
294318
}

0 commit comments

Comments
 (0)