• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

waketzheng / fast-dev-cli / 26871492901

03 Jun 2026 07:55AM UTC coverage: 85.342% (+0.3%) from 85.025%
26871492901

push

github

waketzheng
test: strip ANSI from deps option error output

1048 of 1228 relevant lines covered (85.34%)

4.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.33
/fast_dev_cli/cli.py
1
from __future__ import annotations
5✔
2

3
import contextlib
5✔
4
import functools
5✔
5
import importlib.metadata as importlib_metadata
5✔
6
import os
5✔
7
import platform
5✔
8
import re
5✔
9
import shlex
5✔
10
import shutil
5✔
11
import subprocess  # nosec:B404
5✔
12
import sys
5✔
13
from functools import cached_property
5✔
14
from pathlib import Path
5✔
15
from typing import TYPE_CHECKING, Any, Literal, cast, get_args, overload
5✔
16

17
import typer
5✔
18
from typer import Exit, Option, echo, secho
5✔
19
from typer.models import ArgumentInfo, OptionInfo
5✔
20

21
try:
5✔
22
    from . import __version__
5✔
23
except ImportError:  # pragma: no cover
24
    from importlib import import_module as _import  # For local unittest
25

26
    __version__ = _import(Path(__file__).parent.name).__version__
27

28
if sys.version_info >= (3, 11):  # pragma: no cover
29
    from enum import StrEnum
30

31
    import tomllib
32
else:  # pragma: no cover
33
    from enum import Enum
34

35
    import tomli as tomllib
36

37
    class StrEnum(str, Enum):
38
        __str__ = str.__str__
39

40

41
if TYPE_CHECKING:
42
    if sys.version_info >= (3, 11):
43
        from typing import Self
44
    else:
45
        from typing_extensions import Self
46

47
cli = typer.Typer(no_args_is_help=True)
5✔
48
DryOption = Option(False, "--dry", help="Only print, not really run shell command")
5✔
49
TOML_FILE = "pyproject.toml"
5✔
50
ToolName = Literal["uv", "pdm", "poetry"]
5✔
51
ToolOption = Option(
5✔
52
    "auto", "--tool", help="Explicit declare manage tool (default to auto detect)"
53
)
54

55

56
class FastDevCliError(Exception):
5✔
57
    """Basic exception of this library, all custom exceptions inherit from it"""
58

59

60
class ShellCommandError(FastDevCliError):
5✔
61
    """Raise if cmd command returncode is not zero"""
62

63

64
class ParseError(FastDevCliError):
5✔
65
    """Raise this if parse dependence line error"""
66

67

68
class EnvError(FastDevCliError):
5✔
69
    """Raise when expected to be managed by poetry, but toml file not found."""
70

71

72
def poetry_module_name(name: str) -> str:
5✔
73
    """Get module name that generated by `poetry new`"""
74
    try:
5✔
75
        from packaging.utils import canonicalize_name
5✔
76
    except ImportError:
×
77
        module_name = re.sub(r"[-_.]+", "-", name)
×
78
    else:
79
        module_name = canonicalize_name(name)
5✔
80
    return module_name.replace("-", "_").replace(" ", "_")
5✔
81

82

83
def is_emoji(char: str) -> bool:
5✔
84
    try:
5✔
85
        import emoji
5✔
86
    except ImportError:
×
87
        pass
×
88
    else:
89
        return emoji.is_emoji(char)
5✔
90
    if re.match(r"[\w\d\s]", char):
×
91
        return False
×
92
    return not "\u4e00" <= char <= "\u9fff"  # Chinese character
×
93

94

95
@functools.cache
5✔
96
def is_windows() -> bool:
5✔
97
    return platform.system() == "Windows"
5✔
98

99

100
@functools.cache
5✔
101
def prefer_uv_tool() -> bool:
5✔
102
    if shutil.which("uv") is None:
5✔
103
        return False
×
104
    cmd = "uv tool list"
5✔
105
    return Shell(cmd).capture_output() != "No tools installed"
5✔
106

107

108
def yellow_warn(msg: str) -> None:
5✔
109
    if is_windows() and (encoding := sys.stdout.encoding) != "utf-8":
5✔
110
        msg = msg.encode(encoding, errors="ignore").decode(encoding)
×
111
    secho(msg, fg="yellow")
5✔
112

113

114
def load_bool(name: str, default: bool = False) -> bool:
5✔
115
    if not (v := os.getenv(name)):
5✔
116
        return default
5✔
117
    if (lower := v.lower()) in ("0", "false", "f", "off", "no", "n"):
5✔
118
        return False
5✔
119
    elif lower in ("1", "true", "t", "on", "yes", "y"):
5✔
120
        return True
5✔
121
    secho(f"WARNING: can not convert value({v!r}) of {name} to bool!")
5✔
122
    return default
5✔
123

124

125
def is_venv() -> bool:
5✔
126
    """Whether in a virtual environment(also work for poetry)"""
127
    return hasattr(sys, "real_prefix") or (
5✔
128
        hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix
129
    )
130

131

132
class Shell:
5✔
133
    def __init__(self, cmd: list[str] | str, **kw: Any) -> None:
5✔
134
        self._cmd = cmd
5✔
135
        self._kw = kw
5✔
136

137
    @staticmethod
5✔
138
    def run_by_subprocess(
5✔
139
        cmd: list[str] | str, **kw: Any
140
    ) -> subprocess.CompletedProcess[str]:
141
        if isinstance(cmd, str):
5✔
142
            kw.setdefault("shell", True)
5✔
143
        return subprocess.run(cmd, **kw)  # nosec:B603
5✔
144

145
    @property
5✔
146
    def command(self) -> list[str] | str:
5✔
147
        command: list[str] | str = self._cmd
5✔
148
        if isinstance(command, str):
5✔
149
            cs = shlex.split(command)
5✔
150
            if "shell" not in self._kw and not (set(self._cmd) & {"|", ">", "&"}):
5✔
151
                command = self.extend_user(cs)
5✔
152
            elif any(i.startswith("~") for i in cs):
5✔
153
                command = re.sub(r" ~", " " + os.path.expanduser("~"), command)
×
154
        else:
155
            command = self.extend_user(command)
5✔
156
        return command
5✔
157

158
    @staticmethod
5✔
159
    def extend_user(cs: list[str]) -> list[str]:
5✔
160
        if cs[0] == "echo":
5✔
161
            return cs
5✔
162
        for i, c in enumerate(cs):
5✔
163
            if c.startswith("~"):
5✔
164
                cs[i] = os.path.expanduser(c)
×
165
        return cs
5✔
166

167
    def _run(self) -> subprocess.CompletedProcess[str]:
5✔
168
        return self.run_by_subprocess(self.command, **self._kw)
5✔
169

170
    def run(self, verbose: bool = False, dry: bool = False) -> int:
5✔
171
        if verbose:
5✔
172
            echo(f"--> {self._cmd}")
5✔
173
        if dry:
5✔
174
            return 0
5✔
175
        return self._run().returncode
5✔
176

177
    def check_call(self) -> bool:
