eia-api-go/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 $(IS_GNU_SED),-i '',-i)
.PHONY: all schema generate build install clean
# Default target
all: schema generate 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)
sed -E $(SED_INLINE) 's/responses\/data/schemas\/DataResponseContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
sed -E $(SED_INLINE) 's/responses\/route/schemas\/RouteResponseContainer/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 ./...
ifeq ($(IS_GNU_SED), true)
# Fix errors in generated code with GNU sed
sed -E -i 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE)
sed -E -i 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE)
else
# Fix errors in generated code with BSD sed
sed -E -i '' 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE)
sed -E -i '' 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE)
endif
# Pretty it up
$(GO_FORMATTER) -w $(CLIENT_GEN_FILE)
$(GO_FORMATTER) -w $(MAPPER_GEN_FILE)
# Build the client command binary
build: generate
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