Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --license and --project-version option to pdm init #2978

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2978.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `--license` and `--project-version` as CLI options to control and streamline them during `pdm init` - especially in automated scenarios with `--non-interactive`
6 changes: 4 additions & 2 deletions src/pdm/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_metadata_from_input(self, project: Project, options: argparse.Namespace)
from pdm.formats.base import array_of_inline_tables, make_array, make_inline_table

name = self.ask_project(project)
version = self.ask("Project version", "0.1.0")
version = self.ask("Project version", options.project_version or "0.1.0")
is_dist = options.dist or bool(options.backend)
if not is_dist and self.interactive:
is_dist = termui.confirm(
Expand Down Expand Up @@ -139,7 +139,7 @@ def get_metadata_from_input(self, project: Project, options: argparse.Namespace)
else:
description = ""
default_python_requires = f"=={python.major}.{python.minor}.*"
license = self.ask("License(SPDX name)", "MIT")
license = self.ask("License(SPDX name)", options.license or "MIT")

git_user, git_email = get_user_email_from_git()
author = self.ask("Author name", git_user)
Expand Down Expand Up @@ -201,6 +201,8 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
"--dist", "--lib", dest="dist", action="store_true", help="Create a package for distribution"
)
group.add_argument("--backend", choices=list(_BACKENDS), help="Specify the build backend, which implies --dist")
group.add_argument("--license", help="Specify the license (SPDX name)")
group.add_argument("--project-version", help="Specify the project's version")
parser.add_argument(
"template", nargs="?", help="Specify the project template, which can be a local path or a Git URL"
)
Expand Down
6 changes: 3 additions & 3 deletions src/pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# BASH completion script for pdm
# Generated by pycomplete 0.4.0

_pdm_a919b69078acdf0a_complete()
_pdm_fd45d3342570d348_complete()
{
local cur script coms opts com
COMPREPLY=()
Expand Down Expand Up @@ -65,7 +65,7 @@ _pdm_a919b69078acdf0a_complete()
;;

(init)
opts="--backend --cookiecutter --copier --dist --global --help --non-interactive --overwrite --project --python --quiet --skip --verbose"
opts="--backend --cookiecutter --copier --dist --global --help --license --non-interactive --overwrite --project --project-version --python --quiet --skip --verbose"
;;

(install)
Expand Down Expand Up @@ -155,4 +155,4 @@ _pdm_a919b69078acdf0a_complete()
fi
}

complete -o default -F _pdm_a919b69078acdf0a_complete pdm
complete -o default -F _pdm_fd45d3342570d348_complete pdm
74 changes: 38 additions & 36 deletions src/pdm/cli/completions/pdm.fish

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function TabExpansion($line, $lastWord) {
@(
[Option]::new(@(
"-g", "--global", "--non-interactive", "-n", "--python", "--dist", "--lib", "--copier",
"--cookiecutter", "--overwrite"
"--cookiecutter", "--overwrite", "--license", "--project-version"
)),
$projectOption,
$skipOption,
Expand Down
2 changes: 2 additions & 0 deletions src/pdm/cli/completions/pdm.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ _pdm() {
'--python[Specify the Python version/path to use]:python:'
'--copier[Use Copier to generate project]'
'--cookiecutter[Use Cookiecutter to generate project]'
'--license[Specify the license (SPDX name)]:license:'
"--project-version[Specify the project's version]:project_version:"
'1:template:'
)
;;
Expand Down
74 changes: 74 additions & 0 deletions tests/cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,77 @@ def test_init_with_backend_default_library(project_no_init, pdm):
def test_init_with_backend_default_library_non_interactive(project_no_init, pdm):
pdm(["init", "-n", "--backend", "flit-core"], obj=project_no_init)
assert project_no_init.backend.__class__.__name__ == "FlitBackend"


def test_init_with_license_non_interactive(project_no_init, pdm, mocker):
mocker.patch(
"pdm.cli.commands.init.get_user_email_from_git",
return_value=("Testing", "[email protected]"),
)
do_use = mocker.patch("pdm.cli.commands.use.Command.do_use", return_value=PythonInfo.from_path(sys.executable))
expected_license = "Proprietary"
result = pdm(["init", "-n", "--license", expected_license], obj=project_no_init)
assert result.exit_code == 0
python_version = f"{project_no_init.python.major}.{project_no_init.python.minor}"
do_use.assert_called_once_with(
project_no_init,
ANY,
first=True,
ignore_remembered=True,
ignore_requires_python=True,
save=False,
hooks=ANY,
)
data = {
"project": {
"authors": [{"email": "[email protected]", "name": "Testing"}],
"dependencies": [],
"description": "Default template for PDM package",
"license": {"text": f"{expected_license}"},
"name": project_no_init.root.name,
"requires-python": f"=={python_version}.*",
"readme": "README.md",
"version": "0.1.0",
},
"tool": {"pdm": {"distribution": False}},
}

with open(project_no_init.root.joinpath("pyproject.toml"), "rb") as fp:
assert tomllib.load(fp) == data


def test_init_with_project_version_non_interactive(project_no_init, pdm, mocker):
mocker.patch(
"pdm.cli.commands.init.get_user_email_from_git",
return_value=("Testing", "[email protected]"),
)
do_use = mocker.patch("pdm.cli.commands.use.Command.do_use", return_value=PythonInfo.from_path(sys.executable))
expected_project_version = "2.0.42"
result = pdm(["init", "-n", "--project-version", expected_project_version], obj=project_no_init)
assert result.exit_code == 0
python_version = f"{project_no_init.python.major}.{project_no_init.python.minor}"
do_use.assert_called_once_with(
project_no_init,
ANY,
first=True,
ignore_remembered=True,
ignore_requires_python=True,
save=False,
hooks=ANY,
)
data = {
"project": {
"authors": [{"email": "[email protected]", "name": "Testing"}],
"dependencies": [],
"description": "Default template for PDM package",
"license": {"text": "MIT"},
"name": project_no_init.root.name,
"requires-python": f"=={python_version}.*",
"readme": "README.md",
"version": f"{expected_project_version}",
},
"tool": {"pdm": {"distribution": False}},
}

with open(project_no_init.root.joinpath("pyproject.toml"), "rb") as fp:
assert tomllib.load(fp) == data
Loading