Add code formatter

This commit is contained in:
Arnie 2018-01-09 17:45:46 +01:00
parent 4c8162b17b
commit dec286b858
9 changed files with 427 additions and 0 deletions

41
code-formatter/Dockerfile Normal file
View File

@ -0,0 +1,41 @@
FROM node:8-slim
# https support
RUN apt-get update && \
apt-get install -y apt-transport-https ca-certificates
# add yarn and php
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && \
apt-get install -y yarn php5-cli && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/* /tmp/* /var/tmp/*
# add php fixer
RUN curl -L http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o /usr/local/bin/php-cs-fixer && \
chmod a+x /usr/local/bin/php-cs-fixer
WORKDIR /app
# add csscomb and prettier
RUN yarn add prettier csscomb --global && \
ln -s /app/node_modules/.bin/prettier /usr/local/bin/prettier && \
ln -s /app/node_modules/.bin/csscomb /usr/local/bin/csscomb
COPY ["src/entry.sh", "/app/entry.sh"]
COPY ["src/inc", "/app/inc"]
WORKDIR /code
CMD ["/app/entry.sh"]
ENTRYPOINT ["/app/entry.sh"]
ARG VERSION
ARG COMMIT_SHA
ENV VERSION=$VERSION
ENV COMMIT_SHA=$COMMIT_SHA

29
code-formatter/Makefile Normal file
View File

@ -0,0 +1,29 @@
#
# Settings
#
# Name of the docker image
APP_NAME := code-formatter
# Select the docker registry to use for the image
DOCKER_REGISTRY := yoursystemcz
# Describe current branch
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
COMMIT = $(shell git rev-parse HEAD)
GIT_TAG = $(shell git describe --tags --exact-match 2>/dev/null)
VERSION := $(or $(GIT_TAG),latest)
all : build release
.PHONY : all
build :
docker build --build-arg VERSION=$(VERSION) --build-arg COMMIT_SHA=$(COMMIT) -t $(DOCKER_REGISTRY)/$(APP_NAME):$(VERSION) .
release : build
docker push $(DOCKER_REGISTRY)/$(APP_NAME):$(VERSION)

85
code-formatter/src/entry.sh Executable file
View File

@ -0,0 +1,85 @@
#!/usr/bin/env bash
SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P )
INCLUDES_PATH="${SCRIPT_PATH}/inc"
source ${INCLUDES_PATH}/functions.shinc
source ${INCLUDES_PATH}/variables.shinc
usage() {
# Todo
__success "Options"
__msg "help, --help, -h" 1
__indent 2
echo "Print this help"
echo
}
main() {
trap shutdown SIGTERM SIGINT
local _version="unknown"
if [[ -z ${VERSION+x} ]] || [[ ${VERSION} = "" ]]; then
if [[ -z ${COMMIT_SHA+x} ]] || [[ ${COMMIT_SHA} = "" ]]; then
_version="${COMMIT_SHA}"
fi
elif [[ ${VERSION} = "latest" ]]; then
_version="${VERSION} - ${COMMIT_SHA}"
else
_version="${VERSION}"
fi
__header "Code-Formatter [${_version}]"
local _cmd=${1}
shift
case "${_cmd}" in
process)
__initVariables "$@"
source ${INCLUDES_PATH}/process.shinc
process
return $?
;;
prettier)
__initVariables "$@"
source ${INCLUDES_PATH}/formatters/prettier.shinc
__prettier
return $?
;;
csscomb)
__initVariables "$@"
source ${INCLUDES_PATH}/formatters/csscomb.shinc
__csscomb
return $?
;;
php-cs-fixer)
__initVariables "$@"
source ${INCLUDES_PATH}/formatters/php-cs-fixer.shinc
__phpFixer
return $?
;;
help|--help|-h)
usage
return 0
;;
*)
if [[ ${1} = "" ]]; then
__warn "You need to provide a command"
return 1
else
__err "Invalid command: $1"
fi
return 137
;;
esac
}
main "$@"
exit $?

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
__csscomb() {
__msg "Csscomb:"
if [[ ${DRY_RUN} -eq 0 ]]; then
csscomb -v "${CSS_FILES[@]}"
else
csscomb --lint -v "${CSS_FILES[@]}"
fi
return $?
}

View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
__phpFixer() {
local failed=0
__msg "PHP-cs-fixer"
# Cannot chain php files without specifying a config CS file :(
if [[ ${DRY_RUN} -eq 0 ]]; then
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix "${file}"
[[ $? -ne 0 ]] && failed=1
done
else
local needs_fixing=()
local invalid_syntax=()
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix --dry-run "${file}"
case $? in
0)
#all good
;;
4)
invalid_syntax+=("${file}")
;;
8)
needs_fixing+=("${file}")
;;
*)
__err "There was an error with php-cs-fixer configuration"
failed=1
;;
esac
done
if [[ ${#needs_fixing[@]} -gt 0 ]]; then
failed=1
__err "Needs fixing:" 1
for file in "${needs_fixing[@]}"; do
__msg "${file}" 2
done
fi
if [[ ${#invalid_syntax[@]} -gt 0 ]]; then
failed=1
__err "Invalid syntax:" 1
for file in "${invalid_syntax[@]}"; do
__msg "${file}" 2
done
fi
fi
return ${failed}
}

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
__prettier() {
__msg "Prettier:"
if [[ ${DRY_RUN} -eq 0 ]]; then
prettier --write "${JS_FILES[@]}"
else
__msg "Listing (: unprettiered :) files:" 1
prettier --list-different "${JS_FILES[@]}"
fi
return $?
}

View File

@ -0,0 +1,103 @@
#!/usr/bin/env bash
#
# Logging functions
#
# Colors
__color_green() {
printf '\033[1;31;32m'
printf -- "%b" "$*"
printf '\033[0m'
}
__color_red() {
printf '\033[1;31m'
printf -- "%b" "$*"
printf '\033[0m'
}
__color_red_bg() {
printf '\033[1;41m'
printf -- "%b" "$*"
printf '\033[0m'
}
__color_white() {
printf '\033[1;37;40m'
printf -- "%b" "$*"
printf '\033[0m'
}
__color_yellow() {
printf '\033[1;31;33m'
printf -- "%b" "$*"
printf '\033[0m'
}
# Indent by 2 x {n} spaces
__indent() {
if [[ ${1} != "" ]] && [[ ${1} -ne 0 ]]; then
printf %$(expr ${1} \* 4)s
fi
}
# Output functions
__header() {
echo
__success "$*"
echo
}
__footer() {
echo $(printf %20s | tr " " "-")
echo
__indent 1
echo "${1}"
}
__msg() {
__indent $2
__color_white "$1"
echo
}
__success() {
__indent $2
__color_green "$1"
echo
}
__warn() {
__indent $2
__color_yellow "$1"
echo
}
__err() {
__indent $2
__color_red "$1"
echo
}
#
# Helper functions
#
# Termination signal trap
shutdown() {
echo
__warn "Received termination signal, shutting down"
exit 0
}
__path_exists() {
if [[ -z ${1+x} ]] || [[ "${1}" = "" ]]; then
__err "Invalid call to __path_exists: No path supplied"
exit 137
fi
[[ -f ${1} ]] || [[ -d ${1} ]] && return 0
return 1
}

View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
source ${INCLUDES_PATH}/formatters/prettier.shinc
source ${INCLUDES_PATH}/formatters/csscomb.shinc
source ${INCLUDES_PATH}/formatters/php-cs-fixer.shinc
process() {
local failed=0
# Run css comb
if [[ ${#CSS_FILES[@]} -ne 0 ]]; then
__csscomb
[[ $? -ne 0 ]] && failed=1
fi
# Run JS prettier
if [[ ${#JS_FILES[@]} -ne 0 ]]; then
__prettier
[[ $? -ne 0 ]] && failed=1
fi
# Run PHP cs fixer
if [[ ${#PHP_FILES[@]} -ne 0 ]]; then
__phpFixer
[[ $? -ne 0 ]] && failed=1
fi
return ${failed}
}

View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
# List of supplied CSS files for csscomb
CSS_FILES=()
# List of supplied JS files for prettier
JS_FILES=()
# List of supplied php files for php-cs-fixer
PHP_FILES=()
DRY_RUN=0
__initVariables() {
# Loop over parameters and set the variables
while [[ ${#} -gt 0 ]]; do
case "${1}" in
--dry-run)
DRY_RUN=1
;;
*.css|*.scss|*.sass|*.less)
__path_exists "${1}"
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
CSS_FILES+=("${1}")
fi
;;
*.js|*.jsx)
__path_exists "${1}"
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
JS_FILES+=("${1}")
fi
;;
*.php|*.phtml)
__path_exists "${1}"
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
PHP_FILES+=("${1}")
fi
;;
esac
shift 1
done
}