aboutsummaryrefslogtreecommitdiff
path: root/CODINGSTYLE.md
diff options
context:
space:
mode:
authorSören Tempel <soeren+git@soeren-tempel.net>2019-07-24 17:33:17 +0200
committerLeo <thinkabit.ukim@gmail.com>2020-02-29 08:21:27 -0300
commit1c9f04e772b35f325b4fd1cd66a4c5749307416e (patch)
treea1d045d45f29bdc9b552d2d07cee218917f7753c /CODINGSTYLE.md
parent1997bc1e8bfc21a7d65ee920d46de091c64be8c6 (diff)
downloadalpine_aports-1c9f04e772b35f325b4fd1cd66a4c5749307416e.tar.bz2
alpine_aports-1c9f04e772b35f325b4fd1cd66a4c5749307416e.tar.xz
alpine_aports-1c9f04e772b35f325b4fd1cd66a4c5749307416e.zip
CODINGSTYLE.md: Expand and rewrite largely
These changes were originally proposed as an alternative to the previous codingstyle document. Some Alpine contributors preferred the document version proposed by myself. As such this change overwrites the previous version of the codingstyle document.
Diffstat (limited to 'CODINGSTYLE.md')
-rw-r--r--CODINGSTYLE.md268
1 files changed, 173 insertions, 95 deletions
diff --git a/CODINGSTYLE.md b/CODINGSTYLE.md
index 74dfcd154e..7f24ca43fa 100644
--- a/CODINGSTYLE.md
+++ b/CODINGSTYLE.md
@@ -1,111 +1,189 @@
1# Alpine Linux coding style 1# Policy for APKBUILDs
2
3Thank you for taking interest in contributing to our aports repository.
4As consistency and readability are so important for the quality of our APKBUILD
5and thus the quality of Alpine Linux, we kindly ask to follow these
6recommendations.
7
8## Language
9Alpine Linux APKBUILD files are inherently just POSIX shell scripts. Please avoid
10extensions, even if they work or are accepted by busybox ash. (using keyword
11`local` is an exception)
12
13## Naming convention
14Use snake_case. Functions, variables etc. should be lower-cased with
15underscores to separate words.
16
17Local 'private' variables and functions in global scope should be prefixed
18with a single underscore to avoid name collisions with internal variables in
19`abuild`.
20
21Double underscores are reserved and should not be used.
22```sh
23_my_variable="data"
24```
25
26### Bracing
27Curly braces for functions should be on the same line.
28
29```sh
30prepare() {
31 ...
32}
33```
34
35Markers to indicate a compound statement should be on the same line.
36
37
38#### if ...; then
39```sh
40 if [ true ]; then
41 echo "It is so"
42 fi
43}
44```
45
46#### while ...; do
47```sh
48 while ...; do
49 ...
50 done
51```
52
53#### for ...; do
54```sh
55 for x in foo bar baz; do
56 ...
57 done
58```
59 2
60### Spacing 3This documents defines a policy for writing APKBUILDs.
61All keywords and operators are separated by a space. 4
5# Standard selection
6
7APKBUILDs are POSIX shell scripts as defined in [POSIX.1-2017 Volume 3]
8[POSIX.1-2017 volume 3]. Additionally, the following extensions are
9supported:
10
111. The `local` keyword for introducing variables local to a function is
12 supported, it is briefly documented in the [bash manual][bash functions].
132. Non-POSIX [parameter extensions][POSIX.1-2017 parameter expansion]
14 are supported. This includes: Substring expansions (e.g.
15 `${var:offset:length}`) and Replacement expansions (e.g.
16 `${var/pattern/string}`). The [bash manual][bash expansion]
17 contains further information on these two expansions.
18
19**NOTE:** `busybox ash` is currently used to evaluate APKBUILDs since it
20supports additional POSIX shell extensions your APKBUILD might be
21evaluated correctly even if it is not confirming to this policy
22document.
23
24# Shell Style Considerations
25
26<!--
27This should be in conformance with most existing APKBUILDs
28Structure and content inspired by Google Shell Style Guide.
29-->
30
31## Formatting
32
33### Indention
34
35Indent with tabs, don't use spaces. Avoid whitespaces.
36
37### Line Length
38
39Maximum line length is 80 characters, this should be considered a
40recommendation and not a string requirement which must be followed at
41all costs. Most notably, automatically generated parts (e.g. by `abuild
42checksum`) are except from this rule.
43
44### Compound Statements
62 45
63For cleanliness sake, ensure there is no trailing whitespace. 46Put `; do` and `; then` on the same line as the `while`, `for` or `if`.
64 47
65## Identation 48### Function Declarations
66Indentation is one tab character per level, alignment is done using spaces.
67For example (using the >------- characters to indicate a tab):
68```sh
69build()
70{
71>-------make DESTDIR="${pkgdir}" \
72>------- PREFIX="/usr"
73}
74```
75 49
76This ensures code is always neatly aligned and properly indented. 50* Always put the function name, the parenthesis and the curly brackets
51 on the same line.
52* Don't use spacing between function name and parenthesis.
53* Do use spacing between function parenthesis and curly brackets.
77 54
78Space before tab should be avoided. 55### Case statement
79 56
80## Line length 57* Don't indent alternatives.
81A line should not be longer than 80 characters. While this is not a hard limit, it 58* A one-line alternative needs a space after the close parenthesis of the pattern and before the `;;`.
82is strongly recommended to avoid having longer lines, as long lines reduce
83readability and invite deep nesting.
84 59
85## Variables 60### Variable expansion
61
62* Use `${var}` over `$var` only when it is strictly necessary. Meaning:
63 Only if the character following the [variable name][POSIX.1-2017 definition name]
64 is an underscore or an alphanumeric character.
86 65
87### Quoting 66### Quoting
88Quote strings containing variables, command substitutions, spaces or shell meta
89characters, unless careful unquoted expansion is required.
90 67
91Don't quote _literal_ integers. 68* Always quote string literals (exceptions are assigning `pkgname` and
69 `pkgver`, more on this below).
70* Always quote variables, command substitutions or shell meta characters
71 when used in strings. Prefer `"$var"/foo/bar` over `"$var/foo/bar"`.
72* Never quote literal integers.
73
74## Features
75
76### Command Substitution
77
78* Always use `$()` instead of backticks.
79
80### Test and [
81
82* Prefer `[` over `test(1)`.
83
84## Naming Conventions
85
86### Function Names
87
88* Lower-case, with underscores to separate words. Prefix all helper
89 functions with an underscore character.
90
91### Variable Names
92
93* Lower-case, with underscores to separate words. Prefix all
94 non-metadata variables with an underscore character.
95
96### Use Local Variables
97
98* Declare function-specific variables with the `local` keyword.
99
100## Calling Commands
101
102### Command Substitutions and Global Variables
103
104* Avoid command Substitutions in global variables, use parameter
105 expansions instead.
106
107### Return Values
108
109* APKBUILDs are executed with `set -e`, explicit `|| return 1`
110 statements must not be used.
111
112## Comments
113
114### Spacing
115
116* All comments begin with an ASCII space character, e.g. `# foo`.
117
118### TODO Comments
119
120* Use TODO comments for code that is temporary, a short-term solution,
121 or good-enough but not perfect.
122
123# APKBUILD style considerations
124
125<!--
126This section attempts to document policies enforced by the linter from
127atools <https://github.com/maxice8/atools>, newapkbuild and existing
128APKBUILDs.
129-->
130
131## Comments
132
133### Contributor Comment
134
135* All APKBUILDs begin with one or more contributor comments (one per
136 line) containing a valid [RFC 5322][RFC 5322] address. For example,
137 `# Contributor: Max Mustermann <max@example.org>`.
138
139### Maintainer Comment
140
141* All APKBUILDs contain exactly one maintainer comment containing a
142 valid RFC 5322 address. For example, `# Maintainer: Max Mustermann
143 <max@example.org>`.
144* In addition to a Maintainer Comment a Contributor Comment must be
145 present for said Maintainer.
146
147## Metadata Variables
148
149Metadata Variables are variables used directly by abuild itself, e.g. `pkgname` or `depends`.
150
151### Metadata Values
152
153* `pkgname` must only contain lowercase characters.
154* `pkgname` must match the name of the directory the `APKBUILD` file is located in.
155
156### Variable Assignments
157
158* Empty Metadata Variable assignments, e.g. `install=""` must be removed.
159* The Metadata Variable `builddir` defaults to `$srcdir/$pkgname-$pkgver`
160 and should only be assigned if the default values is not appropriate.
161
162### Assignment Order
163
164* All Metadata Variables (except checksums) must be declared before the
165 first function declaration.
166* Checksum Metadata Variables must be declared after the last function
167 declaration, `abuild checksum` will automatically take care of this.
92 168
93### Bracing 169## Build Functions
94Only use curly braces around variables when needed.
95 170
96```sh 171### Function Declaration
97foo="${foo}_bar"
98foo_bar="$foo"
99```
100 172
101## Subshell usage 173* Functions should be declared in the order they are called by abuild.
102Use `$()` syntax, not backticks. 174* All functions are executed in `"$builddir"` explicit directory changes
175 to `$builddir` must be avoided (if possible).
176* Variables local to functions must always be declared with the `local`
177 keyword.
103 178
104## Sorting 179### Function Overwriting
105Some items tend to benefit from being sorted. A list of sources, dependencies
106etc. Always try to find a reasonable sort order, where alphabetically tends to
107be the most useful one.
108 180
109## Eval 181* If the `prepare` function is overwritten it should always call
110`eval` is evil and should be avoided. 182 `default_prepare`.
111 183
184[POSIX.1-2017 volume 3]: https://pubs.opengroup.org/onlinepubs/9699919799/idx/xcu.html
185[POSIX.1-2017 parameter expansion]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
186[POSIX.1-2017 definition name]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235
187[bash functions]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions
188[bash expansion]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
189[RFC 5322]: https://tools.ietf.org/html/rfc5322