From 53f37ff2af4e1bd047802de8a4367a24713fd6b1 Mon Sep 17 00:00:00 2001 From: "Mazzella, Jesse D. (ARC-TI)[KBR Wyle Services, LLC]" Date: Fri, 26 Jul 2024 14:21:20 -0700 Subject: [PATCH 1/3] feat(Makefile): add `clean` target, run as part of `all` target --- docker/Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docker/Makefile b/docker/Makefile index 8645a3d..46bbaf4 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -6,7 +6,7 @@ endif .DEFAULT_GOAL := help -all: ## run all, yamcs-up (yamcs-down) and yamcs-simulator +all: clean ## run all, clean, yamcs-up (yamcs-down) and yamcs-simulator $(MAKE) yamcs-simulator yamcs-up: | yamcs-down ## bring up yamcs system @@ -22,6 +22,12 @@ yamcs-simulator: yamcs-up ## run yamcs simulator yamcs-shell: ## shell into yamcs container docker compose up -d && docker compose exec yamcs bash +clean: ## remove yamcs build artifacts and Docker resources created by this Makefile + docker compose down -v --remove-orphans + docker compose rm -f -s -v + docker image rm -f docker-yamcs || true + rm -rf ../target + help: @printf "\033[37m%-30s\033[0m %s\n" "#----------------------------------------------------------------------------------" @printf "\033[37m%-30s\033[0m %s\n" "# Makefile " @@ -31,3 +37,4 @@ help: print-%: @echo $* = $($*) + \ No newline at end of file From 954c86111f2f6e18ac41fe2e039e627c906c9e3d Mon Sep 17 00:00:00 2001 From: "Mazzella, Jesse D. (ARC-TI)[KBR Wyle Services, LLC]" Date: Fri, 26 Jul 2024 14:22:53 -0700 Subject: [PATCH 2/3] refactor: Add `print_message` def, add informative output to `clean` --- docker/Makefile | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docker/Makefile b/docker/Makefile index 46bbaf4..4a8327a 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -6,6 +6,10 @@ endif .DEFAULT_GOAL := help +define print_message + @printf "\033[32m$1\033[0m\n" +endef + all: clean ## run all, clean, yamcs-up (yamcs-down) and yamcs-simulator $(MAKE) yamcs-simulator @@ -23,16 +27,21 @@ yamcs-shell: ## shell into yamcs container docker compose up -d && docker compose exec yamcs bash clean: ## remove yamcs build artifacts and Docker resources created by this Makefile + $(call print_message, "Stopping any running docker-yamcs containers...") docker compose down -v --remove-orphans + $(call print_message, "Cleaning up docker-yamcs resources (containers, volumes, networks)...") docker compose rm -f -s -v + $(call print_message, "Removing docker-yamcs image...") docker image rm -f docker-yamcs || true + $(call print_message, "Cleaning up yamcs build artifacts...") rm -rf ../target + $(call print_message, "Done!") help: - @printf "\033[37m%-30s\033[0m %s\n" "#----------------------------------------------------------------------------------" - @printf "\033[37m%-30s\033[0m %s\n" "# Makefile " - @printf "\033[37m%-30s\033[0m %s\n" "#----------------------------------------------------------------------------------" - @printf "\033[37m%-30s\033[0m %s\n" "#-targets----------------------description-----------------------------------------" + $(call print_message, "#----------------------------------------------------------------------------------") + $(call print_message, "# Makefile ") + $(call print_message, "#----------------------------------------------------------------------------------") + $(call print_message, "#-targets----------------------description-----------------------------------------") @grep -E '^[a-zA-Z_-].+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' print-%: From f764c32ad8770ba252c5ce365d1507fe27a67c40 Mon Sep 17 00:00:00 2001 From: "Mazzella, Jesse D. (ARC-TI)[KBR Wyle Services, LLC]" Date: Fri, 26 Jul 2024 15:10:43 -0700 Subject: [PATCH 3/3] refactor: modernize the Makefile Sets Makefile options best practices to make it more robust (detailed below). Adds a `print_message` helper function to print colored messages to the terminal. Dynamically calculates the width of the terminal in order to print a better `help` dialog. - use the full path to the python3 executable - use /bin/bash as the shell - `.ONESHELL` - run all commands in a recipe in a single shell - .SHELLFLAGS - `-e`: Exit immediately if a command exits with non-zero exit code - `-u`: Treat unset variables as an error when substituting - `-o pipefail`: Return value of a pipeline is the status of the last command to exit with a non-zero status --- docker/Makefile | 67 ++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/docker/Makefile b/docker/Makefile index 4a8327a..934ae82 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -1,49 +1,58 @@ -ifeq ($(shell which python3),) - PYTHON = python -else - PYTHON = python3 -endif +SHELL := /bin/bash +.ONESHELL: +.SHELLFLAGS := -eu -o pipefail -c +.DELETE_ON_ERROR: +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules + +PYTHON := $(shell command -v python3 2>/dev/null || echo python) .DEFAULT_GOAL := help +# Define function to print messages in different colors +# Usage: $(call print_message,ANSI_COLOR_CODE,Your message here) define print_message - @printf "\033[32m$1\033[0m\n" + @printf "\033[$(1)m$(2)\033[0m\n" endef -all: clean ## run all, clean, yamcs-up (yamcs-down) and yamcs-simulator - $(MAKE) yamcs-simulator - +.PHONY: all clean yamcs-up yamcs-down yamcs-simulator yamcs-shell help + +all: clean yamcs-simulator ## run all: clean, yamcs-up (yamcs-down) and yamcs-simulator + yamcs-up: | yamcs-down ## bring up yamcs system docker compose up -d yamcs-down: ## bring down yamcs system + $(call print_message,33,Stopping any running docker-yamcs containers...) docker compose down -v --remove-orphans yamcs-simulator: yamcs-up ## run yamcs simulator - @echo "connect via http://localhost:8090/ system make take about 50 seconds to startup" && \ + $(call print_message,36,Connect via http://localhost:8090/ system may take about 50 seconds to startup) cd .. && $(PYTHON) ./simulator.py yamcs-shell: ## shell into yamcs container docker compose up -d && docker compose exec yamcs bash -clean: ## remove yamcs build artifacts and Docker resources created by this Makefile - $(call print_message, "Stopping any running docker-yamcs containers...") - docker compose down -v --remove-orphans - $(call print_message, "Cleaning up docker-yamcs resources (containers, volumes, networks)...") +clean: | yamcs-down ## remove yamcs build artifacts and docker resources created by this Makefile + $(call print_message,33,Cleaning up docker-yamcs resources (containers, volumes, networks)...) docker compose rm -f -s -v - $(call print_message, "Removing docker-yamcs image...") - docker image rm -f docker-yamcs || true - $(call print_message, "Cleaning up yamcs build artifacts...") + $(call print_message,33,Removing docker-yamcs image...) + docker image rm -f docker-yamcs 2>/dev/null || true + $(call print_message,33,Cleaning up yamcs build artifacts...) rm -rf ../target - $(call print_message, "Done!") - -help: - $(call print_message, "#----------------------------------------------------------------------------------") - $(call print_message, "# Makefile ") - $(call print_message, "#----------------------------------------------------------------------------------") - $(call print_message, "#-targets----------------------description-----------------------------------------") - @grep -E '^[a-zA-Z_-].+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' - -print-%: - @echo $* = $($*) - \ No newline at end of file + $(call print_message,32,Done!) + +TERM_WIDTH := $(shell tput cols 2>/dev/null || echo 80) + +define print_header + @printf '%*s\n' "$(TERM_WIDTH)" '' | tr ' ' '-' + @printf '%-*s\n' "$(TERM_WIDTH)" "$(1)" + @printf '%*s\n' "$(TERM_WIDTH)" '' | tr ' ' '-' +endef + +help: ## display this help message + $(call print_header,"Makefile") + @awk 'BEGIN {FS = ":.*##"; printf "\033[36m%-30s\033[0m %s\n", "Target", "Description"} /^[a-zA-Z_-]+:.*?##/ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort + +print-%: ## Print any variable (e.g., make print-PYTHON) + @echo $* = $($*) \ No newline at end of file