#!/usr/bin/env make -f

# Pip-Licenses Makefile
# ..................................
# Copyright (c) 2018-2024, raimon
# Copyright (c) 2024-2026, Mr. Walls
# ..................................
# Licensed under MIT (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# ..........................................
# https://github.com/raimon49/pip-licenses/tree/HEAD/LICENSE
# ..........................................
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ifeq "$(LANG)" ""
	LANG="en_US.UTF-8"
endif

ifeq "$(LC_CTYPE)" ""
	LC_CTYPE="en_US.UTF-8"
endif

ifndef SHELL
	SHELL:=command -pv bash
endif

ifeq "$(ERROR_LOG_PATH)" ""
	ERROR_LOG_PATH="/dev/null"
endif

# find correct python in cross-platform way
ifeq "$(COMMAND)" ""
	COMMAND_CMD!=`command -v xcrun || command which which || command -v which || command -v command`
	ifeq "$(COMMAND_CMD)" "*xcrun"
		COMMAND_ARGS=--find
	endif
	ifeq "$(COMMAND_CMD)" "*command"
		COMMAND_ARGS=-pv
	endif
	COMMAND=$(COMMAND_CMD) $(COMMAND_ARGS)
endif

ifeq "$(MAKE)" ""
	#  just no cmake please
	MAKEFLAGS=$(MAKEFLAGS) -s
	MAKE!=`$(COMMAND) make 2>$(ERROR_LOG_PATH) || $(COMMAND) gnumake 2>$(ERROR_LOG_PATH)`
endif

ifeq "$(ECHO)" ""
	ECHO=printf "%s\n"
endif

ifndef PYTHONUTF8
	PYTHONUTF8 := 1
endif

ifeq "$(PYTHON)" ""
	PY_CMD=$(COMMAND) python3
	ifneq "$(PY_CMD)" ""
		PY_ARGS=-B
	else
		PY_CMD=$(COMMAND) python
	endif
	PYTHON=$(PY_CMD) $(PY_ARGS)
endif

# SUPPORT PEP-517 with --use-pep517 when available

ifndef PIP_COMMON_FLAGS
	# Define probable pip install flags based on python command
	ifneq "$(PY_CMD)" ""
		# Define probable pip install flags
		PIP_PREFIX_FLAGS := --no-input
	else
		# Define common pip install flags
		PIP_PREFIX_FLAGS := --no-input
	endif
	# Define common pip install flags
	PIP_COMMON_FLAGS := --exists-action s --upgrade --upgrade-strategy only-if-needed
endif

ifeq "$(DEV_PAT)" ""
	# Replaces 'filelock==*' with 'filelock>=3.19.1' for Python 3.9 support
	DEV_PAT=s/^\(filelock\)\(=+\)\(\[0-9.\]+\)\$$/\\1\>\=3.19.1/g
endif

ifeq "$(LOG)" ""
	LOG=no
endif

ifeq "$(LOG)" "no"
	QUIET=@
	ifeq "$(DO_FAIL)" ""
		DO_FAIL=$(COMMAND) true
	endif
endif

# else
ifeq "$(DO_FAIL)" ""
	DO_FAIL=$(ECHO) "ok"
endif

ifeq "$(SED)" ""
	SED=$(COMMAND) sed -r
endif

ifeq "$(TEST)" ""
	TEST=builtin test
endif

ifeq "$(RM)" ""
	RM=$(COMMAND) rm -f
endif

ifeq "$(RMDIR)" ""
	RMDIR=$(RM)rd
endif

ifeq "$(ZIPINFO)" ""
	ZIPINFO_CMD=$(COMMAND) zipinfo
	ifneq "$(ZIPINFO_CMD)" ""
		ZIPINFO_ARGS=
	else
		ZIPINFO_CMD=$(COMMAND) unzip
		ZIPINFO_ARGS=-Z
	endif
	ZIPINFO=$(ZIPINFO_CMD) $(ZIPINFO_ARGS)
endif

