Alpine Linux coding style
Thank you for taking interest in contributing to our aports repository. As consistency and readability are so important for the quality of our APKBUILD and thus the quality of Alpine Linux, we kindly ask to follow these recommendations.
Language
Alpine Linux APKBUILD files are inherently just POSIX shell scripts. Please avoid extensions, even if they work or are accepted by busybox ash. (using keyword local is an exception)
Naming convention
Use snake_case. Functions, variables etc. should be lower-cased with underscores to separate words.
Local ‘private’ variables and functions in global scope should be prefixed with a single underscore to avoid name collisions with internal variables in abuild.
Double underscores are reserved and should not be used.
_my_variable="data"
Bracing
Curly braces for functions should be on the same line.
Markers to indicate a compound statement should be on the same line.
if …; then
if [; then
fi
while …; do
while ; do
done
for …; do
for; do
done
Spacing
All keywords and operators are separated by a space.
For cleanliness sake, ensure there is no trailing whitespace.
Identation
Indentation is one tab character per level, alignment is done using spaces. For example (using the >—–– characters to indicate a tab):
This ensures code is always neatly aligned and properly indented.
Space before tab should be avoided.
Line length
A line should not be longer than 80 characters. While this is not a hard limit, it is strongly recommended to avoid having longer lines, as long lines reduce readability and invite deep nesting.
Variables
Quoting
Quote strings containing variables, command substitutions, spaces or shell meta characters, unless careful unquoted expansion is required.
Don’t quote literal integers.
Bracing
Only use curly braces around variables when needed.
foo="_bar"
foo_bar=""
Subshell usage
Use $() syntax, not backticks.
Sorting
Some items tend to benefit from being sorted. A list of sources, dependencies etc. Always try to find a reasonable sort order, where alphabetically tends to be the most useful one.
Eval
eval is evil and should be avoided.
| 1 | |
| 2 | |
| 3 | |
| 4 | As consistency and readability are so important for the quality of our APKBUILD |
| 5 | and thus the quality of Alpine Linux, we kindly ask to follow these |
| 6 | recommendations. |
| 7 | |
| 8 | |
| 9 | |
| 10 | extensions, even if they work or are accepted by busybox ash. (using keyword |
| 11 | `local` is an exception) |
| 12 | |
| 13 | |
| 14 | |
| 15 | underscores to separate words. |
| 16 | |
| 17 | |
| 18 | with a single underscore to avoid name collisions with internal variables in |
| 19 | `abuild`. |
| 20 | |
| 21 | |
| 22 | ```sh |
| 23 | _my_variable="data" |
| 24 | ``` |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | prepare() { |
| 31 | ... |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | if [ true ]; then |
| 41 | echo "It is so" |
| 42 | fi |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | |
| 47 | |
| 48 | while ...; do |
| 49 | ... |
| 50 | done |
| 51 | ``` |
| 52 | |
| 53 | |
| 54 | |
| 55 | for x in foo bar baz; do |
| 56 | ... |
| 57 | done |
| 58 | ``` |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | For example (using the >------- characters to indicate a tab): |
| 68 | ```sh |
| 69 | build() |
| 70 | { |
| 71 | >-------make DESTDIR="${pkgdir}" \ |
| 72 | >------- PREFIX="/usr" |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | is strongly recommended to avoid having longer lines, as long lines reduce |
| 83 | readability and invite deep nesting. |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | characters, unless careful unquoted expansion is required. |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | foo="${foo}_bar" |
| 98 | foo_bar="$foo" |
| 99 | ``` |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | etc. Always try to find a reasonable sort order, where alphabetically tends to |
| 107 | be the most useful one. |
| 108 | |
| 109 | |
| 110 | |
| 111 |