-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmatlab.ts
More file actions
109 lines (95 loc) · 3.41 KB
/
Copy pathmatlab.ts
File metadata and controls
109 lines (95 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020-2025 The MathWorks, Inc.
import { promises as fs } from "fs";
import * as os from "os";
import * as path from "path";
import { v4 as uuid } from "uuid";
import * as script from "./script";
/**
* Helper interface to represent a MATLAB script.
*/
export interface HelperScript {
dir: string;
command: string;
}
/**
* Type of a function that executes a command on a runner and returns the error
* code.
*/
export type ExecFn = (command: string, args?: string[]) => Promise<number>;
/**
* Generate a MATLAB script in the temporary directory that runs a command in
* the workspace.
*
* @param workspaceDir CI job workspace directory
* @param command MATLAB command to run
*/
export async function generateScript(workspaceDir: string, command: string): Promise<HelperScript> {
const scriptName = script.safeName(`command_${uuid()}`);
const temporaryDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "run_matlab_command-"));
const scriptPath = path.join(temporaryDirectory, scriptName + ".m");
await fs.writeFile(scriptPath, script.cdAndCall(command), {
encoding: "utf8",
});
return {
dir: temporaryDirectory,
command: scriptName
};
}
/**
* Run a HelperScript in MATLAB.
*
* Create the HelperScript using `generateScript`.
*
* @param hs HelperScript pointing to the script containing the command
* @param platform Operating system of the runner (e.g., "win32" or "linux")
* @param architecture Architecture of the runner (e.g., "x64")
* @param fn ExecFn that will execute a command on the runner
*/
export async function runCommand(hs: HelperScript, platform: string, architecture: string, fn: ExecFn, args?: string[]): Promise<void> {
const rmcPath = getRunMATLABCommandScriptPath(platform, architecture);
await fs.chmod(rmcPath, 0o777);
const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER',cd('${script.pathToCharVec(hs.dir)}')); ${hs.command}`;
let execArgs = [rmcArg];
if (args) {
execArgs = execArgs.concat(args);
}
const exitCode = await fn(rmcPath, execArgs);
if (exitCode !== 0) {
return Promise.reject(Error(`Exited with non-zero code ${exitCode}`));
}
}
/**
* Get the path of the script containing RunMATLABCommand for the host OS.
*
* @param platform Operating system of the runner (e.g., "win32" or "linux")
* @param architecture Architecture of the runner (e.g., "x64")
*/
export function getRunMATLABCommandScriptPath(platform: string, architecture: string): string {
if (architecture != "x64" && !(platform == "darwin" && architecture == "arm64")) {
throw new Error(`This action is not supported on ${platform} runners using the ${architecture} architecture.`);
}
let ext;
let platformDir;
switch (platform) {
case "win32":
ext = ".exe";
platformDir = "win64";
break;
case "darwin":
ext = "";
if (architecture == "x64") {
platformDir = "maci64";
} else {
platformDir = "maca64";
}
break;
case "linux":
ext = "";
platformDir = "glnxa64";
break;
default:
throw new Error(`This action is not supported on ${platform} runners using the ${architecture} architecture.`);
}
const rmcPath = path.join(__dirname, "bin", platformDir, `run-matlab-command${ext}`);
return rmcPath;
}