Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
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
61 changes: 52 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.4</version>
<version>3.57</version>
<relativePath />
</parent>
<artifactId>matlab</artifactId>
Expand All @@ -20,7 +20,7 @@
</developers>

<properties>
<jenkins.version>2.7.3</jenkins.version>
<jenkins.version>2.164.3</jenkins.version>
<java.level>8</java.level>
</properties>

Expand Down Expand Up @@ -52,14 +52,57 @@
<url>http://github.com/jenkinsci/matlab-plugin</url>
<tag>HEAD</tag>
</scm>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.164.x</artifactId>
<version>4</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.14</version>
</dependency>

<!-- Pipeline Step dependencies -->

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
</dependency>

<!-- Jenkins workflow test dependencies -->

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/mathworks/ci/MatlabCommandStepExecution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.mathworks.ci;

import java.io.IOException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.ProcStarter;
import hudson.model.Result;
import hudson.model.TaskListener;

public class MatlabCommandStepExecution extends StepExecution implements MatlabBuild {

private static final long serialVersionUID = 1957239693658914450L;

private String command;
private EnvVars env;


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

private String getCommand() {
return this.env == null ? this.command : this.env.expand(this.command );
}

private void setEnv(EnvVars env) {
this.env = env;
}

@Override
public boolean start() throws Exception {
final Launcher launcher = getContext().get(Launcher.class);
final FilePath workspace = getContext().get(FilePath.class);
final TaskListener listener = getContext().get(TaskListener.class);
final EnvVars env = getContext().get(EnvVars.class);
setEnv(env);

//Make sure the Workspace exists before run

workspace.mkdirs();

int res = execMatlabCommand(workspace, launcher, listener, env);

getContext().setResult((res == 0) ? Result.SUCCESS : Result.FAILURE);

getContext().onSuccess(true);

//return false represents the asynchronous run.
return false;
}

@Override
public void stop(Throwable cause) throws Exception {
getContext().onFailure(cause);
}

private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
final String uniqueCommandFile =
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
final FilePath uniqeTmpFolderPath =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);

// Create MATLAB script
createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener);
ProcStarter matlabLauncher;

try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
uniqueCommandFile, uniqueTmpFldrName);
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
listener.getLogger()
.println("#################### Starting command output ####################");
return matlabLauncher.pwd(uniqeTmpFolderPath).join();

} catch (Exception e) {
listener.getLogger().println(e.getMessage());
return 1;
} finally {
// Cleanup the tmp directory
if (uniqeTmpFolderPath.exists()) {
uniqeTmpFolderPath.deleteRecursive();
}
}
}

private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, 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");
final String matlabCommandFileContent =
"cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + getCommand();

// Display the commands on console output for users reference
listener.getLogger()
.println("Generating MATLAB script with content:\n" + getCommand() + "\n");

matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/mathworks/ci/MatlabRunTestsStepExecution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mathworks.ci;

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

import java.io.IOException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.ProcStarter;
import hudson.model.Result;
import hudson.model.TaskListener;

public class MatlabRunTestsStepExecution extends StepExecution implements MatlabBuild {

private static final long serialVersionUID = 6704588180717665100L;

private String command;


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

private String getCommand() {
return this.command;
}

@Override
public boolean start() throws Exception {
final Launcher launcher = getContext().get(Launcher.class);
final FilePath workspace = getContext().get(FilePath.class);
final TaskListener listener = getContext().get(TaskListener.class);
final EnvVars env = getContext().get(EnvVars.class);

//Make sure the Workspace exists before run

workspace.mkdirs();

int res = execMatlabCommand(workspace, launcher, listener, env);

getContext().setResult((res == 0) ? Result.SUCCESS : Result.FAILURE);

getContext().onSuccess(true);

//return false represents the asynchronous run.
return false;
}

@Override
public void stop(Throwable cause) throws Exception {
getContext().onFailure(cause);
}

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);


return matlabLauncher.pwd(workspace).join();
} catch (Exception e) {
listener.getLogger().println(e.getMessage());
return 1;
} finally {
// Cleanup the runner File from tmp directory
final FilePath matlabRunnerScript =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
if (matlabRunnerScript.exists()) {
matlabRunnerScript.deleteRecursive();
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private EnvVars getEnv() {
return this.env;
}

@Symbol("RunMatlabCommand")

@Extension
public static class RunMatlabCommandDescriptor extends BuildStepDescriptor<Builder> {

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/mathworks/ci/RunMatlabCommandStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.mathworks.ci;

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

import java.util.Set;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import com.google.common.collect.ImmutableSet;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;

public class RunMatlabCommandStep extends Step {


private String command;

@DataBoundConstructor
public RunMatlabCommandStep(String command) {
this.command = command;
}


public String getCommand() {
return this.command;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new MatlabCommandStepExecution(context, getCommand());
}

@Extension
public static class CommandStepDescriptor extends StepDescriptor {

@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(TaskListener.class, FilePath.class, Launcher.class,
EnvVars.class, Run.class);
}

@Override
public String getFunctionName() {
return Message.getValue("matlab.command.build.step.name");
}

@Override
public String getDisplayName() {
return Message.getValue("matlab.command.step.display.name");
}
}
}


2 changes: 1 addition & 1 deletion src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected Object readResolve() {
}


@Symbol("RunMatlabTests")

@Extension
public static class RunMatlabTestsDescriptor extends BuildStepDescriptor<Builder> {

Expand Down
Loading