ifeq "$(PIPL_REQ_WHEEL_PATHS)" ""
	PIPL_MAIN_PATHS := "piplicenses/__init__.py" "piplicenses/__main__.py"
	PIPL_CORE_PATHS := "piplicenses/core.py" "piplicenses/tomli_bridge.py" "piplicenses/sorting.py" "piplicenses/_license_expressions.py"
	PIPL_CLI_PATHS := "piplicenses/cli/__init__.py" "piplicenses/cli/_argparse_bridge.py" "piplicenses/cli/_format_config.py" "piplicenses/cli/_mutually_exclusive_toggle.py" "piplicenses/cli/_flags.py" "piplicenses/cli/config.py"
	PIPL_OUTPUT_PATHS := "piplicenses/output/__init__.py" "piplicenses/output/consoles.py" "piplicenses/output/tables.py"
	PIPL_REQ_WHEEL_PATHS := $(PIPL_MAIN_PATHS) $(PIPL_CORE_PATHS) $(PIPL_OUTPUT_PATHS) $(PIPL_CLI_PATHS)
endif

ifeq "$(VENV_NAME)" ""
	ifeq "$(REPO_NAME)" ""
		# based on remote (requires remote named origin)
		# REPO_NAME:=$(shell basename -s .git `git remote get-url origin`)
		# get the local clone directory name (always will exist in clones, even if you call your remote fork on github "github" and setup the remote "upstream")
		REPO_NAME := $(shell basename `git rev-parse --show-toplevel`)
	endif
	# create a venv for THIS git clone (allows developers to have multiple clones if needed)
	VENV_NAME := venv/$(REPO_NAME)
endif

# file path to extra developer requirements generated by PEP-735 tools
DEV_DEPENDS:='dev-requirements'
EXAMPLE_DEPENDS:='examples-requirements'
LINT_DEPENDS:='lint-requirements'
TEST_DEPENDS:='test-requirements'

.SUFFIXES: .py .pyc .pyi

.DEFAULT_GOAL:= help
.PHONY: help setup-venv setup-PEP-735 setup-examples setup-testing setup-linting setup local-install local-uninstall update-lockfiles lint test deploy test-deploy build clean full-clean un-setup

help:
	$(QUIET)$(ECHO) 'Usage: make <subcommand>'
	$(QUIET)$(ECHO) ''
	$(QUIET)$(ECHO) 'Subcommands:'
	$(QUIET)$(ECHO) '    setup           Setup for development'
	$(QUIET)$(ECHO) '      setup-testing Just setup for testing'
	$(QUIET)$(ECHO) '      setup-linting Just setup for linting'
	$(QUIET)$(ECHO) '    local-install   Install locally (editable mode)'
	$(QUIET)$(ECHO) '    local-uninstall Uninstall locally'
	$(QUIET)$(ECHO) '    local-ci-check  Run acceptance tests and linting checks locally'
	$(QUIET)$(ECHO) '    update-style    Re-lint and auto-format with pyproject.toml'
	$(QUIET)$(ECHO) '    build           Build package'
	$(QUIET)$(ECHO) '    lint            Re-lint formatting and typing with pyproject.toml'
	$(QUIET)$(ECHO) '    test            Run project tests'
	$(QUIET)$(ECHO) '    clean           Clean directories (for building)'
	$(QUIET)$(ECHO) '    un-setup        Reset from development'

# utility for some developer environments
venv:
	test -d $@ || mkdir -m 751 ./$@
	$(QUIET)test -d $@ || exit 1 ;

$(VENV_NAME): venv
	test -d ${VENV_NAME} || $(PYTHON) -m venv --without-pip --clear $(VENV_NAME) ;
	$(QUIET)test -d $@ || exit 1 ;

setup-venv: $(VENV_NAME) $(VENV_NAME)/bin/python
	$(QUIET)$(VENV_NAME)/bin/python -B -m ensurepip -vvv || exit 2 ;

setup-PEP-735: $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv
	$(QUIET)$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) pip || exit 21 ;
	$(QUIET)$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) "dependency-groups>=1.3.2" "packaging==26.3" || exit 22 ;

