Skip to content

Commit

Permalink
Merge pull request #130 from mathworks/genscript_changes
Browse files Browse the repository at this point in the history
Genscript changes
  • Loading branch information
nbhoski committed Aug 14, 2020
2 parents 9358389 + 1ed84b0 commit 4cb26c9
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 231 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@
<url>https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v0/run-matlab-command.zip</url>
<unpack>true</unpack>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<skipCache>true</skipCache>
<overwrite>true</overwrite>
</configuration>
</execution>
<execution>
<id>get-matlab-gen-script</id>
<phase>validate</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://ssd.mathworks.com/supportfiles/ci/matlab-script-generator/v0/matlab-script-generator.zip</url>
<unpack>false</unpack>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<skipCache>true</skipCache>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -186,6 +202,7 @@
<include>**/*.bat</include>
<include>**/*.sh</include>
<include>**/*.txt</include>
<include>**/*.zip</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/mathworks/ci/MatlabBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,21 @@ default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOExcepti
default String getUniqueNameForRunnerFile() {
return UUID.randomUUID().toString();
}

// This method prepares the temp folder by coping all helper files in it.
default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedException {
// Copy MATLAB scratch file in temp.
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
+ tmpFldr.getBaseName().replaceAll("-", "_") + ".m",
tmpFldr);
// copy genscript package
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
FilePath zipFileLocation =
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR);

// Unzip the file in temp folder.
zipFileLocation.unzip(tmpFldr);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/mathworks/ci/MatlabBuilderConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public class MatlabBuilderConstants {
// Matlab Runner files
static final String BAT_RUNNER_SCRIPT = "run_matlab_command.bat";
static final String SHELL_RUNNER_SCRIPT = "run_matlab_command.sh";

//Matlab Script generator package
static final String MATLAB_SCRIPT_GENERATOR = "matlab-script-generator.zip";

//Test runner file prefix
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "test_runner_";
}
15 changes: 12 additions & 3 deletions src/main/java/com/mathworks/ci/MatlabRunTestsStepExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,19 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
try {
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
envVars.expand(getCommand()), uniqueTmpFldrName);
FilePath genScriptLocation =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
final String cmdPrefix =
"addpath('" + genScriptLocation.getRemote().replaceAll("'", "''") + "'); ";
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
+ genScriptLocation.getBaseName().replaceAll("-", "_");

ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener,
envVars, cmdPrefix + matlabFunctionName+ "("+envVars.expand(getCommand()+")"), uniqueTmpFldrName);


//prepare temp folder by coping genscript package.
prepareTmpFldr(genScriptLocation);

return matlabLauncher.pwd(workspace).join();
} catch (Exception e) {
listener.getLogger().println(e.getMessage());
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.FilenameUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -238,13 +235,15 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
ProcStarter matlabLauncher;
try {
FilePath genScriptLocation =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);

matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
constructCommandForTest(getInputArguments()), uniqueTmpFldrName);
constructCommandForTest(getInputArguments(), genScriptLocation),
uniqueTmpFldrName);

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

return matlabLauncher.pwd(workspace).join();
} catch (Exception e) {
Expand All @@ -259,11 +258,12 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
}
}
}

public String constructCommandForTest(String inputArguments) {
final String matlabFunctionName =
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";

public String constructCommandForTest(String inputArguments, FilePath scriptPath) {
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
+ scriptPath.getBaseName().replaceAll("-", "_");
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''") + "'); "
+ matlabFunctionName + "(" + inputArguments + ")";
return runCommand;
}

Expand Down
40 changes: 2 additions & 38 deletions src/main/java/com/mathworks/ci/RunMatlabTestsStep.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
package com.mathworks.ci;
/**
* Copyright 2020 The MathWorks, Inc.
*
*/
import java.io.IOException;
import java.io.InputStream;

/**
* Copyright 2020 The MathWorks, Inc.
*
Expand All @@ -16,7 +9,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FilenameUtils;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
Expand Down Expand Up @@ -104,16 +96,9 @@ public void setModelCoverageCobertura(String modelCoverageCobertura) {

@Override
public StepExecution start(StepContext context) throws Exception {
Launcher launcher = context.get(Launcher.class);
FilePath workspace = context.get(FilePath.class);

//Copy Scratch file needed to run MATLAB tests in workspace
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
copyScratchFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
return new MatlabRunTestsStepExecution(context,constructCommandForTest(getInputArgs()));
return new MatlabRunTestsStepExecution(context, getInputArgs());
}

@Extension
public static class RunTestsStepDescriptor extends StepDescriptor {

Expand All @@ -134,14 +119,6 @@ public String getDisplayName() {
}
}

public String constructCommandForTest(String inputArguments) {
final String matlabFunctionName =
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
return runCommand;
}


private String getInputArgs() {
final List<String> inputArgs = new ArrayList<>();
final Map<String, String> args = getMatlabArgs();
Expand Down Expand Up @@ -169,17 +146,4 @@ private Map<String, String> getMatlabArgs() {
args.put("CoberturaModelCoveragePath", getModelCoverageCobertura());
return args;
}

/*
* Method to copy given file from source to target node specific workspace.
*/
private void copyScratchFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace)
throws IOException, InterruptedException {
final ClassLoader classLoader = getClass().getClassLoader();
FilePath targetFilePath = new FilePath(targetWorkspace, targetFile);
InputStream in = classLoader.getResourceAsStream(sourceFile);
targetFilePath.copyFrom(in);
// set executable permission
targetFilePath.chmod(0755);
}
}
Loading

0 comments on commit 4cb26c9

Please sign in to comment.