57 lines
2.1 KiB
Makefile
57 lines
2.1 KiB
Makefile
# Variables
|
|
SCHEMA_DIR := ./schema
|
|
SCHEMA_ZIP := eia-api-swagger.zip
|
|
SCHEMA_YML := eia-api-swagger.yaml
|
|
CLIENT_PKG := ./cmd/eia-client
|
|
CLIENT_GEN_FILE := ./api/eiaapi.gen.go
|
|
MAPPER_GEN_FILE := ./api/eiaapi_funcmap.gen.go
|
|
GO_FORMATTER := gofumpt
|
|
|
|
IS_GNU_SED := $(shell sed --version >/dev/null 2>&1 && echo true || echo false)
|
|
SED_INLINE := $(if $(filter true,$(IS_GNU_SED)),-i,-i '')
|
|
|
|
.PHONY: all schema generate test build install clean
|
|
|
|
# Default target
|
|
all: schema generate test build install
|
|
|
|
# Retrieve and prepare schema
|
|
schema:
|
|
curl https://www.eia.gov/opendata/$(SCHEMA_ZIP) -o $(SCHEMA_DIR)/$(SCHEMA_ZIP)
|
|
unzip -o $(SCHEMA_DIR)/$(SCHEMA_ZIP) -d $(SCHEMA_DIR)
|
|
@echo "Using GNU sed: $(IS_GNU_SED)"
|
|
sed -E $(SED_INLINE) 's/responses\/data/schemas\/DataResponseContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
sed -E $(SED_INLINE) 's/responses\/route/schemas\/RouteResponse/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
sed -E $(SED_INLINE) 's/responses\/facets/schemas\/FacetOptionListContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
sed -E $(SED_INLINE) 's/responses\/facet/schemas\/FacetDetailsContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
sed -E $(SED_INLINE) 's/responses\/final-route/schemas\/FinalRouteResponse/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
sed -E $(SED_INLINE) 's/[[:space:]]+Frequency:/ Frequency:\n x-go-name: FrequencyObject/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
|
|
|
|
|
|
# Generate code
|
|
generate:
|
|
go generate ./...
|
|
@echo "Using GNU sed: $(IS_GNU_SED)"
|
|
sed -E $(SED_INLINE) 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE)
|
|
sed -E $(SED_INLINE) 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE)
|
|
sed -E $(SED_INLINE) 's/Routes[[:space:]]+\*\[\]string/Routes *[]Routes/g' $(CLIENT_GEN_FILE)
|
|
# Pretty it up
|
|
$(GO_FORMATTER) -w $(CLIENT_GEN_FILE)
|
|
$(GO_FORMATTER) -w $(MAPPER_GEN_FILE)
|
|
|
|
test:
|
|
# Test EIA API client package
|
|
go test -v ./pkg/eia
|
|
|
|
# Build the client command binary
|
|
build: generate test
|
|
go build -o bin/eia-client $(CLIENT_PKG)
|
|
|
|
# Install the client command binary
|
|
install:
|
|
go install -v $(CLIENT_PKG)
|
|
|
|
# Clean up generated files and build artifacts
|
|
clean:
|
|
rm -rf bin/eia-client
|