apkbuild-fixer 4.13 KB
Newer Older
Leo's avatar
Leo committed
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
#!/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