Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary update #250

Merged
merged 21 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
455 changes: 238 additions & 217 deletions pom.xml

Large diffs are not rendered by default.

48 changes: 34 additions & 14 deletions src/main/java/com/mathworks/ci/MatlabBuild.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.mathworks.ci;
/**
* Copyright 2019-2020 The MathWorks, Inc.
* Copyright 2019-2023 The MathWorks, Inc.
*
* Build Interface has two default methods. MATLAB builders can override the default behavior.
*
*/

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import org.apache.commons.lang.RandomStringUtils;
import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -29,28 +30,47 @@ public interface MatlabBuild {
* @return matlabLauncher returns the process launcher to run MATLAB commands
*/
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String startupOpts, String uniqueName)
throws IOException, InterruptedException {
// Get node specific temp .matlab directory to copy matlab runner script
FilePath targetWorkspace = new FilePath(launcher.getChannel(),
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
FilePath targetWorkspace;
ProcStarter matlabLauncher;
if (launcher.isUnix()) {
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
targetWorkspace = new FilePath(launcher.getChannel(),
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);

// Determine whether we're on Mac on Linux
ByteArrayOutputStream kernelStream = new ByteArrayOutputStream();
launcher.launch()
.cmds("uname")
.masks(true)
.stdout(kernelStream)
.join();

String binaryName;
String runnerName = uniqueName + "/run-matlab-command";
if (kernelStream.toString("UTF-8").contains("Linux")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine, but I'm more used to special casing to mac and defaulting to Linux rather than visa versa. Not a huge deal though

binaryName = "glnxa64/run-matlab-command";
} else {
binaryName = "maci64/run-matlab-command";
}

matlabLauncher = launcher.launch().envs(envVars);
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerScriptName, matlabCommand).stdout(listener);
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerName, matlabCommand, startupOpts).stdout(listener);

// Copy runner .sh for linux platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
targetWorkspace);
// Copy runner for linux platform in workspace.
copyFileInWorkspace(binaryName, runnerName, targetWorkspace);
} else {
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
targetWorkspace = new FilePath(launcher.getChannel(),
workspace.getRemote() + "\\" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);

final String runnerName = uniqueName + "\\run-matlab-command.exe";
matlabLauncher = launcher.launch().envs(envVars);
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
matlabLauncher.cmds(targetWorkspace.toString() + "\\" + runnerName, "\"" + matlabCommand + "\"", startupOpts)
.stdout(listener);
// Copy runner.bat for Windows platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,

// Copy runner for Windows platform in workspace.
copyFileInWorkspace("win64\\run-matlab-command.exe", runnerName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer static strings to be part of either properties or Constants just in cas they are changed in future, Its more manageable that way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think that this is a good suggestion. The part that makes me hesitate here is that we're already having to hardcode in the program name on lines 54, 57, and 69. I don't really like keeping that information in two separate places, since it means if the program name were to change it would be easy for a user to just change the constant name and miss the fact that there are also hardcoded instances.

I don't have a strong preference either way, it just feels a little bit odd to me to have a property in another file define one instance and a hardcoded string define another. There's also a slight readability sacrifice when future contributors have to cross reference between the file they're working on and the constants file but that's not a real issue in the long run.

Also I realize I left a line of code commented out, I'll update the pull request to remove that (and also probably condense lines 54 and 57 to be one line after the if block).

What are your thoughts @davidbuzinski?

Copy link
Member

@davidbuzinski davidbuzinski Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion unless it's being used all over the code. I think it's basically just in 1 or 2 places right? I'm fine either way

Copy link
Member Author

@sameagen-MW sameagen-MW Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, iirc that's the only place that specific property is used

targetWorkspace);
}
return matlabLauncher;
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/mathworks/ci/MatlabBuildStepExecution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mathworks.ci;

/**
* Copyright 2022 The MathWorks, Inc.
* Copyright 2022-2023 The MathWorks, Inc.
*
*/

