From 043803e041ed722a871f6d67f6168e5c81734ebd Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Wed, 18 Dec 2024 23:21:12 -0600 Subject: [PATCH] Makefile! --- .air.toml | 46 -------------- Dockerfile | 25 +++++--- Makefile | 142 ++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 6 ++ lib/stylegen/gen.sh | 55 +++++++++-------- 5 files changed, 196 insertions(+), 78 deletions(-) delete mode 100644 .air.toml create mode 100644 Makefile create mode 100644 docker-compose.yml diff --git a/.air.toml b/.air.toml deleted file mode 100644 index 39d217b..0000000 --- a/.air.toml +++ /dev/null @@ -1,46 +0,0 @@ -root = "." -testdata_dir = "testdata" -tmp_dir = "tmp" - -[build] - args_bin = ["-ip", "127.0.0.1", "-port", "3000"] - bin = "./tmp/main" - pre_cmd = [] - cmd = "cd lib/stylegen && ./gen.sh -e html -d ../../pages/templates -o ../../public/css && cd ../.. && go build -o ./tmp/main ." - delay = 1000 - exclude_dir = ["assets", "tmp", "vendor", "testdata", "lib/stylegen"] - exclude_file = [] - exclude_regex = ["_test.go"] - exclude_unchanged = false - follow_symlink = false - full_bin = "" - include_dir = [] - include_ext = ["go", "tpl", "tmpl", "html"] - include_file = [] - kill_delay = "0s" - log = "build-errors.log" - poll = false - poll_interval = 0 - post_cmd = [] - rerun = false - rerun_delay = 500 - send_interrupt = false - stop_on_error = false - -[color] - app = "" - build = "yellow" - main = "magenta" - runner = "green" - watcher = "cyan" - -[log] - main_only = false - time = false - -[misc] - clean_on_exit = false - -[screen] - clear_on_rebuild = false - keep_scroll = true diff --git a/Dockerfile b/Dockerfile index 036a0c3..f585b48 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,18 +2,26 @@ FROM golang:1.23.2-alpine AS builder WORKDIR /app -# Download dependencies +# Download dependencies and install required tools COPY go.mod go.sum ./ -RUN go mod download +RUN go mod download && \ + go install github.com/swaggo/swag/cmd/swag@latest -# Copy source code +# Copy source code and Makefile COPY . . -# Install UPX -RUN apk add --no-cache upx +# Install required tools and make scripts executable +RUN apk add --no-cache upx make bash && \ + chmod +x /app/lib/stylegen/gen.sh && \ + chmod +x /app/lib/stylegen/tw/* -# Build with optimizations -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +# Create necessary directories +RUN mkdir -p /app/public/css + +# Generate assets and build with optimizations +SHELL ["/bin/bash", "-c"] +RUN cd /app && make generate && \ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ -ldflags='-s -w -extldflags "-static"' \ -tags netgo,osusergo \ -o /go/bin/app @@ -21,6 +29,9 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ # Final stage FROM scratch +# Copy static files and assets +COPY --from=builder /app/public/css/styles.css /public/css/styles.css +COPY --from=builder /app/docs /docs COPY --from=builder /go/bin/app /app ENTRYPOINT ["/app"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c9f34e7 --- /dev/null +++ b/Makefile @@ -0,0 +1,142 @@ +# Colors +CYAN := \033[36m +YELLOW := \033[33m +GREEN := \033[32m +RED := \033[31m +RESET := \033[0m + +# Emoji +CHECK := โœ… +BUILD := ๐Ÿ”จ +CLEAN := ๐Ÿงน +RUN := ๐Ÿš€ +CSS := ๐ŸŽจ +TEST := ๐Ÿงช +DOCKER := ๐Ÿณ +WARN := โš ๏ธ +DOCS := ๐Ÿ“š + +# Variables +BINARY_NAME := goth.stack +DOCKER_IMAGE := goth-stack +GO_FILES := $(wildcard *.go) +CSS_INPUT := lib/stylegen/base.css +CSS_OUTPUT := public/css/styles.css + +# Docker detection +DOCKER_ENV := $(shell if [ -f /.dockerenv ]; then echo true; else echo false; fi) +ifeq ($(DOCKER_ENV),true) + BASE_PATH := /app +else + BASE_PATH := $(CURDIR) +endif + +.PHONY: all build clean run dev stylegen docker-dev docker-build docker-run test help reset ensure-swag + +help: + @echo "$(CYAN)Available commands:$(RESET)" + @echo "$(YELLOW)make clean$(RESET) - Remove build artifacts" + @echo "$(YELLOW)make reset$(RESET) - Clean and reset the project to initial state" + @echo "$(YELLOW)make build$(RESET) - Generate CSS, docs, and build Go binary" + @echo "$(YELLOW)make run$(RESET) - Build and run the binary" + @echo "$(YELLOW)make dev$(RESET) - Run in development mode" + @echo "$(YELLOW)make docker-dev$(RESET)- Run development environment in Docker" + @echo "$(YELLOW)make docker-run$(RESET)- Run production container" + @echo "$(YELLOW)make test$(RESET) - Run tests" + @echo "$(YELLOW)make prod$(RESET) - Full production build" + +# Check if swag is installed +ensure-swag: + @command -v swag >/dev/null 2>&1 || { \ + echo "$(WARN) $(RED)Swagger CLI (swag) is not installed. Installing...$(RESET)"; \ + go install github.com/swaggo/swag/cmd/swag@latest; \ + } + +reset: + @echo "$(CLEAN) $(CYAN)Performing complete project reset...$(RESET)" + @rm -f $(BINARY_NAME) + @rm -f $(CSS_OUTPUT) + @rm -rf public/css/* + @rm -f tailwind.config.js + @rm -rf docs/docs.go docs/swagger.json docs/swagger.yaml + @go clean -cache -testcache -modcache + @echo "$(CHECK) $(GREEN)Project reset complete$(RESET)" + +clean: + @echo "$(CLEAN) $(CYAN)Cleaning build artifacts...$(RESET)" + @rm -f $(BINARY_NAME) + @rm -f $(CSS_OUTPUT) + @echo "$(CHECK) $(GREEN)Cleanup complete$(RESET)" + +stylegen: + @echo "$(CSS) $(CYAN)Generating CSS styles...$(RESET)" + @echo "Current working directory: $$(pwd)" + @echo "Contents of current directory:" + @ls -la + @echo "\nContents of lib/stylegen:" + @ls -la lib/stylegen + @echo "\nContents of lib/stylegen/tw:" + @ls -la lib/stylegen/tw + @chmod +x $(BASE_PATH)/lib/stylegen/gen.sh + @chmod +x $(BASE_PATH)/lib/stylegen/tw/* + @$(BASE_PATH)/lib/stylegen/gen.sh \ + -e "html" \ + -d "$(BASE_PATH)/pages/templates" \ + -o "$(BASE_PATH)/public/css" + @echo "$(CHECK) $(GREEN)CSS generation complete$(RESET)" + +swaggergen: ensure-swag + @echo "$(DOCS) $(CYAN)Generating Swagger documentation...$(RESET)" + @swag init + @echo "$(CHECK) $(GREEN)Swagger docs generated$(RESET)" + +# Combined generation target +generate: stylegen swaggergen + +build: generate + @echo "$(BUILD) $(CYAN)Building binary...$(RESET)" + @go build -o $(BINARY_NAME) + @echo "$(CHECK) $(GREEN)Build complete: $(BINARY_NAME)$(RESET)" + +test: + @echo "$(TEST) $(CYAN)Running tests...$(RESET)" + @go test ./... + @echo "$(CHECK) $(GREEN)Tests complete$(RESET)" + +dev: generate + @echo "$(RUN) $(CYAN)Starting development server...$(RESET)" + @go run main.go + +run: build + @echo "$(RUN) $(CYAN)Running server...$(RESET)" + @./$(BINARY_NAME) + +docker-dev: + @echo "$(DOCKER) $(CYAN)Starting development container...$(RESET)" + @docker-compose -f docker-compose.dev.yml up --build + +docker-build: generate + @echo "$(DOCKER) $(CYAN)Building production Docker image...$(RESET)" + @docker build -t $(DOCKER_IMAGE) . + @echo "$(CHECK) $(GREEN)Docker image build complete$(RESET)" + +docker-run: docker-build + @echo "$(DOCKER) $(CYAN)Running production container...$(RESET)" + @docker-compose up + +deps: + @echo "$(BUILD) $(CYAN)Installing dependencies...$(RESET)" + @go mod download + @go install github.com/swaggo/swag/cmd/swag@latest + @echo "$(CHECK) $(GREEN)Dependencies installed$(RESET)" + +fmt: + @echo "$(BUILD) $(CYAN)Formatting code...$(RESET)" + @go fmt ./... + @echo "$(CHECK) $(GREEN)Code formatting complete$(RESET)" + +# Full production build +prod: clean generate build + @echo "$(CHECK) $(GREEN)Production build complete$(RESET)" + +.DEFAULT_GOAL := help diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8db5eb0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + app: + image: ${IMAGE} + command: ["/app"] + ports: + - "${APP_PORT}:3000" diff --git a/lib/stylegen/gen.sh b/lib/stylegen/gen.sh index 0702882..04bd6ba 100755 --- a/lib/stylegen/gen.sh +++ b/lib/stylegen/gen.sh @@ -22,6 +22,10 @@ while [[ $# -gt 0 ]]; do esac done +# Get the directory where this script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +echo "Script directory: $SCRIPT_DIR" + OS=$(uname -s) ARCH=$(uname -m) @@ -46,7 +50,7 @@ case $ARCH in "x86_64") ARCH="x64" ;; - "arm64") + "arm64"|"aarch64") ARCH="arm64" ;; *) @@ -54,28 +58,34 @@ case $ARCH in exit 1 ;; esac -# Construct the binary file name -BINARY="./tw/${OS}-${ARCH}" + +echo "Detected OS: $OS" +echo "Detected Architecture: $ARCH" + +# Use tw directory in script location +BINARY="${SCRIPT_DIR}/tw/${OS}-${ARCH}" if [ "$OS" = "windows" ]; then BINARY="${BINARY}.exe" -else - # Set execute permissions on the binary - chmod +x $BINARY fi +echo "Looking for binary at: $BINARY" + +# Check if binary exists +if [ ! -f "$BINARY" ]; then + echo "Binary not found: $BINARY" + echo "Contents of ${SCRIPT_DIR}/tw/:" + ls -la "${SCRIPT_DIR}/tw/" + exit 1 +fi + +# Make binary executable +chmod +x "$BINARY" + +echo "Using binary: $BINARY" echo "Extensions: $EXTENSIONS" echo "Directory: $DIRECTORY" echo "Output Directory: $OUTPUT_DIR" -# Set default values if not provided -OUTPUT_DIR="${OUTPUT_DIR:-../../public/css}" -DIRECTORY="${DIRECTORY:-.}" - -if [[ -z "$EXTENSIONS" ]]; then - echo "No extensions provided." - exit 1 -fi - # Initialize an array for name conditions name_conditions=() @@ -90,12 +100,10 @@ INCLUDE_FILES=$(find "$DIRECTORY" -type f \( "${name_conditions[@]}" \)) echo "Files found: $INCLUDE_FILES" -# Optionally, remove leading './' if necessary -INCLUDE_FILES=$(echo "$INCLUDE_FILES" | sed 's|^\./||') - INCLUDE_FILES_ARRAY=$(echo "$INCLUDE_FILES" | awk '{printf "\"%s\",", $0}' | sed 's/,$//') -# Generate Tailwind config +# Generate Tailwind config in script directory +CONFIG_FILE="${SCRIPT_DIR}/tailwind.config.js" echo "module.exports = { content: [$INCLUDE_FILES_ARRAY], theme: { @@ -105,13 +113,10 @@ echo "module.exports = { themes: [\"night\"], }, plugins: [require('daisyui'), require('@tailwindcss/typography')], -}" > tailwind.config.js - -# Delete original CSS file if it exists -rm -f "${OUTPUT_DIR}/styles.css" +}" > "$CONFIG_FILE" # Run the binary with the generated config -$BINARY build -i ./base.css -c tailwind.config.js -o "${OUTPUT_DIR}/styles.css" --minify +"$BINARY" build -i "${SCRIPT_DIR}/base.css" -c "$CONFIG_FILE" -o "${OUTPUT_DIR}/styles.css" --minify # Wait for all background processes to finish -wait \ No newline at end of file +wait