-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfix
More file actions
executable file
·99 lines (83 loc) · 2.93 KB
/
fix
File metadata and controls
executable file
·99 lines (83 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $SCRIPT_DIR/..
# docker configured and not inside docker
CONSOLE_PREFIX=""
if test ! -f /.dockerenv; then
CONSOLE_PREFIX="./script/console"
fi
# colors and fonts, only loaded on interactive terminals
if [ -t 0 ]; then
T_ERROR=$(tput setaf 7; tput setab 1)
T_INFO=$(tput setaf 7; tput setab 4)
T_WARNING=$(tput setaf 0; tput setab 3)
T_SUCCESS=$(tput setaf 0; tput setab 2)
T_BOLD=$(tput bold)
T_RESET=$(tput sgr0)
fi
if test "$1" = "--help"; then
echo "${T_INFO}./script/fix [<type>] [--everything] [extra arguments]${T_RESET}"
echo 'Lets linters fix code, defaults to phpcbf (phpcs) and rector on the *git-diff* between the base and the head branch'
echo ''
echo "${T_BOLD}<type>${T_RESET}"
echo 'Optional, if set it runs on the *changed files* (not only git-diff) between the base and the head branch'
echo '- phpcbf (or phpcs)'
echo '- rector'
echo ''
echo "${T_BOLD}--everything${T_RESET}"
echo 'Optional, if set it runs on the *whole codebase* instead of the diff/changed-files (ignored when no type is given)'
echo ''
echo "${T_BOLD}extra arguments${T_RESET}"
echo 'Optional, passed to the linter of the specified type (ignored when no type is given)'
echo ''
echo "${T_INFO}./script/fix --help${T_RESET}"
echo 'Shows this help'
exit 0
fi
# check arguments
test "$2" = "--everything" && HAS_EVERYTHING=1 || HAS_EVERYTHING=0
test "$#" -gt 0 && HAS_TYPE=1 || HAS_TYPE=0
# store type and its extra arguments
TYPE=""
EXTRA_ARGUMENTS=""
if test "$HAS_TYPE" = 1; then
TYPE="$1"
EXTRA_ARGUMENTS="${@:2}"
if test "$HAS_EVERYTHING" = 1; then
EXTRA_ARGUMENTS="${@:3}"
fi
if test -n "$EXTRA_ARGUMENTS"; then
echo "${T_INFO}Running $TYPE with extra arguments: $EXTRA_ARGUMENTS${T_RESET}"
fi
fi
# gather files to test
GIT_MERGE_BASE=$(git merge-base main @)
if test "$HAS_EVERYTHING" = 1; then
FILE_FILTER=""
else
# get names of files changed in this branch
DIFF_FILE_NAMES=`git diff $GIT_MERGE_BASE --name-only --diff-filter=d`
if test -z "$DIFF_FILE_NAMES"; then
echo "${T_WARNING}No files changed${T_RESET}"
exit 0
fi
# convert filenames with newlines to filenames with spaces
FILE_FILTER="${DIFF_FILE_NAMES//$'\n'/ }"
fi
if test "$HAS_TYPE" = 1; then
if test "$TYPE" = "phpcbf" || test "$TYPE" = "phpcs"; then
$CONSOLE_PREFIX ./vendor/bin/phpcbf $EXTRA_ARGUMENTS $FILE_FILTER
elif test "$TYPE" = "rector"; then
$CONSOLE_PREFIX ./vendor/bin/rector process $EXTRA_ARGUMENTS $FILE_FILTER
else
echo "${T_ERROR}Unknown type specified: $TYPE${T_RESET}"
exit 1
fi
else
echo -e "${T_BOLD}Running fixes on the changed files (not only git-diff) between the base and the head branch${T_RESET}\n"
echo "${T_INFO}Fixing refactors (rector)${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/rector process $FILE_FILTER || true
echo "${T_INFO}Fixing coding standards (phpcs)${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/phpcbf $FILE_FILTER || true
fi