-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
127 lines (95 loc) · 3.37 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
SHELL := /bin/bash
VERSION ?=
TYPE ?= patch
CURRENT_VERSION := $(shell cat VERSION)
GOOS ?= $(word 1, $(subst /, " ", $(word 4, $(shell go version))))
SOURCES := $(wildcard *.go **/*.go) $(MAKEFILE)
BUILD_DIR := build
BIN_DIR := bin
BINARY32 := forest_$(GOOS)_386
BINARY64 := forest_$(GOOS)_amd64
DOCKER_IMAGE := robinmitra/forest
DOCKER_APP_PATH := /go/src/app
DOCKER_VOL := $$(pwd):$(DOCKER_APP_PATH)
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_M),x86_64)
BINARY := $(BINARY64)
else ifeq ($(UNAME_M),amd64)
BINARY := $(BINARY64)
else ifeq ($(UNAME_M),i686)
BINARY := $(BINARY32)
else ifeq ($(UNAME_M),i386)
BINARY := $(BINARY32)
else
$(error "Build on $(UNAME_M) is not supported yet.")
endif
.PHONY: ask_version check_version_provided check_version_not_current check_type_provided
.PHONY: check_prerequisites update_files commit tag bump push release
#########
# Build #
#########
# Build binary corresponding to the current architecture.
build: $(BUILD_DIR)/$(BINARY)
# Build the docker image.
docker_build:
docker build -t $(DOCKER_IMAGE) .
$(BUILD_DIR)/$(BINARY32): $(SOURCES)
docker run --rm -v $(DOCKER_VOL) -e GOARCH=386 -e GOOS=$(GOOS) $(DOCKER_IMAGE) go build -v -o $@
$(BUILD_DIR)/$(BINARY64): $(SOURCES)
docker run --rm -v $(DOCKER_VOL) -e GOARCH=amd64 -e GOOS=$(GOOS) $(DOCKER_IMAGE) \
go build -v -o $@
install: $(BIN_DIR)/forest
$(BIN_DIR):
mkdir -p $@
# Ensure that target doesn't get rebuilt if $(BIN_DIR) gets updated, while still having it as a
# pre-requisite.
$(BIN_DIR)/forest: $(BUILD_DIR)/$(BINARY) | $(BIN_DIR)
cp -f $< $@
########
# Test #
########
docker_test:
docker run --rm -v $$(pwd):/go/src/app robinmitra/forest go test -v ./...
###########
# Version #
###########
ask_version:
$(eval VERSION=$(shell read -p "* What's the new version (current: $$(cat VERSION))? " version; echo $$version))
$(eval TYPE=$(shell read -p "* What's the release type [major/minor/patch]? " t; echo $$t))
check_version_provided:
# Too bad `ifndef` doesn't work with dynamically defined variables!
@if [ "$(VERSION)" == "" ]; then echo "VERSION must be specified"; exit 1; fi
check_version_not_current:
# Too bad `ifndef` doesn't work with dynamically defined variables!
@if [ "$(VERSION)" == "$(CURRENT_VERSION)" ]; then \
echo "VERSION cannot be same as CURRENT_VERSION"; exit 1; fi
check_type_provided:
# Too bad `ifndef` doesn't work with dynamically defined variables!
@if [ "$(TYPE)" == "" ]; then echo "TYPE must be specified"; exit 1; fi
check_prerequisites: check_version_provided check_version_not_current check_type_provided
# Update files with the new version.
update_files:
@echo "==> Bumping version to $(VERSION)"
@for file in "main.go" "VERSION"; do \
sed -i '' "s/$(CURRENT_VERSION)/$(VERSION)/g" $$file; \
done
# Commit version bump changes.
commit:
@echo "==> Commiting version bump changes"
git add main.go VERSION
git commit -m "Bump version for $(TYPE) release to $(VERSION)."
# Create tag for the new version.
tag:
@echo "==> Tagging $(TYPE) release v$(VERSION)"
git tag v$(VERSION)
# Bump the version.
bump: ask_version check_prerequisites update_files commit
bump_and_tag: ask_version check_prerequisites update_files commit
push:
@echo "* Do you want to push the changes? [y/N]: "
@read sure; \
case "$$sure" in \
[yY]) echo "==> Pushing changes up" && git push origin master && git push origin --tags;; \
esac
@echo "==> Finished"
release: bump push