|
| 1 | +ifneq (,) |
| 2 | +.error This Makefile requires GNU Make. |
| 3 | +endif |
| 4 | + |
| 5 | +.PHONY: build rebuild lint test _test-php-cs-fixer-version _test-php-version _test-run tag pull login push enter |
| 6 | + |
| 7 | +CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 8 | + |
| 9 | +DIR = . |
| 10 | +FILE = Dockerfile |
| 11 | +IMAGE = cytopia/php-cs-fixer |
| 12 | +TAG = latest |
| 13 | + |
| 14 | +PHP = latest |
| 15 | +PCF = latest |
| 16 | + |
| 17 | +build: |
| 18 | +ifeq ($(PHP),latest) |
| 19 | + docker build --build-arg PHP=7-cli-alpine --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) |
| 20 | +else |
| 21 | + docker build --build-arg PHP=$(PHP)-cli-alpine --build-arg PCF=$(PCF) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) |
| 22 | +endif |
| 23 | + |
| 24 | +rebuild: pull |
| 25 | +ifeq ($(PHP),latest) |
| 26 | + docker build --no-cache --build-arg PHP=7-cli-alpine -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) |
| 27 | +else |
| 28 | + docker build --no-cache --build-arg PHP=$(PHP)-cli-alpine -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) |
| 29 | +endif |
| 30 | + |
| 31 | +lint: |
| 32 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-cr --text --ignore '.git/,.github/,tests/' --path . |
| 33 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-crlf --text --ignore '.git/,.github/,tests/' --path . |
| 34 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-single-newline --text --ignore '.git/,.github/,tests/' --path . |
| 35 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-space --text --ignore '.git/,.github/,tests/' --path . |
| 36 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8 --text --ignore '.git/,.github/,tests/' --path . |
| 37 | + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8-bom --text --ignore '.git/,.github/,tests/' --path . |
| 38 | + |
| 39 | +test: |
| 40 | + @$(MAKE) --no-print-directory _test-php-cs-fixer-version |
| 41 | + @$(MAKE) --no-print-directory _test-php-version |
| 42 | + @$(MAKE) --no-print-directory _test-run |
| 43 | + |
| 44 | +_test-php-cs-fixer-version: |
| 45 | + @echo "------------------------------------------------------------" |
| 46 | + @echo "- Testing correct phpcs version" |
| 47 | + @echo "------------------------------------------------------------" |
| 48 | + @echo "Fetching latest version from GitHub"; \ |
| 49 | + if [ "$(PCF)" = "latest" ]; then \ |
| 50 | + LATEST="$$( \ |
| 51 | + curl -L -sS https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases \ |
| 52 | + | tac | tac \ |
| 53 | + | grep -Eo 'tag/v?[.0-9]+?\.[.0-9]+\"' \ |
| 54 | + | grep -Eo '[.0-9]+' \ |
| 55 | + | sort -V \ |
| 56 | + | tail -1 \ |
| 57 | + )"; \ |
| 58 | + echo "Testing for latest: $${LATEST}"; \ |
| 59 | + if ! docker run --rm $(IMAGE) --version | grep -E "^PHP CS Fixer (version)?$${LATEST}"; then \ |
| 60 | + echo "Failed"; \ |
| 61 | + exit 1; \ |
| 62 | + fi; \ |
| 63 | + else \ |
| 64 | + echo "Testing for tag: $(PCF).x.x"; \ |
| 65 | + if ! docker run --rm $(IMAGE) --version | grep -E "^PHP CS Fixer (version[[:space:]])?$(PCF)\.[.0-9]+"; then \ |
| 66 | + echo "Failed"; \ |
| 67 | + exit 1; \ |
| 68 | + fi; \ |
| 69 | + fi; \ |
| 70 | + echo "Success"; \ |
| 71 | + |
| 72 | +_test-php-version: |
| 73 | + @echo "------------------------------------------------------------" |
| 74 | + @echo "- Testing correct PHP version" |
| 75 | + @echo "------------------------------------------------------------" |
| 76 | + @if [ "$(PHP)" = "latest" ]; then \ |
| 77 | + echo "Fetching latest version from GitHub"; \ |
| 78 | + LATEST="$$( \ |
| 79 | + curl -L -sS https://github.com/php/php-src/releases \ |
| 80 | + | tac | tac \ |
| 81 | + | grep -Eo '/php-[.0-9]+?\.[.0-9]+"' \ |
| 82 | + | grep -Eo '[.0-9]+' \ |
| 83 | + | sort -V \ |
| 84 | + | tail -1 \ |
| 85 | + )"; \ |
| 86 | + echo "Testing for latest: $${LATEST}"; \ |
| 87 | + if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+$${LATEST}[[:space:]]"; then \ |
| 88 | + echo "Failed"; \ |
| 89 | + exit 1; \ |
| 90 | + fi; \ |
| 91 | + else \ |
| 92 | + echo "Testing for tag: $(PHP).x"; \ |
| 93 | + if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+$(PHP)\.[.0-9]+[[:space:]]"; then \ |
| 94 | + echo "Failed"; \ |
| 95 | + exit 1; \ |
| 96 | + fi; \ |
| 97 | + fi; \ |
| 98 | + echo "Success"; \ |
| 99 | + |
| 100 | +_test-run: |
| 101 | + @echo "------------------------------------------------------------" |
| 102 | + @echo "- Testing phpcs (success)" |
| 103 | + @echo "------------------------------------------------------------" |
| 104 | + @if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) fix --dry-run --diff .; then \ |
| 105 | + echo "Failed"; \ |
| 106 | + exit 1; \ |
| 107 | + fi; \ |
| 108 | + echo "Success"; |
| 109 | + @echo "------------------------------------------------------------" |
| 110 | + @echo "- Testing phpcs (failure)" |
| 111 | + @echo "------------------------------------------------------------" |
| 112 | + @if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) fix --dry-run --diff .; then \ |
| 113 | + echo "Failed"; \ |
| 114 | + exit 1; \ |
| 115 | + fi; \ |
| 116 | + echo "Success"; |
| 117 | + |
| 118 | +tag: |
| 119 | + docker tag $(IMAGE) $(IMAGE):$(TAG) |
| 120 | + |
| 121 | +pull: |
| 122 | + @grep -E '^\s*FROM' Dockerfile \ |
| 123 | + | sed -e 's/^FROM//g' -e 's/[[:space:]]*as[[:space:]]*.*$$//g' \ |
| 124 | + | xargs -n1 docker pull; |
| 125 | + |
| 126 | +login: |
| 127 | + yes | docker login --username $(USER) --password $(PASS) |
| 128 | + |
| 129 | +push: |
| 130 | + @$(MAKE) tag TAG=$(TAG) |
| 131 | + docker push $(IMAGE):$(TAG) |
| 132 | + |
| 133 | +enter: |
| 134 | + docker run --rm --name $(subst /,-,$(IMAGE)) -it --entrypoint=/bin/sh $(ARG) $(IMAGE):$(TAG) |
0 commit comments