@@ -11,33 +11,28 @@ shift # Remove tool name from arguments
1111setup_environment () {
1212 # UV-first approach (fastest and most reliable)
1313 if command -v uv > /dev/null 2>&1 ; then
14- # Check if we're in a UV project
15- if [ -f " pyproject.toml" ] || [ -f " uv.lock" ]; then
16- echo " Using UV-managed environment..."
17- return 0
18- fi
19- fi
20-
21- # Fallback to .venv if it exists
22- if [ -f " .venv/bin/activate" ]; then
23- echo " Using existing .venv environment..."
24- # shellcheck disable=SC1091
25- source .venv/bin/activate
26- return 0
14+ # Check if we're in a UV project (current dir or parent dirs)
15+ current_dir=" $PWD "
16+ while [ " $current_dir " != " /" ]; do
17+ if [ -f " $current_dir /pyproject.toml" ] || [ -f " $current_dir /uv.lock" ]; then
18+ echo " Using UV-managed environment..."
19+ return 0
20+ fi
21+ current_dir=$( dirname " $current_dir " )
22+ done
2723 fi
2824
29- # Create venv if none exists and we're in a project directory
30- if [ -f " pyproject.toml" ] && [ ! -d " .venv" ]; then
31- echo " Creating virtual environment..."
32- if command -v uv > /dev/null 2>&1 ; then
33- uv venv
34- else
35- python3 -m venv .venv
25+ # Fallback to .venv if it exists (check parent dirs too)
26+ current_dir=" $PWD "
27+ while [ " $current_dir " != " /" ]; do
28+ if [ -f " $current_dir /.venv/bin/activate" ]; then
29+ echo " Using existing .venv environment..."
3630 # shellcheck disable=SC1091
37- source .venv/bin/activate
31+ source " $current_dir /.venv/bin/activate"
32+ return 0
3833 fi
39- return 0
40- fi
34+ current_dir= $( dirname " $current_dir " )
35+ done
4136
4237 # Use system environment as last resort
4338 echo " Using system environment..."
@@ -48,10 +43,45 @@ run_tool() {
4843
4944 echo " Running ${TOOL_NAME} ..."
5045
46+ # Find project root for UV
47+ project_root=" $PWD "
48+ while [ " $project_root " != " /" ]; do
49+ if [ -f " $project_root /pyproject.toml" ] || [ -f " $project_root /uv.lock" ]; then
50+ break
51+ fi
52+ project_root=$( dirname " $project_root " )
53+ done
54+
5155 # Try different execution methods in order of preference
52- if command -v uv > /dev/null 2>&1 && { [ -f " pyproject.toml" ] || [ -f " uv.lock " ] ; } ; then
56+ if command -v uv > /dev/null 2>&1 && [ -f " $project_root / pyproject.toml" ]; then
5357 echo " Executing with UV..."
54- uv run " ${TOOL_NAME} " " $@ "
58+ # If we're in a subdirectory of the project, adjust paths for UV
59+ if [ " $PWD " != " $project_root " ]; then
60+ # We're in a subdirectory (like src/), need to adjust paths
61+ adjusted_args=" "
62+ for arg in " $@ " ; do
63+ case " $arg " in
64+ .* )
65+ # Relative path - adjust it relative to project root
66+ rel_to_root=$( realpath --relative-to=" $project_root " " $PWD /$arg " 2> /dev/null || python3 -c " import os; print(os.path.relpath(os.path.join('$PWD ', '$arg '), '$project_root '))" )
67+ adjusted_args=" $adjusted_args $rel_to_root "
68+ ;;
69+ * )
70+ # Other arguments - keep as is
71+ adjusted_args=" $adjusted_args $arg "
72+ ;;
73+ esac
74+ done
75+ if [ -n " $adjusted_args " ]; then
76+ # shellcheck disable=SC2086
77+ (cd " $project_root " && uv run " ${TOOL_NAME} " $adjusted_args )
78+ else
79+ (cd " $project_root " && uv run " ${TOOL_NAME} " )
80+ fi
81+ else
82+ # We're in project root
83+ uv run " ${TOOL_NAME} " " $@ "
84+ fi
5585 elif [ -f " .venv/bin/${TOOL_NAME} " ]; then
5686 echo " Executing with venv..."
5787 .venv/bin/" ${TOOL_NAME} " " $@ "
@@ -60,7 +90,33 @@ run_tool() {
6090 " ${TOOL_NAME} " " $@ "
6191 else
6292 echo " Executing as Python module..."
63- python3 -m " ${TOOL_NAME} " " $@ "
93+ if command -v uv > /dev/null 2>&1 && [ -f " $project_root /pyproject.toml" ]; then
94+ # Same path adjustment for python -m
95+ if [ " $PWD " != " $project_root " ]; then
96+ adjusted_args=" "
97+ for arg in " $@ " ; do
98+ case " $arg " in
99+ .* )
100+ rel_to_root=$( realpath --relative-to=" $project_root " " $PWD /$arg " 2> /dev/null || python3 -c " import os; print(os.path.relpath(os.path.join('$PWD ', '$arg '), '$project_root '))" )
101+ adjusted_args=" $adjusted_args $rel_to_root "
102+ ;;
103+ * )
104+ adjusted_args=" $adjusted_args $arg "
105+ ;;
106+ esac
107+ done
108+ if [ -n " $adjusted_args " ]; then
109+ # shellcheck disable=SC2086
110+ (cd " $project_root " && uv run python -m " ${TOOL_NAME} " $adjusted_args )
111+ else
112+ (cd " $project_root " && uv run python -m " ${TOOL_NAME} " )
113+ fi
114+ else
115+ uv run python -m " ${TOOL_NAME} " " $@ "
116+ fi
117+ else
118+ python3 -m " ${TOOL_NAME} " " $@ "
119+ fi
64120 fi
65121}
66122
0 commit comments