From 04989ff86d38e09ee447e8fa547d11b595e98c41 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 20 Dec 2019 18:26:26 +0100 Subject: [PATCH 1/3] Add lint script Co-authored-by: Michele Locati Co-authored-by: Quan TRAN --- .editorconfig | 4 ++++ .travis.yml | 2 +- README.md | 4 ++++ scripts/Dockerfile-shfmt | 3 +++ scripts/lint | 30 ++++++++++++++++++++++++++++++ scripts/lint.bat | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 scripts/Dockerfile-shfmt create mode 100755 scripts/lint create mode 100644 scripts/lint.bat diff --git a/.editorconfig b/.editorconfig index 21985b5..c6b90d4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,3 +8,7 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true + +[*.{bat,cmd}] +end_of_line = crlf +charset = latin1 diff --git a/.travis.yml b/.travis.yml index f8c318a..afa3cde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ jobs: language: go go: '1.13' before_script: GO111MODULE=on go install mvdan.cc/sh/v3/cmd/shfmt - script: shfmt -d -s -ln posix -i 0 -ci -kp install-php-extensions scripts/common scripts/travisci-test-extensions scripts/travisci-update-readme scripts/update-readme + script: shfmt -d -s -ln posix -i 0 -ci -kp install-php-extensions scripts/common scripts/lint scripts/travisci-test-extensions scripts/travisci-update-readme scripts/update-readme - <<: *test-extensions-job-template name: Test extensions on Alpine 3.7 diff --git a/README.md b/README.md index 69ac319..de913fc 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,10 @@ Some extension has special requirements: ## How to contribute +### Formatting code + +Before submitting any pull request, be sure to execute the `lint` script in the `scripts` directory (or `lint.bat` on Windows). + ### Adding support to a new PHP extension? 1. change the `install-php-extensions` script diff --git a/scripts/Dockerfile-shfmt b/scripts/Dockerfile-shfmt new file mode 100644 index 0000000..5decefa --- /dev/null +++ b/scripts/Dockerfile-shfmt @@ -0,0 +1,3 @@ +FROM golang + +RUN go get github.com/mvdan/sh/cmd/shfmt diff --git a/scripts/lint b/scripts/lint new file mode 100755 index 0000000..3499919 --- /dev/null +++ b/scripts/lint @@ -0,0 +1,30 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +if ! docker --version >/dev/null 2>/dev/null; then + printf 'Docker is not installed, or it is not running\n' >&2 + exit 1 +fi + +#SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. pwd)" + +if ! docker build -t docker-php-extension-installer-shfmt:latest -f scripts/Dockerfile-shfmt -q .; then + printf 'ERROR!' >&2 + exit 1 +fi + +fix() { + printf 'Fixing %s... ' "$1" + if docker run --rm -v "$SRC_DIR:/src" -w /src docker-php-extension-installer-shfmt:latest shfmt -s -ln posix -i 0 -ci -kp -w "$1"; then + printf 'done.\n' + fi +} + +fix install-php-extensions +fix scripts/common +fix scripts/lint +fix scripts/travisci-test-extensions +fix scripts/travisci-update-readme +fix scripts/update-readme diff --git a/scripts/lint.bat b/scripts/lint.bat new file mode 100644 index 0000000..19d9e55 --- /dev/null +++ b/scripts/lint.bat @@ -0,0 +1,34 @@ +@echo off +setlocal + +docker --version >NUL 2>NUL +if errorlevel 1 goto :no-docker + +cd /d "%~dp0.." +if errorlevel 1 goto err +set SRC_DIR=%CD% + +docker build -t docker-php-extension-installer-shfmt:latest -f scripts\Dockerfile-shfmt -q . +if errorlevel 1 goto :err + +call :fix install-php-extensions +call :fix scripts/common +call :fix scripts/lint +call :fix scripts/travisci-test-extensions +call :fix scripts/travisci-update-readme +call :fix scripts/update-readme +goto :eof + +:no-docker +echo Docker is not installed, or it's not running >&2 +goto :eof + +:err +echo ERROR! >&2 +goto :eof + +:fix +echo|set /p="Fixing %1... " +docker run --rm -v "%SRC_DIR%:/src" -w /src docker-php-extension-installer-shfmt:latest shfmt -s -ln posix -i 0 -ci -kp -w %1 +if not errorlevel 1 echo done. +exit /b 0 From e5541c42a549b50814b71f2011bd4dc5d385c016 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 20 Dec 2019 18:44:45 +0100 Subject: [PATCH 2/3] Simplify linting --- .travis.yml | 2 +- scripts/invoke-shfmt | 28 ++++++++++++++++++++++++++++ scripts/lint | 19 +++++-------------- scripts/lint.bat | 14 ++------------ 4 files changed, 36 insertions(+), 27 deletions(-) create mode 100755 scripts/invoke-shfmt diff --git a/.travis.yml b/.travis.yml index afa3cde..5545520 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ jobs: language: go go: '1.13' before_script: GO111MODULE=on go install mvdan.cc/sh/v3/cmd/shfmt - script: shfmt -d -s -ln posix -i 0 -ci -kp install-php-extensions scripts/common scripts/lint scripts/travisci-test-extensions scripts/travisci-update-readme scripts/update-readme + script: ./scripts/invoke-shfmt check - <<: *test-extensions-job-template name: Test extensions on Alpine 3.7 diff --git a/scripts/invoke-shfmt b/scripts/invoke-shfmt new file mode 100755 index 0000000..801c6f6 --- /dev/null +++ b/scripts/invoke-shfmt @@ -0,0 +1,28 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +PARAMS='-s -ln posix -i 0 -ci -kp' + +case "${1:-}" in + check) + PARAMS="$PARAMS -d" + ;; + fix) + PARAMS="$PARAMS -w" + ;; + *) + fprintf 'Syntax: %s ' "$0" >&2 + exit 1 + ;; +esac + +shfmt $PARAMS \ + install-php-extensions \ + scripts/common \ + scripts/invoke-shfmt \ + scripts/lint \ + scripts/travisci-test-extensions \ + scripts/travisci-update-readme \ + scripts/update-readme diff --git a/scripts/lint b/scripts/lint index 3499919..7e6f7c8 100755 --- a/scripts/lint +++ b/scripts/lint @@ -8,23 +8,14 @@ if ! docker --version >/dev/null 2>/dev/null; then exit 1 fi -#SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. pwd)" +SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. pwd)" if ! docker build -t docker-php-extension-installer-shfmt:latest -f scripts/Dockerfile-shfmt -q .; then printf 'ERROR!' >&2 exit 1 fi -fix() { - printf 'Fixing %s... ' "$1" - if docker run --rm -v "$SRC_DIR:/src" -w /src docker-php-extension-installer-shfmt:latest shfmt -s -ln posix -i 0 -ci -kp -w "$1"; then - printf 'done.\n' - fi -} - -fix install-php-extensions -fix scripts/common -fix scripts/lint -fix scripts/travisci-test-extensions -fix scripts/travisci-update-readme -fix scripts/update-readme +if ! docker run --rm -v "$SRC_DIR:/src" -w /src docker-php-extension-installer-shfmt:latest ./scripts/invoke-shfmt fix; then + printf 'ERROR!' >&2 + exit 1 +fi diff --git a/scripts/lint.bat b/scripts/lint.bat index 19d9e55..d5fba17 100644 --- a/scripts/lint.bat +++ b/scripts/lint.bat @@ -11,12 +11,8 @@ set SRC_DIR=%CD% docker build -t docker-php-extension-installer-shfmt:latest -f scripts\Dockerfile-shfmt -q . if errorlevel 1 goto :err -call :fix install-php-extensions -call :fix scripts/common -call :fix scripts/lint -call :fix scripts/travisci-test-extensions -call :fix scripts/travisci-update-readme -call :fix scripts/update-readme +docker run --rm -v "%SRC_DIR%:/src" -w /src docker-php-extension-installer-shfmt:latest ./scripts/invoke-shfmt fix +if errorlevel 1 goto :err goto :eof :no-docker @@ -26,9 +22,3 @@ goto :eof :err echo ERROR! >&2 goto :eof - -:fix -echo|set /p="Fixing %1... " -docker run --rm -v "%SRC_DIR%:/src" -w /src docker-php-extension-installer-shfmt:latest shfmt -s -ln posix -i 0 -ci -kp -w %1 -if not errorlevel 1 echo done. -exit /b 0 From 5f6fbb81fc1224b96bb72d0554e133fc69c103a7 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 20 Dec 2019 19:10:33 +0100 Subject: [PATCH 3/3] Use official shfmt docker image --- scripts/Dockerfile-shfmt | 3 --- scripts/lint | 7 +------ scripts/lint.bat | 5 +---- 3 files changed, 2 insertions(+), 13 deletions(-) delete mode 100644 scripts/Dockerfile-shfmt diff --git a/scripts/Dockerfile-shfmt b/scripts/Dockerfile-shfmt deleted file mode 100644 index 5decefa..0000000 --- a/scripts/Dockerfile-shfmt +++ /dev/null @@ -1,3 +0,0 @@ -FROM golang - -RUN go get github.com/mvdan/sh/cmd/shfmt diff --git a/scripts/lint b/scripts/lint index 7e6f7c8..01edbeb 100755 --- a/scripts/lint +++ b/scripts/lint @@ -10,12 +10,7 @@ fi SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. pwd)" -if ! docker build -t docker-php-extension-installer-shfmt:latest -f scripts/Dockerfile-shfmt -q .; then - printf 'ERROR!' >&2 - exit 1 -fi - -if ! docker run --rm -v "$SRC_DIR:/src" -w /src docker-php-extension-installer-shfmt:latest ./scripts/invoke-shfmt fix; then +if ! docker run --rm -v "$SRC_DIR:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix; then printf 'ERROR!' >&2 exit 1 fi diff --git a/scripts/lint.bat b/scripts/lint.bat index d5fba17..80a6386 100644 --- a/scripts/lint.bat +++ b/scripts/lint.bat @@ -8,10 +8,7 @@ cd /d "%~dp0.." if errorlevel 1 goto err set SRC_DIR=%CD% -docker build -t docker-php-extension-installer-shfmt:latest -f scripts\Dockerfile-shfmt -q . -if errorlevel 1 goto :err - -docker run --rm -v "%SRC_DIR%:/src" -w /src docker-php-extension-installer-shfmt:latest ./scripts/invoke-shfmt fix +docker run --rm -v "%SRC_DIR%:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix if errorlevel 1 goto :err goto :eof