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" MACOSX_DEPLOYMENT_TARGET = "10.10"
[build] [build]
rustdocflags = ["--document-private-items"] rustdocflags = ["--document-private-items"]

View file

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

View file

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

View file

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

View file

@ -8,6 +8,7 @@
# except according to those terms. # except according to those terms.
import os import os
import shutil
import subprocess import subprocess
from typing import Dict, Optional from typing import Dict, Optional
@ -96,9 +97,21 @@ class Base:
) )
def bootstrap(self, force: bool): 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!") 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: def passive_bootstrap(self) -> bool:
"""A bootstrap method that is called without explicitly invoking `./mach bootstrap` """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 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"]} 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 @CommandProvider
class MachCommands(CommandBase): class MachCommands(CommandBase):
DEFAULT_RENDER_MODE = "cpu" DEFAULT_RENDER_MODE = "cpu"
@ -279,7 +293,9 @@ class MachCommands(CommandBase):
if rustfmt_failed: if rustfmt_failed:
print("Run `./mach fmt` to fix the formatting") 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', @Command('test-scripts',
description='Run tests for all build and support scripts.', description='Run tests for all build and support scripts.',
@ -370,9 +386,12 @@ class MachCommands(CommandBase):
return wpt.manifestupdate.update(check_clean=False) return wpt.manifestupdate.update(check_clean=False)
@Command('fmt', @Command('fmt',
description='Format the Rust and CPP source files with rustfmt', description='Format Rust and TOML files',
category='testing') category='testing')
def format_code(self): def format_code(self):
result = format_toml_files_with_taplo(check_only=False)
if result != 0:
return result
return call(["cargo", "fmt"]) return call(["cargo", "fmt"])
@Command('update-wpt', @Command('update-wpt',