66 * builders. Author : Nikhil Bhoski email : [email protected] Date : 11/02/2020 77 */
88
9- import java .util .ArrayList ;
10- import java .util .Arrays ;
11- import java .util .Collections ;
12- import java .util .List ;
9+ import java .io .IOException ;
10+ import java .io .InputStream ;
1311import org .apache .commons .io .FilenameUtils ;
14- import org .apache .commons .lang .ArrayUtils ;
1512import hudson .FilePath ;
1613import hudson .Launcher ;
1714
@@ -31,7 +28,7 @@ public void setLauncher(Launcher launcher) {
3128 public String getMatlabRoot () {
3229 return matlabRoot ;
3330 }
34-
31+
3532
3633 /*
3734 * Constructor to accepts the current launcher instance of the build with two parameters
@@ -43,90 +40,28 @@ public CommandConstructUtil(Launcher launcher, String matlabRoot) {
4340 this .launcher = launcher ;
4441 this .matlabRoot = matlabRoot ;
4542 }
43+
44+ /*
45+ * New set of methods for new changes with run matlab command script
46+ */
4647
47- public List < String > constructBatchCommandForTestRun (String inputArguments ) {
48- final String runCommand ;
48+ public String constructCommandForTest (String inputArguments ) {
49+ String runCommand ;
4950 String matlabFunctionName = FilenameUtils .removeExtension (
5051 Message .getValue (MatlabBuilderConstants .MATLAB_RUNNER_TARGET_FILE ));
5152 runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))" ;
52- return getPlatformSpecificBatchCommand (runCommand );
53- }
54-
55- public List <String > constructBatchCommandForScriptRun (String customCommand ) {
56- return getPlatformSpecificBatchCommand (customCommand );
57- }
58-
59- private List <String > getPlatformSpecificBatchCommand (String command ) {
60- final List <String > matlabDefaultArgs ;
61- final String nodeSpecificFileSep = getNodeSpecificFileSeperator ();
62- matlabDefaultArgs = Arrays .asList (
63- getMatlabRoot () + nodeSpecificFileSep + "bin" + nodeSpecificFileSep + "matlab" , "-batch" ,
64- command );
65- return matlabDefaultArgs ;
66- }
67-
68- public List <String > constructDefaultCommandForTestRun (String inputArguments )
69- throws MatlabVersionNotFoundException {
70- final List <String > matlabDefaultArgs = new ArrayList <String >();
71- Collections .addAll (matlabDefaultArgs , getPreRunnerSwitches ());
72- Collections .addAll (matlabDefaultArgs , getRunnerSwitch (inputArguments ));
73- Collections .addAll (matlabDefaultArgs , getPostRunnerSwitches ());
74- return matlabDefaultArgs ;
75- }
76-
77- public List <String > constructDefaultCommandForScriptRun (String command )
78- throws MatlabVersionNotFoundException {
79- final List <String > matlabDefaultArgs = new ArrayList <String >();
80- Collections .addAll (matlabDefaultArgs , getPreRunnerSwitches ());
81- Collections .addAll (matlabDefaultArgs , getRunnerForScriptRun (command ));
82- Collections .addAll (matlabDefaultArgs , getPostRunnerSwitches ());
83- return matlabDefaultArgs ;
84- }
85-
86-
87- private String [] getPreRunnerSwitches () throws MatlabVersionNotFoundException {
88- String nodeSpecificFileSep = getNodeSpecificFileSeperator ();
89- FilePath nodeSpecificMatlabRoot = new FilePath (getLauncher ().getChannel (), getMatlabRoot ());
90- MatlabReleaseInfo matlabRel = new MatlabReleaseInfo (nodeSpecificMatlabRoot );
91- String [] preRunnerSwitches =
92- {getMatlabRoot () + nodeSpecificFileSep + "bin" + nodeSpecificFileSep + "matlab" ,
93- "-nosplash" , "-nodesktop" };
94-
95- if (!isUnix ()) {
96- preRunnerSwitches = (String []) ArrayUtils .add (preRunnerSwitches , "-noDisplayDesktop" );
53+ if (isUnix ()) {
54+ runCommand = getBashCompatibleCommandString (runCommand );
9755 }
98-
99- if (!matlabRel
100- .verLessThan (MatlabBuilderConstants .BASE_MATLAB_VERSION_NO_APP_ICON_SUPPORT )) {
101- preRunnerSwitches = (String []) ArrayUtils .add (preRunnerSwitches , "-noAppIcon" );
102- }
103- return preRunnerSwitches ;
56+ return runCommand ;
10457 }
105-
106- private String [] getPostRunnerSwitches ( ) {
107- String [] postRunnerSwitch = { "-log" } ;
108- if (! isUnix ()) {
109- postRunnerSwitch = ( String []) ArrayUtils . add ( postRunnerSwitch , "-wait" );
58+
59+ public String constructCommandForRunCommand ( String command ) {
60+ String runCommand = command ;
61+ if ( isUnix ()) {
62+ runCommand = getBashCompatibleCommandString ( command );
11063 }
111- return postRunnerSwitch ;
112- }
113-
114- private String [] getRunnerSwitch (String inputArguments ) throws MatlabVersionNotFoundException {
115- final String runCommand ;
116- String matlabFunctionName = FilenameUtils .removeExtension (
117- Message .getValue (MatlabBuilderConstants .MATLAB_RUNNER_TARGET_FILE ));
118- runCommand = "try,exit(" + matlabFunctionName + "(" + inputArguments
119- + ")),catch e,disp(getReport(e,'extended')),exit(1),end" ;
120- final String [] runnerSwitch = {"-r" , runCommand };
121- return runnerSwitch ;
122- }
123-
124- private String [] getRunnerForScriptRun (String command ) {
125- final String runCommand ;
126- runCommand = "try,eval('" + command .replaceAll ("'" , "''" )
127- + "'),catch e,disp(getReport(e,'extended')),exit(1),end,exit" ;
128- final String [] runnerSwitch = {"-r" , runCommand };
129- return runnerSwitch ;
64+ return runCommand ;
13065 }
13166
13267 public String getNodeSpecificFileSeperator () {
@@ -140,4 +75,28 @@ public String getNodeSpecificFileSeperator() {
14075 public boolean isUnix () {
14176 return this .launcher .isUnix ();
14277 }
78+
79+ /*
80+ * This Method is to escape all open and closing brackets and single quotes
81+ * to make it compatible with /bin/bash -c command on linux
82+ *
83+ */
84+ private String getBashCompatibleCommandString (String command ) {
85+ return command .replaceAll ("'" ,"\\ \\ '" ).replaceAll ("\\ (" , "\\ \\ (" ).replaceAll ("\\ )" , "\\ \\ )" );
86+ }
87+
88+ public void copyMatlabScratchFileInWorkspace (String matlabRunnerResourcePath ,
89+ String matlabRunnerTarget , FilePath targetWorkspace )
90+ throws IOException , InterruptedException {
91+ final ClassLoader classLoader = getClass ().getClassLoader ();
92+ FilePath targetFile =
93+ new FilePath (targetWorkspace , Message .getValue (matlabRunnerTarget ));
94+ InputStream in = classLoader .getResourceAsStream (matlabRunnerResourcePath );
95+ targetFile .copyFrom (in );
96+ //set executable permission to the file on Unix.
97+ if (isUnix ()) {
98+ targetFile .chmod (0777 );
99+ }
100+
101+ }
143102}
0 commit comments