From 22e354ea5e0434c6f4731c4a7dc0e5c1161c61d5 Mon Sep 17 00:00:00 2001 From: sameagen Date: Wed, 13 Dec 2023 14:18:47 -0500 Subject: [PATCH 01/11] Update scripts to cd back to MATLAB startup directory --- src/matlab.ts | 4 ++-- src/script.ts | 6 +++--- src/script.unit.test.ts | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/matlab.ts b/src/matlab.ts index 5fde9b6..7242e21 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -33,7 +33,7 @@ export async function generateScript(workspaceDir: string, command: string): Pro 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(workspaceDir, command), { + await fs.writeFile(scriptPath, script.cdAndCall(scriptName, command), { encoding: "utf8", }); @@ -54,7 +54,7 @@ export async function runCommand(hs: HelperScript, platform: string, architectur const rmcPath = getRunMATLABCommandScriptPath(platform, architecture); await fs.chmod(rmcPath, 0o777); - const rmcArg = script.cdAndCall(hs.dir, hs.command); + const rmcArg = `${hs.command}(cd('${hs.dir}'));`; let execArgs = [rmcArg]; diff --git a/src/script.ts b/src/script.ts index c27c44f..f47074e 100644 --- a/src/script.ts +++ b/src/script.ts @@ -1,4 +1,4 @@ -// Copyright 2020 The MathWorks, Inc. +// Copyright 2020-2023 The MathWorks, Inc. /** * Generate MATLAB command for changing directories and calling a command in it. @@ -7,8 +7,8 @@ * @param command Command to run in directory. * @returns MATLAB command. */ -export function cdAndCall(dir: string, command: string): string { - return `cd('${pathToCharVec(dir)}');${command}`; +export function cdAndCall(scriptName: string, command: string): string { + return `function ${scriptName}(folder)\ncd(folder);${command}`; } /** diff --git a/src/script.unit.test.ts b/src/script.unit.test.ts index 6eb324d..c198215 100644 --- a/src/script.unit.test.ts +++ b/src/script.unit.test.ts @@ -1,15 +1,15 @@ -// Copyright 2020 The MathWorks, Inc. +// Copyright 2020-2023 The MathWorks, Inc. import * as script from "./script"; describe("call generation", () => { it("ideally works", () => { // I know what your thinking - const testDir = String.raw`C:\Users\you\You're Documents`; + const testName = "command_uuid_123"; const testCommand = "disp('hello world')"; - const expectedString = String.raw`cd('C:\Users\you\You''re Documents');${testCommand}`; + const expectedString = `function command_uuid_123(folder)\ncd(folder);${testCommand}`; - expect(script.cdAndCall(testDir, testCommand)).toMatch(expectedString); + expect(script.cdAndCall(testName, testCommand)).toMatch(expectedString); }); }); From 528968e1a7203a9281bd19560fb69f2761bab2da Mon Sep 17 00:00:00 2001 From: sameagen Date: Wed, 13 Dec 2023 14:31:42 -0500 Subject: [PATCH 02/11] Debug --- .github/workflows/bat.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index c35de53..f37f3a1 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -67,10 +67,10 @@ jobs: with: command: assert(isfile("mylog.log")); - - run: echo 'onetyone = 11' > startup.m + - run: pwd && echo 'onetyone = 11' > startup.m shell: bash - name: MATLAB runs startup.m automatically uses: ./ with: - command: assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') + command: disp(pwd());assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') From 64eae8632ec51b267a797237ed9bcd7269ee7fd9 Mon Sep 17 00:00:00 2001 From: sameagen Date: Wed, 13 Dec 2023 15:44:31 -0500 Subject: [PATCH 03/11] Revert to using a script, pass the old directory through via environment variable --- .github/workflows/bat.yml | 4 ++-- src/matlab.ts | 4 ++-- src/script.ts | 4 ++-- src/script.unit.test.ts | 5 ++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index f37f3a1..c35de53 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -67,10 +67,10 @@ jobs: with: command: assert(isfile("mylog.log")); - - run: pwd && echo 'onetyone = 11' > startup.m + - run: echo 'onetyone = 11' > startup.m shell: bash - name: MATLAB runs startup.m automatically uses: ./ with: - command: disp(pwd());assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') + command: assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') diff --git a/src/matlab.ts b/src/matlab.ts index 7242e21..deec096 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -33,7 +33,7 @@ export async function generateScript(workspaceDir: string, command: string): Pro 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(scriptName, command), { + await fs.writeFile(scriptPath, script.cdAndCall(command), { encoding: "utf8", }); @@ -54,7 +54,7 @@ export async function runCommand(hs: HelperScript, platform: string, architectur const rmcPath = getRunMATLABCommandScriptPath(platform, architecture); await fs.chmod(rmcPath, 0o777); - const rmcArg = `${hs.command}(cd('${hs.dir}'));`; + const rmcArg = `setenv("MW_ORIG_WORKING_FOLDER", cd('${hs.dir}'));${hs.command}`; let execArgs = [rmcArg]; diff --git a/src/script.ts b/src/script.ts index f47074e..316e876 100644 --- a/src/script.ts +++ b/src/script.ts @@ -7,8 +7,8 @@ * @param command Command to run in directory. * @returns MATLAB command. */ -export function cdAndCall(scriptName: string, command: string): string { - return `function ${scriptName}(folder)\ncd(folder);${command}`; +export function cdAndCall(command: string): string { + return `cd(getenv("MW_ORIG_WORKING_FOLDER"));${command}`; } /** diff --git a/src/script.unit.test.ts b/src/script.unit.test.ts index c198215..569326a 100644 --- a/src/script.unit.test.ts +++ b/src/script.unit.test.ts @@ -5,11 +5,10 @@ import * as script from "./script"; describe("call generation", () => { it("ideally works", () => { // I know what your thinking - const testName = "command_uuid_123"; const testCommand = "disp('hello world')"; - const expectedString = `function command_uuid_123(folder)\ncd(folder);${testCommand}`; + const expectedString = `cd(getenv("MW_ORIG_WORKING_FOLDER"));${testCommand}`; - expect(script.cdAndCall(testName, testCommand)).toMatch(expectedString); + expect(script.cdAndCall(testCommand)).toMatch(expectedString); }); }); From e45a2b112fc8fd4b3f0bc0baddda902312307bd3 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 08:54:44 -0500 Subject: [PATCH 04/11] Add check --- .github/workflows/bat.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index c35de53..bc016c2 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -74,3 +74,13 @@ jobs: uses: ./ with: command: assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') + + - run: | + mkdir subdir + echo 'onetyonetyone = 111' > startup.m + shell: bash + + - name: MATLAB sd startup option is not overwritten + uses: ./ + with: + command: [~, f] = fileparts(pwd); assert(onetyone==111); assert(strcmp(f, 'subdir')); From cf379d1c61b1ceb938d8c7e864d40429ccf778dd Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 08:57:29 -0500 Subject: [PATCH 05/11] Fix syntax --- .github/workflows/bat.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index bc016c2..d5ca7ca 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -83,4 +83,8 @@ jobs: - name: MATLAB sd startup option is not overwritten uses: ./ with: - command: [~, f] = fileparts(pwd); assert(onetyone==111); assert(strcmp(f, 'subdir')); + command: > + assert(onetyone==111); + [~, f] = fileparts(pwd); + assert(strcmp(f, 'subdir')); + startup-options: -sd subdir From b3dd7cf693fdb1dd57ea062d9194779552ec2ca2 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 09:03:16 -0500 Subject: [PATCH 06/11] Sneaky sneaky variable name --- .github/workflows/bat.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index d5ca7ca..905bac4 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -84,7 +84,7 @@ jobs: uses: ./ with: command: > - assert(onetyone==111); + assert(onetyonentyone==111); [~, f] = fileparts(pwd); assert(strcmp(f, 'subdir')); startup-options: -sd subdir From 435144e1273b03d2bb33408e6737b022023781f5 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 09:10:07 -0500 Subject: [PATCH 07/11] Put startup in the correct directory --- .github/workflows/bat.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index 905bac4..8e0bac7 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -77,7 +77,7 @@ jobs: - run: | mkdir subdir - echo 'onetyonetyone = 111' > startup.m + echo 'onetyonetyone = 111' > subdir/startup.m shell: bash - name: MATLAB sd startup option is not overwritten From 4c8ee92bd07bd76ac7d3d65c015d82816fa166b9 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 09:16:18 -0500 Subject: [PATCH 08/11] Spelling is hard --- .github/workflows/bat.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index 8e0bac7..6f9782d 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -84,7 +84,7 @@ jobs: uses: ./ with: command: > - assert(onetyonentyone==111); + assert(onetyonetyone==111); [~, f] = fileparts(pwd); assert(strcmp(f, 'subdir')); startup-options: -sd subdir From 3fdaf005251ee4c0384a76869ced1b983939c62e Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 09:43:25 -0500 Subject: [PATCH 09/11] Character vector changes --- src/matlab.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/matlab.ts b/src/matlab.ts index deec096..3f7f7da 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -37,7 +37,10 @@ export async function generateScript(workspaceDir: string, command: string): Pro encoding: "utf8", }); - return { dir: temporaryDirectory, command: scriptName }; + return { + dir: script.pathToCharVec(temporaryDirectory), + command: scriptName + }; } /** @@ -54,7 +57,7 @@ export async function runCommand(hs: HelperScript, platform: string, architectur const rmcPath = getRunMATLABCommandScriptPath(platform, architecture); await fs.chmod(rmcPath, 0o777); - const rmcArg = `setenv("MW_ORIG_WORKING_FOLDER", cd('${hs.dir}'));${hs.command}`; + const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER', cd('${hs.dir}'));${hs.command}`; let execArgs = [rmcArg]; From 6b3cf41d4946b91eb1e68665e5efb4d18ba2fd37 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 10:08:57 -0500 Subject: [PATCH 10/11] Update quotes --- src/script.ts | 2 +- src/script.unit.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/script.ts b/src/script.ts index 316e876..cb2a464 100644 --- a/src/script.ts +++ b/src/script.ts @@ -8,7 +8,7 @@ * @returns MATLAB command. */ export function cdAndCall(command: string): string { - return `cd(getenv("MW_ORIG_WORKING_FOLDER"));${command}`; + return `cd(getenv('MW_ORIG_WORKING_FOLDER'));${command}`; } /** diff --git a/src/script.unit.test.ts b/src/script.unit.test.ts index 569326a..e76ea3b 100644 --- a/src/script.unit.test.ts +++ b/src/script.unit.test.ts @@ -6,7 +6,7 @@ describe("call generation", () => { it("ideally works", () => { // I know what your thinking const testCommand = "disp('hello world')"; - const expectedString = `cd(getenv("MW_ORIG_WORKING_FOLDER"));${testCommand}`; + const expectedString = `cd(getenv('MW_ORIG_WORKING_FOLDER'));${testCommand}`; expect(script.cdAndCall(testCommand)).toMatch(expectedString); }); From 0e4d45eff35e89b9a345b24a195288c354ecd34e Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 14 Dec 2023 11:05:04 -0500 Subject: [PATCH 11/11] Move function call location --- src/matlab.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/matlab.ts b/src/matlab.ts index 3f7f7da..6017d97 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -38,7 +38,7 @@ export async function generateScript(workspaceDir: string, command: string): Pro }); return { - dir: script.pathToCharVec(temporaryDirectory), + dir: temporaryDirectory, command: scriptName }; } @@ -57,7 +57,7 @@ export async function runCommand(hs: HelperScript, platform: string, architectur const rmcPath = getRunMATLABCommandScriptPath(platform, architecture); await fs.chmod(rmcPath, 0o777); - const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER', cd('${hs.dir}'));${hs.command}`; + const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER', cd('${script.pathToCharVec(hs.dir)}'));${hs.command}`; let execArgs = [rmcArg];