diff --git a/src/main/java/com/mathworks/ci/MatlabBuild.java b/src/main/java/com/mathworks/ci/MatlabBuild.java index 0cf6858d..94544969 100644 --- a/src/main/java/com/mathworks/ci/MatlabBuild.java +++ b/src/main/java/com/mathworks/ci/MatlabBuild.java @@ -29,8 +29,8 @@ public interface MatlabBuild { * @param matlabCommand MATLAB command to execute on shell * @return matlabLauncher returns the process launcher to run MATLAB commands */ - default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher, - TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName) + default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, + Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName) throws IOException, InterruptedException { // Get node specific tmp directory to copy matlab runner script String tmpDir = getNodeSpecificTmpFolderPath(workspace); @@ -38,8 +38,8 @@ default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher la ProcStarter matlabLauncher; if (launcher.isUnix()) { final String runnerScriptName = uniqueName + "/run_matlab_command.sh"; - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars) - .cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener); + matlabLauncher = launcher.launch().envs(envVars); + matlabLauncher.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener); // Copy runner .sh for linux platform in workspace. copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, @@ -47,8 +47,8 @@ default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher la } else { final String runnerScriptName = uniqueName + "\\run_matlab_command.bat"; launcher = launcher.decorateByPrefix("cmd.exe", "/C"); - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars) - .cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"") + matlabLauncher = launcher.launch().envs(envVars); + matlabLauncher.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"") .stdout(listener); // Copy runner.bat for Windows platform in workspace. copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName, diff --git a/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java b/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java index 9dbcf410..69bc619b 100644 --- a/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java +++ b/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java @@ -101,6 +101,7 @@ public void perform(@Nonnull Run, ?> build, @Nonnull FilePath workspace, // Invoke MATLAB command and transfer output to standard // Output Console + buildResult = execMatlabCommand(workspace, launcher, listener, getEnv()); @@ -112,22 +113,46 @@ public void perform(@Nonnull Run, ?> build, @Nonnull FilePath workspace, private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException { final String uniqueTmpFldrName = getUniqueNameForRunnerFile(); + final String uniqueCommandFile = + "command_" + getUniqueNameForRunnerFile().replaceAll("-", "_"); + final FilePath uniqeTmpFolderPath = + getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace); + + // Create MATLAB script + createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener); ProcStarter matlabLauncher; + try { matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars, - getCommand(), uniqueTmpFldrName); - return matlabLauncher.join(); + uniqueCommandFile, uniqueTmpFldrName); + launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars); + listener.getLogger() + .println("#################### Starting command output ####################"); + return matlabLauncher.pwd(uniqeTmpFolderPath).join(); + } catch (Exception e) { listener.getLogger().println(e.getMessage()); return 1; } finally { - // Cleanup the runner File from tmp directory - FilePath matlabRunnerScript = - getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace); - if (matlabRunnerScript.exists()) { - matlabRunnerScript.deleteRecursive(); + // Cleanup the tmp directory + if (uniqeTmpFolderPath.exists()) { + uniqeTmpFolderPath.deleteRecursive(); } } - + } + + private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { + + // Create a new command runner script in the temp folder. + final FilePath matlabCommandFile = + new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m"); + final String matlabCommandFileContent = + "cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + getCommand(); + + // Display the commands on console output for users reference + listener.getLogger() + .println("Generating MATLAB script with content:\n" + getCommand() + "\n"); + + matlabCommandFile.write(matlabCommandFileContent, "UTF-8"); } } diff --git a/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java b/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java index 93840829..800cd77b 100644 --- a/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java +++ b/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java @@ -240,12 +240,13 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher try { matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars, constructCommandForTest(getInputArguments()), uniqueTmpFldrName); - + // Copy MATLAB scratch file into the workspace. FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote()); copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE, MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace); - return matlabLauncher.join(); + + return matlabLauncher.pwd(workspace).join(); } catch (Exception e) { listener.getLogger().println(e.getMessage()); return 1; diff --git a/src/main/resources/com/mathworks/ci/RunMatlabCommandBuilder/help-matlabCommand.html b/src/main/resources/com/mathworks/ci/RunMatlabCommandBuilder/help-matlabCommand.html index 2fd0a4ce..34f7c8ac 100644 --- a/src/main/resources/com/mathworks/ci/RunMatlabCommandBuilder/help-matlabCommand.html +++ b/src/main/resources/com/mathworks/ci/RunMatlabCommandBuilder/help-matlabCommand.html @@ -1,10 +1,10 @@