From dec286b858a53803364eac00a7afff04d64cdbeb Mon Sep 17 00:00:00 2001 From: Arnie Date: Tue, 9 Jan 2018 17:45:46 +0100 Subject: [PATCH] Add code formatter --- code-formatter/Dockerfile | 41 +++++++ code-formatter/Makefile | 29 +++++ code-formatter/src/entry.sh | 85 +++++++++++++++ .../src/inc/formatters/csscomb.shinc | 14 +++ .../src/inc/formatters/php-cs-fixer.shinc | 58 ++++++++++ .../src/inc/formatters/prettier.shinc | 15 +++ code-formatter/src/inc/functions.shinc | 103 ++++++++++++++++++ code-formatter/src/inc/process.shinc | 31 ++++++ code-formatter/src/inc/variables.shinc | 51 +++++++++ 9 files changed, 427 insertions(+) create mode 100644 code-formatter/Dockerfile create mode 100644 code-formatter/Makefile create mode 100755 code-formatter/src/entry.sh create mode 100644 code-formatter/src/inc/formatters/csscomb.shinc create mode 100644 code-formatter/src/inc/formatters/php-cs-fixer.shinc create mode 100644 code-formatter/src/inc/formatters/prettier.shinc create mode 100755 code-formatter/src/inc/functions.shinc create mode 100644 code-formatter/src/inc/process.shinc create mode 100644 code-formatter/src/inc/variables.shinc diff --git a/code-formatter/Dockerfile b/code-formatter/Dockerfile new file mode 100644 index 0000000..c3b6aa4 --- /dev/null +++ b/code-formatter/Dockerfile @@ -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 diff --git a/code-formatter/Makefile b/code-formatter/Makefile new file mode 100644 index 0000000..6dce26b --- /dev/null +++ b/code-formatter/Makefile @@ -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) diff --git a/code-formatter/src/entry.sh b/code-formatter/src/entry.sh new file mode 100755 index 0000000..b0e41e7 --- /dev/null +++ b/code-formatter/src/entry.sh @@ -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 $? diff --git a/code-formatter/src/inc/formatters/csscomb.shinc b/code-formatter/src/inc/formatters/csscomb.shinc new file mode 100644 index 0000000..a26f474 --- /dev/null +++ b/code-formatter/src/inc/formatters/csscomb.shinc @@ -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 $? +} diff --git a/code-formatter/src/inc/formatters/php-cs-fixer.shinc b/code-formatter/src/inc/formatters/php-cs-fixer.shinc new file mode 100644 index 0000000..8f0d500 --- /dev/null +++ b/code-formatter/src/inc/formatters/php-cs-fixer.shinc @@ -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} +} diff --git a/code-formatter/src/inc/formatters/prettier.shinc b/code-formatter/src/inc/formatters/prettier.shinc new file mode 100644 index 0000000..4e2acb3 --- /dev/null +++ b/code-formatter/src/inc/formatters/prettier.shinc @@ -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 $? +} diff --git a/code-formatter/src/inc/functions.shinc b/code-formatter/src/inc/functions.shinc new file mode 100755 index 0000000..8ef325c --- /dev/null +++ b/code-formatter/src/inc/functions.shinc @@ -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 +} diff --git a/code-formatter/src/inc/process.shinc b/code-formatter/src/inc/process.shinc new file mode 100644 index 0000000..8513a08 --- /dev/null +++ b/code-formatter/src/inc/process.shinc @@ -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} +} diff --git a/code-formatter/src/inc/variables.shinc b/code-formatter/src/inc/variables.shinc new file mode 100644 index 0000000..dfb2773 --- /dev/null +++ b/code-formatter/src/inc/variables.shinc @@ -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 +}