From 21f05264c0ae9c0d892d4110b00157fa898a3992 Mon Sep 17 00:00:00 2001 From: Menci Date: Sat, 23 Mar 2019 21:52:47 +0800 Subject: [PATCH] Add mirgrate for code formatting --- README.en.md | 2 ++ README.md | 2 ++ migrates/format-old-codes.js | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100755 migrates/format-old-codes.js diff --git a/README.en.md b/README.en.md index 4c3abcc..886a02f 100644 --- a/README.en.md +++ b/README.en.md @@ -45,4 +45,6 @@ Who upgraded from a commit BEFORE [d8be150fc6b8c43af61c5e4aca4fc0fe0445aef3](htt ALTER TABLE `user` ADD `prefer_formatted_code` TINYINT(1) NOT NULL DEFAULT 1 AFTER `public_email`; ``` +To make code formatting work, `clang-format` needs to be installed. [migrates/format-old-codes.js](migrates/format-old-codes.js) may help formating old submissions' codes. + Who upgraded from a commit BEFORE [c192e8001ac81cab132ae033b39f09a094587633](https://github.com/syzoj/syzoj/commit/c192e8001ac81cab132ae033b39f09a094587633) (Mar 23, 2019) **MUST** install `redis-server` and [pygments](http://pygments.org/) on the web server. Markdown contents may be broken by switching to new renderer, [migrates/html-table-merge-cell-to-md.js](migrates/html-table-merge-cell-to-md.js) may help the migration。 diff --git a/README.md b/README.md index a0405a4..de290c4 100644 --- a/README.md +++ b/README.md @@ -45,4 +45,6 @@ ALTER TABLE `problem` ADD `publicize_time` DATETIME NOT NULL DEFAULT CURRENT_TIM ALTER TABLE `user` ADD `prefer_formatted_code` TINYINT(1) NOT NULL DEFAULT 1 AFTER `public_email`; ``` +为使代码格式化功能正常工作,`clang-format` 需要被安装。[migrates/format-old-codes.js](migrates/format-old-codes.js) 可能对格式化旧提交记录的代码有帮助。 + 从该 commit [c192e8001ac81cab132ae033b39f09a094587633](https://github.com/syzoj/syzoj/commit/c192e8001ac81cab132ae033b39f09a094587633)(2019 年 3 月 23 日)前更新的用户**必须**在网站服务器上安装 `redis-server` 与 [pygments](http://pygments.org/)。旧的 Markdown 内容可能因切换到新渲染器被破坏,[migrates/html-table-merge-cell-to-md.js](migrates/html-table-merge-cell-to-md.js) 可能对迁移有所帮助。 diff --git a/migrates/format-old-codes.js b/migrates/format-old-codes.js new file mode 100755 index 0000000..28b6599 --- /dev/null +++ b/migrates/format-old-codes.js @@ -0,0 +1,47 @@ +/* + * This script will help format all submissions' codes whose id in a interval. + * + * Useful when upgrading from a version that doesn't support code formatting. + */ + +const JudgeState = syzoj.model('judge_state'); +const FormattedCode = syzoj.model('formatted_code'); +const CodeFormatter = syzoj.lib('code_formatter'); +require('.'); + +const fn = async (begin, end) => { + for (let i = begin; i < end; i++) { + const judge_state = await JudgeState.fromID(i); + if (!judge_state) continue; + + if (!judge_state.language) continue; + + const key = syzoj.utils.getFormattedCodeKey(judge_state.code, judge_state.language); + if (!key) continue; + + let formatted_code = await FormattedCode.findOne({ where: { key: key } }); + + const code = await CodeFormatter(judge_state.code, syzoj.languages[judge_state.language].format); + if (code === null) { + console.error(`Format ${i} failed.`); + continue; + } + + if (!formatted_code) { + formatted_code = await FormattedCode.create({ + key: key, + code: code + }); + } else continue; // formatted_code.code = code; + + try { + await formatted_code.save(); + console.error(`Format and save ${i} success.`); + } catch (e) { + console.error(`Save ${i} failed:`, e); + } + } +}; + +// NOTE: Uncomment and fill arguments to run. +// fn(begin, end) // [begin, end)