Skip to content

Commit 8eb71d1

Browse files
committed
Add MANIFEST.in and enhance build hook for script inclusion
This commit introduces a new MANIFEST.in file to include the get-next-version.cjs script and all files in the scripts directory during package builds. The CustomBuildHook is updated to dynamically locate the scripts directory, improving flexibility for different project layouts and ensuring all relevant scripts are included in the wheel package.
1 parent 30a5ec3 commit 8eb71d1

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include src/python_package_folder/scripts/get-next-version.cjs
2+
recursive-include src/python_package_folder/scripts *.cjs

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ local = false
8888
[tool.hatch.build.targets.wheel]
8989
# The source location for the package.
9090
packages = ["src/python_package_folder"]
91-
# Include the scripts directory in the wheel
92-
# Using force-include to ensure get-next-version.cjs is included
93-
force-include = { "src/python_package_folder/scripts/get-next-version.cjs" = "python_package_folder/scripts/get-next-version.cjs" }
94-
# Also keep the build hook as a fallback (though force-include should work)
91+
# Using a build hook to automatically include all files in the scripts directory
92+
# The build hook dynamically adds files to force_include, which works for both sdist and wheel builds
9593
[tool.hatch.build.hooks.custom]
9694
path = "src/python_package_folder/_hatch_build.py"
9795

9896

97+
9998
# ---- Settings ----
10099

101100
[tool.ruff]

src/python_package_folder/_hatch_build.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,55 @@ class CustomBuildHook(BuildHookInterface):
1818

1919
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
2020
"""Initialize the build hook and add scripts directory files."""
21-
# Get the source directory for the package
22-
source_dir = Path(self.root) / "src" / "python_package_folder"
23-
scripts_dir = source_dir / "scripts"
24-
2521
# Debug: Print to stderr so it shows in build output
2622
print(f"[DEBUG] Build hook called. Root: {self.root}", file=sys.stderr)
27-
print(f"[DEBUG] Scripts dir exists: {scripts_dir.exists()}", file=sys.stderr)
23+
24+
# Try multiple possible locations for the scripts directory
25+
# 1. Source layout: src/python_package_folder/scripts
26+
# 2. Sdist layout: python_package_folder/scripts (after extraction)
27+
# 3. Alternative sdist layout: scripts/ (if extracted differently)
28+
possible_scripts_dirs = [
29+
Path(self.root) / "src" / "python_package_folder" / "scripts",
30+
Path(self.root) / "python_package_folder" / "scripts",
31+
Path(self.root) / "scripts",
32+
]
33+
34+
scripts_dir = None
35+
for possible_dir in possible_scripts_dirs:
36+
if possible_dir.exists() and possible_dir.is_dir():
37+
scripts_dir = possible_dir
38+
print(f"[DEBUG] Found scripts dir at: {scripts_dir}", file=sys.stderr)
39+
break
40+
41+
if scripts_dir is None:
42+
print(f"[DEBUG] Scripts directory not found. Tried: {[str(d) for d in possible_scripts_dirs]}", file=sys.stderr)
43+
return
2844

2945
# If scripts directory exists, include all files from it
3046
if scripts_dir.exists() and scripts_dir.is_dir():
3147
# Add all files from scripts directory to force-include
3248
# This ensures they're included in the wheel at the correct location
3349
for script_file in scripts_dir.iterdir():
3450
if script_file.is_file():
35-
# Calculate relative paths
36-
source_path = script_file.relative_to(self.root)
37-
# Target path inside the wheel package
51+
# Calculate relative paths from project root
52+
try:
53+
source_path = script_file.relative_to(self.root)
54+
except ValueError:
55+
# If relative_to fails, try to construct path manually
56+
# This can happen with sdist layouts
57+
if "python_package_folder" in str(script_file):
58+
# Extract the part after python_package_folder
59+
parts = script_file.parts
60+
try:
61+
idx = parts.index("python_package_folder")
62+
source_path = Path(*parts[idx:])
63+
except (ValueError, IndexError):
64+
# Fallback: use the filename
65+
source_path = Path("python_package_folder") / "scripts" / script_file.name
66+
else:
67+
source_path = Path("python_package_folder") / "scripts" / script_file.name
68+
69+
# Target path inside the wheel package (always the same)
3870
target_path = f"python_package_folder/scripts/{script_file.name}"
3971

4072
print(f"[DEBUG] Adding {source_path} -> {target_path}", file=sys.stderr)
@@ -46,8 +78,6 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
4678
build_data["force_include"][str(source_path)] = target_path
4779

4880
print(f"[DEBUG] force_include now has {len(build_data.get('force_include', {}))} entries", file=sys.stderr)
49-
else:
50-
print(f"[DEBUG] Scripts directory not found at {scripts_dir}", file=sys.stderr)
5181

5282

5383
# Export the hook class (hatchling might need this)

0 commit comments

Comments
 (0)