import { promises } from 'fs' import { join } from 'path' import AbstractLanguage from './AbstractLanguage' import { ProblemSet } from '../CodeforcesScraper' const { writeFile } = promises const template = ` #include using namespace std; int main() { return 0; }` export default class extends AbstractLanguage { constructor() { super('C++', '//', 'cpp', template) } getOptions(baseOptions?: { [key: string]: any }): {}[] { let options: {}[] = [ { name: 'createMakeFile', type: 'confirm', message: 'Generate Makefile?', default: true } ] if(baseOptions && baseOptions.fetchExamples) { options.push({ name: 'automateTesting', type: 'confirm', message: 'Automate test cases with make?', default: true, when(responses: { [key: string]: any }) { return responses.createMakeFile } }) } return options } async handleOptions(problemSet: ProblemSet, responses: { [key: string]: any }): Promise { if(responses.createMakeFile) { const cwd = process.cwd() const makefilePath = join(cwd, problemSet.id.toString(), 'Makefile') await writeFile(makefilePath, this.getMakefileText(problemSet, responses.automateTesting)) } } private getMakefileText(problemSet: ProblemSet, automateTesting: boolean) { return ` ########################### # BUILD ########################### ${problemSet.problems.map(problem => { return `${problem.difficulty} : ${problem.difficulty}.cpp \tg++ -std=c++14 -o $@ -g $^ ` }).join('\n')} ########################### # RUN ########################### ${problemSet.problems.map(problem => { return `.PHONY : run-${problem.difficulty} run-${problem.difficulty} : ${problem.difficulty} ${problem.examples.map((_example, index) => `${problem.difficulty}-${index + 1}.in ${problem.difficulty}-${index + 1}.out`).join(' ')} ${automateTesting ? problem.examples.map((_example, index) => { const exampleNumber = index + 1 return `\t./${problem.difficulty} < ${problem.difficulty}-${exampleNumber}.in | diff - ${problem.difficulty}-${exampleNumber}.out` }).join('\n') : ''} ` }).join('\n')}` } }