-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotfiles-lint
executable file
·130 lines (108 loc) · 3.78 KB
/
dotfiles-lint
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
# MIT License
#
# Copyright (c) 2023 John Karahalis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Use "strict mode."
#
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
COMMAND_NAME=$(basename "$0")
eval "$(docopts --help=- -A ARGS : "$@" <<EOF
Lint tracked dotfiles.
Usage:
$COMMAND_NAME
$COMMAND_NAME -h | --help
Options:
-h, --help
Show this documentation
EOF
)"
# cd to $HOME so that "vcsh list-tracked" lists all files, not just tracked
# files in the current directory and its subdirectories.
cd "$HOME" || exit 1
# Use vcsh to list all tracked dotfiles and use mapfile to convert the lines of
# output into an array.
#
# https://stackoverflow.com/a/13825568/715866
mapfile -t ALL_DOTFILES < <(vcsh list-tracked)
# Create an array of all dotfiles which are actually present on the filesystem.
# Some might be managed by vcsh but not present because they are ignored via
# sparse-checkout.
ALL_PRESENT_DOTFILES=()
for DOTFILE in "${ALL_DOTFILES[@]}"; do
if [[ -f $DOTFILE ]]; then
ALL_PRESENT_DOTFILES+=("$DOTFILE")
fi
done
function is-shellcheck-file() {
local FILENAME=$1
local FIRST_LINE
FIRST_LINE=$(head --lines 1 "$FILENAME")
[[ $FILENAME =~ ^\.profile ]] ||
[[ $FILENAME =~ ^\.bash ]] ||
[[ $FILENAME =~ \.sh$ ]] ||
[[ $FILENAME =~ \.bash$ ]] ||
[[ $FIRST_LINE == '#!/usr/bin/env sh' ]] ||
[[ $FIRST_LINE == '#!/usr/bin/env bash' ]]
}
function is-vint-file() {
local FILENAME=$1
local BASENAME
BASENAME=$(basename "$FILENAME")
[[ $FILENAME =~ \.vim$ ]] ||
[[ $BASENAME == ideavimrc ]]
}
function run-shellcheck() {
local SHELLCHECK_FILES=()
for DOTFILE in "${ALL_PRESENT_DOTFILES[@]}"; do
if is-shellcheck-file "$DOTFILE"; then
SHELLCHECK_FILES+=("$DOTFILE")
fi
done
# If the --external-sources flag is not provided, ShellCheck will not follow
# even literal paths, like that paths to Bash completions in .bashrc, and will
# complain about error SC1091.
#
# I'd rather enable --external-sources in shellcheckrc, but at the time of
# this writing, doing so is not possible.
#
# https://github.com/koalaman/shellcheck/issues/1818
shellcheck --external-sources "${SHELLCHECK_FILES[@]}"
}
function run-vint() {
local VINT_FILES=()
for DOTFILE in "${ALL_PRESENT_DOTFILES[@]}"; do
if is-vint-file "$DOTFILE"; then
VINT_FILES+=("$DOTFILE")
fi
done
# The "2>&1" (redirecting stderr to stdout in xargs output) and the
# inverse-grep are used to work around the following bug:
#
# https://github.com/Vimjas/vint/issues/329#issuecomment-1029628054
#
# Disable SC2016 because we don't _indent_ to expand `line`.
#
# shellcheck disable=SC2016
vint "${VINT_FILES[@]}" 2>&1 | grep --invert-match 'vint WARNING: Policy `line` is not defined'
}
run-shellcheck
run-vint