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

Commit 34ba3a5

Browse files
committed
Refactored the common code using default methods.
1 parent 45c5193 commit 34ba3a5

File tree

8 files changed

+83
-72
lines changed

8 files changed

+83
-72
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.mathworks.ci;
2+
/**
3+
* Copyright 2019-2020 The MathWorks, Inc.
4+
*
5+
* Build Interface has two default methods. MATLAB builders can override the
6+
* default behavior.
7+
*
8+
*/
9+
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import hudson.EnvVars;
13+
import hudson.FilePath;
14+
import hudson.Launcher;
15+
import hudson.Launcher.ProcStarter;
16+
import hudson.model.TaskListener;
17+
18+
public interface MatlabBuild {
19+
20+
/**
21+
* This Method decorates the launcher with MATLAB command provided and returns the Process
22+
* object to launch MATLAB with appropriate startup options like -r or -batch
23+
* @param workspace
24+
* @param launcher
25+
* @param listener
26+
* @param envVars
27+
* @param matlabCommand
28+
* @return
29+
* @throws IOException
30+
* @throws InterruptedException
31+
*/
32+
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException {
33+
ProcStarter matlabLauncher;
34+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
35+
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
36+
if(launcher.isUnix()) {
37+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("./run_matlab_command.sh",matlabCommand).stdout(listener);
38+
//Copy runner .sh for linux platform in workspace.
39+
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
40+
}else {
41+
launcher = launcher.decorateByPrefix("cmd.exe","/C");
42+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener);
43+
//Copy runner.bat for Windows platform in workspace.
44+
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
45+
}
46+
return matlabLauncher;
47+
}
48+
49+
/**
50+
* Method to copy given file from source to target node specific workspace.
51+
* @param sourceFile
52+
* @param targetFile
53+
* @param targetWorkspace
54+
* @throws IOException
55+
* @throws InterruptedException
56+
*/
57+
default void copyFileInWorkspace(String sourceFile, String targetFile,
58+
FilePath targetWorkspace) throws IOException, InterruptedException {
59+
final ClassLoader classLoader = getClass().getClassLoader();
60+
FilePath targetFilePath = new FilePath(targetWorkspace, Message.getValue(targetFile));
61+
InputStream in = classLoader.getResourceAsStream(sourceFile);
62+
targetFilePath.copyFrom(in);
63+
// set executable permission
64+
targetFilePath.chmod(0755);
65+
66+
}
67+
68+
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14-
import java.nio.file.Files;
15-
import java.nio.file.Path;
16-
import java.nio.file.StandardCopyOption;
1714
import java.util.ArrayList;
1815
import java.util.Arrays;
1916
import java.util.Collections;

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

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

99
import java.io.IOException;
10-
import java.io.InputStream;
1110
import javax.annotation.Nonnull;
1211
import org.jenkinsci.Symbol;
1312
import org.kohsuke.stapler.DataBoundConstructor;
@@ -27,7 +26,7 @@
2726
import jenkins.tasks.SimpleBuildStep;
2827
import net.sf.json.JSONObject;
2928

30-
public class RunMatlabCommandBuilder extends Builder implements SimpleBuildStep {
29+
public class RunMatlabCommandBuilder extends Builder implements SimpleBuildStep, MatlabBuild {
3130
private int buildResult;
3231
private EnvVars env;
3332
private String matlabCommand;
@@ -114,33 +113,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
114113
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
115114
ProcStarter matlabLauncher;
116115
try {
117-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
118-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
119-
if(launcher.isUnix()) {
120-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("./run_matlab_command.sh",getCommand()).stdout(listener);
121-
//Copy runner .sh for linux platform in workspace.
122-
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
123-
}else {
124-
launcher = launcher.decorateByPrefix("cmd.exe","/C");
125-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\""+getCommand()+"\"").stdout(listener);
126-
//Copy runner.bat for Windows platform in workspace.
127-
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
128-
}
116+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand());
129117
} catch (Exception e) {
130118
listener.getLogger().println(e.getMessage());
131119
return 1;
132120
}
133121
return matlabLauncher.join();
134-
}
135-
136-
public void copyFileInWorkspace(String matlabRunnerResourcePath, String matlabRunnerTarget,
137-
FilePath targetWorkspace) throws IOException, InterruptedException {
138-
final ClassLoader classLoader = getClass().getClassLoader();
139-
FilePath targetFile = new FilePath(targetWorkspace, Message.getValue(matlabRunnerTarget));
140-
InputStream in = classLoader.getResourceAsStream(matlabRunnerResourcePath);
141-
targetFile.copyFrom(in);
142-
// set executable permission
143-
targetFile.chmod(0755);
144-
}
145-
122+
}
146123
}

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.io.File;
1212
import java.io.IOException;
13-
import java.io.InputStream;
1413
import java.util.ArrayList;
1514
import java.util.List;
1615
import java.util.function.Function;
@@ -39,7 +38,7 @@
3938
import jenkins.tasks.SimpleBuildStep;
4039
import net.sf.json.JSONObject;
4140

