|
| 1 | +# Thirdweb Makefile |
| 2 | +# Cross-platform targets to mirror tw.bat functionality |
| 3 | +# Requires: GNU Make, dotnet SDK, optional CSharpier |
| 4 | + |
| 5 | +# Use bash for consistent behavior across platforms (Git Bash/MSYS2/WSL/macOS/Linux) |
| 6 | +SHELL := bash |
| 7 | +.SHELLFLAGS := -o pipefail -c |
| 8 | + |
| 9 | +# Default target |
| 10 | +.DEFAULT_GOAL := help |
| 11 | + |
| 12 | +# Tools and paths |
| 13 | +DOTNET := dotnet |
| 14 | +API_CLIENT := Thirdweb/Thirdweb.Api/ThirdwebApi.cs |
| 15 | +CONSOLE_PROJ := Thirdweb.Console |
| 16 | +GENERATOR_PROJ := Thirdweb.Generator |
| 17 | +LIB_PROJ := Thirdweb/Thirdweb.csproj |
| 18 | + |
| 19 | +# Defaults for publishing/building |
| 20 | +CONFIG ?= Release |
| 21 | +TFM ?= netstandard2.1 |
| 22 | +RID ?= |
| 23 | +OUT ?= |
| 24 | + |
| 25 | +# Colors (best effort; will be empty if tput is unavailable) |
| 26 | +C_RST := $(shell tput sgr0 2>/dev/null || echo "") |
| 27 | +C_BOLD := $(shell tput bold 2>/dev/null || echo "") |
| 28 | +C_DIM := $(shell tput dim 2>/dev/null || echo "") |
| 29 | +C_RED := $(shell tput setaf 1 2>/dev/null || echo "") |
| 30 | +C_GRN := $(shell tput setaf 2 2>/dev/null || echo "") |
| 31 | +C_YEL := $(shell tput setaf 3 2>/dev/null || echo "") |
| 32 | +C_BLU := $(shell tput setaf 4 2>/dev/null || echo "") |
| 33 | +C_MAG := $(shell tput setaf 5 2>/dev/null || echo "") |
| 34 | +C_CYN := $(shell tput setaf 6 2>/dev/null || echo "") |
| 35 | + |
| 36 | +# Icons |
| 37 | +IC_BUILD := BUILD |
| 38 | +IC_CLEAN := CLEAN |
| 39 | +IC_RESTORE := RESTORE |
| 40 | +IC_TEST := TEST |
| 41 | +IC_PACK := PACK |
| 42 | +IC_RUN := RUN |
| 43 | +IC_GEN := GEN |
| 44 | +IC_INFO := INFO |
| 45 | +IC_OK := OK |
| 46 | +IC_WARN := WARN |
| 47 | +IC_ERR := ERR |
| 48 | +IC_FMT := FMT |
| 49 | +IC_PUB := PUBLISH |
| 50 | + |
| 51 | +hr = printf '$(C_DIM)%s$(C_RST)\n' '--------------------------------------------------------------------' |
| 52 | +msg = printf '%s[%s]%s %s\n' '$(1)' '$(2)' '$(C_RST)' '$(3)' |
| 53 | + |
| 54 | +.PHONY: help |
| 55 | +help: |
| 56 | + @printf '\n$(C_CYN)$(C_BOLD)%s$(C_RST)\n' 'Thirdweb Tools' |
| 57 | + @$(hr) |
| 58 | + @printf 'Usage: $(C_BOLD)make$(C_RST) $(C_CYN)[target]$(C_RST)\n\n' |
| 59 | + @printf '$(C_BOLD)Targets:$(C_RST)\n' |
| 60 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'build' 'Generate API and build the solution' |
| 61 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'clean' 'Clean build artifacts' |
| 62 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'restore' 'Restore NuGet packages' |
| 63 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'test' 'Run tests' |
| 64 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'pack' 'Generate API (if needed) and create NuGet package' |
| 65 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'publish' 'Publish the Thirdweb project (dotnet publish)' |
| 66 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'run' 'Run the console application' |
| 67 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'generate' 'Generate API client from OpenAPI spec' |
| 68 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'lint' 'Check code formatting (dry run)' |
| 69 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'fix' 'Fix code formatting issues' |
| 70 | + @printf ' $(C_CYN)%-12s$(C_RST) - %s\n' 'help' 'Show this help message' |
| 71 | + @$(hr) |
| 72 | + |
| 73 | +.PHONY: publish |
| 74 | +# Publish the Thirdweb library project |
| 75 | +# Usage examples: |
| 76 | +# make publish # Release publish |
| 77 | +# make publish CONFIG=Debug # Debug config |
| 78 | +# make publish RID=win-x64 # Target runtime |
| 79 | +# make publish OUT=artifacts/publish # Custom output dir |
| 80 | +publish: |
| 81 | + @if [ ! -f '$(API_CLIENT)' ]; then \ |
| 82 | + $(call msg,$(C_YEL),$(IC_WARN),API client not found, generating it first) ; \ |
| 83 | + $(MAKE) --no-print-directory generate ; \ |
| 84 | + fi |
| 85 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_PUB) Publishing Thirdweb project) |
| 86 | + @CMD="$(DOTNET) publish '$(LIB_PROJ)' -c '$(CONFIG)' -f '$(TFM)'"; \ |
| 87 | + if [ -n "$(RID)" ]; then CMD="$$CMD -r '$(RID)'"; fi; \ |
| 88 | + if [ -n "$(OUT)" ]; then CMD="$$CMD -o '$(OUT)'"; fi; \ |
| 89 | + echo $$CMD; eval $$CMD && \ |
| 90 | + $(call msg,$(C_GRN),$(IC_OK),Publish succeeded) || \ |
| 91 | + $(call msg,$(C_RED),$(IC_ERR),Publish failed) |
| 92 | + |
| 93 | +.PHONY: generate |
| 94 | +# Clean previous file and generate API client |
| 95 | +generate: |
| 96 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_GEN) Cleaning generated API files) |
| 97 | + @rm -f '$(API_CLIENT)' 2>/dev/null || true |
| 98 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_GEN) Generating Thirdweb API client with custom generator) |
| 99 | + @$(DOTNET) run --project '$(GENERATOR_PROJ)' --no-build >/dev/null 2>&1 \ |
| 100 | + || ( \ |
| 101 | + $(call msg,$(C_MAG),>> ,Building generator) ; \ |
| 102 | + $(DOTNET) build '$(GENERATOR_PROJ)' ; \ |
| 103 | + $(call msg,$(C_MAG),>> ,Running generator) ; \ |
| 104 | + $(DOTNET) run --project '$(GENERATOR_PROJ)' \ |
| 105 | + ) |
| 106 | + @$(call msg,$(C_GRN),$(IC_OK),API client generation complete) |
| 107 | + |
| 108 | +.PHONY: build |
| 109 | +build: |
| 110 | + @$(MAKE) --no-print-directory generate |
| 111 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_BUILD) Building with dotnet build) |
| 112 | + @$(DOTNET) build && \ |
| 113 | + $(call msg,$(C_GRN),$(IC_OK),Build succeeded) || \ |
| 114 | + $(call msg,$(C_RED),$(IC_ERR),Build failed) |
| 115 | + |
| 116 | +.PHONY: clean |
| 117 | +clean: |
| 118 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_CLEAN) Cleaning with dotnet clean) |
| 119 | + @$(DOTNET) clean && \ |
| 120 | + $(call msg,$(C_GRN),$(IC_OK),Clean completed) || \ |
| 121 | + $(call msg,$(C_RED),$(IC_ERR),Clean failed) |
| 122 | + |
| 123 | +.PHONY: restore |
| 124 | +restore: |
| 125 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_RESTORE) Restoring with dotnet restore) |
| 126 | + @$(DOTNET) restore && \ |
| 127 | + $(call msg,$(C_GRN),$(IC_OK),Restore completed) || \ |
| 128 | + $(call msg,$(C_RED),$(IC_ERR),Restore failed) |
| 129 | + |
| 130 | +.PHONY: test |
| 131 | +test: |
| 132 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_TEST) Running dotnet test) |
| 133 | + @$(DOTNET) test && \ |
| 134 | + $(call msg,$(C_GRN),$(IC_OK),All tests passed) || \ |
| 135 | + $(call msg,$(C_RED),$(IC_ERR),Some tests failed) |
| 136 | + |
| 137 | +.PHONY: pack |
| 138 | +pack: |
| 139 | + @if [ ! -f '$(API_CLIENT)' ]; then \ |
| 140 | + $(call msg,$(C_YEL),$(IC_WARN),API client not found, generating it first) ; \ |
| 141 | + $(MAKE) --no-print-directory generate ; \ |
| 142 | + fi |
| 143 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_BUILD) Building Release) |
| 144 | + @$(DOTNET) build --configuration Release || { $(call msg,$(C_RED),$(IC_ERR),Build (Release) failed); exit 1; } |
| 145 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_PACK) Packing NuGet package(s)) |
| 146 | + @$(DOTNET) pack --configuration Release && \ |
| 147 | + $(call msg,$(C_GRN),$(IC_OK),Pack completed) || \ |
| 148 | + $(call msg,$(C_RED),$(IC_ERR),Packing failed) |
| 149 | + |
| 150 | +.PHONY: run |
| 151 | +run: |
| 152 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_RUN) dotnet run --project $(CONSOLE_PROJ)) |
| 153 | + @$(DOTNET) run --project '$(CONSOLE_PROJ)' && \ |
| 154 | + $(call msg,$(C_GRN),$(IC_OK),Application exited) || \ |
| 155 | + $(call msg,$(C_RED),$(IC_ERR),Application exited with errors) |
| 156 | + |
| 157 | +.PHONY: lint |
| 158 | +lint: |
| 159 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_FMT) Checking code formatting with CSharpier) |
| 160 | + @csharpier --help >/dev/null 2>&1 || { \ |
| 161 | + $(call msg,$(C_YEL),$(IC_WARN),CSharpier is not installed) ; \ |
| 162 | + printf ' Install it with: dotnet tool install -g csharpier\n' ; \ |
| 163 | + exit 0 ; \ |
| 164 | + } |
| 165 | + @csharpier check . >/dev/null 2>&1 || { \ |
| 166 | + $(call msg,$(C_YEL),$(IC_WARN),Formatting issues found) ; \ |
| 167 | + printf ' Run "make fix" to automatically fix them.\n' ; \ |
| 168 | + exit 0 ; \ |
| 169 | + } |
| 170 | + @$(call msg,$(C_GRN),$(IC_OK),Code formatting is correct) |
| 171 | + |
| 172 | +.PHONY: fix |
| 173 | +fix: |
| 174 | + @$(call msg,$(C_BLU),$(IC_INFO),$(IC_FMT) Running CSharpier formatter) |
| 175 | + @csharpier --help >/dev/null 2>&1 || { \ |
| 176 | + $(call msg,$(C_YEL),$(IC_WARN),CSharpier is not installed) ; \ |
| 177 | + printf ' Install it with: dotnet tool install -g csharpier\n' ; \ |
| 178 | + exit 0 ; \ |
| 179 | + } |
| 180 | + @csharpier format . >/dev/null 2>&1 || { \ |
| 181 | + $(call msg,$(C_RED),$(IC_ERR),CSharpier formatting failed) ; \ |
| 182 | + exit 1 ; \ |
| 183 | + } |
| 184 | + @$(call msg,$(C_GRN),$(IC_OK),Code formatting completed) |
0 commit comments