Skip to content

Commit

Permalink
feat: add option --lib to init command to create a library project (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Mar 23, 2023
1 parent 8a0cf0d commit 06ffe09
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 30 deletions.
1 change: 1 addition & 0 deletions news/1708.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce `--lib` option to `init` command to create a library project without prompting.
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions src/pdm/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
)
parser.add_argument("--python", help="Specify the Python version/path to use")
parser.add_argument("--backend", choices=list(_BACKENDS), help="Specify the build backend")
parser.add_argument("--lib", action="store_true", help="Create a library project")
parser.set_defaults(search_parent=False)

def handle(self, project: Project, options: argparse.Namespace) -> None:
Expand Down Expand Up @@ -89,15 +90,13 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
"For more info, please visit https://peps.python.org/pep-0582/",
style="success",
)
is_library = (
termui.confirm(
is_library = options.lib
if not is_library and self.interactive:
is_library = termui.confirm(
"Is the project a library that is installable?\n"
"A few more questions will be asked to include a project name "
"and build backend"
)
if self.interactive
else False
)
build_backend: type[BuildBackend] | None = None
if is_library:
name = self.ask("Project name", project.root.name)
Expand Down
8 changes: 1 addition & 7 deletions src/pdm/cli/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
class Command(BaseCommand):
"""Install dependencies from lock file"""

arguments = BaseCommand.arguments + [
groups_group,
install_group,
dry_run_option,
lockfile_option,
skip_option,
]
arguments = [*BaseCommand.arguments, groups_group, install_group, dry_run_option, lockfile_option, skip_option]

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
Expand Down
7 changes: 1 addition & 6 deletions src/pdm/cli/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
class Command(BaseCommand):
"""Remove packages from pyproject.toml"""

arguments = BaseCommand.arguments + [
install_group,
dry_run_option,
lockfile_option,
skip_option,
]
arguments = [*BaseCommand.arguments, install_group, dry_run_option, lockfile_option, skip_option]

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
Expand Down
3 changes: 2 additions & 1 deletion src/pdm/cli/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
class Command(BaseCommand):
"""Synchronize the current working set with lock file"""

arguments = BaseCommand.arguments + [
arguments = [
*BaseCommand.arguments,
groups_group,
dry_run_option,
lockfile_option,
Expand Down
4 changes: 1 addition & 3 deletions src/pdm/cli/commands/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class Command(BaseCommand):

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument("--path", help="Show the path to the given virtualenv")
parser.add_argument(
"--python", help="Show the python interpreter path for the given virtualenv"
)
parser.add_argument("--python", help="Show the python interpreter path for the given virtualenv")
subparser = parser.add_subparsers()
CreateCommand.register_to(subparser, "create")
ListCommand.register_to(subparser, "list")
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _pdm_a919b69078acdf0a_complete()
;;

(init)
opts="--backend --global --help --non-interactive --project --python --skip --verbose"
opts="--backend --global --help --lib --non-interactive --project --python --skip --verbose"
;;

(install)
Expand Down
1 change: 1 addition & 0 deletions src/pdm/cli/completions/pdm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from info' -l where -d 'Show the p
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l backend -d 'Specify the build backend'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l lib -d 'Create a library project'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l non-interactive -d 'Don\'t ask questions but use default values'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l project -d 'Specify another path as the project root, which changes the base of pyproject.toml and __pypackages__'
complete -c pdm -A -n '__fish_seen_subcommand_from init' -l python -d 'Specify the Python version/path to use'
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ function TabExpansion($line, $lastWord) {
"init" {
$completer.AddOpts(
@(
[Option]::new(@("-g", "--global", "--non-interactive", "-n", "--python")),
[Option]::new(@("-g", "--global", "--non-interactive", "-n", "--python", "--lib")),
$projectOption,
$skipOption,
[Option]::new(@("--backend")).WithValues(@("pdm-backend", "setuptools", "flit", "hatching", "pdm-pep517"))
Expand Down
1 change: 1 addition & 0 deletions src/pdm/cli/completions/pdm.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ _pdm() {
{-n,--non-interactive}"[Don't ask questions but use default values]"
{-k,--skip}'[Skip some tasks and/or hooks by their comma-separated names]'
'--backend[Specify the build backend]:backend:(pdm-backend setuptools hatchling flit pdm-pep517)'
'--lib[Create a library project]'
'--python[Specify the Python version/path to use]:python:'
)
;;
Expand Down
4 changes: 1 addition & 3 deletions src/pdm/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ def from_splitted_env(name: str, separator: str) -> list[str] | None:
dest="no_self",
help="Don't install the project itself",
)
install_group.add_argument(
"--fail-fast", "-x", action="store_true", help="Abort on first installation error"
)
install_group.add_argument("--fail-fast", "-x", action="store_true", help="Abort on first installation error")


def no_isolation_callback(
Expand Down
1 change: 1 addition & 0 deletions src/pdm/formats/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from argparse import Namespace

from pdm._types import RequirementDict
from pdm.project import Project


def check_fingerprint(project: Project | None, filename: Path | str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ def get_lock_metadata(self, groups: Iterable[str] | None = None) -> dict[str, An
groups = self.iter_groups()
return {
"lock_version": self.lockfile.spec_version,
"content_hash": content_hash,
"groups": sorted(groups, key=lambda x: (x != "default", x)),
"content_hash": content_hash,
}

def write_lockfile(
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def style(text: str, *args: str, style: str | None = None, **kwargs: Any) -> str
return capture.get()


def confirm(*args: str, **kwargs: Any) -> str:
def confirm(*args: str, **kwargs: Any) -> bool:
kwargs.setdefault("default", False)
return Confirm.ask(*args, **kwargs)

Expand Down

0 comments on commit 06ffe09

Please sign in to comment.