diff --git a/.github/workflows/bat.yml b/.github/workflows/bat.yml index c35de53..6f9782d 100644 --- a/.github/workflows/bat.yml +++ b/.github/workflows/bat.yml @@ -74,3 +74,17 @@ jobs: uses: ./ with: command: assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m') + + - run: | + mkdir subdir + echo 'onetyonetyone = 111' > subdir/startup.m + shell: bash + + - name: MATLAB sd startup option is not overwritten + uses: ./ + with: + command: > + assert(onetyonetyone==111); + [~, f] = fileparts(pwd); + assert(strcmp(f, 'subdir')); + startup-options: -sd subdir diff --git a/src/matlab.ts b/src/matlab.ts index 5fde9b6..6017d97 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -33,11 +33,14 @@ 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(command), { encoding: "utf8", }); - return { dir: temporaryDirectory, command: scriptName }; + return { + dir: 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 = script.cdAndCall(hs.dir, hs.command); + const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER', cd('${script.pathToCharVec(hs.dir)}'));${hs.command}`; let execArgs = [rmcArg]; diff --git a/src/script.ts b/src/script.ts index c27c44f..cb2a464 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(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 6eb324d..e76ea3b 100644 --- a/src/script.unit.test.ts +++ b/src/script.unit.test.ts @@ -1,15 +1,14 @@ -// 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 testCommand = "disp('hello world')"; - const expectedString = String.raw`cd('C:\Users\you\You''re Documents');${testCommand}`; + const expectedString = `cd(getenv('MW_ORIG_WORKING_FOLDER'));${testCommand}`; - expect(script.cdAndCall(testDir, testCommand)).toMatch(expectedString); + expect(script.cdAndCall(testCommand)).toMatch(expectedString); }); });