Skip to content

Commit

Permalink
feat: add a fixer for deprecating cross platform strategy
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Nov 6, 2024
1 parent 0097247 commit 65e0351
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pdm/cli/commands/fix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions src/pdm/cli/commands/fix/fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."

Check warning on line 108 in src/pdm/cli/commands/fix/fixers.py

View check run for this annotation

Codecov / codecov/patch

src/pdm/cli/commands/fix/fixers.py#L108

Added line #L108 was not covered by tests

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)

Check warning on line 120 in src/pdm/cli/commands/fix/fixers.py

View check run for this annotation

Codecov / codecov/patch

src/pdm/cli/commands/fix/fixers.py#L117-L120

Added lines #L117 - L120 were not covered by tests

0 comments on commit 65e0351

Please sign in to comment.