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

Commit 1feebaa

Browse files
author
Nikhil Bhoski
committed
Updated review comments and fixed indentation
1 parent 42524be commit 1feebaa

File tree

3 files changed

+147
-126
lines changed

3 files changed

+147
-126
lines changed

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +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;
1211
import java.util.UUID;
13-
1412
import hudson.EnvVars;
1513
import hudson.FilePath;
1614
import hudson.Launcher;
@@ -19,70 +17,79 @@
1917
import hudson.model.TaskListener;
2018

2119
public interface MatlabBuild {
22-
20+
2321
/**
24-
* 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
2523
* object to launch MATLAB with appropriate startup options like -r or -batch
26-
* @param workspace Current build workspace
24+
*
25+
* @param workspace Current build workspace
2726
* @param launcher Current build launcher
28-
* @param listener Current build listener
27+
* @param listener Current build listener
2928
* @param envVars Environment variables of the current build
30-
* @param matlabCommand MATLAB command to execute on shell
29+
* @param matlabCommand MATLAB command to execute on shell
3130
* @return matlabLauncher returns the process launcher to run MATLAB commands
3231
*/
33-
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand,String uniqueName) throws IOException, InterruptedException {
34-
//Get node specific tmp directory to copy matlab runner script
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
3536
String tmpDir = getNodeSpecificTmpFolderPath();
3637
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
3738
ProcStarter matlabLauncher;
38-
if(launcher.isUnix()) {
39-
final String runnerScriptName = uniqueName+"/run_matlab_command.sh";
40-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/"+runnerScriptName,matlabCommand).stdout(listener);
41-
42-
//Copy runner .sh for linux platform in workspace.
43-
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, targetWorkspace);
44-
}else {
45-
final String runnerScriptName = uniqueName+"\\run_matlab_command.bat";
46-
launcher = launcher.decorateByPrefix("cmd.exe","/C");
47-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+runnerScriptName,"\""+matlabCommand+"\"").stdout(listener);
48-
//Copy runner.bat for Windows platform in workspace.
49-
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName, 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);
5056
}
5157
return matlabLauncher;
5258
}
5359

5460
/*
5561
* Method to copy given file from source to target node specific workspace.
5662
*/
57-
default void copyFileInWorkspace(String sourceFile, String targetFile,
58-
FilePath targetWorkspace) throws IOException, InterruptedException {
63+
default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace)
64+
throws IOException, InterruptedException {
5965
final ClassLoader classLoader = getClass().getClassLoader();
6066
FilePath targetFilePath = new FilePath(targetWorkspace, targetFile);
6167
InputStream in = classLoader.getResourceAsStream(sourceFile);
6268
targetFilePath.copyFrom(in);
6369
// set executable permission
64-
targetFilePath.chmod(0755);
70+
targetFilePath.chmod(0755);
6571
}
66-
67-
default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher,String uniqueName) throws IOException, InterruptedException {
72+
73+
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName)
74+
throws IOException, InterruptedException {
6875
Computer cmp = Computer.currentComputer();
6976
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
70-
if(launcher.isUnix()) {
71-
tmpDir = tmpDir+"/"+uniqueName;
72-
}else {
73-
tmpDir = tmpDir+"\\"+uniqueName;
77+
if (launcher.isUnix()) {
78+
tmpDir = tmpDir + "/" + uniqueName;
79+
} else {
80+
tmpDir = tmpDir + "\\" + uniqueName;
7481
}
75-
return new FilePath(launcher.getChannel(), tmpDir);
82+
return new FilePath(launcher.getChannel(), tmpDir);
7683
}
77-
84+
7885
default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException {
7986
Computer cmp = Computer.currentComputer();
8087
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
8188
return tmpDir;
8289
}
83-
90+
8491
default String getUniqueNameForRunnerFile() {
85-
return UUID.randomUUID().toString();
92+
return UUID.randomUUID().toString();
8693
}
8794

8895
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import java.io.IOException;
10-
1110
import javax.annotation.Nonnull;
1211
import org.jenkinsci.Symbol;
1312
import org.kohsuke.stapler.DataBoundConstructor;
@@ -96,37 +95,38 @@ public boolean isApplicable(
9695
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
9796
@Nonnull Launcher launcher, @Nonnull TaskListener listener)
9897
throws InterruptedException, IOException {
99-
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-
}
105+
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
106+
107+
if (buildResult != 0) {
108+
build.setResult(Result.FAILURE);
109+
}
111110
}
112111

113112
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
114113
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
115-
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
114+
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
116115
ProcStarter matlabLauncher;
117116
try {
118-
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand(),uniqueTmpFldrName);
117+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
118+
getCommand(), uniqueTmpFldrName);
119119
return matlabLauncher.join();
120120
} catch (Exception e) {
121121
listener.getLogger().println(e.getMessage());
122122
return 1;
123-
}finally {
124-
// Cleanup the runner File from tmp directory
125-
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueTmpFldrName);
126-
if(matlabRunnerScript.isDirectory()) {
123+
} finally {
124+
// Cleanup the runner File from tmp directory
125+
FilePath matlabRunnerScript = getFilePathForUniqueFolder(launcher, uniqueTmpFldrName);
126+
if (matlabRunnerScript.exists()) {
127127
matlabRunnerScript.deleteRecursive();
128128
}
129129
}
130-
131-
}
130+
131+
}
132132
}

0 commit comments

Comments
 (0)