42-
public class RunMatlabTestsBuilder extends Builder implements SimpleBuildStep {
41+
public class RunMatlabTestsBuilder extends Builder implements SimpleBuildStep,MatlabBuild {
4342

4443
private int buildResult;
4544
private EnvVars env;
@@ -284,21 +283,13 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
284283
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
285284
ProcStarter matlabLauncher;
286285
try {
287-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
288-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
289-
if (launcher.isUnix()) {
290-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("./run_matlab_command.sh",constructCommandForTest(getInputArguments())).stdout(listener);
291-
// Copy runner .sh for linux platform in workspace.
292-
cpoyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT,"Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
293-
} else {
294-
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
295-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\"" + constructCommandForTest(getInputArguments()) + "\"").stdout(listener);
296-
// Copy runner.bat for Windows platform in workspace.
297-
cpoyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT,"Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
298-
}
286+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
287+
constructCommandForTest(getInputArguments()));
299288

300289
// Copy MATLAB scratch file into the workspace.
301-
cpoyFileInWorkspace(MatlabBuilderConstants.MATLAB_RUNNER_RESOURCE,MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE, targetWorkspace);
290+
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
291+
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_RUNNER_RESOURCE,
292+
MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE, targetWorkspace);
302293
} catch (Exception e) {
303294
listener.getLogger().println(e.getMessage());
304295
return 1;
@@ -314,16 +305,6 @@ public String constructCommandForTest(String inputArguments) {
314305
return runCommand;
315306
}
316307

317-
private void cpoyFileInWorkspace(String matlabRunnerResourcePath, String matlabRunnerTarget,
318-
FilePath targetWorkspace) throws IOException, InterruptedException {
319-
final ClassLoader classLoader = getClass().getClassLoader();
320-
FilePath targetFile = new FilePath(targetWorkspace, Message.getValue(matlabRunnerTarget));
321-
InputStream in = classLoader.getResourceAsStream(matlabRunnerResourcePath);
322-
targetFile.copyFrom(in);
323-
// set executable permission to the file.
324-
targetFile.chmod(0755);
325-
}
326-
327308
// Concatenate the input arguments
328309
private String getInputArguments() {
329310
String pdfReport = MatlabBuilderConstants.PDF_REPORT + "," + this.getPdfReportChkBx();

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33

44

55
import static org.junit.Assert.assertFalse;
6-
import hudson.EnvVars;
7-
import hudson.FilePath;
8-
import hudson.model.FreeStyleBuild;
9-
import hudson.model.Result;
10-
import hudson.slaves.EnvironmentVariablesNodeProperty;
11-
import hudson.model.FreeStyleProject;
12-
import hudson.tasks.Builder;
136
import java.io.File;
147
import java.io.IOException;
158
import java.net.URISyntaxException;
169
import java.net.URL;
17-
import java.util.List;
1810
import java.util.Optional;
1911
import java.util.concurrent.ExecutionException;
2012
import org.junit.After;
@@ -29,6 +21,12 @@
2921
import com.gargoylesoftware.htmlunit.html.HtmlPage;
3022
import com.mathworks.ci.MatlabBuilder.RunTestsAutomaticallyOption;
3123
import com.mathworks.ci.MatlabBuilder.RunTestsWithCustomCommandOption;
24+
import hudson.EnvVars;
25+
import hudson.FilePath;
26+
import hudson.model.FreeStyleBuild;
27+
import hudson.model.FreeStyleProject;
28+
import hudson.model.Result;
29+
import hudson.slaves.EnvironmentVariablesNodeProperty;
3230

3331

3432
/*

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13-
import hudson.EnvVars;
1413
import hudson.Extension;
15-
import hudson.Launcher;
1614
import hudson.model.AbstractProject;
1715
import hudson.tasks.BuildStepDescriptor;
1816
import hudson.tasks.Builder;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212
import javax.annotation.Nonnull;
13-
import org.kohsuke.stapler.DataBoundSetter;
1413
import org.kohsuke.stapler.StaplerRequest;
1514
import hudson.EnvVars;
1615
import hudson.Extension;
@@ -29,7 +28,6 @@ public class RunMatlabCommandBuilderTester extends RunMatlabCommandBuilder {
2928
private int buildResult;
3029
private EnvVars env;
3130
private MatlabReleaseInfo matlabRel;
32-
private String matlabCommand;
3331
private String commandParameter;
3432
private String matlabExecutorPath;
3533

@@ -42,10 +40,6 @@ public RunMatlabCommandBuilderTester(String matlabExecutorPath, String customTes
4240

4341
// Getter and Setters to access local members
4442

45-
public void setMatlabCommand(String matlabCommand) {
46-
this.matlabCommand = matlabCommand;
47-
}
48-
4943
private void setEnv(EnvVars env) {
5044
this.env = env;
5145
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
import com.gargoylesoftware.htmlunit.WebAssert;
2424
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
2525
import com.gargoylesoftware.htmlunit.html.HtmlPage;
26-
import hudson.EnvVars;
2726
import hudson.FilePath;
2827
import hudson.model.FreeStyleBuild;
2928
import hudson.model.FreeStyleProject;
3029
import hudson.model.Result;
31-
import hudson.slaves.EnvironmentVariablesNodeProperty;
3230
import hudson.tasks.Builder;
3331

3432
public class RunMatlabTestsBuilderTest {

0 commit comments

Comments
 (0)