Enforce formatting of TOML files (#30128)

* Fmt all toml files

* bootstrap taplo

* enforce toml formatting with taplo

* Install taplo in CI using cargo-install action
This commit is contained in:
Samson 2023-09-19 10:57:27 +02:00 committed by GitHub
parent 576887907c
commit 7caac9790d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 4 deletions

View file

@ -41,4 +41,4 @@ linker = "lld-link.exe"
MACOSX_DEPLOYMENT_TARGET = "10.10"
[build]
rustdocflags = ["--document-private-items"]
rustdocflags = ["--document-private-items"]

View file

@ -71,6 +71,11 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install taplo
uses: baptiste0928/cargo-install@v2
with:
crate: taplo-cli
locked: true
- name: Bootstrap Python
run: python3 -m pip install --upgrade pip virtualenv
- name: Bootstrap dependencies

View file

@ -60,6 +60,11 @@ jobs:
fetch-depth: 2
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
- name: Install taplo
uses: baptiste0928/cargo-install@v2
with:
crate: taplo-cli
locked: true
- name: Bootstrap
run: |
python3 -m pip install --upgrade pip virtualenv

View file

@ -55,6 +55,11 @@ jobs:
fetch-depth: 2
- name: ccache
uses: hendrikmuhs/ccache-action@v1.2
- name: Install taplo
uses: baptiste0928/cargo-install@v2
with:
crate: taplo-cli
locked: true
- name: Install wixtoolset
run: |
choco install wixtoolset

View file

@ -8,6 +8,7 @@
# except according to those terms.
import os
import shutil
import subprocess
from typing import Dict, Optional
@ -96,9 +97,21 @@ class Base:
)
def bootstrap(self, force: bool):
if not self._platform_bootstrap(force):
installed_something = self._platform_bootstrap(force)
installed_something |= self.install_taplo(force)
if not installed_something:
print("Dependencies were already installed!")
def install_taplo(self, force: bool) -> bool:
if not force and shutil.which("taplo") is not None:
return False
if subprocess.call(["cargo", "install", "taplo-cli", "--locked"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE) != 0:
raise EnvironmentError("Installation of taplo failed.")
return True
def passive_bootstrap(self) -> bool:
"""A bootstrap method that is called without explicitly invoking `./mach bootstrap`
but that is executed in the process of other `./mach` commands. This should be

View file

@ -57,6 +57,20 @@ TEST_SUITES = OrderedDict([
TEST_SUITES_BY_PREFIX = {path: k for k, v in TEST_SUITES.items() if "paths" in v for path in v["paths"]}
def format_toml_files_with_taplo(check_only: bool = True) -> int:
taplo = shutil.which("taplo")
if taplo is None:
print("Taplo is not installed.")
print("It should be installed using `./mach bootstrap`, \
but it can be installed manually using `cargo install taplo-cli --locked`")
return 1
if check_only:
return call([taplo, "fmt", "--check"], env={'RUST_LOG': 'error'})
else:
return call([taplo, "fmt"], env={'RUST_LOG': 'error'})
@CommandProvider
class MachCommands(CommandBase):
DEFAULT_RENDER_MODE = "cpu"
@ -279,7 +293,9 @@ class MachCommands(CommandBase):
if rustfmt_failed:
print("Run `./mach fmt` to fix the formatting")
return tidy_failed or manifest_dirty or rustfmt_failed
taplo_failed = format_toml_files_with_taplo()
return tidy_failed or manifest_dirty or rustfmt_failed or taplo_failed
@Command('test-scripts',
description='Run tests for all build and support scripts.',
@ -370,9 +386,12 @@ class MachCommands(CommandBase):
return wpt.manifestupdate.update(check_clean=False)
@Command('fmt',
description='Format the Rust and CPP source files with rustfmt',
description='Format Rust and TOML files',
category='testing')
def format_code(self):
result = format_toml_files_with_taplo(check_only=False)
if result != 0:
return result
return call(["cargo", "fmt"])
@Command('update-wpt',