1+ package com .mathworks .ci ;
2+
3+ import java .io .IOException ;
4+ import org .jenkinsci .plugins .workflow .steps .StepContext ;
5+ import org .jenkinsci .plugins .workflow .steps .StepExecution ;
6+ import hudson .EnvVars ;
7+ import hudson .FilePath ;
8+ import hudson .Launcher ;
9+ import hudson .Launcher .ProcStarter ;
10+ import hudson .model .Result ;
11+ import hudson .model .TaskListener ;
12+
13+ public class MatlabCommandStepExecution extends StepExecution implements MatlabBuild {
14+
15+ private static final long serialVersionUID = 1957239693658914450L ;
16+
17+ private String command ;
18+ private EnvVars env ;
19+
20+
21+ public MatlabCommandStepExecution (StepContext context , String command ) {
22+ super (context );
23+ this .command = command ;
24+ }
25+
26+ private String getCommand () {
27+ return this .env == null ? this .command : this .env .expand (this .command );
28+ }
29+
30+ private void setEnv (EnvVars env ) {
31+ this .env = env ;
32+ }
33+
34+ @ Override
35+ public boolean start () throws Exception {
36+ final Launcher launcher = getContext ().get (Launcher .class );
37+ final FilePath workspace = getContext ().get (FilePath .class );
38+ final TaskListener listener = getContext ().get (TaskListener .class );
39+ final EnvVars env = getContext ().get (EnvVars .class );
40+ setEnv (env );
41+
42+ //Make sure the Workspace exists before run
43+
44+ workspace .mkdirs ();
45+
46+ int res = execMatlabCommand (workspace , launcher , listener , env );
47+
48+ getContext ().setResult ((res == 0 ) ? Result .SUCCESS : Result .FAILURE );
49+
50+ getContext ().onSuccess (true );
51+
52+ //return false represents the asynchronous run.
53+ return false ;
54+ }
55+
56+ @ Override
57+ public void stop (Throwable cause ) throws Exception {
58+ getContext ().onFailure (cause );
59+ }
60+
61+ private synchronized int execMatlabCommand (FilePath workspace , Launcher launcher ,
62+ TaskListener listener , EnvVars envVars ) throws IOException , InterruptedException {
63+ final String uniqueTmpFldrName = getUniqueNameForRunnerFile ();
64+ final String uniqueCommandFile =
65+ "command_" + getUniqueNameForRunnerFile ().replaceAll ("-" , "_" );
66+ final FilePath uniqeTmpFolderPath =
67+ getFilePathForUniqueFolder (launcher , uniqueTmpFldrName , workspace );
68+
69+ // Create MATLAB script
70+ createMatlabScriptByName (uniqeTmpFolderPath , uniqueCommandFile , workspace , listener );
71+ ProcStarter matlabLauncher ;
72+
73+ try {
74+ matlabLauncher = getProcessToRunMatlabCommand (workspace , launcher , listener , envVars ,
75+ uniqueCommandFile , uniqueTmpFldrName );
76+ launcher .launch ().pwd (uniqeTmpFolderPath ).envs (envVars );
77+ listener .getLogger ()
78+ .println ("#################### Starting command output ####################" );
79+ return matlabLauncher .pwd (uniqeTmpFolderPath ).join ();
80+
81+ } catch (Exception e ) {
82+ listener .getLogger ().println (e .getMessage ());
83+ return 1 ;
84+ } finally {
85+ // Cleanup the tmp directory
86+ if (uniqeTmpFolderPath .exists ()) {
87+ uniqeTmpFolderPath .deleteRecursive ();
88+ }
89+ }
90+ }
91+
92+ private void createMatlabScriptByName (FilePath uniqeTmpFolderPath , String uniqueScriptName , FilePath workspace , TaskListener listener ) throws IOException , InterruptedException {
93+
94+ // Create a new command runner script in the temp folder.
95+ final FilePath matlabCommandFile =
96+ new FilePath (uniqeTmpFolderPath , uniqueScriptName + ".m" );
97+ final String matlabCommandFileContent =
98+ "cd '" + workspace .getRemote ().replaceAll ("'" , "''" ) + "';\n " + getCommand ();
99+
100+ // Display the commands on console output for users reference
101+ listener .getLogger ()
102+ .println ("Generating MATLAB script with content:\n " + getCommand () + "\n " );
103+
104+ matlabCommandFile .write (matlabCommandFileContent , "UTF-8" );
105+ }
106+ }
0 commit comments