Skip to content

Commit

Permalink
Handdled multiple try catch issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nbhoski committed Jun 27, 2024
1 parent 0df5883 commit 9f9e5d5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
27 changes: 11 additions & 16 deletions src/buildSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,24 @@ export function getBuildSummaryTable(tasks: Task[]): string[][] {
}

export function writeSummary(taskSummaryTableRows: string[][]) {
core.summary
.addTable(taskSummaryTableRows)
.write();
core.summary
.addTable(taskSummaryTableRows)
.write();
}

export function processAndDisplayBuildSummary() {
const runId = process.env.GITHUB_RUN_ID;
const runnerTemp = process.env.RUNNER_TEMP;
let filePath: string;

try {

if (!runId) {
filePath = join(runnerTemp as string, `buildSummary_.json`);
} else {
filePath = join(runnerTemp as string, `buildSummary_${runId as string}.json`);
}

const data =JSON.parse(readFileSync(filePath, { encoding: 'utf8' }));
const taskSummaryTableRows = getBuildSummaryTable(data);
writeSummary(taskSummaryTableRows);
} catch (error) {
console.error('An error occurred while reading the build summary file or adding the build summary table:', error);
if (!runId) {
filePath = join(runnerTemp as string, `buildSummary_.json`);
} else {
filePath = join(runnerTemp as string, `buildSummary_${runId as string}.json`);
}

const data = JSON.parse(readFileSync(filePath, { encoding: 'utf8' }));
const taskSummaryTableRows = getBuildSummaryTable(data);
writeSummary(taskSummaryTableRows);
}

77 changes: 47 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,52 @@ import * as buildSummary from "./buildSummary";
* Gather action inputs and then run action.
*/
async function run() {
const platform = process.platform;
const architecture = process.arch;
const workspaceDir = process.cwd();

const options: buildtool.RunBuildOptions = {
Tasks: core.getInput("tasks"),
BuildOptions: core.getInput("build-options"),
};

const command = buildtool.generateCommand(options);
const startupOptions = core.getInput("startup-options").split(" ");

const helperScript = await matlab.generateScript(workspaceDir, command);
const execOptions = { env: {
...process.env,
"MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE":"ciplugins.github.getDefaultPlugins",
}};

await matlab.runCommand(
helperScript,
platform,
architecture,
(cmd,args)=>exec.exec(cmd,args,execOptions),
startupOptions
);
buildSummary.processAndDisplayBuildSummary();
try {

const platform = process.platform;
const architecture = process.arch;
const workspaceDir = process.cwd();

const options: buildtool.RunBuildOptions = {
Tasks: core.getInput("tasks"),
BuildOptions: core.getInput("build-options"),
};

const command = buildtool.generateCommand(options);
const startupOptions = core.getInput("startup-options").split(" ");

const helperScript = await matlab.generateScript(workspaceDir, command);
const execOptions = {
env: {
...process.env,
"MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE": "ciplugins.github.getDefaultPlugins",
}
};

await matlab.runCommand(
helperScript,
platform,
architecture,
(cmd, args) => exec.exec(cmd, args, execOptions),
startupOptions
);

buildSummary.processAndDisplayBuildSummary();

} catch (e) {
try {
buildSummary.processAndDisplayBuildSummary();
} catch (summaryError) {
console.error('An error occurred while reading the build summary file or adding the build summary table:', summaryError);
}
if (e instanceof Error || typeof e === "string") {
core.setFailed(e);
} else {
core.setFailed("An unknown error occurred while runing MATLAB build.");
}

}

}

run().catch((e) => {
buildSummary.processAndDisplayBuildSummary();
core.setFailed(e);
});
run();

0 comments on commit 9f9e5d5

Please sign in to comment.