Commit a122fd82 authored by Leo's avatar Leo

add apkbuild-fixer

parent 23ccedb7
#!/bin/sh
remove_line() {
[ -z "$1" ] || [ -z "$2" ] && return 0
# path to file first, lines as a whitespace delimited string second
for l in $2; do
sed -i "${l}d" "$1"
[ -z "$3" ] && return 0
# While the line we get is not empty, remove it, this serves to
# remove any follow-up lines after we remove the first one.
# We certainly don't want stray empty lines
while ! sed -n "${l}p" "$1" | grep -q '.'; do
sed -i "${l}d" "$1"
done
done
}
remove_return_1() {
[ -z "$1" ] || [ -z "$2" ] && return 0
# path to file first, lines as a whitespace delimited string second
for l in $2; do
prev_line=$(( l - 1 )) # The line before the one we should delete
# Check if the line we were given contains only || return 1
if sed -n "${l}p" "$1" | grep -q '^[[:blank:]]*|| return 1[[:blank:]]*$'; then
# If that is true then delete the line outright, no point in keeping
# an empty line around
sed -i "${l}d" "$1"
# Remove backslash of previous line, this is to deal with cases
# where return is in a line of its own and the command is in the
# previous line, commonly used in autoconf scripts, as an example:
# autoconf \
# --prefix=/usr \
# || return 1
sed -i "${prev_line}s/\\\\$//g" "$1"
else
# Remove the || return 1 from the specific line
sed -i "${l}s/|| return 1//g" "$1"
fi
done
}
# Rename _builddir to builddir, both the variable in the global scope
# and any other occurrences
rename__builddir() {
sed -i 's/_builddir/builddir/g' "$1"
}
# Self-explanatory, remove any whitespace after ending text
remove_trailing_whitespace() {
sed -i 's/[[:space:]]\+$//' "$1"
}
# Given a file $1 and lines $2, rewrite the function which is composed of
# letters and numbers
rewrite_function() {
[ -z "$1" ] || [ -z "$2" ] && return 0
for l in $2; do
sed -ri " {$l s|^([A-Za-z0-9]*).*|\\1() {|g }" "$1"
done
}
for apkbuild; do
if [ -f "$apkbuild" ]; then
# Remove trailing whitespace
remove_trailing_whitespace "$apkbuild"
# Before running apkbuild-lint, perform rename on builddir
grep -q "^_builddir=" "$apkbuild" && rename__builddir "$apkbuild"
# Get all the violations
violations="$(apkbuild-lint "$apkbuild")"
[ -z "$violations" ] && return 0
# Rewrite functions (no chance of deleting line)
rewrite_functions="$(echo "$violations" | \
grep -E '\[AL(10|11)\]'| \
cut -d : -f4 | \
sort -rV | \
paste -sd ' ')"
[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
# Remove || return 1 (might delete lines)
return_1_lines="$(echo "$violations" | \
grep -F '[AL2]' | \
cut -d : -f4 | \
sort -rV | \
paste -sd ' ')"
[ -n "$return_1_lines" ] && remove_return_1 "$apkbuild" "$return_1_lines"
# Get all violations again now that lines were deleted and it can potentially cause lines
# to be misdeleted
violations="$(apkbuild-lint "$apkbuild")"
[ -z "$violations" ] && return 0
# Remove lines with empty variables, greedily (will certainly remove lines)
removable_lines="$(echo "$violations" | \
grep -E '\[AL(5|13)\]'| \
cut -d : -f4 | \
sort -rV | \
paste -sd ' ')"
# Third argument enables greedyness
[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines" greedy
# Get all violations again now that lines were deleted and it can potentially cause lines
# to be misdeleted
violations="$(apkbuild-lint "$apkbuild")"
[ -z "$violations" ] && return 0
# Remove lines with default builddir (will certainly remove lines)
removable_lines="$(echo "$violations" | \
grep '\[AL[1]\]'| \
cut -d : -f4 | \
sort -rV | \
paste -sd ' ')"
[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines"
# Get all violations again now that lines were deleted and it can potentially cause lines
# to be misdeleted
violations="$(apkbuild-lint "$apkbuild")"
[ -z "$violations" ] && return 0
# Rewrite functions
rewrite_functions="$(echo "$violations" | \
grep -E '\[AL(10|11)\]'| \
cut -d : -f4 | \
sort -rV | \
paste -sd ' ')"
[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
fi
done
apkbuild-fixer(1)
# NAME
apkbuild-fixer - Fix violations found by apkbuild-lint
# SYNOPSIS
*apkbuild-fixer* <apkbuild...>
# DESCRIPTION
apkbuild-fixer fixes linting violations found by *apkbuild-lint(1)* by modifying
the APKBUILD in-place. It calls *apkbuild-lint* after each fix to a particular
violation done.
Note that not all violations have automatic fixes and while care is
taken to avoid breaking the APKBUILD it might happen in some edge cases.
# FIXES
The following violations can be fixed automatically, but manual review is recommended:
- AL10 (space-before-function-parenthesis)
- Fixed by rewriting the function name to match the norm.
- AL11 (space-after-function-parenthesis)
- Fixed by rewriting the function name to match the norm.
- AL2 (unnecessary-return-1)
- Removes the *|| return 1* from the line
- Removes the line if only the *|| return 1* is present
- Checks previous line for *\\* and removes it.
- AL5 (empty-variable)
- Remove the empty variable
- AL13 (superfluous-cd-builddir)
- Remove the *cd "$builddir"* and any following empty lines
- AL1 (default-builddir-value)
- Remove the *builddir* variable
apkbuild-fixer will also rename *\_builddir* to *builddir* as it is very common from
before the *builddir* variable was a thing.
# AUTHORS
Maintained by Leo <thinkabit.ukim@gmail.com>
# SEE ALSO
*alint(5)* *apkbuild-lint(1)*
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment