2020import org .kohsuke .stapler .DataBoundSetter ;
2121import org .kohsuke .stapler .QueryParameter ;
2222import org .kohsuke .stapler .StaplerRequest ;
23- import com .mathworks .ci .AddMatlabToPathBuildWrapper .AddMatlabToPathDescriptor ;
2423import hudson .EnvVars ;
2524import hudson .Extension ;
2625import hudson .FilePath ;
3332import hudson .tasks .BuildStepDescriptor ;
3433import hudson .tasks .Builder ;
3534import hudson .util .FormValidation ;
36- import hudson .util .FormValidation .Kind ;
37- import jenkins .model .Jenkins ;
3835import jenkins .tasks .SimpleBuildStep ;
3936import net .sf .json .JSONObject ;
4037
@@ -155,14 +152,18 @@ public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends Abstra
155152 /*
156153 * Validation for Test artifact generator checkBoxes
157154 */
155+
156+ //Get the MATLAB root entered in build wrapper descriptor
157+
158+
158159
159160 public FormValidation doCheckCoberturaChkBx (@ QueryParameter boolean coberturaChkBx ) {
160161 List <Function <String , FormValidation >> listOfCheckMethods =
161162 new ArrayList <Function <String , FormValidation >>();
162163 if (coberturaChkBx ) {
163164 listOfCheckMethods .add (chkCoberturaSupport );
164165 }
165- return getFirstErrorOrWarning (listOfCheckMethods );
166+ return FormValidationUtil . getFirstErrorOrWarning (listOfCheckMethods );
166167 }
167168
168169 Function <String , FormValidation > chkCoberturaSupport = (String matlabRoot ) -> {
@@ -190,7 +191,7 @@ public FormValidation doCheckModelCoverageChkBx(@QueryParameter boolean modelCov
190191 if (modelCoverageChkBx ) {
191192 listOfCheckMethods .add (chkModelCoverageSupport );
192193 }
193- return getFirstErrorOrWarning (listOfCheckMethods );
194+ return FormValidationUtil . getFirstErrorOrWarning (listOfCheckMethods );
194195 }
195196
196197 Function <String , FormValidation > chkModelCoverageSupport = (String matlabRoot ) -> {
@@ -218,7 +219,7 @@ public FormValidation doCheckStmResultsChkBx(@QueryParameter boolean stmResultsC
218219 if (stmResultsChkBx ) {
219220 listOfCheckMethods .add (chkSTMResultsSupport );
220221 }
221- return getFirstErrorOrWarning (listOfCheckMethods );
222+ return FormValidationUtil . getFirstErrorOrWarning (listOfCheckMethods );
222223 }
223224
224225 Function <String , FormValidation > chkSTMResultsSupport = (String matlabRoot ) -> {
@@ -235,47 +236,33 @@ public FormValidation doCheckStmResultsChkBx(@QueryParameter boolean stmResultsC
235236 return FormValidation .warning (Message .getValue ("Builder.invalid.matlab.root.warning" ));
236237 }
237238 }
238-
239-
240239 return FormValidation .ok ();
241240 };
242-
243- public FormValidation getFirstErrorOrWarning (
244- List <Function <String , FormValidation >> validations ) {
245- if (validations == null || validations .isEmpty ())
246- return FormValidation .ok ();
247- try {
248- final String matlabRoot = Jenkins .getInstance ()
249- .getDescriptorByType (AddMatlabToPathDescriptor .class ).getMatlabRootFolder ();
250- for (Function <String , FormValidation > val : validations ) {
251- FormValidation validationResult = val .apply (matlabRoot );
252- if (validationResult .kind .compareTo (Kind .ERROR ) == 0
253- || validationResult .kind .compareTo (Kind .WARNING ) == 0 ) {
254- return validationResult ;
255- }
256- }
257- }catch (Exception e ) {
258- return FormValidation .warning (Message .getValue ("Builder.invalid.matlab.root.warning" ));
259- }
260-
261- return FormValidation .ok ();
262- }
263- }
241+ }
264242
265243 @ Override
266244 public void perform (@ Nonnull Run <?, ?> build , @ Nonnull FilePath workspace ,
267245 @ Nonnull Launcher launcher , @ Nonnull TaskListener listener )
268246 throws InterruptedException , IOException {
269- //Set the environment variable specific to the this build
270- setEnv (build .getEnvironment (listener ));
271-
272- // Invoke MATLAB command and transfer output to standard
273- // Output Console
274247
275- buildResult = execMatlabCommand (workspace , launcher , listener , getEnv ());
248+ try {
249+ // Set the environment variable specific to the this build
250+ setEnv (build .getEnvironment (listener ));
276251
277- if (buildResult != 0 ) {
278- build .setResult (Result .FAILURE );
252+ // Invoke MATLAB command and transfer output to standard
253+ // Output Console
254+
255+ buildResult = execMatlabCommand (workspace , launcher , listener , getEnv ());
256+
257+ if (buildResult != 0 ) {
258+ build .setResult (Result .FAILURE );
259+ }
260+ } finally {
261+ // Cleanup the runner File from tmp directory
262+ FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript (launcher );
263+ if (matlabRunnerScript .exists ()) {
264+ matlabRunnerScript .delete ();
265+ }
279266 }
280267 }
281268
@@ -298,22 +285,21 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
298285 }
299286
300287 public String constructCommandForTest (String inputArguments ) {
301- String runCommand ;
302- String matlabFunctionName = FilenameUtils .removeExtension (
288+ final String matlabFunctionName = FilenameUtils .removeExtension (
303289 Message .getValue (MatlabBuilderConstants .MATLAB_RUNNER_TARGET_FILE ));
304- runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))" ;
290+ final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))" ;
305291 return runCommand ;
306292 }
307293
308294 // Concatenate the input arguments
309295 private String getInputArguments () {
310- String pdfReport = MatlabBuilderConstants .PDF_REPORT + "," + this .getPdfReportChkBx ();
311- String tapResults = MatlabBuilderConstants .TAP_RESULTS + "," + this .getTapChkBx ();
312- String junitResults = MatlabBuilderConstants .JUNIT_RESULTS + "," + this .getJunitChkBx ();
313- String stmResults = MatlabBuilderConstants .STM_RESULTS + "," + this .getStmResultsChkBx ();
314- String coberturaCodeCoverage = MatlabBuilderConstants .COBERTURA_CODE_COVERAGE + "," + this .getCoberturaChkBx ();
315- String coberturaModelCoverage = MatlabBuilderConstants .COBERTURA_MODEL_COVERAGE + "," + this .getModelCoverageChkBx ();
316- String inputArgsToMatlabFcn = pdfReport + "," + tapResults + "," + junitResults + ","
296+ final String pdfReport = MatlabBuilderConstants .PDF_REPORT + "," + this .getPdfReportChkBx ();
297+ final String tapResults = MatlabBuilderConstants .TAP_RESULTS + "," + this .getTapChkBx ();
298+ final String junitResults = MatlabBuilderConstants .JUNIT_RESULTS + "," + this .getJunitChkBx ();
299+ final String stmResults = MatlabBuilderConstants .STM_RESULTS + "," + this .getStmResultsChkBx ();
300+ final String coberturaCodeCoverage = MatlabBuilderConstants .COBERTURA_CODE_COVERAGE + "," + this .getCoberturaChkBx ();
301+ final String coberturaModelCoverage = MatlabBuilderConstants .COBERTURA_MODEL_COVERAGE + "," + this .getModelCoverageChkBx ();
302+ final String inputArgsToMatlabFcn = pdfReport + "," + tapResults + "," + junitResults + ","
317303 + stmResults + "," + coberturaCodeCoverage + "," + coberturaModelCoverage ;
318304
319305 return inputArgsToMatlabFcn ;
0 commit comments