Skip to content

Commit 67a5098

Browse files
committed
tasks: Add os & package helpers to tasklib
Signed-off-by: Stephen Brennan <[email protected]>
1 parent 9d954e9 commit 67a5098

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

doc/guide/tasks.rst

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ find the script when you ask to use it.
6868
Special Task Syntax
6969
-------------------
7070

71-
While tasks are generally normal bash scripts, there are a few special bash
72-
functions which are available to you:
71+
While tasks are generally normal bash scripts, there are a few special syntactic
72+
elements which are available to you. These are implemented in a rather unique
73+
way: as Yo loads your script, it reads each line and detects the use of the
74+
following keywords, somewhat like a macro processor. Most of these keywords also
75+
have a corresponding bash function that Yo provides to implement the necessary
76+
functionality.
7377

7478
- ``DEPENDS_ON <task name here>`` - this declares that your task depends on
7579
another one. Yo will search for these lines in your script and automatically
@@ -105,14 +109,46 @@ functions which are available to you:
105109
ensure that A runs to completion before task ``B`` begins.
106110

107111
These functions can be used anywhere in your script, however bash syntax such as
108-
quoting or variable expansion is not respected when Yo locates them. So, while
109-
the following is valid bash, it won't work with Yo:
112+
quoting or variable expansion is not respected where Yo interprets them. So,
113+
while the following is valid bash, it won't work with Yo:
110114

111115
.. code:: bash
112116
113117
TASK=drgn
114118
DEPENDS_ON $TASK
115119
120+
121+
Other Task Variables and Functions
122+
----------------------------------
123+
124+
Additionally, Yo's bash task library makes a few conveniences available to you.
125+
It sources the contents of ``/etc/os-release`` so that you can use common
126+
variables from this file, such as ``$NAME``, ``$VERSION_ID``, etc. In addition,
127+
Yo provides the following variables:
128+
129+
- ``$ORAVER`` - an integer representing the current Oracle Linux release. The
130+
variable is undefined if not running on Oracle Linux. You can detect when
131+
Oracle Linux is running by matching the ``NAME`` variable against ``Oracle*``
132+
133+
- ``$UBUVER`` - an integer representing the Ubuntu version (e.g. for 24.10, this
134+
would be "24"). If you would like the full version (e.g. to distinguish 24.04
135+
and 24.10), use the ``$VERSION_ID`` field directly.
136+
137+
- ``$FEDVER`` - an integer representing the Fedora version
138+
139+
- ``$DEBVER`` - an integer representing the Debian version
140+
141+
- ``$PKGMGR`` - the name of the system package manager (only detected for the
142+
above operating systems)
143+
144+
And below are the simple bash functions (not interpreted by Yo) provided in the
145+
task library:
146+
147+
- ``PKG_INSTALL [package [...]]`` install one or more packages. This relies on
148+
the detected ``$PKGMGR`` from above, and ensures that the correct options are
149+
used for the specific package manager, especially to avoid interactive
150+
confirmation.
151+
116152
Managing Tasks
117153
--------------
118154

yo/data/yo_tasklib.sh

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,49 @@ PREREQ_FOR() {
3939
true
4040
}
4141

42-
# Setup the "$ORAVER" variable for Oracle Linux
43-
if [ -f /etc/oracle-release ]; then
44-
. /etc/os-release
45-
ORAVER="${VERSION//\.*}"
46-
fi
42+
# Operating system information
43+
source /etc/os-release
44+
case "${NAME}" in
45+
Oracle*)
46+
# Setup the "$ORAVER" variable for Oracle Linux. It is a single integer
47+
# number describing the current Oracle Linux distribution release. EG:
48+
# "8", "9", "10", etc.
49+
ORAVER="${VERSION//\.*}"
50+
case "${ORAVER}" in
51+
6|7)
52+
PKGMGR=yum
53+
;;
54+
*)
55+
PKGMGR=dnf
56+
;;
57+
esac
58+
;;
59+
Ubuntu*)
60+
UBUVER="${VERSION_ID//\.*}"
61+
PKGMGR=apt-get
62+
;;
63+
Debian*)
64+
DEBVER="$VERSION_ID"
65+
PKGMGR=apt-get
66+
;;
67+
Fedora*)
68+
FEDVER="$VERSION_ID"
69+
PKGMGR=dnf
70+
;;
71+
Arch*)
72+
PKGMGR=pacman
73+
;;
74+
esac
75+
76+
PKG_INSTALL() {
77+
if [ "$PKGMGR" = "pacman" ]; then
78+
pacman -Sy --noconfirm "$@"
79+
elif [ -n "$PKGMGR" ]; then
80+
$PKGMGR install -y "$@"
81+
else
82+
echo "error: package manager is unknown"
83+
exit 1
84+
fi
85+
}
4786

4887
## END: yo_tasklib.sh

0 commit comments

Comments
 (0)