Skip to content

Commit

Permalink
feat: Makefile update (#306)
Browse files Browse the repository at this point in the history
Implements Tailwind CSS and tmpl Makefile build + improve templating instructions based on the specific OS
  • Loading branch information
Ujstor authored Sep 18, 2024
1 parent ae0e782 commit e81b0ba
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 88 deletions.
16 changes: 12 additions & 4 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -258,7 +266,7 @@ func (p *Project) CreateMainFile() error {
}

// Define Operating system
p.CheckOs()
p.CheckOS()

// Create the map for our program
p.createFrameworkMap()
Expand Down
25 changes: 16 additions & 9 deletions cmd/template/framework/files/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
```
2 changes: 1 addition & 1 deletion cmd/template/framework/files/air.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
90 changes: 76 additions & 14 deletions cmd/template/framework/files/makefile.tmpl
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -31,27 +76,36 @@ 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:
@echo "Cleaning..."
@rm -f main

# Live Reload
{{if .UnixBased}}
{{- if .OSCheck.UnixBased }}
watch:
@if command -v air > /dev/null; then \
air; \
Expand All @@ -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 }}
32 changes: 31 additions & 1 deletion docs/docs/advanced-flag/htmx-templ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
39 changes: 27 additions & 12 deletions docs/docs/advanced-flag/tailwind.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Loading

0 comments on commit e81b0ba

Please sign in to comment.