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

Commit 4cb26c9

Browse files
authored
Merge pull request #130 from mathworks/genscript_changes
Genscript changes
2 parents 9358389 + 1ed84b0 commit 4cb26c9

File tree

10 files changed

+103
-231
lines changed

10 files changed

+103
-231
lines changed

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@
124124
<url>https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v0/run-matlab-command.zip</url>
125125
<unpack>true</unpack>
126126
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
127+
<skipCache>true</skipCache>
128+
<overwrite>true</overwrite>
129+
</configuration>
130+
</execution>
131+
<execution>
132+
<id>get-matlab-gen-script</id>
133+
<phase>validate</phase>
134+
<goals>
135+
<goal>wget</goal>
136+
</goals>
137+
<configuration>
138+
<url>https://ssd.mathworks.com/supportfiles/ci/matlab-script-generator/v0/matlab-script-generator.zip</url>
139+
<unpack>false</unpack>
140+
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
141+
<skipCache>true</skipCache>
142+
<overwrite>true</overwrite>
127143
</configuration>
128144
</execution>
129145
</executions>
@@ -186,6 +202,7 @@
186202
<include>**/*.bat</include>
187203
<include>**/*.sh</include>
188204
<include>**/*.txt</include>
205+
<include>**/*.zip</include>
189206
</includes>
190207
<followSymlinks>false</followSymlinks>
191208
</fileset>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,21 @@ default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOExcepti
9696
default String getUniqueNameForRunnerFile() {
9797
return UUID.randomUUID().toString();
9898
}
99+
100+
// This method prepares the temp folder by coping all helper files in it.
101+
default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedException {
102+
// Copy MATLAB scratch file in temp.
103+
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
104+
MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
105+
+ tmpFldr.getBaseName().replaceAll("-", "_") + ".m",
106+
tmpFldr);
107+
// copy genscript package
108+
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
109+
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
110+
FilePath zipFileLocation =
111+
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR);
112+
113+
// Unzip the file in temp folder.
114+
zipFileLocation.unzip(tmpFldr);
115+
}
99116
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public class MatlabBuilderConstants {
2828
// Matlab Runner files
2929
static final String BAT_RUNNER_SCRIPT = "run_matlab_command.bat";
3030
static final String SHELL_RUNNER_SCRIPT = "run_matlab_command.sh";
31+
32+
//Matlab Script generator package
33+
static final String MATLAB_SCRIPT_GENERATOR = "matlab-script-generator.zip";
34+
35+
//Test runner file prefix
36+
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "test_runner_";
3137
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
5858
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
5959
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
6060
try {
61-
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
62-
envVars.expand(getCommand()), uniqueTmpFldrName);
61+
FilePath genScriptLocation =
62+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
63+
final String cmdPrefix =
64+
"addpath('" + genScriptLocation.getRemote().replaceAll("'", "''") + "'); ";
65+
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
66+
+ genScriptLocation.getBaseName().replaceAll("-", "_");
67+
68+
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener,
69+
envVars, cmdPrefix + matlabFunctionName+ "("+envVars.expand(getCommand()+")"), uniqueTmpFldrName);
6370

64-
71+
//prepare temp folder by coping genscript package.
72+
prepareTmpFldr(genScriptLocation);
73+
6574
return matlabLauncher.pwd(workspace).join();
6675
} catch (Exception e) {
6776
listener.getLogger().println(e.getMessage());

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import javax.annotation.Nonnull;
18-
import org.apache.commons.collections.map.HashedMap;
19-
import org.apache.commons.io.FilenameUtils;
20-
import org.jenkinsci.Symbol;
2118
import org.kohsuke.stapler.DataBoundConstructor;
2219
import org.kohsuke.stapler.DataBoundSetter;
2320
import org.kohsuke.stapler.StaplerRequest;
@@ -238,13 +235,15 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
238235
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
239236
ProcStarter matlabLauncher;
240237
try {
238+
FilePath genScriptLocation =
239+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
240+
241241
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
242-
constructCommandForTest(getInputArguments()), uniqueTmpFldrName);
242+
constructCommandForTest(getInputArguments(), genScriptLocation),
243+
uniqueTmpFldrName);
243244

244-
// Copy MATLAB scratch file into the workspace.
245-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
246-
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
247-
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
245+
// copy genscript package in temp folder
246+
prepareTmpFldr(genScriptLocation);
248247

249248
return matlabLauncher.pwd(workspace).join();
250249
} catch (Exception e) {
@@ -259,11 +258,12 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
259258
}
260259
}
261260
}
262-
263-
public String constructCommandForTest(String inputArguments) {
264-
final String matlabFunctionName =
265-
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
266-
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
261+
262+
public String constructCommandForTest(String inputArguments, FilePath scriptPath) {
263+
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
264+
+ scriptPath.getBaseName().replaceAll("-", "_");
265+
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''") + "'); "
266+
+ matlabFunctionName + "(" + inputArguments + ")";
267267
return runCommand;
268268
}
269269

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
package com.mathworks.ci;
2-
/**
3-
* Copyright 2020 The MathWorks, Inc.
4-
*
5-
*/
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
92
/**
103
* Copyright 2020 The MathWorks, Inc.
114
*
@@ -16,7 +9,6 @@
169
import java.util.List;
1710
import java.util.Map;
1811
import java.util.Set;
19-
import org.apache.commons.io.FilenameUtils;
2012
import org.jenkinsci.plugins.workflow.steps.Step;
2113
import org.jenkinsci.plugins.workflow.steps.StepContext;
2214
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
@@ -104,16 +96,9 @@ public void setModelCoverageCobertura(String modelCoverageCobertura) {
10496

10597
@Override
10698
public StepExecution start(StepContext context) throws Exception {
107-
Launcher launcher = context.get(Launcher.class);
108-
FilePath workspace = context.get(FilePath.class);
109-
110-
//Copy Scratch file needed to run MATLAB tests in workspace
111-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
112-
copyScratchFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
113-
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
114-
return new MatlabRunTestsStepExecution(context,constructCommandForTest(getInputArgs()));
99+
return new MatlabRunTestsStepExecution(context, getInputArgs());
115100
}
116-
101+
117102
@Extension
118103
public static class RunTestsStepDescriptor extends StepDescriptor {
119104

@@ -134,14 +119,6 @@ public String getDisplayName() {
134119
}
135120
}
136121

137-
public String constructCommandForTest(String inputArguments) {
138-
final String matlabFunctionName =
139-
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
140-
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
141-
return runCommand;
142-
}
143-
144-
145122
private String getInputArgs() {
146123
final List<String> inputArgs = new ArrayList<>();
147124
final Map<String, String> args = getMatlabArgs();
@@ -169,17 +146,4 @@ private Map<String, String> getMatlabArgs() {
169146
args.put("CoberturaModelCoveragePath", getModelCoverageCobertura());
170147
return args;
171148
}
172-
173-
/*
174-
* Method to copy given file from source to target node specific workspace.
175-
*/
176-
private void copyScratchFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace)
177-
throws IOException, InterruptedException {
178-
final ClassLoader classLoader = getClass().getClassLoader();
179-
FilePath targetFilePath = new FilePath(targetWorkspace, targetFile);
180-
InputStream in = classLoader.getResourceAsStream(sourceFile);
181-
targetFilePath.copyFrom(in);
182-
// set executable permission
183-
targetFilePath.chmod(0755);
184-
}
185149
}

0 commit comments

Comments
 (0)