|
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; |
| 11 | +import java.util.UUID; |
12 | 12 | import hudson.EnvVars; |
13 | 13 | import hudson.FilePath; |
14 | 14 | import hudson.Launcher; |
|
17 | 17 | import hudson.model.TaskListener; |
18 | 18 |
|
19 | 19 | public interface MatlabBuild { |
20 | | - |
| 20 | + |
21 | 21 | /** |
22 | | - * 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 |
23 | 23 | * object to launch MATLAB with appropriate startup options like -r or -batch |
24 | | - * @param workspace Current build workspace |
| 24 | + * |
| 25 | + * @param workspace Current build workspace |
25 | 26 | * @param launcher Current build launcher |
26 | | - * @param listener Current build listener |
| 27 | + * @param listener Current build listener |
27 | 28 | * @param envVars Environment variables of the current build |
28 | | - * @param matlabCommand MATLAB command to execute on shell |
| 29 | + * @param matlabCommand MATLAB command to execute on shell |
29 | 30 | * @return matlabLauncher returns the process launcher to run MATLAB commands |
30 | 31 | */ |
31 | | - default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException { |
32 | | - //Get node specific tmp directory to copy matlab runner script |
33 | | - String tmpDir = getNodeSpecificTmpFolderPath(); |
| 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 |
| 36 | + String tmpDir = getNodeSpecificTmpFolderPath(workspace); |
34 | 37 | FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir); |
35 | 38 | ProcStarter matlabLauncher; |
36 | | - if(launcher.isUnix()) { |
37 | | - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/run_matlab_command.sh",matlabCommand).stdout(listener); |
38 | | - |
39 | | - //Copy runner .sh for linux platform in workspace. |
40 | | - copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace); |
41 | | - }else { |
42 | | - launcher = launcher.decorateByPrefix("cmd.exe","/C"); |
43 | | - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+"run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener); |
44 | | - //Copy runner.bat for Windows platform in workspace. |
45 | | - copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", 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); |
46 | 56 | } |
47 | 57 | return matlabLauncher; |
48 | 58 | } |
49 | 59 |
|
50 | | - /** |
| 60 | + /* |
51 | 61 | * Method to copy given file from source to target node specific workspace. |
52 | 62 | */ |
53 | | - default void copyFileInWorkspace(String sourceFile, String targetFile, |
54 | | - FilePath targetWorkspace) throws IOException, InterruptedException { |
| 63 | + default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace) |
| 64 | + throws IOException, InterruptedException { |
55 | 65 | final ClassLoader classLoader = getClass().getClassLoader(); |
56 | | - FilePath targetFilePath = new FilePath(targetWorkspace, Message.getValue(targetFile)); |
| 66 | + FilePath targetFilePath = new FilePath(targetWorkspace, targetFile); |
57 | 67 | InputStream in = classLoader.getResourceAsStream(sourceFile); |
58 | 68 | targetFilePath.copyFrom(in); |
59 | 69 | // set executable permission |
60 | 70 | targetFilePath.chmod(0755); |
| 71 | + } |
| 72 | + |
| 73 | + default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName, FilePath workspace) |
| 74 | + throws IOException, InterruptedException { |
| 75 | + /*Use of Computer is not recommended as jenkins hygeine for pipeline support |
| 76 | + * https://javadoc.jenkins-ci.org/jenkins/tasks/SimpleBuildStep.html */ |
61 | 77 |
|
| 78 | + String tmpDir = getNodeSpecificTmpFolderPath(workspace); |
| 79 | + return new FilePath(launcher.getChannel(), tmpDir+"/"+uniqueName); |
62 | 80 | } |
63 | | - |
64 | | - default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher) throws IOException, InterruptedException { |
65 | | - Computer cmp = Computer.currentComputer(); |
66 | | - String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); |
67 | | - if(launcher.isUnix()) { |
68 | | - tmpDir = tmpDir+"/run_matlab_command.sh"; |
69 | | - }else { |
70 | | - tmpDir = tmpDir+"\\"+"run_matlab_command.bat"; |
| 81 | + |
| 82 | + default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOException, InterruptedException { |
| 83 | + Computer cmp = workspace.toComputer(); |
| 84 | + if (cmp == null) { |
| 85 | + throw new IOException(Message.getValue("build.workspace.computer.not.found")); |
71 | 86 | } |
72 | | - return new FilePath(launcher.getChannel(), tmpDir); |
73 | | - } |
74 | | - |
75 | | - default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException { |
76 | | - Computer cmp = Computer.currentComputer(); |
77 | 87 | String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); |
78 | 88 | return tmpDir; |
79 | 89 | } |
80 | 90 |
|
| 91 | + default String getUniqueNameForRunnerFile() { |
| 92 | + return UUID.randomUUID().toString(); |
| 93 | + } |
81 | 94 | } |
0 commit comments