setup-examples: setup-venv setup-PEP-735 local-install
	$(QUIET)test -d docs/examples || exit 31 ;
	$(QUITE)$(RM) docs/examples/$(EXAMPLE_DEPENDS).txt 2>$(ERROR_LOG_PATH) || true ;
	$(VENV_NAME)/bin/python -B -m dependency_groups --pyproject-file pyproject.toml --output docs/examples/$(EXAMPLE_DEPENDS).txt examples ;
	$(QUIET)test -f docs/examples/$(EXAMPLE_DEPENDS).txt || exit 32 ;
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -r docs/examples/$(EXAMPLE_DEPENDS).txt

$(LINT_DEPENDS).txt:: pyproject.toml $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv setup-PEP-735
	$(QUITE)$(RM) $(LINT_DEPENDS).txt 2>$(ERROR_LOG_PATH) || true ;
	$(VENV_NAME)/bin/python -B -m dependency_groups --pyproject-file pyproject.toml --output $(LINT_DEPENDS).txt linting ;
	$(QUIET)test -f $(LINT_DEPENDS).txt || exit 34 ;

$(TEST_DEPENDS).txt:: pyproject.toml $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv setup-PEP-735
	$(QUITE)$(RM) $(TEST_DEPENDS).txt 2>$(ERROR_LOG_PATH) || true ;
	$(VENV_NAME)/bin/python -B -m dependency_groups --pyproject-file pyproject.toml --output $(TEST_DEPENDS).txt testing ;
	$(QUIET)test -f $(TEST_DEPENDS).txt || exit 35 ;

$(DEV_DEPENDS).txt:: pyproject.toml $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv setup-PEP-735
	$(QUITE)$(RM) $(DEV_DEPENDS).txt 2>$(ERROR_LOG_PATH) || true ;
	$(VENV_NAME)/bin/python -B -m dependency_groups --pyproject-file pyproject.toml --output $(DEV_DEPENDS).txt dev ;
	$(QUIET)test -f $(DEV_DEPENDS).txt || exit 33 ;

# setup for just linting (experimental)
setup-linting: $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv setup-PEP-735 $(LINT_DEPENDS).txt
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -r $(LINT_DEPENDS).txt

# setup for just testing (experimental)
setup-testing: $(VENV_NAME)/bin/python setup-venv setup-PEP-735 $(TEST_DEPENDS).txt
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -r $(TEST_DEPENDS).txt

# full setup for development workflows (default isolation)
setup:: $(VENV_NAME) $(VENV_NAME)/bin/python setup-venv setup-PEP-735 $(DEV_DEPENDS).txt
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -r ./.github/gha-requirements.txt
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -r $(DEV_DEPENDS).txt

local-install:: setup-PEP-735
	$(VENV_NAME)/bin/python -m pip $(PIP_PREFIX_FLAGS) install $(PIP_COMMON_FLAGS) -e .

local-uninstall:
	$(QUIET){ test ! -e $(VENV_NAME)/bin/python && test ! -e $(VENV_NAME)/bin/pip ; } || \
	$(VENV_NAME)/bin/python -B -m pip $(PIP_PREFIX_FLAGS) uninstall -y pip-licenses ;

local-ci-check: local-install lint test
	$(QUIET)$(DO_FAIL)  # does nothing successfully (if reached)

update-lockfiles: setup-PEP-735
	$(QUITE)$(RM) $(DEV_DEPENDS).txt 2>$(ERROR_LOG_PATH) || true ;
	$(VENV_NAME)/bin/python -B -m dependency_groups --pyproject-file pyproject.toml --output $(DEV_DEPENDS).in dev ;
	pip-compile -o $(DEV_DEPENDS).out --quiet --rebuild -U pyproject.toml $(DEV_DEPENDS).in
	sed -r -e $(DEV_PAT) $(DEV_DEPENDS).out | tee $(DEV_DEPENDS).txt
	$(QUITE)$(RM) $(DEV_DEPENDS).out 2>$(ERROR_LOG_PATH) || true ;
	$(QUITE)$(RM) $(DEV_DEPENDS).in 2>$(ERROR_LOG_PATH) || true ;

