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

Commit 2f4ce43

Browse files
author
Nikhil Bhoski
committed
Accomodated command as script changes to DSL pipeline.
1 parent 5fe94f0 commit 2f4ce43

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.mathworks.ci;
2+
3+
import java.io.IOException;
4+
import org.jenkinsci.plugins.workflow.steps.StepContext;
5+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
6+
import hudson.EnvVars;
7+
import hudson.FilePath;
8+
import hudson.Launcher;
9+
import hudson.Launcher.ProcStarter;
10+
import hudson.model.Result;
11+
import hudson.model.TaskListener;
12+
13+
public class MatlabCommandStepExecution extends StepExecution implements MatlabBuild {
14+
15+
private static final long serialVersionUID = 1957239693658914450L;
16+
17+
private String command;
18+
private EnvVars env;
19+
20+
21+
public MatlabCommandStepExecution(StepContext context, String command) {
22+
super(context);
23+
this.command = command;
24+
}
25+
26+
private String getCommand() {
27+
return this.env == null ? this.command : this.env.expand(this.command );
28+
}
29+
30+
private void setEnv(EnvVars env) {
31+
this.env = env;
32+
}
33+
34+
@Override
35+
public boolean start() throws Exception {
36+
final Launcher launcher = getContext().get(Launcher.class);
37+
final FilePath workspace = getContext().get(FilePath.class);
38+
final TaskListener listener = getContext().get(TaskListener.class);
39+
final EnvVars env = getContext().get(EnvVars.class);
40+
setEnv(env);
41+
42+
//Make sure the Workspace exists before run
43+
44+
workspace.mkdirs();
45+
46+
int res = execMatlabCommand(workspace, launcher, listener, env);
47+
48+
getContext().setResult((res == 0) ? Result.SUCCESS : Result.FAILURE);
49+
50+
getContext().onSuccess(true);
51+
52+
//return false represents the asynchronous run.
53+
return false;
54+
}
55+
56+
@Override
57+
public void stop(Throwable cause) throws Exception {
58+
getContext().onFailure(cause);
59+
}
60+
61+
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
62+
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
63+
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
64+
final String uniqueCommandFile =
65+
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
66+
final FilePath uniqeTmpFolderPath =
67+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
68+
69+
// Create MATLAB script
70+
createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener);
71+
ProcStarter matlabLauncher;
72+
73+
try {
74+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
75+
uniqueCommandFile, uniqueTmpFldrName);
76+
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
77+
listener.getLogger()
78+
.println("#################### Starting command output ####################");
79+
return matlabLauncher.pwd(uniqeTmpFolderPath).join();
80+
81+
} catch (Exception e) {
82+
listener.getLogger().println(e.getMessage());
83+
return 1;
84+
} finally {
85+
// Cleanup the tmp directory
86+
if (uniqeTmpFolderPath.exists()) {
87+
uniqeTmpFolderPath.deleteRecursive();
88+
}
89+
}
90+
}
91+
92+
private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
93+
94+
// Create a new command runner script in the temp folder.
95+
final FilePath matlabCommandFile =
96+
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
97+
final String matlabCommandFileContent =
98+
"cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + getCommand();
99+
100+
// Display the commands on console output for users reference
101+
listener.getLogger()
102+
.println("Generating MATLAB script with content:\n" + getCommand() + "\n");
103+
104+
matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
105+
}
106+
}

src/main/java/com/mathworks/ci/MatlabStepExecution.java renamed to src/main/java/com/mathworks/ci/MatlabRunTestsStepExecution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
import hudson.model.Result;
1616
import hudson.model.TaskListener;
1717

18-
public class MatlabStepExecution extends StepExecution implements MatlabBuild {
18+
public class MatlabRunTestsStepExecution extends StepExecution implements MatlabBuild {
1919

2020
private static final long serialVersionUID = 6704588180717665100L;
2121

2222
private String command;
2323

2424

25-
public MatlabStepExecution(StepContext context, String command) {
25+
public MatlabRunTestsStepExecution(StepContext context, String command) {
2626
super(context);
2727
this.command = command;
2828
}
@@ -65,7 +65,7 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
6565
envVars.expand(getCommand()), uniqueTmpFldrName);
6666

6767

68-
return matlabLauncher.join();
68+
return matlabLauncher.pwd(workspace).join();
6969
} catch (Exception e) {
7070
listener.getLogger().println(e.getMessage());
7171
return 1;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String getCommand() {
3636

3737
@Override
3838
public StepExecution start(StepContext context) throws Exception {
39-
return new MatlabStepExecution(context, getCommand());
39+
return new MatlabCommandStepExecution(context, getCommand());
4040
}
4141

4242
@Extension
@@ -52,6 +52,11 @@ public Set<? extends Class<?>> getRequiredContext() {
5252
public String getFunctionName() {
5353
return Message.getValue("matlab.command.build.step.name");
5454
}
55+
56+
@Override
57+
public String getDisplayName() {
58+
return Message.getValue("matlab.command.step.display.name");
59+
}
5560
}
5661
}
5762

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public StepExecution start(StepContext context) throws Exception {
111111
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
112112
copyScratchFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
113113
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
114-
return new MatlabStepExecution(context,constructCommandForTest(getInputArgs()));
114+
return new MatlabRunTestsStepExecution(context,constructCommandForTest(getInputArgs()));
115115
}
116116

117117
@Extension
@@ -127,6 +127,11 @@ public Set<? extends Class<?>> getRequiredContext() {
127127
public String getFunctionName() {
128128
return Message.getValue("matlab.tests.build.step.name");
129129
}
130+
131+
@Override
132+
public String getDisplayName() {
133+
return Message.getValue("matlab.tests.step.display.name");
134+
}
130135
}
131136

132137
public String constructCommandForTest(String inputArguments) {

src/main/resources/config.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh
1818
Builder.matlab.runner.script.target.file.windows.name = run_matlab_command.bat
1919
build.workspace.computer.not.found = Unable to access the computer for this build.
2020
matlab.command.build.step.name = runMATLABCommand
21-
matlab.tests.build.step.name = runMATLABTests
21+
matlab.tests.build.step.name = runMATLABTests
22+
matlab.command.step.display.name = Runs MATLAB command and scripts
23+
matlab.tests.step.display.name = Runs MATLAB tests and produce test artifacts

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import hudson.Launcher.ProcStarter;
1313
import hudson.model.TaskListener;
1414

15-
public class TestStepExecution extends MatlabStepExecution {
15+
public class TestStepExecution extends MatlabRunTestsStepExecution {
1616

1717
public TestStepExecution(StepContext context, String command) {
1818
super(context, command);

0 commit comments

Comments
 (0)