diff --git a/.taplo.toml b/.taplo.toml index 0d0d4461..6e25fde3 100644 --- a/.taplo.toml +++ b/.taplo.toml @@ -1,8 +1,4 @@ exclude = [ - "**/Cargo.toml", - "**/Manifest.toml", - "**/Project.toml", - "labels*.toml", "labels/*.toml", ] diff --git a/.vscode/settings.json b/.vscode/settings.json index 96590612..402199ce 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -57,13 +57,13 @@ ".constraints/*.txt": true, ".github/workflows/cd.yml": true, ".github/workflows/ci.yml": true, + ".taplo.toml": true, "src/repoma/.github/workflows/clean-caches.yml": true, "src/repoma/.github/workflows/pr-linting.yml": true, "src/repoma/.github/workflows/release-drafter.yml": true, "src/repoma/.template/.cspell.json": true, "src/repoma/.template/.gitpod.yml": true, - "src/repoma/.template/.prettierrc": true, - "src/repoma/.template/.taplo.toml": true + "src/repoma/.template/.prettierrc": true }, "yaml.schemas": { "https://citation-file-format.github.io/1.2.0/schema.json": "CITATION.cff", diff --git a/src/repoma/.template/.taplo.toml b/src/repoma/.template/.taplo.toml deleted file mode 120000 index 0c8b8cb5..00000000 --- a/src/repoma/.template/.taplo.toml +++ /dev/null @@ -1 +0,0 @@ -../../../.taplo.toml \ No newline at end of file diff --git a/src/repoma/.template/.taplo.toml b/src/repoma/.template/.taplo.toml new file mode 100644 index 00000000..0d0d4461 --- /dev/null +++ b/src/repoma/.template/.taplo.toml @@ -0,0 +1,20 @@ +exclude = [ + "**/Cargo.toml", + "**/Manifest.toml", + "**/Project.toml", + "labels*.toml", + "labels/*.toml", +] + +[formatting] +align_comments = false +align_entries = false +allowed_blank_lines = 1 +array_auto_collapse = false +array_auto_expand = true +array_trailing_comma = true +column_width = 88 +compact_inline_tables = true +indent_string = " " +reorder_arrays = true +reorder_keys = true diff --git a/src/repoma/check_dev_files/toml.py b/src/repoma/check_dev_files/toml.py index 4a8370b0..b543e25f 100644 --- a/src/repoma/check_dev_files/toml.py +++ b/src/repoma/check_dev_files/toml.py @@ -5,6 +5,7 @@ from pathlib import Path from typing import List, Union +import tomlkit from ruamel.yaml.comments import CommentedMap from repoma.errors import PrecommitError @@ -94,12 +95,20 @@ def _update_taplo_config() -> None: msg = f"Added {CONFIG_PATH.taplo} config for TOML formatting" raise PrecommitError(msg) with open(template_path) as f: - expected_content = f.read() + expected = tomlkit.load(f) + excludes: List[str] = [p for p in expected["exclude"] if glob(p, recursive=True)] # type: ignore[union-attr] + if excludes: + excludes = sorted(excludes, key=str.lower) + expected["exclude"] = to_toml_array(excludes, enforce_multiline=True) + else: + del expected["exclude"] with open(CONFIG_PATH.taplo) as f: - existing_content = f.read() - if existing_content != expected_content: + existing = tomlkit.load(f) + expected_str = tomlkit.dumps(expected) + existing_str = tomlkit.dumps(existing) + if existing_str != expected_str: with open(CONFIG_PATH.taplo, "w") as stream: - stream.write(expected_content) + stream.write(expected_str) msg = f"Updated {CONFIG_PATH.taplo} config file" raise PrecommitError(msg) diff --git a/src/repoma/utilities/pyproject.py b/src/repoma/utilities/pyproject.py index 2f02e5c9..c7b7b32f 100644 --- a/src/repoma/utilities/pyproject.py +++ b/src/repoma/utilities/pyproject.py @@ -45,10 +45,10 @@ def write_pyproject(config: TOMLDocument) -> None: stream.write(src) -def to_toml_array(items: Iterable[Any]) -> Array: +def to_toml_array(items: Iterable[Any], enforce_multiline: bool = False) -> Array: array = tomlkit.array() array.extend(items) - if len(array) > 1: + if enforce_multiline or len(array) > 1: array.multiline(True) else: array.multiline(False)