diff --git a/bin/temporary_action.js b/bin/temporary_action.js deleted file mode 100644 index 596566debf57d99a47dfe95d36296cfbbc83ab92..0000000000000000000000000000000000000000 --- a/bin/temporary_action.js +++ /dev/null @@ -1,16 +0,0 @@ -const path = require('path'); -const fs = require('fs'); - -const categories = fs.readdirSync(path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms')); -for (const category of categories) { - if (category.startsWith('.')) continue; - const algorithms = fs.readdirSync(path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms', category)); - for (const algorithm of algorithms) { - if (algorithm.startsWith('.')) continue; - const dir = path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms', category, algorithm); - try { - fs.renameSync(path.resolve(dir, 'desc.md'), path.resolve(dir, 'README.md')); - } catch (e) { - } - } -} \ No newline at end of file diff --git a/bin/www b/bin/www old mode 100644 new mode 100755 diff --git a/package.json b/package.json index 457f3206306a19b9d5c73728feccb10f50afdd31..2ebbadfd4c89ae4c196492c3ba20c520a850afd3 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,12 @@ "version": "2.0.0", "description": "Algorithm Visualizer", "scripts": { - "postinstall": "docker pull openjdk:8 && docker pull gcc:8.1", - "dev": "NODE_ENV=development node bin/www", - "start": "NODE_ENV=production node bin/www", + "dev": "NODE_ENV=development ./bin/www", + "start": "NODE_ENV=production ./bin/www", "build": "npm run build:frontend && npm run build:backend", "build:frontend": "NODE_ENV=production node ./node_modules/webpack/bin/webpack --bail --progress --config webpack.frontend.config.js", "build:backend": "NODE_ENV=production node ./node_modules/webpack/bin/webpack --bail --progress --config webpack.backend.config.js", - "lint": "./node_modules/.bin/eslint --fix src", - "publish": "npm run lint && npm run build && git add . && git commit && git push" + "lint": "./node_modules/.bin/eslint --fix src" }, "repository": { "type": "git", diff --git a/src/backend/controllers/tracers.js b/src/backend/controllers/tracers.js index e93709c80c93205734c4b7d606792ca3f81e835c..7781381a9d734eff2b28cceb8f57c70c37cc2301 100644 --- a/src/backend/controllers/tracers.js +++ b/src/backend/controllers/tracers.js @@ -9,51 +9,36 @@ import { CompileError, RuntimeError } from '/common/error'; const router = express.Router(); const repoPath = path.resolve(__dirname, '..', 'public', 'tracers'); -const getLibsPath = (...args) => path.resolve(repoPath, 'libs', ...args); const getCodesPath = (...args) => path.resolve(__dirname, '..', 'public', 'codes', ...args); const buildRelease = release => ( fs.pathExistsSync(repoPath) ? execute(`git fetch && ! git diff-index --quiet ${release.target_commitish}`, repoPath) : execute(`git clone git@github.com:algorithm-visualizer/tracers ${repoPath}`, __dirname) -).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build`, repoPath)); +).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build && ./bin/build`, repoPath)); GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(buildRelease); // TODO: build release when webhooked const getJsWorker = (req, res, next) => { - res.sendFile(getLibsPath('js', 'tracers.js')); + res.sendFile(path.resolve(repoPath, 'src', 'languages', 'js', 'tracers', 'build', 'tracers.js')); }; -const executeInDocker = (imageId, ext, srcPath, command) => { - // TODO: memory limit + time limit + space limit? - const libsPath = getLibsPath(ext); - const dockerCommand = [ - `docker run --rm`, - '-w=/usr/judge', - `-v=${libsPath}:/usr/bin/tracers:ro`, - `-v=${srcPath}:/usr/judge:rw`, - `-e MAX_TRACES=${1e6} -e MAX_TRACERS=${1e2}`, - imageId, - ].join(' '); - return execute(`${dockerCommand} ${command}`, repoPath, { stdout: null, stderr: null }); -}; - -const trace = ({ ext, imageId, compileCommand, runCommand }) => (req, res, next) => { +const trace = lang => (req, res, next) => { const { code } = req.body; - const srcPath = getCodesPath(uuid.v4()); - fs.outputFile(path.resolve(srcPath, `Main.${ext}`), code) - .then(() => executeInDocker(imageId, ext, srcPath, compileCommand) + const tempPath = getCodesPath(uuid.v4()); + fs.outputFile(path.resolve(tempPath, `Main.${lang}`), code) + .then(() => execute(`LANG=${lang} TEMP_PATH=${tempPath} ./bin/compile`, repoPath, { stdout: null, stderr: null }) .catch(error => { throw new CompileError(error); })) - .then(() => executeInDocker(imageId, ext, srcPath, runCommand) + .then(() => execute(`LANG=${lang} TEMP_PATH=${tempPath} ./bin/run`, repoPath, { stdout: null, stderr: null }) .catch(error => { throw new RuntimeError(error); })) - .then(() => res.sendFile(path.resolve(srcPath, 'traces.json'))) + .then(() => res.sendFile(path.resolve(tempPath, 'traces.json'))) .catch(next) - .finally(() => fs.remove(srcPath)); + .finally(() => fs.remove(tempPath)); }; @@ -61,19 +46,9 @@ router.route('/js') .get(getJsWorker); router.route('/java') - .post(trace({ - ext: 'java', - imageId: 'openjdk:8', - compileCommand: 'javac -cp /usr/bin/tracers/tracers.jar Main.java', - runCommand: 'java -cp /usr/bin/tracers/tracers.jar:. Main', - })); + .post(trace('java')); router.route('/cpp') - .post(trace({ - ext: 'cpp', - imageId: 'gcc:8.1', - compileCommand: 'g++ Main.cpp -o Main -O2 -std=c++11 -L/usr/bin/tracers/lib -l:tracers.a -I/usr/bin/tracers/include', - runCommand: './Main', - })); + .post(trace('cpp')); export default router; \ No newline at end of file