Browse Source

Add mirgrate for code formatting

master
Menci 6 years ago
parent
commit
21f05264c0
  1. 2
      README.en.md
  2. 2
      README.md
  3. 47
      migrates/format-old-codes.js

2
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。

2
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) 可能对迁移有所帮助。

47
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)
Loading…
Cancel
Save