Skip to content

Commit 471cc2e

Browse files
REFACTOR: Major improvements in Makefile from Main Template
1 parent f1c20c7 commit 471cc2e

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

Main Template/Makefile

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,77 @@ VENV := venv
33
OS := $(shell uname 2>/dev/null || echo Windows)
44

55
# Detect correct Python and Pip commands based on OS
6-
ifeq ($(OS), Windows)
6+
ifeq ($(OS), Windows) # Windows
77
PYTHON := $(VENV)/Scripts/python.exe
88
PIP := $(VENV)/Scripts/pip.exe
99
PYTHON_CMD := python
1010
CLEAR_CMD := cls
1111
TIME_CMD :=
12-
else
12+
else # Unix-like
1313
PYTHON := $(VENV)/bin/python3
1414
PIP := $(VENV)/bin/pip
1515
PYTHON_CMD := python3
1616
CLEAR_CMD := clear
1717
TIME_CMD := time
1818
endif
1919

20-
# Main target that runs the scripts
20+
# Logs directory
21+
LOG_DIR := ./Logs
22+
23+
# Ensure logs directory exists (cross-platform)
24+
ENSURE_LOG_DIR := @mkdir -p $(LOG_DIR) 2>/dev/null || $(PYTHON_CMD) -c "import os; os.makedirs('$(LOG_DIR)', exist_ok=True)"
25+
26+
# Run-and-log function, supports DETACH variable
27+
# If DETACH is set, runs the script in detached mode and tails the log file
28+
# Else, runs the script normally
29+
# Run-and-log function, DETACH controls detached execution
30+
ifeq ($(OS), Windows) # Windows
31+
RUN_AND_LOG = \
32+
if [ -z "$(DETACH)" ]; then \
33+
$(PYTHON) $(1); \
34+
else \
35+
LOG_FILE=$(LOG_DIR)/$$(basename $(basename $(1))).log; \
36+
start /B cmd /c "$(PYTHON) $(1)"; \
37+
powershell -Command "Get-Content -Path '$$LOG_FILE' -Wait"; \
38+
fi
39+
else # Unix-like
40+
RUN_AND_LOG = \
41+
if [ -z "$(DETACH)" ]; then \
42+
$(PYTHON) $(1); \
43+
else \
44+
LOG_FILE=$(LOG_DIR)/$$(basename $(basename $(1))).log; \
45+
nohup $(PYTHON) $(1) > $$LOG_FILE 2>&1 & \
46+
tail -f $$LOG_FILE; \
47+
fi
48+
endif
49+
50+
# Default target
2151
all: run
2252

23-
# Main Scripts:
24-
run: $(VENV)
53+
# Make Rules
54+
run: dependencies
55+
$(ENSURE_LOG_DIR)
2556
$(CLEAR_CMD)
26-
$(TIME_CMD) $(PYTHON) ./main.py
57+
$(call RUN_AND_LOG, ./main.py)
2758

28-
# Setup Virtual Environment and Install Dependencies
59+
# Create virtual environment if missing
2960
$(VENV):
61+
@echo "Creating virtual environment..."
3062
$(PYTHON_CMD) -m venv $(VENV)
31-
$(PIP) install -r requirements.txt
63+
$(PIP) install --upgrade pip
3264

33-
# Install the project dependencies
3465
dependencies: $(VENV)
66+
@echo "Installing/Updating Python dependencies..."
67+
$(PIP) install -r requirements.txt
3568

36-
# Generate requirements.txt from the current venv
69+
# Generate requirements.txt from current venv
3770
generate_requirements: $(VENV)
3871
$(PIP) freeze > requirements.txt
3972

40-
# Utility rule for cleaning the project
73+
# Clean artifacts
4174
clean:
42-
rm -rf $(VENV)
75+
rm -rf $(VENV) || rmdir /S /Q $(VENV) 2>nul
4376
find . -type f -name '*.pyc' -delete || del /S /Q *.pyc 2>nul
4477
find . -type d -name '__pycache__' -delete || rmdir /S /Q __pycache__ 2>nul
4578

46-
.PHONY: run clean dependencies generate_requirements
79+
.PHONY: all run clean dependencies generate_requirements

0 commit comments

Comments
 (0)