Skip to content

Commit

Permalink
merge: [BE] 컴파일이 필요한 언어 코드 실행 fix
Browse files Browse the repository at this point in the history
[BE] fix: 컴파일이 필요한 언어 코드 실행 fix
  • Loading branch information
Gseungmin authored Dec 13, 2023
2 parents 4e488ea + 291dc08 commit e50ed55
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
41 changes: 38 additions & 3 deletions backEnd/running/src/codes/codes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class CodesService {
try {
fs.writeFileSync(filePath, code);
const { stdout, stderr } = await this.runCommand(filePaths, language);
this.logger.debug(`${stdout}, ${stderr}`);
if (stderr) {
console.log(stderr);
throw new RunningException(stderr.trim());
}
return this.getOutput(stdout);
Expand All @@ -57,11 +57,23 @@ export class CodesService {
}
}

runCommand(
async runCommand(
filePaths: string[],
language: supportLang,
): Promise<runCommandResult> {
const command = languageCommand(language, filePaths);
const commands = languageCommand(language, filePaths);
this.logger.debug(JSON.stringify(commands));

let command;
if (commands.length > 1) {
const { stdout, stderr } = await this.compile(commands[0]);
if (stderr) {
return { stdout, stderr };
}
command = commands[1];
} else {
command = commands[0];
}
return new Promise((resolve) => {
const commandParts = command.split(' ');
const stdout = [];
Expand Down Expand Up @@ -98,6 +110,29 @@ export class CodesService {
}
});
}
compile(command: string): Promise<runCommandResult> {
const commandParts = command.split(' ');
return new Promise((resolve) => {
const stdout = [];
const stderr = [];
const childProcess = spawn(commandParts[0], commandParts.slice(1));

childProcess.stdout.on('data', (data) => {
stdout.push(data);
});

childProcess.stderr.on('data', (data) => {
stderr.push(data);
});

childProcess.on('close', (code, signal) => {
this.logger.log(`complied done with code ${code}, ${signal}`);
const out = Buffer.concat(stdout).toString();
const err = Buffer.concat(stderr).toString();
resolve({ stdout: out, stderr: err });
});
});
}

getOutput(stdout: string): ResponseCodeDto {
return { output: stdout.trim() };
Expand Down
17 changes: 10 additions & 7 deletions backEnd/running/src/common/supportLang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ export const distExtName = {
kotlin: '.jar',
};

export function languageCommand(language, filePaths) {
export function languageCommand(language, filePaths): string[] {
const [filepath, compile_dist] = filePaths;
switch (language) {
case 'python':
return `python3 ${filepath}`;
return [`python3 ${filepath}`];
case 'javascript':
return `node ${filepath}`;
return [`node ${filepath}`];
case 'java':
return `java ${filepath}`;
return [`java ${filepath}`];
case 'c':
return `gcc -o ${compile_dist} ${filepath} && ${compile_dist}`;
return [`gcc -o ${compile_dist} ${filepath}`, compile_dist];
case 'swift':
return `swiftc -o ${compile_dist} ${filepath} && ${compile_dist}`;
return [`swiftc -o ${compile_dist} ${filepath}`, compile_dist];
case 'kotlin':
return `kotlinc ${filepath} -include-runtime -d ${compile_dist} && java -jar ${compile_dist}`;
return [
`kotlinc ${filepath} -include-runtime -d ${compile_dist}`,
`java -jar ${compile_dist}`,
];
}
}
export const needCompile = ['c', 'swift', 'kotlin'];

0 comments on commit e50ed55

Please sign in to comment.