|
2 | 2 | /** |
3 | 3 | * Copyright 2019-2020 The MathWorks, Inc. |
4 | 4 | * |
5 | | - * Build Interface has two default methods. MATLAB builders can override the |
6 | | - * default behavior. |
| 5 | + * Build Interface has two default methods. MATLAB builders can override the default behavior. |
7 | 6 | * |
8 | 7 | */ |
9 | 8 |
|
10 | 9 | import java.io.IOException; |
11 | 10 | import java.io.InputStream; |
12 | 11 | import java.util.UUID; |
13 | | - |
14 | 12 | import hudson.EnvVars; |
15 | 13 | import hudson.FilePath; |
16 | 14 | import hudson.Launcher; |
|
19 | 17 | import hudson.model.TaskListener; |
20 | 18 |
|
21 | 19 | public interface MatlabBuild { |
22 | | - |
| 20 | + |
23 | 21 | /** |
24 | | - * This Method decorates the launcher with MATLAB command provided and returns the Process |
| 22 | + * This Method decorates the launcher with MATLAB command provided and returns the Process |
25 | 23 | * object to launch MATLAB with appropriate startup options like -r or -batch |
26 | | - * @param workspace Current build workspace |
| 24 | + * |
| 25 | + * @param workspace Current build workspace |
27 | 26 | * @param launcher Current build launcher |
28 | | - * @param listener Current build listener |
| 27 | + * @param listener Current build listener |
29 | 28 | * @param envVars Environment variables of the current build |
30 | | - * @param matlabCommand MATLAB command to execute on shell |
| 29 | + * @param matlabCommand MATLAB command to execute on shell |
31 | 30 | * @return matlabLauncher returns the process launcher to run MATLAB commands |
32 | 31 | */ |
33 | | - default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand,String uniqueName) throws IOException, InterruptedException { |
34 | | - //Get node specific tmp directory to copy matlab runner script |
| 32 | + default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher, |
| 33 | + TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName) |
| 34 | + throws IOException, InterruptedException { |
| 35 | + // Get node specific tmp directory to copy matlab runner script |
35 | 36 | String tmpDir = getNodeSpecificTmpFolderPath(); |
36 | 37 | FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir); |
37 | 38 | ProcStarter matlabLauncher; |
38 | | - if(launcher.isUnix()) { |
39 | | - final String runnerScriptName = uniqueName+"/run_matlab_command.sh"; |
40 | | - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/"+runnerScriptName,matlabCommand).stdout(listener); |
41 | | - |
42 | | - //Copy runner .sh for linux platform in workspace. |
43 | | - copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, targetWorkspace); |
44 | | - }else { |
45 | | - final String runnerScriptName = uniqueName+"\\run_matlab_command.bat"; |
46 | | - launcher = launcher.decorateByPrefix("cmd.exe","/C"); |
47 | | - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+runnerScriptName,"\""+matlabCommand+"\"").stdout(listener); |
48 | | - //Copy runner.bat for Windows platform in workspace. |
49 | | - copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName, targetWorkspace); |
| 39 | + if (launcher.isUnix()) { |
| 40 | + final String runnerScriptName = uniqueName + "/run_matlab_command.sh"; |
| 41 | + matlabLauncher = launcher.launch().pwd(workspace).envs(envVars) |
| 42 | + .cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener); |
| 43 | + |
| 44 | + // Copy runner .sh for linux platform in workspace. |
| 45 | + copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, |
| 46 | + targetWorkspace); |
| 47 | + } else { |
| 48 | + final String runnerScriptName = uniqueName + "\\run_matlab_command.bat"; |
| 49 | + launcher = launcher.decorateByPrefix("cmd.exe", "/C"); |
| 50 | + matlabLauncher = launcher.launch().pwd(workspace).envs(envVars) |
| 51 | + .cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"") |
| 52 | + .stdout(listener); |
| 53 | + // Copy runner.bat for Windows platform in workspace. |
| 54 | + copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName, |
| 55 | + targetWorkspace); |
50 | 56 | } |
51 | 57 | return matlabLauncher; |
52 | 58 | } |
53 | 59 |
|
54 | 60 | /* |
55 | 61 | * Method to copy given file from source to target node specific workspace. |
56 | 62 | */ |
57 | | - default void copyFileInWorkspace(String sourceFile, String targetFile, |
58 | | - FilePath targetWorkspace) throws IOException, InterruptedException { |
| 63 | + default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace) |
| 64 | + throws IOException, InterruptedException { |
59 | 65 | final ClassLoader classLoader = getClass().getClassLoader(); |
60 | 66 | FilePath targetFilePath = new FilePath(targetWorkspace, targetFile); |
61 | 67 | InputStream in = classLoader.getResourceAsStream(sourceFile); |
62 | 68 | targetFilePath.copyFrom(in); |
63 | 69 | // set executable permission |
64 | | - targetFilePath.chmod(0755); |
| 70 | + targetFilePath.chmod(0755); |
65 | 71 | } |
66 | | - |
67 | | - default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher,String uniqueName) throws IOException, InterruptedException { |
| 72 | + |
| 73 | + default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName) |
| 74 | + throws IOException, InterruptedException { |
68 | 75 | Computer cmp = Computer.currentComputer(); |
69 | 76 | String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); |
70 | | - if(launcher.isUnix()) { |
71 | | - tmpDir = tmpDir+"/"+uniqueName; |
72 | | - }else { |
73 | | - tmpDir = tmpDir+"\\"+uniqueName; |
| 77 | + if (launcher.isUnix()) { |
| 78 | + tmpDir = tmpDir + "/" + uniqueName; |
| 79 | + } else { |
| 80 | + tmpDir = tmpDir + "\\" + uniqueName; |
74 | 81 | } |
75 | | - return new FilePath(launcher.getChannel(), tmpDir); |
| 82 | + return new FilePath(launcher.getChannel(), tmpDir); |
76 | 83 | } |
77 | | - |
| 84 | + |
78 | 85 | default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException { |
79 | 86 | Computer cmp = Computer.currentComputer(); |
80 | 87 | String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); |
81 | 88 | return tmpDir; |
82 | 89 | } |
83 | | - |
| 90 | + |
84 | 91 | default String getUniqueNameForRunnerFile() { |
85 | | - return UUID.randomUUID().toString(); |
| 92 | + return UUID.randomUUID().toString(); |
86 | 93 | } |
87 | 94 |
|
88 | 95 | } |
0 commit comments