-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinter.sh
More file actions
executable file
·161 lines (144 loc) · 3.57 KB
/
Copy pathlinter.sh
File metadata and controls
executable file
·161 lines (144 loc) · 3.57 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
set -euo pipefail
export PATH="${HOME}/.local/bin:${PATH}"
APT_UPDATED=0
PYTHON_BIN=""
log() {
printf '%s\n' "$1"
}
log_warn() {
printf 'Warning: %s\n' "$1" >&2
}
die() {
printf 'Error: %s\n' "$1" >&2
exit 1
}
apt_install() {
local package="$1"
local -a cmd_prefix=()
if ! command -v apt-get >/dev/null 2>&1; then
die "apt-get not available to install ${package}"
fi
if ((EUID != 0)); then
if ! command -v sudo >/dev/null 2>&1; then
die "sudo required to run apt-get as non-root to install ${package}"
fi
cmd_prefix=(sudo)
fi
if ((APT_UPDATED == 0)); then
log "Running apt-get update..."
if ! "${cmd_prefix[@]}" apt-get update; then
die "apt-get update failed"
fi
APT_UPDATED=1
fi
log "Installing ${package} via apt-get..."
if ! "${cmd_prefix[@]}" apt-get install -y "${package}"; then
die "Failed to install ${package} via apt-get"
fi
}
ensure_shellcheck() {
if command -v shellcheck >/dev/null 2>&1; then
return
fi
log "Installing shellcheck..."
if command -v apt-get >/dev/null 2>&1; then
apt_install shellcheck
elif command -v brew >/dev/null 2>&1; then
brew install shellcheck
else
die "Unable to install shellcheck (supported: apt-get, brew)"
fi
}
ensure_shfmt() {
if command -v shfmt >/dev/null 2>&1; then
return
fi
log "Installing shfmt..."
if command -v apt-get >/dev/null 2>&1; then
apt_install shfmt
elif command -v brew >/dev/null 2>&1; then
brew install shfmt
elif command -v go >/dev/null 2>&1; then
local gobin="${GOBIN:-${HOME}/go/bin}"
mkdir -p "${gobin}"
if ! GO111MODULE=on GOBIN="${gobin}" go install mvdan.cc/sh/v3/cmd/shfmt@latest; then
die "go install for shfmt failed"
fi
export PATH="${gobin}:${PATH}"
else
die "Unable to install shfmt (supported: apt-get, brew, go)"
fi
command -v shfmt >/dev/null 2>&1 || die "shfmt installation failed"
}
detect_python() {
if command -v python3 >/dev/null 2>&1; then
PYTHON_BIN="$(command -v python3)"
elif command -v python >/dev/null 2>&1; then
PYTHON_BIN="$(command -v python)"
else
die "Python interpreter not found (python3 or python)"
fi
log "Using Python interpreter: ${PYTHON_BIN}"
}
ensure_pip() {
if "${PYTHON_BIN}" -m pip --version >/dev/null 2>&1; then
return
fi
log "Bootstrapping pip..."
"${PYTHON_BIN}" -m ensurepip --upgrade >/dev/null 2>&1 || die "Failed to bootstrap pip"
}
ensure_python_tools() {
local -a missing=() # Use an array for missing packages
if ! "${PYTHON_BIN}" -m ruff --version >/dev/null 2>&1; then
missing+=("ruff")
fi
if ! "${PYTHON_BIN}" -m mypy --version >/dev/null 2>&1; then
missing+=("mypy")
fi
if ((${#missing[@]} > 0)); then
ensure_pip
log "Installing Python tools: ${missing[*]}"
"${PYTHON_BIN}" -m pip install --upgrade --user "${missing[@]}"
fi
}
ensure_shellcheck
ensure_shfmt
detect_python
ensure_python_tools
SHFMT_FILES=(
install
linter.sh
templates/install_and_run
templates/launch.sh
templates/runnable.sh
)
SHELLCHECK_FILES=(
install
fdevc.sh
linter.sh
templates/install_and_run
templates/launch.sh
templates/runnable.sh
)
log "Shell Formatting..."
for script in "${SHFMT_FILES[@]}"; do
if [[ ! -f "${script}" ]]; then
log_warn "File not found, skipping shfmt: ${script}"
continue
fi
shfmt -w "${script}"
done
log "Shell Linting..."
for script in "${SHELLCHECK_FILES[@]}"; do
if [[ ! -f "${script}" ]]; then
log_warn "File not found, skipping shellcheck: ${script}"
continue
fi
shellcheck "${script}"
done
log "Python Formatting..."
"${PYTHON_BIN}" -m ruff format .
log "Python Linting..."
"${PYTHON_BIN}" -m ruff check . --fix
"${PYTHON_BIN}" -m mypy . --strict