# Makefile for ellama project

.PHONY: build test test-detailed test-integration test-srt-integration docker-build-srt-parity test-srt-integration-linux check-compile-warnings checkdocs manual format-elisp refill-news refill-readme

SRT_PARITY_DOCKER_IMAGE ?= ellama-srt-parity:latest
SRT_PARITY_DOCKERFILE ?= docker/srt-parity-linux.Dockerfile
SRT_PARITY_DOCKER_RUN_FLAGS ?= --rm --privileged

# This order is based on the packages dependency graph.
ELLAMA_COMPILE_ORDER = \
	ellama-tools-dlp.el \
	ellama-tools.el \
	ellama-skills.el \
	ellama.el \
	ellama-context.el \
	ellama-transient.el \
	ellama-blueprint.el \
	ellama-community-prompts.el \
	ellama-manual.el

build:
	emacs -batch \
		--eval "(package-initialize)" \
		--eval "(require 'cl-lib)" \
		--eval "(setq load-path (cl-remove-if (lambda (dir) (string-match-p \"/elpa/org-[^/]+/?$$\" dir)) (cons (expand-file-name \".\") load-path)))" \
		-f batch-byte-compile ellama*.el

test:
	emacs -Q -batch \
		--eval "(package-initialize)" \
		--eval "(require 'cl-lib)" \
		--eval "(setq load-path (cl-remove-if (lambda (dir) (string-match-p \"/elpa/org-[^/]+/?$$\" dir)) load-path))" \
		-l ellama.el \
		-l tests/test-ellama.el \
		-l tests/test-ellama-context.el \
		-l tests/test-ellama-tools.el \
		-l tests/test-ellama-tools-dlp.el \
		-l tests/test-ellama-skills.el \
		-l tests/test-ellama-transient.el \
		-l tests/test-ellama-blueprint.el \
		-l tests/test-ellama-manual.el \
		-l tests/test-ellama-community-prompts.el \
		--eval "(ert t)"

test-detailed:
	emacs -Q -batch \
		--eval "(package-initialize)" \
		--eval "(require 'cl-lib)" \
		--eval "(setq load-path (cl-remove-if (lambda (dir) (string-match-p \"/elpa/org-[^/]+/?$$\" dir)) load-path))" \
		-l ellama.el \
		-l tests/test-ellama.el \
		-l tests/test-ellama-context.el \
		-l tests/test-ellama-tools.el \
		-l tests/test-ellama-tools-dlp.el \
		-l tests/test-ellama-skills.el \
		-l tests/test-ellama-transient.el \
		-l tests/test-ellama-blueprint.el \
		-l tests/test-ellama-manual.el \
		-l tests/test-ellama-community-prompts.el \
		--eval "(setq ert-batch-backtrace-right-margin 200)" \
		--eval "(ert-run-tests-batch-and-exit t)"

test-integration:
	emacs -Q -batch \
		--eval "(package-initialize)" \
		--eval "(require 'cl-lib)" \
		--eval "(setq load-path (cl-remove-if (lambda (dir) (string-match-p \"/elpa/org-[^/]+/?$$\" dir)) load-path))" \
		-l ellama.el \
		-l tests/integration-test-ellama.el \
		--eval "(ert t)"

test-srt-integration:
	ELLAMA_SRT_INTEGRATION=1 emacs -batch --eval "(package-initialize)" \
		-l tests/test-ellama-tools-srt-integration.el \
		--eval "(ert-run-tests-batch-and-exit \"test-ellama-tools-srt-integration-\")"

docker-build-srt-parity:
	docker build -t $(SRT_PARITY_DOCKER_IMAGE) -f $(SRT_PARITY_DOCKERFILE) .

test-srt-integration-linux: docker-build-srt-parity
	docker run $(SRT_PARITY_DOCKER_RUN_FLAGS) \
		-v $(CURDIR):/work \
		-w /work \
		$(SRT_PARITY_DOCKER_IMAGE) \
		make test-srt-integration

check-compile-warnings:
	emacs --batch --eval "(package-initialize)" --eval "(setq native-comp-eln-load-path (list default-directory))" -L . -f batch-native-compile $(ELLAMA_COMPILE_ORDER)

checkdocs:
	emacs -Q -batch \
		--eval "(require 'checkdoc)" \
		--eval "(let* ((files (append (file-expand-wildcards \"ellama*.el\") (file-expand-wildcards \"tests/*.el\"))) (checkdoc-autofix-flag 'never) (checkdoc-diagnostic-buffer \"*ellama checkdoc errors*\") (issues 0)) (dolist (file files) (with-current-buffer (find-file-noselect file) (checkdoc-current-buffer t))) (when (get-buffer checkdoc-diagnostic-buffer) (with-current-buffer checkdoc-diagnostic-buffer (goto-char (point-min)) (while (re-search-forward \"^\\\\(.*\\\\):\\\\([0-9]+\\\\): \\\\(.*\\\\)$$\" nil t) (setq issues (1+ issues)) (princ (format \"%s:%s: %s\\n\" (match-string 1) (match-string 2) (match-string 3))))) (kill-buffer checkdoc-diagnostic-buffer)) (if (> issues 0) (progn (princ (format \"checkdocs: %d issue(s)\\n\" issues)) (kill-emacs 1)) (princ \"checkdocs: OK\\n\")))"

manual:
	emacs -batch --eval "(package-initialize)" \
		--eval "(require 'project)" \
		--eval "(require 'cl-lib)" \
		--eval "(setq load-path (cl-remove-if (lambda (dir) (string-match-p \"/elpa/org-[^/]+/?$$\" dir)) load-path))" \
		-l ellama-manual.el \
		--eval "(ellama-manual-export)"

format-elisp:
	emacs -batch --eval "(let ((files (append (file-expand-wildcards \"ellama*.el\") (file-expand-wildcards \"tests/*.el\")))) (package-initialize) (require 'transient) (dolist (file files) (with-current-buffer (find-file-noselect file) (emacs-lisp-mode) (indent-region (point-min) (point-max)) (untabify (point-min) (point-max)) (delete-trailing-whitespace) (save-buffer))))"

refill-org := "(with-current-buffer (find-file-noselect \"FILE\") (package-initialize) (require (quote org)) (require (quote org-element)) (setq fill-column 80) (save-excursion (org-with-wide-buffer (cl-loop for el in (reverse (org-element-map (org-element-parse-buffer) (quote (paragraph quote-block item)) (quote identity))) do (goto-char (org-element-property :contents-begin el)) (org-fill-paragraph)))) (save-buffer))"

refill-news:
	emacs -batch --eval $(subst FILE,./NEWS.org,$(refill-org))

refill-readme:
	emacs -batch --eval $(subst FILE,./README.org,$(refill-org))
