619534d73dd99531c4cd1ba8723fc550792749ce
[webi-installers/.git] / _example / install.bash
1 #!/bin/bash
2
3 # TODO: migrate from shmatter to frontmarker
4
5 set -e
6 set -u
7
8 ## The defaults can be assumed if these are not set
9
10 ## The command name may be different from the package name
11 ## (i.e. golang => go, rustlang => cargo, ripgrep => rg)
12 ## Note: $HOME may contain special characters and should alway be quoted
13
14 pkg_cmd_name="xmpl"
15
16 ## Some of these directories may be the same, in some cases
17 #pkg_dst="$HOME/.local/opt/xmpl"
18 #pkg_dst_bin="$HOME/.local/opt/xmpl/bin"
19 #pkg_dst_cmd="$HOME/.local/opt/xmpl/bin/xmpl"
20
21 #pkg_src="$HOME/.local/opt/xmpl-v$WEBI_VERSION"
22 #pkg_src_bin="$HOME/.local/opt/xmpl-v$WEBI_VERSION/bin"
23 #pkg_src_cmd="$HOME/.local/opt/xmpl-v$WEBI_VERSION/bin/xmpl"
24
25 # Different packages represent the version in different ways
26 # ex: node v12.8.0 (leading 'v')
27 # ex: go1.14 (no space, nor trailing '.0's)
28 # ex: flutter 1.17.2 (plain)
29 pkg_format_cmd_version() {
30     my_version=$1
31     echo "$pkg_cmd_name v$my_version"
32 }
33
34 # The version info should be reduced to a sortable version, without any leading characters
35 # (i.e. v12.8.0 => 12.8.0, go1.14 => 1.14, 1.12.13+hotfix => 1.12.13+hotfix)
36 pkg_get_current_version() {
37     echo "$(xmpl --version 2>/dev/null | head -n 1 | cut -d' ' -f2)"
38 }
39
40 # For (re-)linking to the desired installed version
41 # (for example: 'go' is special and needs both $HOME/go and $HOME/.local/opt/go)
42 # (others like 'rg', 'hugo', and 'caddy' are single files that just get replaced)
43 pkg_link_src_dst() {
44     rm -rf "$pkg_dst"
45     ln -s "$pkg_src" "$pkg_dst"
46 }
47
48 pkg_pre_install() {
49     # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
50
51     # if selected version is installed, re-link it and quit
52     webi_check
53
54     # will save to ~/Downloads/$WEBI_PKG_FILE by default
55     webi_download
56
57     # supported formats (.xz, .tar.*, .zip) will be extracted to $WEBI_TMP
58     webi_extract
59 }
60
61 # For installing from the extracted package tmp directory
62 pkg_install() {
63     pushd "$WEBI_TMP" 2>&1 >/dev/null
64
65         # remove the versioned folder, just in case it's there with junk
66         rm -rf "$pkg_src"
67
68         # rename the entire extracted folder to the new location
69         # (this will be "$HOME/.local/opt/xmpl-v$WEBI_VERSION" by default)
70         mv ./"$pkg_cmd_name"* "$pkg_src"
71
72     popd 2>&1 >/dev/null
73 }
74
75 # For updating PATHs and installing companion tools
76 pkg_post_install() {
77     pkg_link_src_dst
78
79     # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
80     webi_path_add "$pkg_dst_bin"
81 }
82
83 pkg_post_install_message() {
84     echo "Installed 'example' as 'xmpl'"
85 }