Expand All @@ -20,10 +20,12 @@ public class MatlabBuildStepExecution extends SynchronousNonBlockingStepExecutio
private static final long serialVersionUID = 4771831219402275744L;

private String tasks;
private String startupOptions;

public MatlabBuildStepExecution(StepContext context, String tasks) {
public MatlabBuildStepExecution(StepContext context, String tasks, String startupOptions) {
super(context);
this.tasks = tasks;
this.startupOptions = startupOptions;
}

private String getTasks() {
Expand Down Expand Up @@ -71,7 +73,7 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,

try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'); "+ uniqueBuildFile, uniqueTmpFldrName);
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'); "+ uniqueBuildFile, startupOptions, uniqueTmpFldrName);
listener.getLogger()
.println("#################### Starting command output ####################");
return matlabLauncher.pwd(workspace).join();
Expand All @@ -84,11 +86,11 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
}
}

private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {

// Create a new command runner script in the temp folder.
final FilePath matlabBuildFile =
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
new FilePath(uniqueTmpFolderPath, uniqueScriptName + ".m");
final String tasks = getContext().get(EnvVars.class).expand(getTasks());
String cmd = "buildtool";

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/mathworks/ci/MatlabBuilderConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public class MatlabBuilderConstants {
static final String STM_RESULTS = "'SimulinkTestResults'";
static final String COBERTURA_CODE_COVERAGE = "'CoberturaCodeCoverage'";
static final String COBERTURA_MODEL_COVERAGE = "'CoberturaModelCoverage'";


// 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";
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/mathworks/ci/MatlabCommandStepExecution.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.mathworks.ci;

/**
* Copyright 2023 The MathWorks, Inc.
*
*/

import java.io.IOException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
Expand All @@ -15,11 +20,12 @@ public class MatlabCommandStepExecution extends SynchronousNonBlockingStepExecut
private static final long serialVersionUID = 1957239693658914450L;

private String command;
private String startupOptions;


public MatlabCommandStepExecution(StepContext context, String command) {
public MatlabCommandStepExecution(StepContext context, String command, String startupOptions) {
super(context);
this.command = command;
this.startupOptions = startupOptions;
}

private String getCommand() {
Expand Down Expand Up @@ -55,6 +61,7 @@ public void stop(Throwable cause) throws Exception {

private int execMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {

final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
final String uniqueCommandFile =
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
Expand All @@ -67,7 +74,7 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,

try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'); "+ uniqueCommandFile, uniqueTmpFldrName);
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'); "+ uniqueCommandFile, startupOptions, uniqueTmpFldrName);
listener.getLogger()
.println("#################### Starting command output ####################");
return matlabLauncher.pwd(workspace).join();
Expand All @@ -80,11 +87,11 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
}
}

private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {

// Create a new command runner script in the temp folder.
final FilePath matlabCommandFile =
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
new FilePath(uniqueTmpFolderPath, uniqueScriptName + ".m");
final String cmd = getContext().get(EnvVars.class).expand(getCommand());
final String matlabCommandFileContent =
"cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + cmd;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mathworks.ci;

/**
* Copyright 2020 The MathWorks, Inc.
* Copyright 2020-2023 The MathWorks, Inc.
*
*/

Expand All @@ -20,11 +20,12 @@ public class MatlabRunTestsStepExecution extends SynchronousNonBlockingStepExecu
private static final long serialVersionUID = 6704588180717665100L;

private String commandArgs;
private String startupOptions;


public MatlabRunTestsStepExecution(StepContext context, String commandArgs) {
public MatlabRunTestsStepExecution(StepContext context, String commandArgs, String startupOptions) {
super(context);
this.commandArgs = commandArgs;
this.startupOptions = startupOptions;
}

private String getCommandArgs() {
Expand Down Expand Up @@ -73,7 +74,7 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
cmdPrefix + matlabScriptName + ",delete('.matlab/"
+ genScriptLocation.getBaseName() + "/" + matlabScriptName
+ ".m'),runnerScript,rmdir(tmpDir,'s')",
uniqueMatlabResourceFldr);
startupOptions, uniqueMatlabResourceFldr);

// prepare temp folder by coping genscript package and writing runner script.
prepareTmpFldr(genScriptLocation,
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/com/mathworks/ci/RunMatlabBuildBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mathworks.ci;

/**
* Copyright 2022 The MathWorks, Inc.
* Copyright 2022-2023 The MathWorks, Inc.
*
*/

Expand All @@ -22,12 +22,14 @@
import hudson.model.Computer;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.Util;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;

public class RunMatlabBuildBuilder extends Builder implements SimpleBuildStep, MatlabBuild {
private int buildResult;
private String tasks;
private StartupOptions startupOptions;

@DataBoundConstructor
public RunMatlabBuildBuilder() {}
Expand All @@ -38,9 +40,18 @@ public void setTasks(String tasks) {
this.tasks = tasks;
}

@DataBoundSetter
public void setStartupOptions(StartupOptions startupOptions) {
this.startupOptions = startupOptions;
}

public String getTasks() {
return this.tasks;
}

public StartupOptions getStartupOptions() {
return this.startupOptions;
}

@Extension
public static class RunMatlabBuildDescriptor extends BuildStepDescriptor<Builder> {
Expand Down Expand Up @@ -102,16 +113,16 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
final String uniqueBuildFile =
"build_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
final FilePath uniqeTmpFolderPath =
final FilePath uniqueTmpFolderPath =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);

// Create MATLAB script
createMatlabScriptByName(uniqeTmpFolderPath, uniqueBuildFile, workspace, listener, envVars);
createMatlabScriptByName(uniqueTmpFolderPath, uniqueBuildFile, workspace, listener, envVars);
ProcStarter matlabLauncher;

String options = getStartupOptions() == null ? "" : getStartupOptions().getOptions();
try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
"cd('"+ uniqeTmpFolderPath.getRemote().replaceAll("'", "''") +"');"+ uniqueBuildFile, uniqueTmpFldrName);
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"');"+ uniqueBuildFile, options, uniqueTmpFldrName);

listener.getLogger()
.println("#################### Starting command output ####################");
Expand All @@ -122,17 +133,17 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
return 1;
} finally {
// Cleanup the tmp directory
if (uniqeTmpFolderPath.exists()) {
uniqeTmpFolderPath.deleteRecursive();
if (uniqueTmpFolderPath.exists()) {
uniqueTmpFolderPath.deleteRecursive();
}
}
}

private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {

// Create a new command runner script in the temp folder.
final FilePath matlabCommandFile =
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
new FilePath(uniqueTmpFolderPath, uniqueScriptName + ".m");
final String tasks = envVars.expand(getTasks());
String cmd = "buildtool";

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/mathworks/ci/RunMatlabBuildStep.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mathworks.ci;

/**
* Copyright 2022 The MathWorks, Inc.
* Copyright 2022-2023 The MathWorks, Inc.
*
*/

Expand All @@ -24,6 +24,7 @@
public class RunMatlabBuildStep extends Step {

private String tasks;
private String startupOptions;

@DataBoundConstructor
public RunMatlabBuildStep() {
Expand All @@ -34,14 +35,23 @@ public String getTasks() {
return Util.fixNull(tasks);
}

public String getStartupOptions() {
return Util.fixNull(startupOptions);
}

@DataBoundSetter
public void setTasks(String tasks) {
this.tasks = tasks;
}

@DataBoundSetter
public void setStartupOptions(String startupOptions) {
this.startupOptions = startupOptions;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new MatlabBuildStepExecution(context, getTasks());
return new MatlabBuildStepExecution(context, getTasks(), getStartupOptions());
}

@Extension
Expand Down
Loading