From 65e0351a6075564de7245d8442c6dcaa344baed3 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Wed, 6 Nov 2024 08:26:55 +0800 Subject: [PATCH] feat: add a fixer for deprecating cross platform strategy Signed-off-by: Frost Ming --- src/pdm/cli/commands/fix/__init__.py | 4 ++-- src/pdm/cli/commands/fix/fixers.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pdm/cli/commands/fix/__init__.py b/src/pdm/cli/commands/fix/__init__.py index 3f86cc7472..7dfa8e1f51 100644 --- a/src/pdm/cli/commands/fix/__init__.py +++ b/src/pdm/cli/commands/fix/__init__.py @@ -3,7 +3,7 @@ import argparse from pdm.cli.commands.base import BaseCommand -from pdm.cli.commands.fix.fixers import BaseFixer, PackageTypeFixer, ProjectConfigFixer +from pdm.cli.commands.fix.fixers import BaseFixer, LockStrategyFixer, PackageTypeFixer, ProjectConfigFixer from pdm.exceptions import PdmUsageError from pdm.project import Project from pdm.termui import Emoji @@ -49,7 +49,7 @@ def check_problems(project: Project, strict: bool = True) -> None: @staticmethod def get_fixers(project: Project) -> list[BaseFixer]: """Return a list of fixers to check, the order matters""" - return [ProjectConfigFixer(project), PackageTypeFixer(project)] + return [ProjectConfigFixer(project), PackageTypeFixer(project), LockStrategyFixer(project)] def handle(self, project: Project, options: argparse.Namespace) -> None: if options.dry_run: diff --git a/src/pdm/cli/commands/fix/fixers.py b/src/pdm/cli/commands/fix/fixers.py index 0c853844ad..46bcd87126 100644 --- a/src/pdm/cli/commands/fix/fixers.py +++ b/src/pdm/cli/commands/fix/fixers.py @@ -2,7 +2,9 @@ import re from pdm.project import Config, Project +from pdm.project.lockfile import FLAG_CROSS_PLATFORM from pdm.termui import Verbosity +from pdm.utils import parse_version class BaseFixer(abc.ABC): @@ -97,3 +99,22 @@ def fix(self) -> None: # Write the updated settings back to the project self.project.pyproject.write(False) + + +class LockStrategyFixer(BaseFixer): + identifier = "deprecated-cross-platform" + + def get_message(self) -> str: + return "Lock strategy [success]`cross_platform`[/] has been deprecated in favor of lock targets." + + def check(self) -> bool: + lockfile_version = self.project.lockfile.file_version + if not lockfile_version or parse_version(lockfile_version) < parse_version("4.5.0"): + return False + return FLAG_CROSS_PLATFORM in self.project.lockfile.strategy + + def fix(self) -> None: + strategies = self.project.lockfile.strategy - {FLAG_CROSS_PLATFORM} + self.project.lockfile._data["metadata"]["strategy"] = sorted(strategies) + self.project.lockfile.write(False) + self.log("Lock strategy [success]`cross_platform` has been removed.", verbosity=Verbosity.DETAIL)