Restructure

This commit is contained in:
Arnie 2017-12-24 17:35:49 +01:00
parent 9d42de5bb9
commit d9163bcff8
7 changed files with 307 additions and 50 deletions

View File

@ -22,10 +22,17 @@ RUN yarn add prettier csscomb --global && \
ln -s /node_modules/.bin/prettier /usr/local/bin/prettier && \
ln -s /node_modules/.bin/csscomb /usr/local/bin/csscomb
COPY ["format.sh", "/"]
COPY ["src/entry.sh", "/entry.sh"]
COPY ["src/inc", "/inc"]
WORKDIR /code
CMD ["/format.sh"]
CMD ["/entry.sh"]
ENTRYPOINT ["/format.sh"]
ENTRYPOINT ["/entry.sh"]
ARG VERSION
ARG COMMIT_SHA
ENV VERSION=$VERSION
ENV COMMIT_SHA=$COMMIT_SHA

View File

@ -1,14 +1,29 @@
#
# Settings
#
all: build publish
# 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)
build:
docker build -t yoursystemcz/code-formatter:latest .
VERSION := $(or $(GIT_TAG),latest)
publish:
docker push yoursystemcz/code-formatter:latest
exec:
docker run --rm -it --entrypoint bash yoursystemcz/code-formatter: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)

View File

@ -1,40 +0,0 @@
#!/usr/bin/env bash
if [[ $@ = "" ]]; then
echo "No files provided"
exit 137
fi
echo "Running code formatting..."
# Agregate CSS and JS files into one command
CSS_FILES=()
JS_FILES=()
# Prepare files for processing
for file in "$@"; do
if [[ ! -f ${file} ]]; then
echo "Could not find the file ${file}"
continue
fi
if [[ ${file} == *.js ]] || [[ ${file} == *.jsx ]]; then
JS_FILES+=("${file}")
elif [[ ${file} == *.css ]] || [[ ${file} == *.scss ]] || [[ ${file} == *.sass ]] || [[ ${file} == *.less ]]; then
CSS_FILES+=("${file}")
elif [[ ${file} == *.php ]] || [[ ${file} == *.phtml ]]; then
# Cannot chain php files without specifying a config CS file :(
php-cs-fixer fix "${file}"
fi
done
# Run css comb
if [[ ${#CSS_FILES[@]} -ne 0 ]]; then
csscomb "${CSS_FILES[@]}"
fi
# Run JS prettier
if [[ ${#JS_FILES[@]} -ne 0 ]]; then
prettier --write "${JS_FILES[@]}"
fi

67
src/entry.sh Executable file
View File

@ -0,0 +1,67 @@
#!/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 $?
;;
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 $?

103
src/inc/functions.shinc Executable file
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
}

54
src/inc/process.shinc Normal file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env bash
__csscomb() {
if [[ ${DRY_RUN} -eq 0 ]]; then
csscomb "$@"
else
csscomb --lint "$@"
fi
}
__prettier() {
if [[ ${DRY_RUN} -eq 0 ]]; then
prettier --write "$@"
else
prettier --list-different "$@"
fi
}
__phpFixer() {
# 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}"
done
else
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix --dry-run "${file}"
done
fi
}
process() {
# Run css comb
if [[ ${#CSS_FILES[@]} -ne 0 ]]; then
__csscomb "${CSS_FILES[@]}"
fi
# Run JS prettier
if [[ ${#JS_FILES[@]} -ne 0 ]]; then
__prettier --write "${JS_FILES[@]}"
fi
# Run PHP cs fixer
if [[ ${#PHP_FILES[@]} -ne 0 ]]; then
__phpFixer ${PHP_FILES[@]}
fi
}

51
src/inc/variables.shinc Normal file
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}"
continue
fi
CSS_FILES+=(${1})
;;
*.js|*.jsx)
__path_exists "${1}"
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
continue
fi
JS_FILES+=(${1})
;;
*.php|*.phtml)
__path_exists "${1}"
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
continue
fi
PHP_FILES+=(${1})
;;
esac
shift 1
done
}