@@ -53,7 +53,8 @@ public class MatlabBuilder extends Builder implements SimpleBuildStep {
53
53
private static final double BASE_MATLAB_VERSION_COBERTURA_SUPPORT = 9.3 ;
54
54
private int buildResult ;
55
55
private TestRunTypeList testRunTypeList ;
56
- private String localMatlab ;
56
+ private String matlabRoot ;
57
+ private EnvVars env ;
57
58
private static final String MATLAB_RUNNER_TARGET_FILE =
58
59
"Builder.matlab.runner.target.file.name" ;
59
60
private static final String MATLAB_RUNNER_RESOURCE =
@@ -70,35 +71,47 @@ public MatlabBuilder() {
70
71
// Getter and Setters to access local members
71
72
72
73
@ DataBoundSetter
73
- public void setLocalMatlab (String localMatlab ) {
74
- this .localMatlab = localMatlab ;
74
+ public void setMatlabRoot (String matlabRoot ) {
75
+ this .matlabRoot = matlabRoot ;
75
76
}
76
77
77
78
@ DataBoundSetter
78
79
public void setTestRunTypeList (TestRunTypeList testRunTypeList ) {
79
80
this .testRunTypeList = testRunTypeList ;
80
81
}
81
82
82
- public String getLocalMatlab () {
83
+ public String getMatlabRoot () {
83
84
84
- return this .localMatlab ;
85
+ return this .matlabRoot ;
85
86
}
86
87
87
88
public TestRunTypeList getTestRunTypeList () {
88
89
return this .testRunTypeList ;
89
90
}
91
+
92
+ private String getLocalMatlab () {
93
+ return this .env == null ? getLocalMatlab (): this .env .expand (getMatlabRoot ());
94
+ }
95
+
96
+ private String getCustomMatlabCommand () {
97
+ return this .env == null ? this .getTestRunTypeList ().getStringByName ("customMatlabCommand" )
98
+ : this .env .expand (this .getTestRunTypeList ().getStringByName ("customMatlabCommand" ));
99
+ }
100
+ private void setEnv (EnvVars env ) {
101
+ this .env = env ;
102
+ }
90
103
91
104
@ Extension
92
105
public static class MatlabDescriptor extends BuildStepDescriptor <Builder > {
93
106
MatlabReleaseInfo rel ;
94
- String localMatlab ;
107
+ String matlabRoot ;
95
108
96
- public String getLocalMatlab () {
97
- return localMatlab ;
109
+ public String getMatlabRoot () {
110
+ return matlabRoot ;
98
111
}
99
112
100
- public void setLocalMatlab (String localMatlab ) {
101
- this .localMatlab = localMatlab ;
113
+ public void setMatlabRoot (String matlabRoot ) {
114
+ this .matlabRoot = matlabRoot ;
102
115
}
103
116
104
117
// Overridden Method used to show the text under build dropdown
@@ -141,22 +154,22 @@ public DescriptorExtensionList<TestRunTypeList, Descriptor<TestRunTypeList>> get
141
154
*/
142
155
143
156
144
- public FormValidation doCheckLocalMatlab (@ QueryParameter String localMatlab ) {
145
- setLocalMatlab ( localMatlab );
157
+ public FormValidation doCheckMatlabRoot (@ QueryParameter String matlabRoot ) {
158
+ setMatlabRoot ( matlabRoot );
146
159
List <Function <String , FormValidation >> listOfCheckMethods =
147
160
new ArrayList <Function <String , FormValidation >>();
148
161
listOfCheckMethods .add (chkMatlabEmpty );
149
162
listOfCheckMethods .add (chkMatlabSupportsRunTests );
150
163
151
- return getFirstErrorOrWarning (listOfCheckMethods , localMatlab );
164
+ return getFirstErrorOrWarning (listOfCheckMethods , matlabRoot );
152
165
}
153
166
154
167
public FormValidation getFirstErrorOrWarning (
155
- List <Function <String , FormValidation >> validations , String localMatalb ) {
168
+ List <Function <String , FormValidation >> validations , String matlabRoot ) {
156
169
if (validations == null || validations .isEmpty ())
157
170
return FormValidation .ok ();
158
171
for (Function <String , FormValidation > val : validations ) {
159
- FormValidation validationResult = val .apply (localMatalb );
172
+ FormValidation validationResult = val .apply (matlabRoot );
160
173
if (validationResult .kind .compareTo (Kind .ERROR ) == 0
161
174
|| validationResult .kind .compareTo (Kind .WARNING ) == 0 ) {
162
175
return validationResult ;
@@ -165,22 +178,26 @@ public FormValidation getFirstErrorOrWarning(
165
178
return FormValidation .ok ();
166
179
}
167
180
168
- Function <String , FormValidation > chkMatlabEmpty = (String localMatlab ) -> {
169
- if (localMatlab .isEmpty ()) {
181
+ Function <String , FormValidation > chkMatlabEmpty = (String matlabRoot ) -> {
182
+ if (matlabRoot .isEmpty ()) {
170
183
return FormValidation .error (Message .getValue ("Builder.matlab.root.empty.error" ));
171
184
}
172
185
return FormValidation .ok ();
173
186
};
174
-
175
- Function <String , FormValidation > chkMatlabSupportsRunTests = (String localMatlab ) -> {
176
- try {
177
- rel = new MatlabReleaseInfo (localMatlab );
178
- if (rel .verLessThan (BASE_MATLAB_VERSION_RUNTESTS_SUPPORT )) {
187
+
188
+ Function <String , FormValidation > chkMatlabSupportsRunTests = (String matlabRoot ) -> {
189
+ final MatrixPatternResolver resolver = new MatrixPatternResolver (matlabRoot );
190
+ if (!resolver .hasVariablePattern ()) {
191
+ try {
192
+ rel = new MatlabReleaseInfo (matlabRoot );
193
+ if (rel .verLessThan (BASE_MATLAB_VERSION_RUNTESTS_SUPPORT )) {
194
+ return FormValidation
195
+ .error (Message .getValue ("Builder.matlab.test.support.error" ));
196
+ }
197
+ } catch (MatlabVersionNotFoundException e ) {
179
198
return FormValidation
180
- .error (Message .getValue ("Builder.matlab.test.support .error" ));
199
+ .error (Message .getValue ("Builder.invalid. matlab.root .error" ));
181
200
}
182
- } catch (MatlabVersionNotFoundException e ) {
183
- return FormValidation .error (Message .getValue ("Builder.invalid.matlab.root.error" ));
184
201
}
185
202
return FormValidation .ok ();
186
203
};
@@ -229,25 +246,29 @@ public static abstract class TestRunTypeDescriptor extends Descriptor<TestRunTyp
229
246
public FormValidation doCheckTaCoberturaChkBx (@ QueryParameter boolean taCoberturaChkBx ) {
230
247
List <Function <String , FormValidation >> listOfCheckMethods =
231
248
new ArrayList <Function <String , FormValidation >>();
232
- final String localMatlab = Jenkins .getInstance ()
233
- .getDescriptorByType (MatlabDescriptor .class ).getLocalMatlab ();
249
+ final String matlabRoot = Jenkins .getInstance ()
250
+ .getDescriptorByType (MatlabDescriptor .class ).getMatlabRoot ();
234
251
if (taCoberturaChkBx ) {
235
252
listOfCheckMethods .add (chkCoberturaSupport );
236
253
}
237
254
return Jenkins .getInstance ().getDescriptorByType (MatlabDescriptor .class )
238
- .getFirstErrorOrWarning (listOfCheckMethods , localMatlab );
239
- }
240
-
241
- Function <String , FormValidation > chkCoberturaSupport = (String localMatlab ) -> {
242
- rel = new MatlabReleaseInfo (localMatlab );
243
- try {
244
- if (rel .verLessThan (BASE_MATLAB_VERSION_COBERTURA_SUPPORT )) {
245
- return FormValidation
246
- .warning (Message .getValue ("Builder.matlab.cobertura.support.warning" ));
255
+ .getFirstErrorOrWarning (listOfCheckMethods , matlabRoot );
256
+ }
257
+
258
+ Function <String , FormValidation > chkCoberturaSupport = (String matlabRoot ) -> {
259
+ rel = new MatlabReleaseInfo (matlabRoot );
260
+ final MatrixPatternResolver resolver = new MatrixPatternResolver (matlabRoot );
261
+ if (!resolver .hasVariablePattern ()) {
262
+ try {
263
+ if (rel .verLessThan (BASE_MATLAB_VERSION_COBERTURA_SUPPORT )) {
264
+ return FormValidation
265
+ .warning (Message .getValue ("Builder.matlab.cobertura.support.warning" ));
266
+ }
267
+ } catch (MatlabVersionNotFoundException e ) {
268
+ return FormValidation .error (Message .getValue ("Builder.invalid.matlab.root.error" ));
247
269
}
248
- } catch (MatlabVersionNotFoundException e ) {
249
- return FormValidation .error (Message .getValue ("Builder.invalid.matlab.root.error" ));
250
270
}
271
+
251
272
252
273
return FormValidation .ok ();
253
274
};
@@ -370,30 +391,30 @@ public String getStringByName(String memberName) {
370
391
@ Override
371
392
public void perform (@ Nonnull Run <?, ?> build , @ Nonnull FilePath workspace ,
372
393
@ Nonnull Launcher launcher , @ Nonnull TaskListener listener )
373
- throws InterruptedException , IOException {
374
- final EnvVars env = build .getEnvironment (listener );
394
+ throws InterruptedException , IOException {
375
395
final boolean isLinuxLauncher = launcher .isUnix ();
376
396
377
397
// Invoke MATLAB command and transfer output to standard
378
398
// Output Console
379
399
380
- buildResult = execMatlabCommand (build , workspace , launcher , listener , env , isLinuxLauncher );
400
+ buildResult = execMatlabCommand (build , workspace , launcher , listener , isLinuxLauncher );
381
401
382
402
if (buildResult != 0 ) {
383
403
build .setResult (Result .FAILURE );
384
404
}
385
405
}
386
406
387
- private int execMatlabCommand (Run <?, ?> build , FilePath workspace , Launcher launcher ,
388
- TaskListener listener , EnvVars env , boolean isLinuxLauncher )
407
+ private synchronized int execMatlabCommand (Run <?, ?> build , FilePath workspace , Launcher launcher ,
408
+ TaskListener listener , boolean isLinuxLauncher )
389
409
throws IOException , InterruptedException {
410
+ setEnv (build .getEnvironment (listener ));
390
411
//Copy MATLAB scratch file into the workspace
391
412
copyMatlabScratchFileInWorkspace (MATLAB_RUNNER_RESOURCE , MATLAB_RUNNER_TARGET_FILE ,
392
413
workspace , getClass ().getClassLoader ());
393
414
ProcStarter matlabLauncher ;
394
415
try {
395
416
MatlabReleaseInfo rel = new MatlabReleaseInfo (getLocalMatlab ());
396
- matlabLauncher = launcher .launch ().pwd (workspace ).envs (env );
417
+ matlabLauncher = launcher .launch ().pwd (workspace ).envs (this . env );
397
418
if (rel .verLessThan (BASE_MATLAB_VERSION_BATCH_SUPPORT )) {
398
419
ListenerLogDecorator outStream = new ListenerLogDecorator (listener );
399
420
matlabLauncher = matlabLauncher .cmds (constructDefaultMatlabCommand (isLinuxLauncher )).stderr (outStream );
@@ -421,13 +442,13 @@ public List<String> constructMatlabCommandWithBatch() {
421
442
+ getTestRunTypeList ().getBooleanByName ("taCoberturaChkBx" ) + "))" ;
422
443
} else {
423
444
424
- runCommand = this . getTestRunTypeList (). getStringByName ( "customMatlabCommand" );
445
+ runCommand = getCustomMatlabCommand ( );
425
446
}
426
447
427
448
matlabDefaultArgs =
428
- Arrays .asList (this . localMatlab + File .separator + "bin" + File .separator + "matlab" ,
449
+ Arrays .asList (getLocalMatlab () + File .separator + "bin" + File .separator + "matlab" ,
429
450
"-batch" , runCommand );
430
-
451
+
431
452
return matlabDefaultArgs ;
432
453
}
433
454
@@ -448,7 +469,7 @@ public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher) {
448
469
449
470
private String [] getPreRunnerSwitches () {
450
471
String [] preRunnerSwitches =
451
- {this . localMatlab + File .separator + "bin" + File .separator + "matlab" , "-nosplash" ,
472
+ {getLocalMatlab () + File .separator + "bin" + File .separator + "matlab" , "-nosplash" ,
452
473
"-nodesktop" , "-noAppIcon" };
453
474
return preRunnerSwitches ;
454
475
}
@@ -471,7 +492,7 @@ private String[] getRunnerSwitch() {
471
492
+ getTestRunTypeList ().getBooleanByName ("taCoberturaChkBx" )
472
493
+ ")),catch e,disp(getReport(e,'extended')),exit(1),end" ;
473
494
} else {
474
- runCommand = "try,eval(\" " + this . getTestRunTypeList (). getStringByName ( "customMatlabCommand" ).replaceAll ("\" " ,"\" \" " )
495
+ runCommand = "try,eval(\" " + getCustomMatlabCommand ( ).replaceAll ("\" " ,"\" \" " )
475
496
+ "\" ),catch e,disp(getReport(e,'extended')),exit(1),end,exit" ;
476
497
}
477
498
0 commit comments