diff --git a/cmd/program/program.go b/cmd/program/program.go index a8198d97..3011967e 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -38,7 +38,7 @@ type Project struct { AdvancedOptions map[string]bool AdvancedTemplates AdvancedTemplates GitOptions flags.Git - UnixBased bool + OSCheck map[string]bool } type AdvancedTemplates struct { @@ -120,9 +120,17 @@ const ( // CheckOs checks Operation system and generates MakeFile and `go build` command // Based on Project.Unixbase -func (p *Project) CheckOs() { +func (p *Project) CheckOS() { + p.OSCheck = make(map[string]bool) + if runtime.GOOS != "windows" { - p.UnixBased = true + p.OSCheck["UnixBased"] = true + } + if runtime.GOOS == "linux" { + p.OSCheck["linux"] = true + } + if runtime.GOOS == "darwin" { + p.OSCheck["darwin"] = true } } @@ -258,7 +266,7 @@ func (p *Project) CreateMainFile() error { } // Define Operating system - p.CheckOs() + p.CheckOS() // Create the map for our program p.createFrameworkMap() diff --git a/cmd/template/framework/files/README.md.tmpl b/cmd/template/framework/files/README.md.tmpl index bd551bc2..9dd538bf 100644 --- a/cmd/template/framework/files/README.md.tmpl +++ b/cmd/template/framework/files/README.md.tmpl @@ -8,42 +8,49 @@ These instructions will get you a copy of the project up and running on your loc ## MakeFile -run all make commands with clean tests +Run build make command with tests ```bash -make all build +make all ``` -build the application +Build the application ```bash make build ``` -run the application +Run the application ```bash make run ``` +{{- if and (ne .DBDriver "none") (ne .DBDriver "sqlite") }} Create DB container ```bash make docker-run ``` -Shutdown DB container +Shutdown DB Container ```bash make docker-down ``` -live reload the application +DB Integrations Test: +```bash +make itest +``` +{{- end }} + +Live reload the application: ```bash make watch ``` -run the test suite +Run the test suite: ```bash make test ``` -clean up binary from the last build +Clean up binary from the last build: ```bash make clean -``` \ No newline at end of file +``` diff --git a/cmd/template/framework/files/air.toml.tmpl b/cmd/template/framework/files/air.toml.tmpl index 813d97df..d057ef1d 100644 --- a/cmd/template/framework/files/air.toml.tmpl +++ b/cmd/template/framework/files/air.toml.tmpl @@ -4,7 +4,7 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = {{if .UnixBased }}"./main"{{ else }}".\\main.exe"{{ end }} + bin = {{if .OSCheck.UnixBased }}"./main"{{ else }}".\\main.exe"{{ end }} cmd = "make build" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata"] diff --git a/cmd/template/framework/files/makefile.tmpl b/cmd/template/framework/files/makefile.tmpl index 8a1d4abd..6ad83f32 100644 --- a/cmd/template/framework/files/makefile.tmpl +++ b/cmd/template/framework/files/makefile.tmpl @@ -1,19 +1,64 @@ # Simple Makefile for a Go project # Build the application -all: build +all: build test -build: +{{- if or .AdvancedOptions.htmx .AdvancedOptions.tailwind }} +{{- if .OSCheck.UnixBased }} +templ-install: + @if ! command -v templ > /dev/null; then \ + read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \ + if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \ + go install github.com/a-h/templ/cmd/templ@latest; \ + if [ ! -x "$$(command -v templ)" ]; then \ + echo "templ installation failed. Exiting..."; \ + exit 1; \ + fi; \ + else \ + echo "You chose not to install templ. Exiting..."; \ + exit 1; \ + fi; \ + fi +{{- else }} +templ-install: + @powershell -ExecutionPolicy Bypass -Command "if (Get-Command templ -ErrorAction SilentlyContinue) { \ + ; \ + } else { \ + Write-Output 'Installing templ...'; \ + go install github.com/a-h/templ/cmd/templ@latest; \ + if (-not (Get-Command templ -ErrorAction SilentlyContinue)) { \ + Write-Output 'templ installation failed. Exiting...'; \ + exit 1; \ + } else { \ + Write-Output 'templ installed successfully.'; \ + } \ + }" +{{- end }} +{{- end }} + +{{- if .AdvancedOptions.tailwind}} +{{- if .OSCheck.UnixBased}} +tailwind: + {{ if .OSCheck.linux }}@if [ ! -f tailwindcss ]; then curl -sL https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 -o tailwindcss; fi{{- end }} + {{ if .OSCheck.darwin }}@if [ ! -f tailwindcss ]; then curl -sL https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-macos-x64 -o tailwindcss; fi{{- end }} + @chmod +x tailwindcss +{{- else }} +tailwind: + @if not exist tailwindcss.exe powershell -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri 'https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-windows-x64.exe' -OutFile 'tailwindcss.exe'"{{- end }} +{{- end }} + +build:{{- if .AdvancedOptions.tailwind }} tailwind{{- end }}{{- if or .AdvancedOptions.htmx .AdvancedOptions.tailwind }} templ-install{{- end }} @echo "Building..." - {{if or ( .AdvancedOptions.htmx ) ( .AdvancedOptions.tailwind )}}@templ generate{{end}} - {{if .AdvancedOptions.tailwind}}@tailwindcss -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css{{end}} - {{if .UnixBased }}@go build -o main cmd/api/main.go{{ else }}@go build -o main.exe cmd/api/main.go{{ end }} + {{ if or .AdvancedOptions.htmx .AdvancedOptions.tailwind }}@templ generate{{- end }} + {{ if .AdvancedOptions.tailwind }}@{{ if .OSCheck.UnixBased }}./tailwindcss{{ else }}.\tailwindcss.exe{{ end }} -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css{{ end }} + {{ if .OSCheck.UnixBased }}@go build -o main cmd/api/main.go{{- else }}@go build -o main.exe cmd/api/main.go{{- end }} # Run the application run: @go run cmd/api/main.go -{{if and (ne .DBDriver "none") (ne .DBDriver "sqlite")}} +{{- if and (ne .DBDriver "none") (ne .DBDriver "sqlite") }} +{{- if .OSCheck.UnixBased }} # Create DB container docker-run: @if docker compose up 2>/dev/null; then \ @@ -31,19 +76,28 @@ docker-down: echo "Falling back to Docker Compose V1"; \ docker-compose down; \ fi -{{end}} +{{- else }} +# Create DB container +docker-run: + @docker compose up + +# Shutdown DB container +docker-down: + @docker compose down +{{- end }} +{{- end }} # Test the application test: @echo "Testing..." @go test ./... -v -{{if and (ne .DBDriver "none") (ne .DBDriver "sqlite")}} +{{- if and (ne .DBDriver "none") (ne .DBDriver "sqlite") }} # Integrations Tests for the application itest: @echo "Running integration tests..." @go test ./internal/database -v -{{end}} +{{- end }} # Clean the binary clean: @@ -51,7 +105,7 @@ clean: @rm -f main # Live Reload -{{if .UnixBased}} +{{- if .OSCheck.UnixBased }} watch: @if command -v air > /dev/null; then \ air; \ @@ -67,9 +121,17 @@ watch: exit 1; \ fi; \ fi -{{else}} +{{- else }} watch: - @air -{{end}} + @powershell -ExecutionPolicy Bypass -Command "if (Get-Command air -ErrorAction SilentlyContinue) { \ + air; \ + Write-Output 'Watching...'; \ + } else { \ + Write-Output 'Installing air...'; \ + go install github.com/air-verse/air@latest; \ + air; \ + Write-Output 'Watching...'; \ + }" +{{- end }} -.PHONY: all build run test clean watch +.PHONY: all build run test clean watch{{- if .AdvancedOptions.tailwind }} tailwind{{- end }}{{- if and (ne .DBDriver "none") (ne .DBDriver "sqlite") }} docker-run docker-down itest{{- end }}{{- if or .AdvancedOptions.htmx .AdvancedOptions.tailwind }} templ-install{{- end }} diff --git a/docs/docs/advanced-flag/htmx-templ.md b/docs/docs/advanced-flag/htmx-templ.md index e8641bed..a1a61492 100644 --- a/docs/docs/advanced-flag/htmx-templ.md +++ b/docs/docs/advanced-flag/htmx-templ.md @@ -41,8 +41,38 @@ templ generate make run ``` +## Makefile + +Automates templ with Makefile entries, which are automatically created if the htmx advanced flag is used. +It detects if templ is installed or not and generates templates with the make build command. +Both Windows and Unix-like OS are supported. + +```bash +all: build + +templ-install: + @if ! command -v templ > /dev/null; then \ + read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \ + if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \ + go install github.com/a-h/templ/cmd/templ@latest; \ + if [ ! -x "$$(command -v templ)" ]; then \ + echo "templ installation failed. Exiting..."; \ + exit 1; \ + fi; \ + else \ + echo "You chose not to install templ. Exiting..."; \ + exit 1; \ + fi; \ + fi + +build: templ-install + @echo "Building..." + @templ generate + @go build -o main cmd/api/main.go +``` + ## Templating Templates are generated using the `templ generate` command after project creation. These templates are then compiled into Go code for efficient execution. -You can test HTMX functionality on `localhost:port/web` endpoint. +You can test HTMX functionality on `localhost:PORT/web` endpoint. diff --git a/docs/docs/advanced-flag/tailwind.md b/docs/docs/advanced-flag/tailwind.md index 7d50d350..9a349db1 100644 --- a/docs/docs/advanced-flag/tailwind.md +++ b/docs/docs/advanced-flag/tailwind.md @@ -35,26 +35,41 @@ The project tree would look like this: ## Standalone Tailwind CLI -The idea is not to use Node.js and npm to build `output.css`. To achieve this, visit the [official repository](https://github.com/tailwindlabs/tailwindcss/releases/latest) and download the latest release version for your OS and Arch: +The The idea is to avoid using Node.js and npm to build output.css. -```bash -curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.4/tailwindcss-linux-x64 -``` - -Give execution permission: +The Makefile will have entries for downloading and compiling CSS. It will automatically detect the OS and download the latest release from the [official repository](https://github.com/tailwindlabs/tailwindcss/releases). +## Linux Makefile Example ```bash -chmod +x tailwindcss-linux-x64 -``` +all: build +templ-install: + @if ! command -v templ > /dev/null; then \ + read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \ + if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \ + go install github.com/a-h/templ/cmd/templ@latest; \ + if [ ! -x "$$(command -v templ)" ]; then \ + echo "templ installation failed. Exiting..."; \ + exit 1; \ + fi; \ + else \ + echo "You chose not to install templ. Exiting..."; \ + exit 1; \ + fi; \ + fi -## Compile and minify your CSS +tailwind: + @if [ ! -f tailwindcss ]; then curl -sL https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 -o tailwindcss; fi + @chmod +x tailwindcss -```bash -./tailwindcss-linux-x64 -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css +build: tailwind templ-install + @echo "Building..." + @templ generate + @./tailwindcss -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css + @go build -o main cmd/api/main.go ``` ## Use Tailwind CSS in your project By default, CSS examples are not included in the codebase. -Update base.templ and hello.templ, then rerun templ generate to see the changes at the localhost:PORT/web endpoint. +Update base.templ and hello.templ, then rerun templ generate to see the changes at the `localhost:PORT/web` endpoint. diff --git a/docs/docs/creating-project/makefile.md b/docs/docs/creating-project/makefile.md index 9ccdac0a..86a0eda1 100644 --- a/docs/docs/creating-project/makefile.md +++ b/docs/docs/creating-project/makefile.md @@ -1,49 +1,62 @@ ## Makefile Project Management -This Makefile is included as a default after project creation. It offers a set of commands to simplify various development tasks for managing a Go project. - -## Commands - -- **Build the Application:** -Compiles the application and generates the executable. -```bash -make build -``` - -- **Run the Application:** -Executes the application using `go run`. -```bash -make run -``` - -- **Create DB Container:** -Utilizes Docker Compose to set up the database container. It includes a fallback for Docker Compose V1. -```bash -make docker-run -``` - -- **Shutdown DB Container:** -Stops and removes the database container. It also has a fallback for Docker Compose V1. -```bash -make docker-down -``` - -- **Test the Application:** -Executes all tests defined in the project. -```bash -make test -``` - -- **Clean the Binary:** -Removes the generated binary file. -```bash -make clean -``` - -- **Live Reload:** -Monitors file changes and automatically rebuilds and restarts the application using `air`. -```bash -make watch -``` - -Makefile simplifies common development tasks, making it easier to build, run, test, and manage dependencies in a Go project. It enhances productivity by providing a standardized approach to project management. +Makefile is designed for building, running, and testing a Go project. It includes support for advanced options like HTMX and Tailwind CSS, and handles OS-specific operations for Unix-based systems (Linux/macOS) and Windows. + +## Targets + +***`all`*** + +The default target that builds the application by running the `build` target. + +***`templ-install`*** + +This target installs the Go-based templating tool, `templ`, if it is not already installed. It supports: + +- **Unix-based systems**: Prompts the user to install `templ` if it is missing. +- **Windows**: Uses PowerShell to check for and install `templ`. + +***`tailwind`*** + +This target downloads and sets up `tailwindcss`, depending on the user's operating system: + +- **Linux**: Downloads the Linux binary. +- **macOS**: Downloads the macOS binary. +- **Windows**: Uses PowerShell to download the Windows executable. + +***`build`*** + +Builds the Go application and generates assets with `templ` and `tailwind`, if the corresponding advanced options are enabled: + +- Uses `templ` to generate templates. +- Runs `tailwindcss` to compile CSS. + +***`run`*** + +Runs the Go application by executing the `cmd/api/main.go` file. + +***`docker-run`*** and ***`docker-down`*** + +These targets manage a database container: + +- **Unix-based systems**: Tries Docker Compose V2 first, falls back to V1 if needed. +- **Windows**: Uses Docker Compose without version fallback. + +***`test`*** + +Runs unit tests for the application using `go test`. + +***`itest`*** + +Runs integration tests if a database Lite) is used. + +***`clean`*** + +Removes the compiled binary (`main` or `main.exe` depending on the OS). + +***`watch`*** + +Enables live reload for the project using the `air` tool: + +- **Unix-based systems**: Checks if `air` is installed and prompts for installation if missing. +- **Windows**: Uses PowerShell to manage `air` installation and execution. +