5✔
178
        self._kw.update(stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
5✔
179
        try:
5✔
180
            return self.run() == 0
5✔
181
        except FileNotFoundError:
×
182
            return False
×
183

184
    def capture_output(self, raises: bool = False) -> str:
5✔
185
        self._kw.update(capture_output=True, encoding="utf-8")
5✔
186
        r = self._run()
5✔
187
        if raises and r.returncode != 0:
5✔
188
            raise ShellCommandError(r.stderr)
5✔
189
        return (r.stdout or r.stderr or "").strip()
5✔
190

191
    def finish(
5✔
192
        self, env: dict[str, str] | None = None, _exit: bool = False, dry: bool = False
193
    ) -> subprocess.CompletedProcess[str]:
194
        self.run(verbose=True, dry=True)
5✔
195
        if _ensure_bool(dry):
5✔
196
            return subprocess.CompletedProcess("", 0)
5✔
197
        if env is not None:
5✔
198
            self._kw["env"] = {**os.environ, **env}
5✔
199
        r = self._run()
5✔
200
        if rc := r.returncode:
5✔
201
            if _exit:
5✔
202
                sys.exit(rc)
5✔
203
            raise Exit(rc)
5✔
204
        return r
5✔
205

206

207
def run_and_echo(
5✔
208
    cmd: str, *, dry: bool = False, verbose: bool = True, **kw: Any
209
) -> int:
210
    """Run shell command with subprocess and print it"""
211
    return Shell(cmd, **kw).run(verbose=verbose, dry=dry)
5✔
212

213

214
def check_call(cmd: str) -> bool:
5✔
215
    return Shell(cmd).check_call()
5✔
216

217

218
def capture_cmd_output(
5✔
219
    command: list[str] | str, *, raises: bool = False, **kw: Any
220
) -> str:
221
    return Shell(command, **kw).capture_output(raises=raises)
5✔
222

223

224
def exit_if_run_failed(
5✔
225
    cmd: str,
226
    env: dict[str, str] | None = None,
227
    _exit: bool = False,
228
    dry: bool = False,
229
    **kw: Any,
230
) -> subprocess.CompletedProcess[str]:
231
    return Shell(cmd, **kw).finish(env=env, _exit=_exit, dry=dry)
5✔
232

233

234
def _parse_version(line: str, pattern: re.Pattern[str]) -> str:
5✔
235
    return pattern.sub("", line).split("#")[0].strip().strip(" '\"")
5✔
236

237

238
def read_version_from_file(
5✔
239
    package_name: str, work_dir: Path | None = None, toml_text: str | None = None
240
) -> str:
241
    if not package_name and toml_text:
5✔
242
        pattern = re.compile(r"version\s*=")
5✔
243
        for line in toml_text.splitlines():
5✔
244
            if pattern.match(line):
5✔
245
                return _parse_version(line, pattern)
5✔
246
    version_file = BumpUp.parse_filename(toml_text, work_dir, package_name)
5✔
247
    if version_file == TOML_FILE:
5✔
248
        if toml_text is None:
5✔
249
            toml_text = Project.load_toml_text()
5✔
250
        context = tomllib.loads(toml_text)
5✔
251
        with contextlib.suppress(KeyError):
5✔
252
            return cast(str, context["project"]["version"])
5✔
253
        with contextlib.suppress(KeyError):  # Poetry V1
5✔
254
            return cast(str, context["tool"]["poetry"]["version"])
5✔
255
        secho(f"WARNING: can not find 'version' item in {version_file}!")
5✔
256
        return "0.0.0"
5✔
257
    pattern = re.compile(r"__version__\s*=")
5✔
258
    for line in Path(version_file).read_text("utf-8").splitlines():
5✔
259
        if pattern.match(line):
5✔
260
            return _parse_version(line, pattern)
5✔
261
    # TODO: remove or refactor the following lines.
262
    if work_dir is None:
×
263
        work_dir = Project.get_work_dir()
×
264
    package_dir = work_dir / package_name
×
265
    if (
×
266
        not (init_file := package_dir / "__init__.py").exists()
267
        and not (init_file := work_dir / "src" / package_name / init_file.name).exists()
268
        and not (init_file := work_dir / "app" / init_file.name).exists()
269
    ):
270
        secho("WARNING: __init__.py file does not exist!")
×
271
        return "0.0.0"
×
272

273
    pattern = re.compile(r"__version__\s*=")
×
274
    for line in init_file.read_text("utf-8").splitlines():
×
275
        if pattern.match(line):
×
276
            return _parse_version(line, pattern)
×
277
    secho(f"WARNING: can not find '__version__' var in {init_file}!")
×
278
    return "0.0.0"
×
279

280

281
def _get_frontend_version() -> tuple[Path, str] | None:
5✔
282
    try:
×
283
        frontend_version_file = Project.get_work_dir("package.json", be_file=True)
×
284
    except EnvError:
×
285
        return None
×
286
    try:
×
287
        from asynctor.jsons import json_loads
×
288
    except ImportError:
×
289
        from json import loads as json_loads  # type:ignore[assignment]
×
290
    content = frontend_version_file.read_bytes()
×
291
    metadata: dict[str, str] = json_loads(content)  # type:ignore
×
292
    try:
×
293
        current_version = metadata["version"]
×
294
    except (KeyError, TypeError):
×
295
        return None
×
296
    with contextlib.suppress(ValueError):
×
297
        frontend_version_file = frontend_version_file.relative_to(Path.cwd())
×
298
    return frontend_version_file, current_version
×
299

300

301
@overload
302
def get_current_version(
303
    verbose: bool = False,
304
    is_poetry: bool | None = None,
305
    package_name: str | None = None,
306
    *,
307
    check_version: Literal[False] = False,
308
) -> str: ...
309

310

311
@overload
312
def get_current_version(
313
    verbose: bool = False,
314
    is_poetry: bool | None = None,
315
    package_name: str | None = None,
316
    *,
317
    check_version: Literal[True],
318
) -> tuple[bool, str]: ...
319

320

321
def get_current_version(
5✔
322
    verbose: bool = False,
323
    is_poetry: bool | None = None,
324
    package_name: str | None = None,
325
    *,
326
    check_version: bool = False,
327
) -> str | tuple[bool, str]:
328
    if is_poetry is True or Project.manage_by_poetry():
5✔
329
        cmd = ["poetry", "version", "-s"]
5✔
330
        if verbose:
5✔
331
            echo(f"--> {' '.join(cmd)}")
5✔
332
        if out := capture_cmd_output(cmd, raises=True):
5✔
333
            out = out.splitlines()[-1].strip().split()[-1]
5✔
334
        if check_version:
5✔
335
            return True, out
5✔
336
        return out
5✔
337
    toml_text = work_dir = None
5✔
338
    if package_name is None:
5✔
339
        try:
5✔
340
            work_dir = Project.get_work_dir()
5✔
341
        except EnvError as e:
×
342
            if (res := _get_frontend_version()) is None:
×
343
                raise e
×
344
            current_version = res[1]
×
345
            if check_version:
×
346
                return False, current_version
×
347
            return current_version
×
348
        else:
349
            toml_text = Project.load_toml_text()
5✔
350
            doc = tomllib.loads(toml_text)
5✔
351
            project_name = doc.get("project", {}).get("name", work_dir.name)
5✔
352
            package_name = re.sub(r"[- ]", "_", project_name)
5✔
353
    local_version = read_version_from_file(package_name, work_dir, toml_text)
5✔
354
    try:
5✔
355
        installed_version = importlib_metadata.version(package_name)
5✔
356
    except importlib_metadata.PackageNotFoundError:
5✔
357
        installed_version = ""
5✔
358
    current_version = local_version or installed_version
5✔
359
    if not current_version:
5✔
360
        raise FastDevCliError(f"Failed to get current version of {package_name!r}")
×
361
    if check_version:
5✔
362
        is_conflict = bool(local_version) and local_version != installed_version
5✔
363
        return is_conflict, current_version
5✔
364
    return current_version
5✔
365

366

367
def _ensure_bool(value: bool | OptionInfo) -> bool:
5✔
368
    if isinstance(value, bool):
5✔
369
        return value
5✔
370
    return bool(getattr(value, "default", False))
5✔
371

372

373
def _ensure_str(value: str | OptionInfo | None) -> str | None:
5✔
374
    if isinstance(value, str) or value is None:
5✔
375
        return value
5✔
376
    return getattr(value, "default", "")
5✔
377

378

379
class DryRun:
5✔
380
    def __init__(self, _exit: bool = False, dry: bool = False) -> None:
5✔
381
        self.dry = _ensure_bool(dry)
5✔
382
        self._exit = _exit
5✔
383

384
    def gen(self) -> str:
5✔
385
        raise NotImplementedError
5✔
386

387
    def run(self) -> None:
5✔
388
        exit_if_run_failed(self.gen(), _exit=self._exit, dry=self.dry)
5✔
389

390

391
class BumpUp(DryRun):
5✔
392
    class PartChoices(StrEnum):
5✔
393
        patch = "patch"
5✔
394
        minor = "minor"
5✔
395
        major = "major"
5✔
396

397
    def __init__(
5✔
398
        self,
399
        commit: bool,
400
        part: str,
401
        filename: str | None = None,
402
        dry: bool = False,
403
        no_sync: bool = False,
404
        emoji: bool | None = None,
405
    ) -> None:
406
        self.commit = commit
5✔
407
        self.part = part
5✔
408
        if filename is None:
5✔
409
            try:
5✔
410
                filename = self.parse_filename()
5✔
411
            except EnvError:
5✔
412
                if (res := _get_frontend_version()) is not None:
×
413
                    filename = res[0].name
×
414
                else:
415
                    raise
×
416
        self.filename = filename
5✔
417
        self._no_sync = no_sync
5✔
418
        self._emoji = emoji
5✔
419
        super().__init__(dry=dry)
5✔
420

421
    @staticmethod
5✔
422
    def get_last_commit_message(raises: bool = False) -> str:
5✔
423
        cmd = 'git show --pretty=format:"%s" -s HEAD'
5✔
424
        return capture_cmd_output(cmd, raises=raises)
5✔
425

426
    @classmethod
5✔
427
    def should_add_emoji(cls) -> bool:
5✔
428
        """
429
        If last commit message is startswith emoji,
430
        add a ⬆️ flag at the prefix of bump up commit message.
431
        """
432
        try:
5✔
433
            first_char = cls.get_last_commit_message(raises=True)[0]
5✔
434
        except (IndexError, ShellCommandError):
5✔
435
            return False
5✔
436
        else:
437
            return is_emoji(first_char)
5✔
438

439
    @staticmethod
5✔
440
    def parse_dynamic_version(
5✔
441
        toml_text: str,
442
        context: dict[str, Any],
443
        work_dir: Path | None = None,
444
    ) -> str | None:
445
        if work_dir is None:
5✔
446
            work_dir = Project.get_work_dir()
5✔
447
        for tool in ("pdm", "hatch"):
5✔
448
            with contextlib.suppress(KeyError):
5✔
449
                version_path = cast(str, context["tool"][tool]["version"]["path"])
5✔
450
                if (
5✔
451
                    Path(version_path).exists()
452
                    or work_dir.joinpath(version_path).exists()
453
                ):
454
                    return version_path
5✔
455
        # version = { source = "file", path = "fast_dev_cli/__init__.py" }
456
        v_key = "version = "
5✔
457
        p_key = 'path = "'
5✔
458
        for line in toml_text.splitlines():
5✔
459
            if not line.startswith(v_key):
×
460
                continue
×
461
            if p_key in (value := line.split(v_key, 1)[-1].split("#")[0]):
×
462
                filename = value.split(p_key, 1)[-1].split('"')[0]
×
463
                if work_dir.joinpath(filename).exists():
×
464
                    return filename
×
465
        return None
5✔
466

467
    @classmethod
5✔
468
    def parse_filename(
5✔
469
        cls,
470
        toml_text: str | None = None,
471
        work_dir: Path | None = None,
472
        package_name: str | None = None,
473
    ) -> str:
474
        if toml_text is None:
5✔
475
            toml_text = Project.load_toml_text()
5✔
476
        context = tomllib.loads(toml_text)
5✔
477
        by_version_plugin = False
5✔
478
        try:
5✔
479
            ver = context["project"]["version"]
5✔
480
        except KeyError:
5✔
481
            pass
5✔
482
        else:
483
            if isinstance(ver, str):
5✔
484
                if ver in ("0", "0.0.0"):
5✔
485
                    by_version_plugin = True
5✔
486
                elif re.match(r"\d+\.\d+\.\d+", ver):
5✔
487
                    return TOML_FILE
5✔
488
        if not by_version_plugin:
5✔
489
            try:
5✔
490
                version_value = context["tool"]["poetry"]["version"]
5✔
491
            except KeyError:
5✔
492
                if not Project.manage_by_poetry() and (
5✔
493
                    filename := cls.parse_dynamic_version(toml_text, context, work_dir)
494
                ):
495
                    return filename
5✔
496
            else:
497
                by_version_plugin = version_value in ("0", "0.0.0", "init")
5✔
498
        if by_version_plugin:
5✔
499
            return cls.parse_plugin_version(context, package_name)
5✔
500

501
        return TOML_FILE
5✔
502

503
    @staticmethod
5✔
504
    def parse_plugin_version(context: dict[str, Any], package_name: str | None) -> str:
5✔
505
        try:
5✔
506
            package_item = context["tool"]["poetry"]["packages"]
5✔
507
        except KeyError:
5✔
508
            try:
5✔
509
                project_name = context["project"]["name"]
5✔
510
            except KeyError:
5✔
511
                packages: list[tuple[str, str]] = []
5✔
512
            else:
513
                packages = [(poetry_module_name(project_name), "")]
×
514
        else:
515
            packages = [
5✔
516
                (j, i.get("from", "")) for i in package_item if (j := i.get("include"))
517
            ]
518
        # In case of managed by `poetry-plugin-version`
519
        cwd = Path.cwd()
5✔
520
        pattern = re.compile(r"__version__\s*=\s*['\"]")
5✔
521
        ds: list[Path] = []
5✔
522
        if package_name is not None:
5✔
523
            packages.insert(0, (package_name, ""))
×
524
        for package_name, source_dir in packages:
5✔
525
            ds.append(cwd / package_name)
5✔
526
            ds.append(cwd / "src" / package_name)
5✔
527
            if source_dir and source_dir != "src":
5✔
528
                ds.append(cwd / source_dir / package_name)
5✔
529
        module_name = poetry_module_name(cwd.name)
5✔
530
        ds.extend([cwd / module_name, cwd / "src" / module_name, cwd])
5✔
531
        for d in ds:
5✔
532
            init_file = d / "__init__.py"
5✔
533
            if (init_file.exists() and pattern.search(init_file.read_text("utf8"))) or (
5✔
534
                (init_file := init_file.with_name("__version__.py")).exists()
535
                and pattern.search(init_file.read_text("utf8"))
536
            ):
537
                break
5✔
538
        else:
539
            raise ParseError("Version file not found! Where are you now?")
5✔
540
        return os.path.relpath(init_file, cwd)
5✔
541

542
    def get_part(self, s: str) -> str:
5✔
543
        choices: dict[str, str] = {}
5✔
544
        for i, p in enumerate(self.PartChoices, 1):
5✔
545
            v = str(p)
5✔
546
            choices.update({str(i): v, v: v})
5✔
547
        try:
5✔
548
            return choices[s]
5✔
549
        except KeyError as e:
5✔
550
            echo(f"Invalid part: {s!r}")
5✔
551
            raise Exit(1) from e
5✔
552

553
    def gen(self) -> str:
5✔
554
        should_sync, _version = get_current_version(check_version=True)
5✔
555
        filename = self.filename
5✔
556
        echo(f"Current version(@{filename}): {_version}")
5✔
557
        if self.part:
5✔
558
            part = self.get_part(self.part)
5✔
559
        else:
560
            part = "patch"
5✔
561
            if a := input("Which one?").strip():
5✔
562
                part = self.get_part(a)
5✔
563
        self.part = part
5✔
564
        parse = r'--parse "(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"'
5✔
565
        cmd = f'bumpversion {parse} --current-version="{_version}" {part} {filename}'
5✔
566
        if self.commit:
5✔
567
            if part != "patch":
5✔
568
                cmd += " --tag"
5✔
569
            cmd += " --commit"
5✔
570
            if self._emoji or (self._emoji is None and self.should_add_emoji()):
5✔
571
                cmd += " --message-emoji=1"
5✔
572
            if not load_bool("DONT_GIT_PUSH"):
5✔
573
                cmd += " && git push && git push --tags && git log -1"
5✔
574
        else:
575
            cmd += " --allow-dirty"
5✔
576
        if (
5✔
577
            should_sync
578
            and not self._no_sync
579
            and (sync := Project.get_sync_command(only_me=True))
580
        ):
581
            cmd = f"{sync} && " + cmd
5✔
582
        return cmd
5✔
583

584
    def run(self) -> None:
5✔
585
        super().run()
5✔
586
        if not self.commit and not self.dry:
5✔
587
            new_version = get_current_version(True)
5✔
588
            echo(new_version)
5✔
589
            if self.part != "patch":
5✔
590
                echo("You may want to pin tag by `fast tag`")
5✔
591

592

593
def _echo_version(version_file: Any, value: str) -> None:
5✔
594
    styled = typer.style(value, bold=True)
5✔
595
    echo(f"Version value in {version_file}: " + styled)
5✔
596

597

598
@cli.command()
5✔
599
def version() -> None:
5✔
600
    """Show the version of this tool"""
601
    echo("Fast Dev Cli Version: " + typer.style(__version__, fg=typer.colors.BLUE))
5✔
602
    with contextlib.suppress(FileNotFoundError, KeyError):
5✔
603
        try:
5✔
604
            toml_text = Project.load_toml_text()
5✔
605
        except EnvError:
×
606
            if (res := _get_frontend_version()) is not None:
×
607
                _echo_version(*res)
×
608
                return
×
609
            raise
×
610
        doc = tomllib.loads(toml_text)
5✔
611
        if value := doc.get("project", {}).get("version", ""):
5✔
612
            styled = typer.style(value, bold=True, fg=typer.colors.CYAN)
×
613
            if project_name := doc["project"].get("name", ""):
×
614
                echo(f"{project_name} version: " + styled)
×
615
            else:
616
                echo(f"Got Version from {TOML_FILE}: " + styled)
×
617
            return
×
618
        version_file = doc["tool"]["pdm"]["version"]["path"]
5✔
619
        text = Project.get_work_dir().joinpath(version_file).read_text(encoding="utf-8")
5✔
620
        varname = "__version__"
5✔
621
        for line in text.splitlines():
5✔
622
            if line.strip().startswith(varname):
5✔
623
                value = line.split("=", 1)[-1].strip().strip('"').strip("'")
5✔
624
                _echo_version(version_file, value)
5✔
625
                break
5✔
626

627

628
@cli.command(name="bump")
5✔
629
def bump_version(
5✔
630
    part: BumpUp.PartChoices,
631
    commit: bool = Option(
632
        False, "--commit", "-c", help="Whether run `git commit` after version changed"
633
    ),
634
    emoji: bool | None = Option(
635
        None, "--emoji", help="Whether add emoji prefix to commit message"
636
    ),
637
    no_sync: bool = Option(
638
        False, "--no-sync", help="Do not run sync command to update version"
639
    ),
640
    dry: bool = DryOption,
641
) -> None:
642
    """Bump up version string in pyproject.toml"""
643
    if emoji is not None:
5✔
644
        emoji = _ensure_bool(emoji)
5✔
645
    return BumpUp(
5✔
646
        _ensure_bool(commit),
647
        getattr(part, "value", part),
648
        no_sync=_ensure_bool(no_sync),
649
        emoji=emoji,
650
        dry=dry,
651
    ).run()
652

653

654
def bump() -> None:
5✔
655
    part, commit = "", False
5✔
656
    if args := sys.argv[2:]:
5✔
657
        if "-c" in args or "--commit" in args:
5✔
658
            commit = True
5✔
659
        for a in args:
5✔
660
            if not a.startswith("-"):
5✔
661
                part = a
5✔
662
                break
5✔
663
    return BumpUp(commit, part, no_sync="--no-sync" in args, dry="--dry" in args).run()
5✔
664

665

666
class Project:
5✔
667
    path_depth = 5
5✔
668
    _tool: ToolName | None = None
5✔
669

670
    @staticmethod
5✔
671
    def is_poetry_v2(text: str) -> bool:
5✔
672
        return 'build-backend = "poetry' in text
5✔
673

674
    @staticmethod
5✔
675
    def get_poetry_version(command: str = "poetry") -> str:
5✔
676
        pattern = r"(\d+\.\d+\.\d+)"
5✔
677
        text = capture_cmd_output(f"{command} --version")
5✔
678
        for expr in (
5✔
679
            rf"Poetry \(version {pattern}\)",
680
            rf"Poetry.*version.*{pattern}.*\)",
681
            rf"{pattern}",
682
        ):
683
            if m := re.search(expr, text):
5✔
684
                return m.group(1)
5✔
685
        return ""
×
686

687
    @staticmethod
5✔
688
    def work_dir(
5✔
689
        name: str, parent: Path, depth: int, be_file: bool = False
690
    ) -> Path | None:
691
        for _ in range(depth):
5✔
692
            if (f := parent.joinpath(name)).exists():
5✔
693
                if be_file:
5✔
694
                    return f
5✔
695
                return parent
5✔
696
            parent = parent.parent
5✔
697
        return None
5✔
698

699
    @classmethod
5✔
700
    def get_work_dir(
5✔
701
        cls: type[Self],
702
        name: str = TOML_FILE,
703
        cwd: Path | None = None,
704
        allow_cwd: bool = False,
705
        be_file: bool = False,
706
    ) -> Path:
707
        cwd = cwd or Path.cwd()
5✔
708
        if d := cls.work_dir(name, cwd, cls.path_depth, be_file):
5✔
709
            return d
5✔
710
        if allow_cwd:
5✔
711
            return cls.get_root_dir(cwd)
5✔
712
        raise EnvError(f"{name} not found! Make sure this is a python project.")
5✔
713

714
    @classmethod
5✔
715
    def load_toml_text(cls: type[Self], name: str = TOML_FILE) -> str:
5✔
716
        toml_file = cls.get_work_dir(name, be_file=True)
5✔
717
        return toml_file.read_text("utf8")
5✔
718

719
    @classmethod
5✔
720
    def manage_by_poetry(cls: type[Self], cache: bool = False) -> bool:
5✔
721
        return cls.get_manage_tool(cache=cache) == "poetry"
5✔
722

723
    @classmethod
5✔
724
    def get_manage_tool(cls: type[Self], cache: bool = False) -> ToolName | None:
5✔
725
        if cache and cls._tool:
5✔
726
            return cls._tool
5✔
727
        try:
5✔
728
            text = cls.load_toml_text()
5✔
729
        except EnvError:
5✔
730
            return None
5✔
731
        backend = ""
5✔
732
        skip_uv = load_bool("FASTDEVCLI_SKIP_UV")
5✔
733
        with contextlib.suppress(KeyError, tomllib.TOMLDecodeError):
5✔
734
            doc = tomllib.loads(text)
5✔
735
            backend = doc["build-system"]["build-backend"]
5✔
736
            if skip_uv:
5✔
737
                for t in ("pdm", "poetry"):
×
738
                    if t in backend:
×
739
                        cls._tool = t
×
740
                        return cls._tool
×
741
        work_dir: Path | None = None
5✔
742
        uv_lock_exists: bool | None = None
5✔
743
        if skip_uv:
5✔
744
            for name in ("pdm", "poetry"):
×
745
                if f"[tool.{name}]" in text:
×
746
                    cls._tool = cast(ToolName, name)
×
747
                    return cls._tool
×
748
            work_dir = cls.get_work_dir(allow_cwd=True)
×
749
            for name in ("pdm", "poetry"):
×
750
                if Path(work_dir, f"{name}.lock").exists():
×
751
                    cls._tool = cast(ToolName, name)
×
752
                    return cls._tool
×
753
            if uv_lock_exists := Path(work_dir, "uv.lock").exists():
×
754
                # Use pdm when uv is not available for uv managed project
755
                cls._tool = "pdm"
×
756
                return cls._tool
×
757
            return None
×
758
        if work_dir is None:
5✔
759
            work_dir = cls.get_work_dir(allow_cwd=True)
5✔
760
        if uv_lock_exists is None:
5✔
761
            uv_lock_exists = Path(work_dir, "uv.lock").exists()
5✔
762
        if uv_lock_exists:
5✔
763
            cls._tool = "uv"
5✔
764
            return cls._tool
5✔
765
        pdm_lock_exists = Path(work_dir, "pdm.lock").exists()
5✔
766
        poetry_lock_exists = Path(work_dir, "poetry.lock").exists()
5✔
767
        match pdm_lock_exists + poetry_lock_exists:
5✔
768
            case 1:
5✔
769
                cls._tool = "pdm" if pdm_lock_exists else "poetry"
×
770
                return cls._tool
×
771
            case _ as x:
5✔
772
                if backend:
5✔
773
                    for t in ("pdm", "poetry"):
5✔
774
                        if t in backend:
5✔
775
                            cls._tool = cast(ToolName, t)
5✔
776
                            return cls._tool
5✔
777
                for name in ("pdm", "poetry"):
5✔
778
                    if f"[tool.{name}]" in text:
5✔
779
                        cls._tool = cast(ToolName, name)
5✔
780
                        return cls._tool
5✔
781
                if x == 2:
5✔
782
                    cls._tool = (
×
783
                        "poetry" if load_bool("FASTDEVCLI_PREFER_POETRY") else "pdm"
784
                    )
785
                    return cls._tool
1✔
786
        if "[tool.uv]" in text or load_bool("FASTDEVCLI_PREFER_uv"):
5✔
787
            cls._tool = "uv"
5✔
788
            return cls._tool
5✔
789
        # Poetry 2.0 default to not include the '[tool.poetry]' section
790
        if cls.is_poetry_v2(text):
5✔
791
            cls._tool = "poetry"
×
792
            return cls._tool
×
793
        return None
5✔
794

795
    @staticmethod
5✔
796
    def python_exec_dir() -> Path:
5✔
797
        return Path(sys.executable).parent
5✔
798

799
    @classmethod
5✔
800
    def get_root_dir(cls: type[Self], cwd: Path | None = None) -> Path:
5✔
801
        root = cwd or Path.cwd()
5✔
802
        venv_parent = cls.python_exec_dir().parent.parent
5✔
803
        if root.is_relative_to(venv_parent):
5✔
804
            root = venv_parent
5✔
805
        return root
5✔
806

807
    @classmethod
5✔
808
    def is_pdm_project(cls, strict: bool = True, cache: bool = False) -> bool:
5✔
809
        if cls.get_manage_tool(cache=cache) != "pdm":
5✔
810
            return False
5✔
811
        if strict:
×
812
            lock_file = cls.get_work_dir() / "pdm.lock"
×
813
            return lock_file.exists()
×
814
        return True
×
815

816
    @classmethod
5✔
817
    def get_sync_command(
5✔
818
        cls, prod: bool = True, doc: dict[str, Any] | None = None, only_me: bool = False
819
    ) -> str:
820
        pdm_i = "pdm install --frozen" + " --prod" * prod
5✔
821
        if cls.is_pdm_project():
5✔
822
            return pdm_i
×
823
        elif cls.manage_by_poetry(cache=True):
5✔
824
            cmd = "poetry install"
5✔
825
            if prod:
5✔
826
                if doc is None:
5✔
827
                    doc = tomllib.loads(cls.load_toml_text())
5✔
828
                if doc.get("project", {}).get("dependencies") or any(
5✔
829
                    i != "python"
830
                    for i in doc.get("tool", {})
831
                    .get("poetry", {})
832
                    .get("dependencies", [])
833
                ):
834
                    cmd += " --only=main"
×
835
            return cmd
5✔
836
        elif cls.get_manage_tool(cache=True) == "uv":
×
837
            install_me = "uv pip install -e ."
×
838
            if doc is None:
×
839
                doc = tomllib.loads(cls.load_toml_text())
×
840
            is_distribution = (
×
841
                doc.get("tool", {}).get("pdm", {}).get("distribution") is not False
842
            )
843
            if only_me:
×
844
                return install_me if is_distribution else pdm_i
×
845
            cmd = "uv sync --inexact" + " --no-dev" * prod
×
846
            if is_distribution:
×
847
                cmd += f" && {install_me}"
×
848
        return ""
×
849

850
    @classmethod
5✔
851
    def sync_dependencies(cls, prod: bool = True) -> None:
5✔
852
        if cmd := cls.get_sync_command():
×
853
            run_and_echo(cmd)
×
854

855

856
class UpgradeDependencies(Project, DryRun):
5✔
857
    def __init__(
5✔
858
        self, _exit: bool = False, dry: bool = False, tool: ToolName = "poetry"
859
    ) -> None:
860
        super().__init__(_exit, dry)
5✔
861
        self._tool = tool
5✔
862

863
    class DevFlag(StrEnum):
5✔
864
        new = "[tool.poetry.group.dev.dependencies]"
5✔
865
        old = "[tool.poetry.dev-dependencies]"
5✔
866

867
    @staticmethod
5✔
868
    def parse_value(version_info: str, key: str) -> str:
5✔
869
        """Pick out the value for key in version info.
870

871
        Example::
872
            >>> s= 'typer = {extras = ["all"], version = "^0.9.0", optional = true}'
873
            >>> UpgradeDependencies.parse_value(s, 'extras')
874
            'all'
875
            >>> UpgradeDependencies.parse_value(s, 'optional')
876
            'true'
877
            >>> UpgradeDependencies.parse_value(s, 'version')
878
            '^0.9.0'
879
        """
880
        sep = key + " = "
5✔
881
        rest = version_info.split(sep, 1)[-1].strip(" =")
5✔
882
        if rest.startswith("["):
5✔
883
            rest = rest[1:].split("]")[0]
5✔
884
        elif rest.startswith('"'):
5✔
885
            rest = rest[1:].split('"')[0]
5✔
886
        else:
887
            rest = rest.split(",")[0].split("}")[0]
5✔
888
        return rest.strip().replace('"', "")
5✔
889

890
    @staticmethod
5✔
891
    def no_need_upgrade(version_info: str, line: str) -> bool:
5✔
892
        if (v := version_info.replace(" ", "")).startswith("{url="):
5✔
893
            echo(f"No need to upgrade for: {line}")
5✔
894
            return True
5✔
895
        if (f := "version=") in v:
5✔
896
            v = v.split(f)[1].strip('"').split('"')[0]
5✔
897
        if v == "*":
5✔
898
            echo(f"Skip wildcard line: {line}")
5✔
899
            return True
5✔
900
        elif v == "[":
5✔
901
            echo(f"Skip complex dependence: {line}")
5✔
902
            return True
5✔
903
        elif v.startswith(">") or v.startswith("<") or v[0].isdigit():
5✔
904
            echo(f"Ignore bigger/smaller/equal: {line}")
5✔
905
            return True
5✔
906
        return False
5✔
907

908
    @classmethod
5✔
909
    def build_args(
5✔
910
        cls: type[Self], package_lines: list[str]
911
    ) -> tuple[list[str], dict[str, list[str]]]:
912
        args: list[str] = []  # ['typer[all]', 'fastapi']
5✔
913
        specials: dict[str, list[str]] = {}  # {'--platform linux': ['gunicorn']}
5✔
914
        for no, line in enumerate(package_lines, 1):
5✔
915
            if (
5✔
916
                not (m := line.strip())
917
                or m.startswith("#")
918
                or m == "]"
919
                or (m.startswith("{") and m.strip(",").endswith("}"))
920
            ):
921
                continue
5✔
922
            try:
5✔
923
                package, version_info = m.split("=", 1)
5✔
924
            except ValueError as e:
5✔
925
                raise ParseError(f"Failed to separate by '='@line {no}: {m}") from e
5✔
926
            if (package := package.strip()).lower() == "python":
5✔
927
                continue
5✔
928
            if cls.no_need_upgrade(version_info := version_info.strip(' "'), line):
5✔
929
                continue
5✔
930
            if (extras_tip := "extras") in version_info:
5✔
931
                package += "[" + cls.parse_value(version_info, extras_tip) + "]"
5✔
932
            item = f'"{package}@latest"'
5✔
933
            key = None
5✔
934
            if (pf := "platform") in version_info:
5✔
935
                platform = cls.parse_value(version_info, pf)
5✔
936
                key = f"--{pf}={platform}"
5✔
937
            if (sc := "source") in version_info:
5✔
938
                source = cls.parse_value(version_info, sc)
5✔
939
                key = ("" if key is None else (key + " ")) + f"--{sc}={source}"
5✔
940
            if "optional = true" in version_info:
5✔
941
                key = ("" if key is None else (key + " ")) + "--optional"
5✔
942
            if key is not None:
5✔
943
                specials[key] = specials.get(key, []) + [item]
5✔
944
            else:
945
                args.append(item)
5✔
946
        return args, specials
5✔
947

948
    @classmethod
5✔
949
    def should_with_dev(cls: type[Self]) -> bool:
5✔
950
        text = cls.load_toml_text()
5✔
951
        return cls.DevFlag.new in text or cls.DevFlag.old in text
5✔
952

953
    @staticmethod
5✔
954
    def parse_item(toml_str: str) -> list[str]:
5✔
955
        lines: list[str] = []
5✔
956
        for line in toml_str.splitlines():
5✔
957
            if (line := line.strip()).startswith("["):
5✔
958
                if lines:
5✔
959
                    break
5✔
960
            elif line:
5✔
961
                lines.append(line)
5✔
962
        return lines
5✔
963

964
    @classmethod
5✔
965
    def get_args(
5✔
966
        cls: type[Self], toml_text: str | None = None
967
    ) -> tuple[list[str], list[str], list[list[str]], str]:
968
        if toml_text is None:
5✔
969
            toml_text = cls.load_toml_text()
5✔
970
        main_title = "[tool.poetry.dependencies]"
5✔
971
        if (no_main_deps := main_title not in toml_text) and not cls.is_poetry_v2(
5✔
972
            toml_text
973
        ):
974
            raise EnvError(
5✔
975
                f"{main_title} not found! Make sure this is a poetry project."
976
            )
977
        text = toml_text.split(main_title)[-1]
5✔
978
        dev_flag = "--group dev"
5✔
979
        new_flag, old_flag = cls.DevFlag.new, cls.DevFlag.old
5✔
980
        if (dev_title := getattr(new_flag, "value", new_flag)) not in text:
5✔
981
            dev_title = getattr(old_flag, "value", old_flag)  # For poetry<=1.2
5✔
982
            dev_flag = "--dev"
5✔
983
        others: list[list[str]] = []
5✔
984
        try:
5✔
985
            main_toml, dev_toml = text.split(dev_title)
5✔
986
        except ValueError:
5✔
987
            dev_toml = ""
5✔
988
            main_toml = text
5✔
989
        mains = [] if no_main_deps else cls.parse_item(main_toml)
5✔
990
        devs = cls.parse_item(dev_toml)
5✔
991
        prod_packs, specials = cls.build_args(mains)
5✔
992
        if specials:
5✔
993
            others.extend([[k] + v for k, v in specials.items()])
5✔
994
        dev_packs, specials = cls.build_args(devs)
5✔
995
        if specials:
5✔
996
            others.extend([[k] + v + [dev_flag] for k, v in specials.items()])
5✔
997
        return prod_packs, dev_packs, others, dev_flag
5✔
998

999
    @classmethod
5✔
1000
    def gen_cmd(cls: type[Self]) -> str:
5✔
1001
        main_args, dev_args, others, dev_flags = cls.get_args()
5✔
1002
        return cls.to_cmd(main_args, dev_args, others, dev_flags)
5✔
1003

1004
    @staticmethod
5✔
1005
    def to_cmd(
5✔
1006
        main_args: list[str],
1007
        dev_args: list[str],
1008
        others: list[list[str]],
1009
        dev_flags: str,
1010
    ) -> str:
1011
        command = "poetry add "
5✔
1012
        _upgrade = ""
5✔
1013
        if main_args:
5✔
1014
            _upgrade = command + " ".join(main_args)
5✔
1015
        if dev_args:
5✔
1016
            if _upgrade:
5✔
1017
                _upgrade += " && "
5✔
1018
            _upgrade += command + dev_flags + " " + " ".join(dev_args)
5✔
1019
        for single in others:
5✔
1020
            _upgrade += f" && poetry add {' '.join(single)}"
5✔
1021
        return _upgrade
5✔
1022

1023
    def gen(self) -> str:
5✔
1024
        if self._tool == "uv":
5✔
1025
            up = "uv lock --upgrade --verbose"
5✔
1026
            deps = "uv sync --inexact --frozen --all-groups --all-extras"
5✔
1027
            return f"{up} && {deps}"
5✔
1028
        elif self._tool == "pdm":
5✔
1029
            return "pdm update --verbose && pdm install -G :all --frozen"
5✔
1030
        return self.gen_cmd() + " && poetry lock && poetry update"
5✔
1031

1032

1033
@cli.command()
5✔
1034
def upgrade(
5✔
1035
    tool: str = ToolOption,
1036
    dry: bool = DryOption,
1037
) -> None:
1038
    """Upgrade dependencies in pyproject.toml to latest versions"""
1039
    if not (tool := _ensure_str(tool) or "") or tool == ToolOption.default:
5✔
1040
        tool = Project.get_manage_tool() or "uv"
5✔
1041
    if tool in get_args(ToolName):
5✔
1042
        UpgradeDependencies(dry=dry, tool=cast(ToolName, tool)).run()
5✔
1043
    else:
1044
        secho(f"Unknown tool {tool!r}", fg=typer.colors.YELLOW)
5✔
1045
        raise typer.Exit(1)
5✔
1046

1047

1048
class GitTag(DryRun):
5✔
1049
    def __init__(self, message: str, dry: bool, no_sync: bool = False) -> None:
5✔
1050
        self.message = message
5✔
1051
        self._no_sync = no_sync
5✔
1052
        super().__init__(dry=dry)
5✔
1053

1054
    @staticmethod
5✔
1055
    def has_v_prefix() -> bool:
5✔
1056
        return "v" in capture_cmd_output("git tag")
5✔
1057

1058
    def should_push(self) -> bool:
5✔
1059
        return "git push" in self.git_status
5✔
1060

1061
    def gen(self) -> str:
5✔
1062
        should_sync, _version = get_current_version(verbose=False, check_version=True)
5✔
1063
        if self.has_v_prefix():
5✔
1064
            # Add `v` at prefix to compare with bumpversion tool
1065
            _version = "v" + _version
5✔
1066
        cmd = f"git tag -a {_version} -m {self.message!r} && git push --tags"
5✔
1067
        if self.should_push():
5✔
1068
            cmd += " && git push"
5✔
1069
        if should_sync and not self._no_sync and (sync := Project.get_sync_command()):
5✔
1070
            cmd = f"{sync} && " + cmd
×
1071
        return cmd
5✔
1072

1073
    @cached_property
5✔
1074
    def git_status(self) -> str:
5✔
1075
        return capture_cmd_output("git status")
5✔
1076

1077
    def mark_tag(self) -> bool:
5✔
1078
        if not re.search(r"working (tree|directory) clean", self.git_status) and (
5✔
1079
            "无文件要提交,干净的工作区" not in self.git_status
1080
        ):
1081
            run_and_echo("git status")
5✔
1082
            echo("ERROR: Please run git commit to make sure working tree is clean!")
5✔
1083
            return False
5✔
1084
        return bool(super().run())
5✔
1085

1086
    def run(self) -> None:
5✔
1087
        if self.mark_tag() and not self.dry:
5✔
1088
            echo("You may want to publish package:\n pdm publish")
5✔
1089

1090

1091
@cli.command()
5✔
1092
def tag(
5✔
1093
    message: str = Option("", "-m", "--message"),
1094
    no_sync: bool = Option(
1095
        False, "--no-sync", help="Do not run sync command to update version"
1096
    ),
1097
    dry: bool = DryOption,
1098
) -> None:
1099
    """Run shell command: git tag -a <current-version-in-pyproject.toml> -m {message}"""
1100
    GitTag(message, dry=dry, no_sync=_ensure_bool(no_sync)).run()
5✔
1101

1102

1103
class LintCode(DryRun):
5✔
1104
    def __init__(
5✔
1105
        self,
1106
        args: list[str] | str | None,
1107
        check_only: bool = False,
1108
        _exit: bool = False,
1109
        dry: bool = False,
1110
        bandit: bool = False,
1111
        skip_mypy: bool = False,
1112
        dmypy: bool = False,
1113
        tool: str = ToolOption.default,
1114
        prefix: bool = False,
1115
        up: bool = False,
1116
        sim: bool = True,
1117
        strict: bool = False,
1118
        ty: bool = False,
1119
        fix: bool = True,
1120
    ) -> None:
1121
        self.args = args
5✔
1122
        self.check_only = check_only
5✔
1123
        self._bandit = bandit
5✔
1124
        self._skip_mypy = skip_mypy
5✔
1125
        self._use_dmypy = dmypy
5✔
1126
        self._tool = tool
5✔
1127
        self._prefix = prefix
5✔
1128
        self._up = up
5✔
1129
        self._sim = sim
5✔
1130
        self._strict = strict
5✔
1131
        self._ty = _ensure_bool(ty)
5✔
1132
        self._fix = _ensure_bool(fix)
5✔
1133
        super().__init__(_exit, dry)
5✔
1134

1135
    @staticmethod
5✔
1136
    def check_lint_tool_installed() -> bool:
5✔
1137
        try:
5✔
1138
            return check_call("ruff --version")
5✔
1139
        except FileNotFoundError:
×
1140
            # Windows may raise FileNotFoundError when ruff not installed
1141
            return False
×
1142

1143
    @staticmethod
5✔
1144
    def missing_mypy_exec() -> bool:
5✔
1145
        return shutil.which("mypy") is None
5✔
1146

1147
    @staticmethod
5✔
1148
    def prefer_dmypy(paths: str, tools: list[str], use_dmypy: bool = False) -> bool:
5✔
1149
        return (
5✔
1150
            paths == "."
1151
            and any(t.startswith("mypy") for t in tools)
1152
            and (use_dmypy or load_bool("FASTDEVCLI_DMYPY"))
1153
        )
1154

1155
    @staticmethod
5✔
1156
    def get_package_name() -> str:
5✔
1157
        root = Project.get_work_dir(allow_cwd=True)
5✔
1158
        module_name = root.name.replace("-", "_").replace(" ", "_")
5✔
1159
        package_maybe = (module_name, "src")
5✔
1160
        for name in package_maybe:
5✔
1161
            if root.joinpath(name).is_dir():
5✔
1162
                return name
5✔
1163
        return "."
5✔
1164

1165
    @classmethod
5✔
1166
    def to_cmd(
5✔
1167
        cls: type[Self],
1168
        paths: str = ".",
1169
        check_only: bool = False,
1170
        bandit: bool = False,
1171
        skip_mypy: bool = False,
1172
        use_dmypy: bool = False,
1173
        tool: str = ToolOption.default,
1174
        with_prefix: bool = False,
1175
        ruff_check_up: bool = False,
1176
        ruff_check_sim: bool = True,
1177
        mypy_strict: bool = False,
1178
        prefer_ty: bool = False,
1179
        ruff_check_fix: bool = True,
1180
    ) -> str:
1181
        if paths != "." and all(i.endswith(".html") for i in paths.split()):
5✔
1182
            return f"prettier -w {paths}"
5✔
1183
        ruff_rules = ["I", "B"]
5✔
1184
        if ruff_check_sim and not load_bool("FASTDEVCLI_NO_SIM"):
5✔
1185
            ruff_rules.append("SIM")
5✔
1186
        if ruff_check_up or load_bool("FASTDEVCLI_UP"):
5✔
1187
            ruff_rules.append("UP")
×
1188
        ruff_check = "ruff check --extend-select=" + ",".join(ruff_rules)
5✔
1189
        if (
5✔
1190
            ruff_check_fix
1191
            and not check_only
1192
            and (not load_bool("NO_FIX") and not load_bool("FASTDEVCLI_NO_FIX"))
1193
        ):
1194
            ruff_check += " --fix"
5✔
1195
        tools = ["ruff format", ruff_check, "mypy"]
5✔
1196
        if check_only:
5✔
1197
            tools[0] += " --check"
5✔
1198
        if skip_mypy or load_bool("SKIP_MYPY") or load_bool("FASTDEVCLI_NO_MYPY"):
5✔
1199
            # Sometimes mypy is too slow
1200
            tools = tools[:-1]
5✔
1201
        else:
1202
            if prefer_ty or load_bool("FASTDEVCLI_TY"):
5✔
1203
                tools[-1] = "ty check"
×
1204
            else:
1205
                if load_bool("IGNORE_MISSING_IMPORTS"):
5✔
1206
                    tools[-1] += " --ignore-missing-imports"
5✔
1207
                if mypy_strict or load_bool("FASTDEVCLI_STRICT"):
5✔
1208
                    tools[-1] += " --strict"
×
1209
        ruff_exists = cls.check_lint_tool_installed()
5✔
1210
        prefix = ""
5✔
1211
        should_run_by_tool = with_prefix
5✔
1212
        requires_mypy = any(tool.startswith("mypy") for tool in tools)
5✔
1213
        if requires_mypy and not should_run_by_tool:
5✔
1214
            if is_venv() and Path(sys.argv[0]).parent != Path.home().joinpath(
5✔
1215
                ".local/bin"
1216
            ):  # Virtual environment activated and fast-dev-cli is installed in it
1217
                if not ruff_exists:
5✔
1218
                    should_run_by_tool = True
5✔
1219
                    command = "pipx install ruff"
5✔
1220
                    if shutil.which("pipx") is None:
5✔
1221
                        ensure_pipx = "pip install --user pipx\n  pipx ensurepath\n  "
×
1222
                        command = ensure_pipx + command
×
1223
                    elif prefer_uv_tool():
5✔
1224
                        command = "uv tool install ruff"
5✔
1225
                    yellow_warn(
5✔
1226
                        "You may need to run the following command"
1227
                        f" to install ruff:\n\n  {command}\n"
1228
                    )
1229
                elif cls.missing_mypy_exec():
5✔
1230
                    should_run_by_tool = True
5✔
1231
                    if check_call('python -c "import fast_dev_cli"'):
5✔
1232
                        command = "python -m pip install -U mypy"
5✔
1233
                        yellow_warn(
5✔
1234
                            "You may need to run the following command"
1235
                            f" to install lint tools:\n\n  {command}\n"
1236
                        )
1237
            elif tool == ToolOption.default:
×
1238
                root = Project.get_work_dir(allow_cwd=True)
×
1239
                if py := shutil.which("python"):
×
1240
                    try:
×
1241
                        Path(py).relative_to(root)
×
1242
                    except ValueError:
×
1243
                        # Virtual environment not activated
1244
                        should_run_by_tool = True
×
1245
            else:
1246
                should_run_by_tool = True
×
1247
        if should_run_by_tool and tool:
5✔
1248
            if tool == ToolOption.default:
5✔
1249
                tool = Project.get_manage_tool() or ""
5✔
1250
            if tool:
5✔
1251
                prefix = tool + " run "
5✔
1252
                if tool == "uv":
5✔
1253
                    if is_windows():
5✔
1254
                        prefix += "--no-sync "
×
1255
                    elif Path(bin_dir := ".venv/bin/").exists():
5✔
1256
                        prefix = bin_dir
5✔
1257
        if cls.prefer_dmypy(paths, tools, use_dmypy=use_dmypy):
5✔
1258
            tools[-1] = "dmypy run"
5✔
1259
        cmd = " && ".join(
5✔
1260
            (
1261
                tool
1262
                # `ruff <command>` get the same result with `pdm run ruff <command>`.
1263
                # Other tools should run inside the selected environment.
1264
                if ruff_exists and tool.startswith("ruff")
1265
                else prefix + tool
1266
            )
1267
            + f" {paths}"
1268
            for tool in tools
1269
        )
1270
        if bandit or load_bool("FASTDEVCLI_BANDIT"):
5✔
1271
            command = prefix + "bandit"
5✔
1272
            if Path("pyproject.toml").exists():
5✔
1273
                toml_text = Project.load_toml_text()
5✔
1274
                if "[tool.bandit" in toml_text:
5✔
1275
                    command += " -c pyproject.toml"
5✔
1276
            if paths == "." and " -c " not in command:
5✔
1277
                paths = cls.get_package_name()
5✔
1278
            command += f" -r {paths}"
5✔
1279
            cmd += " && " + command
5✔
1280
        return cmd
5✔
1281

1282
    def gen(self) -> str:
5✔
1283
        paths = "."
5✔
1284
        if args := self.args:
5✔
1285
            ps = args.split() if isinstance(args, str) else [str(i) for i in args]
5✔
1286
            if len(ps) == 1:
5✔
1287
                paths = ps[0]
5✔
1288
                if (
5✔
1289
                    paths != "."
1290
                    # `Path("a.").suffix` got "." in py3.14 and got "" with py<3.14
1291
                    and (p := Path(paths)).suffix in ("", ".")
1292
                    and not p.exists()
1293
                ):
1294
                    # e.g.:
1295
                    # stem -> stem.py
1296
                    # me. -> me.py
1297
                    if paths.endswith("."):
×
1298
                        p = p.with_name(paths[:-1])
×
1299
                    for suffix in (".py", ".html"):
×
1300
                        p = p.with_suffix(suffix)
×
1301
                        if p.exists():
×
1302
                            paths = p.name
×
1303
                            break
×
1304
            else:
1305
                paths = " ".join(ps)
×
1306
        return self.to_cmd(
5✔
1307
            paths,
1308
            self.check_only,
1309
            self._bandit,
1310
            self._skip_mypy,
1311
            self._use_dmypy,
1312
            tool=self._tool,
1313
            with_prefix=self._prefix,
1314
            ruff_check_up=self._up,
1315
            ruff_check_sim=self._sim,
1316
            mypy_strict=self._strict,
1317
            prefer_ty=self._ty,
1318
            ruff_check_fix=self._fix,
1319
        )
1320

1321

1322
def parse_files(args: list[str] | tuple[str, ...]) -> list[str]:
5✔
1323
    return [i for i in args if not i.startswith("-")]
5✔
1324

1325

1326
def lint(
5✔
1327
    files: list[str] | str | None = None,
1328
    dry: bool = False,
1329
    bandit: bool = False,
1330
    skip_mypy: bool = False,
1331
    dmypy: bool = False,
1332
    tool: str = ToolOption.default,
1333
    prefix: bool = False,
1334
    up: bool = False,
1335
    sim: bool = True,
1336
    strict: bool = False,
1337
    ty: bool = False,
1338
    fix: bool = True,
1339
) -> None:
1340
    if files is None:
5✔
1341
        files = parse_files(sys.argv[1:])
5✔
1342
    if files and files[0] == "lint":
5✔
1343
        files = files[1:]
5✔
1344
    LintCode(
5✔
1345
        files,
1346
        dry=dry,
1347
        skip_mypy=skip_mypy,
1348
        bandit=bandit,
1349
        dmypy=dmypy,
1350
        tool=tool,
1351
        prefix=prefix,
1352
        up=up,
1353
        sim=sim,
1354
        strict=strict,
1355
        ty=ty,
1356
        fix=fix,
1357
    ).run()
1358

1359

1360
def check(
5✔
1361
    files: list[str] | str | None = None,
1362
    dry: bool = False,
1363
    bandit: bool = False,
1364
    skip_mypy: bool = False,
1365
    dmypy: bool = False,
1366
    tool: str = ToolOption.default,
1367
    up: bool = False,
1368
    sim: bool = True,
1369
    strict: bool = False,
1370
    ty: bool = False,
1371
) -> None:
1372
    LintCode(
5✔
1373
        files,
1374
        check_only=True,
1375
        _exit=True,
1376
        dry=dry,
1377
        bandit=bandit,
1378
        skip_mypy=skip_mypy,
1379
        dmypy=dmypy,
1380
        tool=tool,
1381
        up=up,
1382
        sim=sim,
1383
        strict=strict,
1384
        ty=ty,
1385
    ).run()
1386

1387

1388
@cli.command(name="lint")
5✔
1389
def make_style(
5✔
1390
    files: list[str] | None = typer.Argument(default=None),  # noqa:B008
1391
    check_only: bool = Option(False, "--check-only", "-c"),
1392
    bandit: bool = Option(False, "--bandit", help="Run `bandit -r <package_dir>`"),
1393
    prefix: bool = Option(
1394
        False,
1395
        "--prefix",
1396
        help="Run lint command with tool prefix, e.g.: pdm run ruff ...",
1397
    ),
1398
    skip_mypy: bool = Option(False, "--skip-mypy"),
1399
    use_dmypy: bool = Option(
1400
        False, "--dmypy", help="Use `dmypy run` instead of `mypy`"
1401
    ),
1402
    tool: str = ToolOption,
1403
    dry: bool = DryOption,
1404
    up: bool = Option(False, help="Whether ruff check with --extend-select=UP"),
1405
    sim: bool = Option(True, help="Whether ruff check with --extend-select=SIM"),
1406
    strict: bool = Option(False, help="Whether run mypy with --strict"),
1407
    ty: bool = Option(False, help="Whether use ty instead of mypy"),
1408
    fix: bool | None = Option(None, help="Whether ruff check with --fix"),
1409
) -> None:
1410
    """Run: ruff check/format to reformat code and then mypy to check"""
1411
    if getattr(files, "default", files) is None:
5✔
1412
        files = ["."]
5✔
1413
    elif isinstance(files, str):
5✔
1414
        files = [files]
5✔
1415
    skip = _ensure_bool(skip_mypy)
5✔
1416
    dmypy = _ensure_bool(use_dmypy)
5✔
1417
    bandit = _ensure_bool(bandit)
5✔
1418
    tool = _ensure_str(tool) or ""
5✔
1419
    up = _ensure_bool(up)
5✔
1420
    sim = _ensure_bool(sim)
5✔
1421
    strict = _ensure_bool(strict)
5✔
1422
    kwargs = {"dry": dry, "skip_mypy": skip, "dmypy": dmypy, "bandit": bandit}
5✔
1423
    if _ensure_bool(check_only):
5✔
1424
        run = check
5✔
1425
    else:
1426
        prefix = _ensure_bool(prefix)
5✔
1427
        if fix is None or not isinstance(fix, bool):
5✔
1428
            fix = load_bool("FASTDEVCLI_FIX", True)
5✔
1429
        run = functools.partial(lint, prefix=prefix, fix=fix)
5✔
1430
    run(files, tool=tool, up=up, sim=sim, strict=strict, ty=ty, **kwargs)
5✔
1431

1432

1433
@cli.command(name="check")
5✔
1434
def only_check(
5✔
1435
    bandit: bool = Option(False, "--bandit", help="Run `bandit -r <package_dir>`"),
1436
    skip_mypy: bool = Option(False, "--skip-mypy"),
1437
    dry: bool = DryOption,
1438
    up: bool = Option(False, help="Whether ruff check with --extend-select=UP"),
1439
    sim: bool = Option(True, help="Whether ruff check with --extend-select=SIM"),
1440
    strict: bool = Option(False, help="Whether run mypy with --strict"),
1441
    ty: bool = Option(False, help="Whether use ty instead of mypy"),
1442
) -> None:
1443
    """Check code style without reformat"""
1444
    bandit = _ensure_bool(bandit)
5✔
1445
    up = _ensure_bool(up)
5✔
1446
    sim = _ensure_bool(sim)
5✔
1447
    skip_mypy = _ensure_bool(skip_mypy)
5✔
1448
    check(dry=dry, bandit=bandit, skip_mypy=skip_mypy, up=up, sim=sim, ty=ty)
5✔
1449

1450

1451
class Sync(DryRun):
5✔
1452
    def __init__(
5✔
1453
        self, filename: str, extras: str, save: bool, dry: bool = False
1454
    ) -> None:
1455
        self.filename = filename
5✔
1456
        self.extras = extras
5✔
1457
        self._save = save
5✔
1458
        super().__init__(dry=dry)
5✔
1459

1460
    def gen(self) -> str:
5✔
1461
        extras, save = self.extras, self._save
5✔
1462
        should_remove = not Path.cwd().joinpath(self.filename).exists()
5✔
1463
        if not (tool := Project.get_manage_tool()):
5✔
1464
            if should_remove or not is_venv():
5✔
1465
                raise EnvError("There project is not managed by uv/pdm/poetry!")
5✔
1466
            return f"python -m pip install -r {self.filename}"
5✔
1467
        prefix = ""
5✔
1468
        if not is_venv():
5✔
1469
            prefix = f"{tool} run " + "--no-sync " * (tool == "uv")
5✔
1470
        ensure_pip = " {1}python -m ensurepip && {1}python -m pip install -U pip &&"
5✔
1471
        export_cmd = "uv export --no-hashes --all-extras --all-groups --frozen"
5✔
1472
        if tool in ("poetry", "pdm"):
5✔
1473
            export_cmd = f"{tool} export --without-hashes --with=dev"
5✔
1474
            if tool == "poetry":
5✔
1475
                ensure_pip = ""
5✔
1476
                if not UpgradeDependencies.should_with_dev():
5✔
1477
                    export_cmd = export_cmd.replace(" --with=dev", "")
5✔
1478
                if extras and isinstance(extras, str | list):
5✔
1479
                    export_cmd += f" --{extras=}".replace("'", '"')
5✔
1480
            elif check_call(prefix + "python -m pip --version"):
×
1481
                ensure_pip = ""
×
1482
        elif check_call(prefix + "python -m pip --version"):
5✔
1483
            ensure_pip = ""
5✔
1484
        install_cmd = (
5✔
1485
            f"{{2}} -o {{0}} &&{ensure_pip} {{1}}python -m pip install -r {{0}}"
1486
        )
1487
        if should_remove and not save:
5✔
1488
            install_cmd += " && rm -f {0}"
5✔
1489
        return install_cmd.format(self.filename, prefix, export_cmd)
5✔
1490

1491

1492
@cli.command()
5✔
1493
def sync(
5✔
1494
    filename: str = "dev_requirements.txt",
1495
    extras: str = Option("", "--extras", "-E"),
1496
    save: bool = Option(
1497
        False, "--save", "-s", help="Whether save the requirement file"
1498
    ),
1499
    dry: bool = DryOption,
1500
) -> None:
1501
    """Export dependencies by poetry to a txt file then install by pip."""
1502
    Sync(filename, extras, save, dry=dry).run()
5✔
1503

1504

1505
def _should_run_test_script(path: Path = Path("scripts")) -> Path | None:
5✔
1506
    for name in ("test.sh", "test.py"):
5✔
1507
        if (file := path / name).exists():
5✔
1508
            return file
5✔
1509
    return None
5✔
1510

1511

1512
def test(dry: bool, ignore_script: bool = False) -> None:
5✔
1513
    cwd = Path.cwd()
5✔
1514
    root = Project.get_work_dir(cwd=cwd, allow_cwd=True)
5✔
1515
    script_dir = root / "scripts"
5✔
1516
    if not _ensure_bool(ignore_script) and (
5✔
1517
        test_script := _should_run_test_script(script_dir)
1518
    ):
1519
        cmd = test_script.relative_to(root).as_posix()
5✔
1520
        if test_script.suffix == ".py":
5✔
1521
            cmd = "python " + cmd
5✔
1522
        if cwd != root:
5✔
1523
            cmd = f"cd {root} && " + cmd
5✔
1524
    else:
1525
        cmd = 'coverage run -m pytest -s && coverage report --omit="tests/*" -m'
5✔
1526
        if not is_venv() or not check_call("coverage --version"):
5✔
1527
            sep = " && "
5✔
1528
            prefix = f"{tool} run " if (tool := Project.get_manage_tool()) else ""
5✔
1529
            cmd = sep.join(prefix + i for i in cmd.split(sep))
5✔
1530
    exit_if_run_failed(cmd, dry=dry)
5✔
1531

1532

1533
@cli.command(name="test")
5✔
1534
def coverage_test(
5✔
1535
    dry: bool = DryOption,
1536
    ignore_script: bool = Option(False, "--ignore-script", "-i"),
1537
) -> None:
1538
    """Run unittest by pytest and report coverage"""
1539
    return test(dry, ignore_script)
5✔
1540

1541

1542
class Publish:
5✔
1543
    class CommandEnum(StrEnum):
5✔
1544
        poetry = "poetry publish --build"
5✔
1545
        pdm = "pdm publish"
5✔
1546
        uv = "uv build && uv publish"
5✔
1547
        twine = "python -m build && twine upload"
5✔
1548

1549
    @classmethod
5✔
1550
    def gen(cls) -> str:
5✔
1551
        if tool := Project.get_manage_tool():
5✔
1552
            return cls.CommandEnum[tool]
5✔
1553
        return cls.CommandEnum.twine
5✔
1554

1555

1556
@cli.command()
5✔
1557
def upload(
5✔
1558
    dry: bool = DryOption,
1559
) -> None:
1560
    """Shortcut for package publish"""
1561
    cmd = Publish.gen()
5✔
1562
    exit_if_run_failed(cmd, dry=dry)
5✔
1563

1564

1565
def should_use_just() -> bool:
5✔
1566
    if shutil.which("just") is None:
5✔
1567
        return False
5✔
1568
    d = Path.cwd()
5✔
1569
    for _ in range(5):
5✔
1570
        f = d / "justfile"
5✔
1571
        if f.exists():
5✔
1572
            text = f.read_text(encoding="utf-8")
5✔
1573
            return any(i.startswith("dev *args:") for i in text.splitlines())
5✔
1574
        if d.joinpath("pyproject.toml").exists():
×
1575
            break
×
1576
    return False
×
1577

1578

1579
def dev(
5✔
1580
    port: int | None | OptionInfo,
1581
    host: str | None | OptionInfo,
1582
    fastapi: bool | None = None,
1583
    uvicorn: bool | None = None,
1584
    prod: bool | None = None,
1585
    reload: bool | None = None,
1586
    file: str | None | ArgumentInfo = None,
1587
    dry: bool = False,
1588
) -> None:
1589
    if should_use_just():
5✔
1590
        args = [i for i in sys.argv[2:] if i != "--dry"]
×
1591
        cmd = "just dev"
×
1592
    else:
1593
        cmd = "uvicorn" if uvicorn else "fastapi dev"
5✔
1594
        args = []
5✔
1595
        if (host := getattr(host, "default", host)) and host not in (
5✔
1596
            "localhost",
1597
            "127.0.0.1",
1598
        ):
1599
            args.append(f"--host={host}")
5✔
1600
        no_port_yet = True
5✔
1601
        if file is not None:
5✔
1602
            try:
5✔
1603
                port = int(str(file))
5✔
1604
            except ValueError:
5✔
1605
                if m := re.search(r"(.*):(\d+)$", str(file)):
5✔
1606
                    h, p = m.group(1), m.group(2)
5✔
1607
                    if h and "--host" not in str(args):
5✔
1608
                        if h == "0":
5✔
1609
                            args.append("--host=0.0.0.0")
5✔
1610
                        else:
1611
                            args.append(f"--host={h}")
5✔
1612
                    args.append(f"--port={p}")
5✔
1613
                    if uvicorn:
5✔
1614
                        p = Path("main.py")
×
1615
                        if p.exists():
×
1616
                            cmd += " main:app"
×
1617
                        elif Path("app", p.name).exists():
×
1618
                            cmd += " app.main:app"
×
1619
                        elif Path("app.py").exists():
×
1620
                            cmd += " app:app"
×
1621
                else:
1622
                    if uvicorn and (
5✔
1623
                        (filepath := Path(str(file))).is_file()
1624
                        or filepath.suffix == ".py"
1625
                    ):
1626
                        file = filepath.stem + ":app"
5✔
1627
                        parent_names = [j for i in filepath.parents if (j := i.name)]
5✔
1628
                        if parent_names:
5✔
1629
                            file = ".".join([*parent_names[::-1], file])
5✔
1630
                    cmd += f" {file}"
5✔
1631
            else:
1632
                if port != 8000:
5✔
1633
                    args.append(f"--port={port}")
5✔
1634
                    no_port_yet = False
5✔
1635
        if (
5✔
1636
            no_port_yet
1637
            and (port := getattr(port, "default", port))
1638
            and str(port) != "8000"
1639
        ):
1640
            args.append(f"--port={port}")
5✔
1641
        if shutil.which("pdm") is not None:
5✔
1642
            cmd = "pdm run " + cmd
5✔
1643
    if args:
5✔
1644
        cmd += " " + " ".join(args)
5✔
1645
    exit_if_run_failed(cmd, dry=dry)
5✔
1646

1647

1648
@cli.command(name="dev")
5✔
1649
def runserver(
5✔
1650
    file_or_port: str | None = typer.Argument(default=None),
1651
    port: int | None = Option(None, "-p", "--port"),
1652
    host: str | None = Option(None, "-h", "--host"),
1653
    fastapi: bool | None = None,
1654
    uvicorn: bool | None = None,
1655
    prod: bool | None = None,
1656
    reload: bool | None = None,
1657
    dry: bool = DryOption,
1658
) -> None:
1659
    """Start a fastapi server(only for fastapi>=0.111.0)"""
1660
    f = functools.partial(dev, port, host, fastapi, uvicorn, prod, reload, dry=dry)
5✔
1661
    if getattr(file_or_port, "default", file_or_port):
5✔
1662
        f(file=file_or_port)
5✔
1663
    else:
1664
        f()
5✔
1665

1666

1667
@cli.command(name="exec")
5✔
1668
def run_by_subprocess(cmd: str, dry: bool = DryOption) -> None:
5✔
1669
    """Run cmd by subprocess, auto set shell=True when cmd contains '|>'"""
1670
    try:
5✔
1671
        rc = run_and_echo(cmd, verbose=True, dry=_ensure_bool(dry))
5✔
1672
    except FileNotFoundError as e:
5✔
1673
        command = cmd.split()[0]
5✔
1674
        if e.filename == command or (
5✔
1675
            e.filename is None and "系统找不到指定的文件" in str(e)
1676
        ):
1677
            echo(f"Command not found: {command}")
5✔
1678
            raise Exit(1) from None
5✔
1679
        raise e
×
1680
    else:
1681
        if rc:
5✔
1682
            raise Exit(rc)
5✔
1683

1684

1685
class MakeDeps(DryRun):
5✔
1686
    def __init__(
5✔
1687
        self,
1688
        tool: str,
1689
        prod: bool = False,
1690
        dry: bool = False,
1691
        active: bool = True,
1692
        inexact: bool = True,
1693
    ) -> None:
1694
        self._tool = tool
5✔
1695
        self._prod = prod
5✔
1696
        self._active = active
5✔
1697
        self._inexact = inexact
5✔
1698
        super().__init__(dry=dry)
5✔
1699

1700
    def should_ensure_pip(self) -> bool:
5✔
1701
        return True
5✔
1702

1703
    def should_upgrade_pip(self) -> bool:
5✔
1704
        return True
5✔
1705

1706
    def get_groups(self) -> list[str]:
5✔
1707
        if self._prod:
5✔
1708
            return []
5✔
1709
        return ["dev"]
5✔
1710

1711
    def gen(self) -> str:
5✔
1712
        if self._tool == "pdm":
5✔
1713
            return "pdm install --frozen " + ("--prod" if self._prod else "-G :all")
5✔
1714
        elif self._tool == "uv":
5✔
1715
            uv_sync = "uv sync" + " --inexact" * self._inexact
5✔
1716
            if self._active:
5✔
1717
                uv_sync += " --active"
5✔
1718
            return uv_sync + ("" if self._prod else " --all-extras --all-groups")
5✔
1719
        elif self._tool == "poetry":
5✔
1720
            return "poetry install " + (
5✔
1721
                "--only=main" if self._prod else "--all-extras --all-groups"
1722
            )
1723
        else:
1724
            cmd = "python -m pip install -e ."
5✔
1725
            if gs := self.get_groups():
5✔
1726
                cmd += " " + " ".join(f"--group {g}" for g in gs)
5✔
1727
            upgrade = "python -m pip install --upgrade pip"
5✔
1728
            if self.should_ensure_pip():
5✔
1729
                cmd = f"python -m ensurepip && {upgrade} && {cmd}"
5✔
1730
            elif self.should_upgrade_pip():
5✔
1731
                cmd = f"{upgrade} && {cmd}"
5✔
1732
            return cmd
5✔
1733

1734

1735
@cli.command(name="deps")
5✔
1736
def make_deps(
5✔
1737
    prod: bool = Option(
1738
        False,
1739
        "--prod",
1740
        help="Only instead production dependencies.",
1741
    ),
1742
    tool: str = ToolOption,
1743
    use_uv: bool = Option(False, "--uv", help="Use `uv` to install deps"),
1744
    use_pdm: bool = Option(False, "--pdm", help="Use `pdm` to install deps"),
1745
    use_pip: bool = Option(False, "--pip", help="Use `pip` to install deps"),
1746
    use_poetry: bool = Option(False, "--poetry", help="Use `poetry` to install deps"),
1747
    active: bool = Option(
1748
        True, help="Add `--active` to uv sync command(Only work for uv project)"
1749
    ),
1750
    inexact: bool = Option(
1751
        True, help="Add `--inexact` to uv sync command(Only work for uv project)"
1752
    ),
1753
    dry: bool = DryOption,
1754
) -> None:
1755
    """Run: ruff check/format to reformat code and then mypy to check"""
1756
    if use_uv + use_pdm + use_pip + use_poetry > 1:
×
1757
        raise typer.BadParameter(
×
1758
            "can only choose one",
1759
            param_hint=("--uv", "--pdm", "--pip", "--poetry"),
1760
        )
1761
    if use_uv:
×
1762
        tool = "uv"
×
1763
    elif use_pdm:
×
1764
        tool = "pdm"
×
1765
    elif use_pip:
×
1766
        tool = "pip"
×
1767
    elif use_poetry:
×
1768
        tool = "poetry"
×
1769
    elif tool == ToolOption.default:
×
1770
        tool = Project.get_manage_tool(cache=True) or "pip"
×
1771
    MakeDeps(tool, prod, active=active, inexact=inexact, dry=dry).run()
×
1772

1773

1774
class UvPypi(DryRun):
5✔
1775
    PYPI = "https://pypi.org/simple"
5✔
1776
    HOST = "https://files.pythonhosted.org"
5✔
1777

1778
    def __init__(
5✔
1779
        self,
1780
        lock_file: Path,
1781
        dry: bool,
1782
        verbose: bool,
1783
        quiet: bool,
1784
        slim: bool = False,
1785
        reverse: bool = False,
1786
    ) -> None:
1787
        super().__init__(dry=dry)
5✔
1788
        self.lock_file = lock_file
5✔
1789
        self._verbose = _ensure_bool(verbose)
5✔
1790
        self._quiet = _ensure_bool(quiet)
5✔
1791
        self._slim = _ensure_bool(slim)
5✔
1792
        self._reverse = _ensure_bool(reverse)
5✔
1793

1794
    def run(self) -> None:
5✔
1795
        try:
5✔
1796
            rc = self.update_lock(
5✔
1797
                self.lock_file, self._verbose, self._quiet, self._slim, self._reverse
1798
            )
1799
        except ValueError as e:
×
1800
            secho(str(e), fg=typer.colors.RED)
×
1801
            raise Exit(1) from e
×
1802
        else:
1803
            if rc != 0:
5✔
1804
                raise Exit(rc)
5✔
1805

1806
    @staticmethod
5✔
1807
    def get_target_content(
5✔
1808
        text: str, verbose: bool, target_registry, target_host: str
1809
    ) -> str | None:
1810
        registry_pattern = r'(registry = ")(.*?)"'
5✔
1811
        registry_urls = {i[1] for i in re.findall(registry_pattern, text)}
5✔
1812
        download_pattern = r'(url = ")(https?://.*?)(/packages/.*?\.)(gz|whl|zip)"'
5✔
1813
        download_hosts = {i[1] for i in re.findall(download_pattern, text)}
5✔
1814
        if not registry_urls:
5✔
1815
            raise ValueError(
×
1816
                f"Failed to find pattern {registry_pattern!r} in uv lock file"
1817
            )
1818

1819
        def replace_registry(s: str) -> str:
5✔
1820
            return re.sub(registry_pattern, rf'\1{target_registry}"', s)
5✔
1821

1822
        def replace_host(s: str) -> str:
5✔
1823
            return re.sub(download_pattern, rf'\1{target_host}\3\4"', s)
5✔
1824

1825
        if len(registry_urls) == 1:
5✔
1826
            current_registry = registry_urls.pop()
5✔
1827
            if current_registry == target_registry:
5✔
1828
                if download_hosts == {target_host}:
5✔
1829
                    return None
5✔
1830
            else:
1831
                text = replace_registry(text)
5✔
1832
                if verbose:
5✔
1833
                    echo(f"{current_registry} --> {target_registry}")
5✔
1834
        else:
1835
            # TODO: ask each one to confirm replace
1836
            text = replace_registry(text)
×
1837
            if verbose:
×
1838
                for current_registry in sorted(registry_urls):
×
1839
                    echo(f"{current_registry} --> {target_registry}")
×
1840
        if len(download_hosts) == 1:
5✔
1841
            current_host = download_hosts.pop()
5✔
1842
            if current_host != target_host:
5✔
1843
                text = replace_host(text)
5✔
1844
                if verbose:
5✔
1845
                    echo(f"{current_host} --> {target_host}")
5✔
1846
        elif download_hosts:
×
1847
            # TODO: ask each one to confirm replace
1848
            text = replace_host(text)
×
1849
            if verbose:
×
1850
                for current_host in sorted(download_hosts):
×
1851
                    echo(f"{current_host} --> {target_host}")
×
1852
        return text
5✔
1853

1854
    @classmethod
5✔
1855
    def update_lock(
5✔
1856
        cls,
1857
        p: Path,
1858
        verbose: bool,
1859
        quiet: bool,
1860
        slim: bool = False,
1861
        reverse: bool = False,
1862
    ) -> int:
1863
        text = p.read_text("utf-8")
5✔
1864
        target_register, target_host = cls.PYPI, cls.HOST
5✔
1865
        if reverse:
5✔
1866
            try:
5✔
1867
                target_register, target_host = cls.get_register_from_uv_config()
5✔
1868
            except FileNotFoundError:
5✔
1869
                if verbose:
5✔
1870
                    echo("Skip register reverse as global uv config file not found.")
5✔
1871
                if quiet:
5✔
1872
                    return 0
5✔
1873
                return 1
×
1874
        new_text = cls.get_target_content(text, verbose, target_register, target_host)
5✔
1875
        if new_text is None:
5✔
1876
            if verbose:
5✔
1877
                echo(f"Registry of {p} is {target_register}, no need to change.")
5✔
1878
            return 0
5✔
1879
        return cls.slim_and_write(new_text, slim, p, verbose, quiet)
5✔
1880

1881
    @classmethod
5✔
1882
    def get_register_from_uv_config(cls) -> tuple[str, str]:
5✔
1883
        config_file = cls.get_uv_config_file()
5✔
1884
        text = config_file.read_text("utf-8")
5✔
1885
        doc = tomllib.loads(text)
5✔
1886
        index_url = doc["index"][0]["url"]
5✔
1887
        return index_url, index_url.replace("/simple", "").rstrip("/")
5✔
1888

1889
    @staticmethod
5✔
1890
    def get_uv_config_file() -> Path:
5✔
1891
        config_dir = "AppData/Roaming" if is_windows() else ".config"
5✔
1892
        return Path.home() / config_dir / "uv/uv.toml"
5✔
1893

1894
    @staticmethod
5✔
1895
    def slim_and_write(
5✔
1896
        text: str, slim: bool, p: Path, verbose: bool, quiet: bool
1897
    ) -> int:
1898
        if slim:
5✔
1899
            pattern = r', size = \d+, upload-time = ".*?"'
5✔
1900
            text = re.sub(pattern, "", text)
5✔
1901
        size = p.write_text(text, encoding="utf-8")
5✔
1902
        if verbose:
5✔
1903
            echo(f"Updated {p} with {size} bytes.")
5✔
1904
        if quiet:
5✔
1905
            return 0
5✔
1906
        return 1
5✔
1907

1908

1909
@cli.command()
5✔
1910
def pypi(
5✔
1911
    file: str | None = typer.Argument(default=None),
1912
    dry: bool = DryOption,
1913
    verbose: bool = False,
1914
    quiet: bool = False,
1915
    slim: bool = False,
1916
    reverse: bool = False,
1917
) -> None:
1918
    """Change registry of uv.lock to be pypi.org"""
1919
    if not (p := Path(_ensure_str(file) or "uv.lock")).exists() and not (
5✔
1920
        (p := Project.get_work_dir() / p.name).exists()
1921
    ):
1922
        yellow_warn(f"{p.name!r} not found!")
5✔
1923
        return
5✔
1924
    UvPypi(p, dry, verbose, quiet, slim, reverse).run()
5✔
1925

1926

1927
def version_callback(value: bool) -> None:
5✔
1928
    if value:
5✔
1929
        echo("Fast Dev Cli Version: " + typer.style(__version__, bold=True))
5✔
1930
        raise Exit()
5✔
1931

1932

1933
@cli.callback()
1934
def common(
1935
    version: bool = Option(
1936
        None,
1937
        "--version",
1938
        "-V",
1939
        callback=version_callback,
1940
        is_eager=True,
1941
        help="Show the version of this tool",
1942
    ),
1943
) -> None: ...
1944

1945

1946
def main() -> None:
5✔
1947
    cli()
5✔
1948

1949

1950
if __name__ == "__main__":  # pragma: no cover
1951
    main()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc