Add code formatter
This commit is contained in:
Executable
+85
@@ -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 $?
|
||||
@@ -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 $?
|
||||
}
|
||||
@@ -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}
|
||||
}
|
||||
@@ -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 $?
|
||||
}
|
||||
Executable
+103
@@ -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
|
||||
}
|
||||
@@ -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}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user