Skip to content

Instantly share code, notes, and snippets.

@yeiichi
Last active December 20, 2025 01:34
Show Gist options
  • Select an option

  • Save yeiichi/a66804ab30c5ec2b26ecd0f2458ac536 to your computer and use it in GitHub Desktop.

Select an option

Save yeiichi/a66804ab30c5ec2b26ecd0f2458ac536 to your computer and use it in GitHub Desktop.
Make file template
# ==============================================================================
# PROJECT: [Insert Project Name Here]
# PURPOSE: Standardized Makefile for Python projects using a virtual environment.
# This file defines common commands for setting up, running, and
# cleaning the project.
# USAGE: Type 'make' or 'make help' to see available targets.
# REQUIRES: make, python3, pip (to be run in a shell environment)
# ==============================================================================
# Define variables for the virtual environment and its executables.
# NOTE: Replace 'venv' with the actual path or name of your venv (e.g., '.venv').
ROOT ?= $(CURDIR)
VENV ?= .venv
# Detect OS to handle bin vs Scripts
ifeq ($(OS),Windows_NT)
BIN := $(VENV)/Scripts
else
BIN := $(VENV)/bin
endif
PYTHON := $(BIN)/python
PIP := $(BIN)/pip
# Set the default target when 'make' is executed without arguments.
.DEFAULT_GOAL := help
# Declare phony targets to prevent conflicts with files of the same name.
.PHONY: help install run clean secret-gen
help: ## Show this help message
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@printf " \033[36m%-15s\033[0m %s\n" "help" "Show this help message"
@grep -E '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) \
| grep -v '^help:' \
| sort \
| awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
install: ## Install dependencies from requirements.txt
@echo "Ensuring virtual environment exists..."
@test -d $(VENV) || python3 -m venv $(VENV)
@echo "Installing dependencies using $(PIP)..."
$(PIP) install --upgrade pip
$(PIP) install -r requirements.txt
@echo "Installation complete."
run: ## Run the main application
@echo "Running the main application: main.py"
# Load .env file if it exists and export variables to the shell
# then execute main.py using the venv interpreter.
@if [ -f .env ]; then \
echo "Loading .env file..."; \
export $$(grep -v '^#' .env | xargs);
fi; \
$(PYTHON) main.py
clean: ## Remove temporary cache files
@if [ -z "$(strip $(ROOT))" ]; then echo "ROOT is empty. Abort."; exit 2; fi
@if [ "$(ROOT)" = "/" ]; then echo "ROOT is /. Abort."; exit 2; fi
@echo "Cleaning under: $(ROOT)"
@find "$(ROOT)" -path "*/$(VENV)" -prune -o -name "__pycache__" -type d -exec rm -rf {} +
@find "$(ROOT)" -path "*/$(VENV)" -prune -o -name "*.pyc" -delete
secret-gen: ## Generate a new Django Secret Key and append to .env
@echo "Generating new Django SECRET_KEY..."
@SECRET=$$($(PYTHON) -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'); \
if [ ! -f .env ]; then \
echo "SECRET_KEY='$$SECRET'" > .env; \
echo ".env file created with new SECRET_KEY."; \
else \
if grep -q "SECRET_KEY" .env; then \
echo "SECRET_KEY already exists in .env. Skipping."; \
else \
echo "SECRET_KEY='$$SECRET'" >> .env; \
echo "SECRET_KEY appended to existing .env."; \
fi \
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment