forked from cytopia/docker-php-cs-fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
204 lines (180 loc) · 6.97 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
ifneq (,)
.error This Makefile requires GNU Make.
endif
.PHONY: lint build rebuild test
# --------------------------------------------------------------------------------------------------
# VARIABLES
# --------------------------------------------------------------------------------------------------
CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CURRENT_PHP_VERSION =
DIR = .
FILE = Dockerfile
IMAGE = cytopia/php-cs-fixer
TAG = latest
NO_CACHE =
PHP = latest
PCF = latest
PHP_LATEST = $(shell \
while ! DATA="$$( curl -sS --fail 'https://github.com/docker-library/php' )"; do \
sleep 1; \
done; \
echo "$${DATA}" | grep -Eo 'php/tree/master/[.0-9]+"' | grep -Eo '[.0-9]+' | sort | tail -1 \
)
# --------------------------------------------------------------------------------------------------
# DEFAULT TARGET
# --------------------------------------------------------------------------------------------------
help:
@echo "lint Lint repository files"
@echo "build [PHP=] [PCF=] Build image with PHP and PCF version"
@echo "test [PHP=] [PCF=] Test image with PHP and PCF version"
# --------------------------------------------------------------------------------------------------
# LINT TARGETS
# --------------------------------------------------------------------------------------------------
lint:
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-cr --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-crlf --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-single-newline --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-space --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8 --text --ignore '.git/,.github/,tests/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8-bom --text --ignore '.git/,.github/,tests/' --path .
# --------------------------------------------------------------------------------------------------
# BUILD TARGETS
# --------------------------------------------------------------------------------------------------
build:
ifeq ($(PCF),1)
ifeq ($(PHP),latest)
@# PHP CS Fixer version 1 goes only up to PHP 7.1
docker build $(NO_CACHE) --build-arg PHP=7.1 --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)
else
docker build $(NO_CACHE) --build-arg PHP=$(PHP) --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)
endif
else
ifeq ($(PHP),latest)
docker build $(NO_CACHE) --build-arg PHP=$(PHP_LATEST) --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)
else
docker build $(NO_CACHE) --build-arg PHP=$(PHP) --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR)
endif
endif
rebuild: _pull
rebuild: NO_CACHE=--no-cache
rebuild: build
# --------------------------------------------------------------------------------------------------
# TEST TARGETS
# --------------------------------------------------------------------------------------------------
test:
@$(MAKE) --no-print-directory _test-php-cs-fixer-version
@$(MAKE) --no-print-directory _test-php-version
@$(MAKE) --no-print-directory _test-run
.PHONY: _test-php-cs-fixer-version
_test-php-cs-fixer-version:
@echo "------------------------------------------------------------"
@echo "- Testing correct phpcs version"
@echo "------------------------------------------------------------"
@echo "Fetching latest version from GitHub"; \
if [ "$(PCF)" = "latest" ]; then \
LATEST="$$( \
curl -L -sS https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases \
| tac | tac \
| grep -Eo 'tag/v?[.0-9]+?\.[.0-9]+\"' \
| grep -Eo '[.0-9]+' \
| sort -u \
| tail -1 \
)"; \
echo "Testing for latest: $${LATEST}"; \
if ! docker run --rm $(IMAGE) --version | grep -E "^PHP CS Fixer (version)?$${LATEST}"; then \
echo "Failed"; \
exit 1; \
fi; \
else \
echo "Testing for tag: $(PCF).x.x"; \
if ! docker run --rm $(IMAGE) --version | grep -E "^PHP CS Fixer (version[[:space:]])?$(PCF)\.[.0-9]+"; then \
echo "Failed"; \
exit 1; \
fi; \
fi; \
echo "Success"; \
.PHONY: _test-php-version
_test-php-version: _get-php-version
@echo "------------------------------------------------------------"
@echo "- Testing correct PHP version"
@echo "------------------------------------------------------------"
@if [ "$(PCF)" = "1" ] && [ "$(PHP)" = "latest" ]; then \
echo "Testing for tag: 7.1.x"; \
if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+7\.1\.[.0-9]+[[:space:]]"; then \
echo "Failed"; \
exit 1; \
fi; \
else \
echo "Testing for tag: $(CURRENT_PHP_VERSION)"; \
if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+$(CURRENT_PHP_VERSION)([.0-9]+)?"; then \
echo "Failed"; \
exit 1; \
fi; \
fi; \
echo "Success"; \
.PHONY: _test-run
_test-run:
@echo "------------------------------------------------------------"
@echo "- Testing phpcs (success)"
@echo "------------------------------------------------------------"
@if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) fix --dry-run --diff .; then \
echo "Failed"; \
exit 1; \
fi; \
echo "Success";
@echo "------------------------------------------------------------"
@echo "- Testing phpcs (failure)"
@echo "------------------------------------------------------------"
@if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) fix --dry-run --diff .; then \
echo "Failed"; \
exit 1; \
fi; \
echo "Success";
# --------------------------------------------------------------------------------------------------
# HELPER TARGETS
# --------------------------------------------------------------------------------------------------
.PHONY: _pull
_pull:
@echo "Pull base image"
ifeq ($(PCF),1)
ifeq ($(PHP),latest)
@# PHP CS Fixer version 1 goes only up to PHP 7.1
docker pull php:7.1
else
docker pull php:$(PHP)-cli-alpine
endif
else
ifeq ($(PHP),latest)
docker pull php:$(PHP_LATEST)-cli-alpine
else
docker pull php:$(PHP)-cli-alpine
endif
endif
# Fetch latest available PHP version for cli-alpine
.PHONY: _get-php-version
_get-php-version:
$(eval CURRENT_PHP_VERSION = $(shell \
if [ "$(PHP)" = "latest" ]; then \
curl -L -sS https://hub.docker.com/api/content/v1/products/images/php \
| tac | tac \
| grep -Eo '`[.0-9]+-cli-alpine' \
| grep -Eo '[.0-9]+' \
| sort -u \
| tail -1; \
else \
echo $(PHP); \
fi; \
))
# --------------------------------------------------------------------------------------------------
# DEPLOY TARGETS
# --------------------------------------------------------------------------------------------------
.PHONY: tag
tag:
docker tag $(IMAGE) $(IMAGE):$(TAG)
.PHONY: login
login:
yes | docker login --username $(USER) --password $(PASS)
.PHONY: push
push:
@$(MAKE) tag TAG=$(TAG)
docker push $(IMAGE):$(TAG)