pdm-default-scripts 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
default_scripts.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from collections.abc import MutableMapping
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
import platformdirs
|
|
5
|
+
import tomlkit
|
|
6
|
+
from pdm.core import Core
|
|
7
|
+
from pdm.project import Project
|
|
8
|
+
from pdm.signals import post_init
|
|
9
|
+
from tomlkit import TOMLDocument
|
|
10
|
+
from tomlkit.items import Table
|
|
11
|
+
|
|
12
|
+
DEFAULT_SCRIPTS_FILE = "default-scripts.toml"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _get_scripts_for_creation() -> Table | None:
|
|
16
|
+
default_scripts = platformdirs.user_config_path("pdm") / DEFAULT_SCRIPTS_FILE
|
|
17
|
+
|
|
18
|
+
if not default_scripts.exists():
|
|
19
|
+
return None
|
|
20
|
+
|
|
21
|
+
with default_scripts.open("r", encoding="utf-8") as fp:
|
|
22
|
+
scripts = tomlkit.load(fp).get("scripts")
|
|
23
|
+
|
|
24
|
+
if scripts is None:
|
|
25
|
+
return None
|
|
26
|
+
|
|
27
|
+
assert isinstance(scripts, Table), "[scripts] must be a TOML table"
|
|
28
|
+
|
|
29
|
+
return scripts
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _add_script_with_dotted_keys(scripts_table: Table, name: str, command: Any):
|
|
33
|
+
if isinstance(command, MutableMapping):
|
|
34
|
+
for key, value in command.items():
|
|
35
|
+
dotted_key = tomlkit.key([name, str(key)])
|
|
36
|
+
scripts_table.append(dotted_key, value)
|
|
37
|
+
return
|
|
38
|
+
scripts_table[name] = command
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _get_or_create_table(node: TOMLDocument | Table, key: str) -> Table:
|
|
42
|
+
|
|
43
|
+
if node.get(key) is None:
|
|
44
|
+
node[key] = tomlkit.table()
|
|
45
|
+
|
|
46
|
+
table = node[key]
|
|
47
|
+
assert isinstance(table, Table), f"[{key}] must be TOML table"
|
|
48
|
+
|
|
49
|
+
return table
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def on_post_init(project: Project, **args):
|
|
53
|
+
scripts_to_add = _get_scripts_for_creation()
|
|
54
|
+
|
|
55
|
+
if scripts_to_add is None:
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
tool_table = _get_or_create_table(project.pyproject.open_for_write(), "tool")
|
|
59
|
+
pdm_table = _get_or_create_table(tool_table, "pdm")
|
|
60
|
+
scripts_table = _get_or_create_table(pdm_table, "scripts")
|
|
61
|
+
|
|
62
|
+
for name, command in scripts_to_add.items():
|
|
63
|
+
if name not in scripts_table:
|
|
64
|
+
_add_script_with_dotted_keys(scripts_table, str(name), command)
|
|
65
|
+
project.core.ui.info(f"Added default script: {name}")
|
|
66
|
+
|
|
67
|
+
project.pyproject.write(show_message=False)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def plugin(core: Core) -> None:
|
|
71
|
+
post_init.connect(on_post_init)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pdm-default-scripts
|
|
3
|
+
Version: 0.1.5
|
|
4
|
+
Summary: PDM plugin that adds default scripts after `pdm init`
|
|
5
|
+
Keywords: pdm,plugin,default scripts
|
|
6
|
+
Author-Email: tepek2 <dev@tepek2.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Requires-Dist: pdm>=2.26.7
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
# pdm-default-scripts
|
|
15
|
+
|
|
16
|
+
`pdm-default-scripts` is a PDM plugin that adds your predefined scripts to new projects right after `pdm init`.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Install the plugin into your PDM environment:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pdm self add pdm-default-scripts
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Configure default scripts
|
|
27
|
+
|
|
28
|
+
Create a `default-scripts.toml` file in your user PDM config directory.
|
|
29
|
+
|
|
30
|
+
- Linux: `~/.config/pdm/default-scripts.toml`
|
|
31
|
+
- macOS: `~/Library/Application Support/pdm/default-scripts.toml`
|
|
32
|
+
- Windows: `%APPDATA%\\pdm\\default-scripts.toml`
|
|
33
|
+
|
|
34
|
+
### Example
|
|
35
|
+
|
|
36
|
+
```toml
|
|
37
|
+
[scripts]
|
|
38
|
+
test = "pytest"
|
|
39
|
+
lint = "ruff check ."
|
|
40
|
+
format = "ruff format ."
|
|
41
|
+
|
|
42
|
+
[scripts.cov]
|
|
43
|
+
cmd = "pytest --cov"
|
|
44
|
+
env = { PYTHONPATH = "src" }
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
1. Configure `default-scripts.toml` once.
|
|
50
|
+
2. Run `pdm init` in any new project.
|
|
51
|
+
3. The plugin adds missing entries into `[tool.pdm.scripts]` in `pyproject.toml`.
|
|
52
|
+
|
|
53
|
+
Existing scripts are not overwritten.
|
|
54
|
+
|
|
55
|
+
## How it behaves
|
|
56
|
+
|
|
57
|
+
- Runs on the `post_init` PDM signal.
|
|
58
|
+
- Reads `[scripts]` from your `default-scripts.toml`.
|
|
59
|
+
- Writes scripts into `[tool.pdm.scripts]` only when a script name is not already present.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
default_scripts.py,sha256=9PE_OXizwjcMrLhwHaRK8O3OwnAgEjpITQa4eAHQMFQ,2015
|
|
2
|
+
pdm_default_scripts-0.1.5.dist-info/METADATA,sha256=kju3VyZj5spJw7ktBZojGVXFA_7xkQ6S-FW7WTubx7M,1499
|
|
3
|
+
pdm_default_scripts-0.1.5.dist-info/WHEEL,sha256=Z36eTX6lG3PITRleSd5hAZHCcz52yg3c0JQVxKBbLW0,90
|
|
4
|
+
pdm_default_scripts-0.1.5.dist-info/entry_points.txt,sha256=wY-LO-se0n15txdbXIOLF52ojLxidokIrAZFnVi_nvM,82
|
|
5
|
+
pdm_default_scripts-0.1.5.dist-info/RECORD,,
|