(developing_for_wagtail)= # 搭建开发环境 搭建一份 [Wagtail git 仓库](https://github.com/wagtail/wagtail)的本地副本比运行 Wagtail 的发布包要稍微复杂一些,因为它需要 [Node.js](https://nodejs.org/) 和 npm 来构建 JavaScript 与 CSS 资源。(运行发布版本时不需要这些,因为编译好的资源已包含在发布包中。) 如果你愿意在本地的虚拟机上进行开发,[docker-wagtail-develop](https://github.com/wagtail/docker-wagtail-develop) 和 [vagrant-wagtail-develop](https://github.com/wagtail/vagrant-wagtail-develop) 这两套脚本是最快的上手方式。它们会为你提供一个正在运行的 [Wagtail Bakery 演示站点](https://github.com/wagtail/bakerydemo/),并把 Wagtail 与 bakerydemo 的代码库作为共享文件夹提供出来,方便你在宿主机上编辑。 你也可以用 [gitpod-wagtail-develop](https://github.com/wagtail/gitpod-wagtail-develop) 项目搭建一个云开发环境,在浏览器里的 IDE 中工作。 (非常欢迎为其它平台编写构建脚本 —— 如果你写了一个,请通过 [Slack 工作区](https://github.com/wagtail/wagtail/wiki/Slack)告诉我们!) 如果你更愿意手动把各个组件都搭好,请继续往下读。以下说明假定你熟悉用 pip 和[虚拟环境](inv:python#tutorial/venv)管理 Python 包。 ## 搭建 Wagtail 代码库 安装正确版本 Node 的首选方式是使用 [Fast Node Manager (fnm)](https://github.com/Schniz/fnm),它总能与项目根目录下提供的 `.nvmrc` 文件对齐版本。要确保你运行的是正确的 Node 版本,在项目根目录运行 `fnm install`。 或者,你也可以直接安装 [Node.js](https://nodejs.org/),但务必安装项目根目录 `.nvmrc` 文件中声明的那个版本。 如果你还没有安装 **libjpeg** 和 **zlib** 库,也需要把它们装上 —— 见 Pillow 的[各平台安装说明](https://pillow.readthedocs.io/en/stable/installation/building-from-source.html#external-libraries)。 Fork [Wagtail 代码库](https://github.com/wagtail/wagtail)并克隆这个 fork 的副本: ```sh git clone https://github.com/username/wagtail.git cd wagtail ``` **在[激活了你偏好的 virtualenv](virtual_environment_creation) 的情况下,** 以开发模式安装 Wagtail 包,并带上其中包含的测试与文档依赖: ```sh pip install -e ."[testing,docs]" --config-settings editable-mode=strict -U ``` 或者在 Windows 上 ```doscon pip install -e .[testing,docs] --config-settings editable-mode=strict -U ``` 安装用于构建静态资源的工具链: ```sh npm ci ``` 编译资源: ```sh npm run build ``` 此后你在这个 virtualenv 中启动的任何 Wagtail 站点,都会对着这个开发版 Wagtail 实例运行。我们建议以 [Wagtail Bakery 演示站点](https://github.com/wagtail/bakerydemo/)作为开发 Wagtail 的基础。请记住,Wagtail 站点的搭建步骤中可能会安装 Wagtail 的发布版本,那会覆盖你刚搭好的开发版本。这种情况下,要在你的 Wagtail 站点的 virtualenv 中安装本地的 Wagtail 开发实例: ```sh pip install -e path/to/wagtail"[testing,docs]" --config-settings editable-mode=strict -U ``` 或者在 Windows 上 ```doscon pip install -e path/to/wagtail[testing,docs] --config-settings editable-mode=strict -U ``` 这里,`path/to/wagtail` 是你本地 Wagtail 副本的路径。 (development_on_windows)= ## 在 Windows 上开发 关于在 Windows 上开发的文档尚有一些空缺,应视为仍在完善中。我们建议用我们现成的脚本 [docker-wagtail-develop](https://github.com/wagtail/docker-wagtail-develop) 或 [vagrant-wagtail-develop](https://github.com/wagtail/vagrant-wagtail-develop) 在本地的虚拟机上搭建。 如果你对 Windows 上的 Python 和 Node 开发有把握并且希望就在此进行,这里有一些有用的提示。 在 Windows 上管理包,我们推荐 [Chocolatey](https://chocolatey.org/install)。安装好 Chocolatey 之后,你就可以安装 [`make`](https://community.chocolatey.org/packages/make) 工具,以便运行常见的构建和开发命令。 我们使用 LF 作为换行符。为了在不同操作系统上的其他开发者有效协作,请通过把 `core.autocrlf` 配置设为 `true`,使用 Git 的自动 CRLF 处理: ```doscon git config --global core.autocrlf true ``` (testing)= ## 测试 在 Wagtail 代码库根目录下,运行以下命令来运行所有 Python 测试: ```sh python runtests.py ``` ### 加快测试运行 Wagtail 的 Python 测试套件相当全面,跑起来要花一些时间。要加快测试运行,你可以传入 `--parallel` 参数并行运行测试: ```sh python runtests.py --parallel ``` Wagtail 自带用于创建内置模型和初始数据的迁移。此外,测试项目还包含测试模型的迁移。运行这些迁移会花一些时间,因此用 `--keepdb` 参数把数据库在多次测试运行之间保留下来会很有用。 Django 中[存在一个已知问题](https://code.djangoproject.com/ticket/25251):使用 `--keepdb` 时,任何使用 `TransactionTestCase` 的测试类都会导致初始数据在多次测试运行之间丢失。为避免这一点,我们在 Wagtail 中给这类测试打上了 `transaction` 标记,当你不在意它们的结果时,可以用 `--exclude-tag` 参数把它们排除掉。 ```sh python runtests.py --parallel --keepdb --exclude-tag=transaction ``` 如果你使用 SQLite,请设置 `DATABASE_NAME` 环境变量,以便使用并保留一个数据库文件,而不是内存数据库。 ```sh DATABASE_NAME=default.sqlite3 python runtests.py --parallel --keepdb --exclude-tag=transaction ``` 如果自上次测试运行以来出现了新的迁移,由于 SQLite 的[一个已知 Django 问题](https://code.djangoproject.com/ticket/26822),你可能需要删除克隆出来的测试数据库。 ### 只运行部分测试 你可以把路径作为参数传给 `runtests.py` 或 `tox`,从而只运行 Wagtail 某一部分的 测试: ```sh # Running in the current environment python runtests.py wagtail.admin # Running in a specified Tox environment tox -e py39-dj32-sqlite-noelasticsearch -- wagtail.admin # See a list of available Tox environments tox -l ``` 你也可以把路径作为参数传给 `runtests.py`,从而运行单个 `TestCase` 或方法的 测试 ```sh # Running in the current environment python runtests.py -- wagtail.tests.test_blocks.TestIntegerBlock wagtail.tests.test_blocks.TestBlock.test_normalize # Running in a specified Tox environment tox -e py39-dj32-sqlite-noelasticsearch -- wagtail.tests.test_blocks.TestIntegerBlock wagtail.tests.test_blocks.TestBlock.test_normalize ``` ### 为测试应用模型运行迁移 你可以在 Wagtail 根目录下运行下面的命令,为测试应用创建迁移。 ```sh django-admin makemigrations --settings=wagtail.test.settings ``` ### 针对 PostgreSQL 测试 ```{note} 要运行这些测试,你必须按 Django 的[数据库文档](inv:django#ref/databases)所述安装 PostgreSQL 所需的模块。 ``` 默认情况下,Wagtail 针对 SQLite 测试。你可以用 `--postgres` 参数 切换到使用 PostgreSQL: ```sh python runtests.py --postgres ``` 如果你需要使用不同的用户、密码、主机或端口,请分别使用 `PGUSER`、`PGPASSWORD`、`PGHOST` 和 `PGPORT` 环境变量。 ### 针对其它数据库测试 ```{note} 要运行这些测试,你必须按 Django 的[数据库文档](inv:django#ref/databases)或第三方数据库后端的文档所述,安装对应数据库所需的客户端库和模块。 ``` 如果你需要针对其它数据库测试,把 `DATABASE_ENGINE` 环境变量设为目标 Django 数据库后端的名称: ```sh DATABASE_ENGINE=django.db.backends.mysql python runtests.py ``` 这会在 MySQL 中新建一个名为 `test_wagtail` 的数据库,并对它运行 测试。 如果你需要使用不同的连接设置,请使用下列环境变量,它们对应 Django [`DATABASES`](inv:django#DATABASES) 设置字典中的相应键: - `DATABASE_ENGINE` - `DATABASE_NAME` - `DATABASE_PASSWORD` - `DATABASE_HOST` - 注意对 MySQL 而言,如果你需要通过 TCP socket 连接,这里必须是 `127.0.0.1` 而不是 `localhost` - `DATABASE_PORT` 也可以设置 `DATABASE_DRIVER`,在使用 SQL Server 引擎时它对应 `OPTIONS` 内的 `driver` 值。 ### 测试 Elasticsearch 与 OpenSearch 你可以传入 `--elasticsearch7`、`--elasticsearch8`、`--elasticsearch9`、`--opensearch2`、`--opensearch3` 中的一个参数(对应你想测试的 Elasticsearch 或 OpenSearch 版本),从而针对 Elasticsearch 或 OpenSearch 测试 Wagtail: ```sh python runtests.py --elasticsearch8 ``` Wagtail 会尝试连接本地实例的 Elasticsearch 或 OpenSearch (`http://localhost:9200`)并使用索引 `test_wagtail`。 如果你的实例位于其它位置,可以设置 `ELASTICSEARCH_URL` 环境变量指向其位置: ```sh ELASTICSEARCH_URL=https://my-elasticsearch-instance:9200 python runtests.py --elasticsearch8 ``` 注意环境变量 `ELASTICSEARCH_URL` 对 Elasticsearch 和 OpenSearch 都适用。 ### JavaScript 单元测试 我们使用 [Jest](https://jestjs.io/) 对客户端业务逻辑或 UI 组件做单元测试。在 Wagtail 代码库根目录下运行以下命令,即可运行所有前端单元测试: ```sh npm run test:unit ``` ### 集成测试 我们的端到端浏览器测试套件同样使用 [Jest](https://jestjs.io/),并结合 [Playwright](https://playwright.dev/)。我们把它安排为单独安装,以免增大现有 Node 工具的安装体积。要运行这些测试,你需要安装依赖,并在另一个终端中运行该测试套件所需的 Django 开发服务器: ```sh export DJANGO_SETTINGS_MODULE=wagtail.test.settings_ui # Assumes the current environment contains a valid installation of Wagtail for local development. ./wagtail/test/manage.py migrate ./wagtail/test/manage.py createcachetable DJANGO_SUPERUSER_EMAIL=admin@example.com DJANGO_SUPERUSER_USERNAME=admin DJANGO_SUPERUSER_PASSWORD=changeme ./wagtail/test/manage.py createsuperuser --noinput ./wagtail/test/manage.py runserver 0:8000 # In a separate terminal: npm --prefix client/tests/integration install npm run test:integration ``` 集成测试默认面向 `http://127.0.0.1:8000`。要使用其它端口或测试远程 Wagtail 实例,请使用 `TEST_ORIGIN` 环境变量:`TEST_ORIGIN=http://127.0.0.1:9000 npm run test:integration`。 ## 编译静态资源 Wagtail 管理后台的所有静态资源(如 JavaScript、CSS、图片和字体)都由 Webpack 从各自的源码编译而来。编译出的资源不会提交到仓库,并且会在打包每个新版本之前编译。编译出的资源不应作为 pull request 的一部分提交。 要编译资源,请运行: ```sh npm run build ``` 每次修改源文件之后都必须执行这一步。要监听源文件的变化并自动重新编译资源,请运行: ```sh npm start ``` ## 编译文档 Wagtail 的文档由 Sphinx 构建。要安装 Sphinx 并编译文档,请运行: ```sh # Starting from the wagtail root directory: # Install the documentation dependencies pip install -e .[docs] --config-settings editable-mode=strict # or if using zsh as your shell: # pip install -e '.[docs]' -U # Compile the docs cd docs/ make html ``` 编译好的文档现在位于 `docs/_build/html`。 在浏览器中打开该目录即可查看。 Python 自带一个模块,可以让在浏览器中预览静态文件变得非常容易。 要启动这个简单的服务器,请运行以下命令: ```sh # Starting from the wagtail root directory: cd docs/_build/html/ python -m http.server 8080 ``` 现在你就可以在浏览器中打开 查看编译好的文档了。 Sphinx 会缓存已构建的文档,以加快后续编译。 可惜这个缓存也会隐藏掉未修改的文档源文件抛出的警告。 要清空已构建的 HTML 并重新开始,从而看到构建文档时抛出的所有警告,请运行: ```sh # Starting from the wagtail root directory: cd docs/ make clean make html ``` Wagtail 还提供了一种在每次修改时自动编译文档的方式。 为此,你可以运行以下命令,在 `localhost:4000` 处自动看到变化: ```sh # Starting from the wagtail root directory: cd docs/ make livehtml ``` (linting_and_formatting)= ## 代码检查与格式化 Wagtail 使用多种工具来保证整个代码库的一致性与可读性: - [Ruff](https://github.com/astral-sh/ruff) 用于 Python 代码的格式化与检查,包括强制执行 [PEP8](https://peps.python.org/pep-0008/) 和 [isort](https://pycqa.github.io/isort/) 规则 - [ty](https://docs.astral.sh/ty/) 用于渐进式类型检查 - [djhtml](https://github.com/rtts/djhtml) 和 [Curlylint](https://www.curlylint.org/) 用于 HTML 模板的格式化与检查 - [Prettier](https://prettier.io/)、[Stylelint](https://stylelint.io/) 和 [ESLint](https://eslint.org/) 用于 JavaScript 与 CSS 代码的格式化与检查 所有贡献都应遵循这些标准,我们也鼓励你在本地运行这些工具,以免你的贡献被接受的过程出现延迟。可用的命令如下: - `make lint` 会运行所有检查,`make lint-server` 检查 Python 与模板代码,`make lint-client` 检查 JS/CSS。 - `make format` 会运行所有格式化并修复检查问题。此外还有 `make format-server` 和 `make format-client`。 如果你更偏好细粒度的选项,可以看看我们 `Makefile` 中的任务和 `package.json` 中的脚本。 ### 在提交时自动做代码检查与格式化 [pre-commit](https://pre-commit.com/) 已配置为在每次提交时自动运行代码检查与格式化。要把 pre-commit 安装到你的 git hooks 中,请运行: ```sh pre-commit install ``` 此后你每次提交时 pre-commit 都会运行。 (developing_using_a_fork)= ## 从 fork 安装 有时可能需要从某个 fork 安装 Wagtail。例如你的站点依赖一个目前正在等待审核的 bug 修复,而你等不起一个新版本。 Wagtail 的发布流程包含静态资源构建和翻译更新的步骤,这意味着你不能通过修改 requirements 文件指向主仓库中某个特定的 git 提交。 要从你的 fork 安装,请确保已安装 `build`(`python -m pip install build`)以及构建静态资源的工具(`npm install`)。然后,在你的 Wagtail git 检出根目录下运行: ```sh python -m build ``` 这会在 `dist/,` 中生成 `.tar.gz` 和 `.whl` 包,可以用 `pip` 安装。 对于远程部署,通常最方便的做法是把它上传到某个公开 URL,并在项目的 requirements 中用该 URL 替代标准的 `wagtail` 那一行。