update-demo:
	$(QUIET)$(ECHO) "Not implemented at this time. See ./docs/scripts/gen_demo.sh"

# developer workflow targets

build: clean
	$(VENV_NAME)/bin/python -B -m build || $(PYTHON) -m build || exit 125; # fail if unsuccessful
	test -f dist/pip_licenses-*-py3-*.whl || exit 41; # fail if not found
	test -r dist/pip_licenses-*-py3-*.whl || exit 42; # fail if not readable
	$(ZIPINFO) -1 dist/pip_licenses-*-py3-*.whl $(PIPL_REQ_WHEEL_PATHS) || exit 43;

lint:: local-install lint-ruff lint-mypy lint-bandit
	$(QUIET)$(DO_FAIL)  # does nothing successfully (if reached)

lint-ruff:: setup-linting
	$(VENV_NAME)/bin/python -m ruff --config pyproject.toml check --output-format=github .
	$(VENV_NAME)/bin/python -m ruff --config pyproject.toml format --check .

lint-mypy:: setup-linting local-install lint-ruff
	$(VENV_NAME)/bin/python -m mypy --install-types --non-interactive .

lint-bandit:: setup-linting lint-ruff lint-mypy
	$(VENV_NAME)/bin/python -m bandit -c .bandit.yml -r ./piplicenses/ ./tests/

update-style: setup-linting
	$(VENV_NAME)/bin/python -m ruff --config pyproject.toml check --output-format=github --fix .
	$(VENV_NAME)/bin/python -m ruff --config pyproject.toml format .

test:: setup-testing
	$(VENV_NAME)/bin/python -m pytest

# cleanup and reset

clean::
	$(QUIET)$(RM) -r dist
	$(QUIET)$(RMDIR) ./build 2>$(ERROR_LOG_PATH) || exit 66 ; # fail if unsuccessful

full-clean:: local-uninstall clean
	$(RMDIR) -v ./build 2>$(ERROR_LOG_PATH) || true ;
	$(RM) -vr *.egg-info 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./__pycache__ 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./{piplicenses,tests}/__pycache__ 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./piplicenses/{cli,output}/__pycache__ 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./.coverage 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./.mypy_cache 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./.ruff_cache 2>$(ERROR_LOG_PATH) || true ;
	$(RMDIR) -v ./.pytest_cache 2>$(ERROR_LOG_PATH) || true ;
	$(RM) -v ./{test,lint,dev}-requirements.txt 2>$(ERROR_LOG_PATH) || true ;
	$(RM) -v ./docs/examples/examples-requirements.txt 2>$(ERROR_LOG_PATH) || true ;

un-setup:: full-clean
	$(RMDIR) -v ./venv 2>$(ERROR_LOG_PATH) || true ;

$(VENV_NAME)/bin/: venv $(VENV_NAME)
	$(QUIET)test -d $(VENV_NAME)/bin/ || exit 3 ; # Hint: use $(VENV_NAME) target to generate venv/*/

# dynamic intermediate targets (use 'make setup' to actually make these targets)
$(VENV_NAME)/bin/%: venv $(VENV_NAME) $(VENV_NAME)/bin/
	$(QUIET)test -x $(VENV_NAME)/bin/$* || exit 4 ; # Hint: use $(VENV_NAME) target to generate venv/bin/python

# historical targets, no-longer supported

update-depends:
	$(QUIET)$(ECHO) "See PEP-735. Project no-longer needs to mislabel groups as extras." ;
	$(QUIET)$(ECHO) "Use of 'update-depends' has been deprecated." ;
	$(QUIET)$(ECHO) "Try 'make full-clean ; make $(DEV_DEPENDS).txt' instead." ;
	exit 125

deploy:
	$(QUIET)$(ECHO) "Manual Deployment has been deprecated."
	exit 125

test-deploy:
	$(QUIET)$(ECHO) "Test deployments are deprecated."
	$(QUIET)$(ECHO) "Testing is now automatically performed prior to any deployment."
	exit 125
