8be47b08b20296f8ef66e209bb87554f1fb8133b
[webi-installers/.git] / flutter / install.bash
1 #!/bin/bash
2
3 # title: Flutter
4 # homepage: https://flutter.dev
5 # tagline: UI Toolkit for mobile, web, and desktop
6 # description: |
7 #   Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase.
8 # examples: |
9 #
10 #   ```bash
11 #   flutter create my_app
12 #   ```
13
14 set -e
15 set -u
16
17 ###################
18 # Install flutter #
19 ###################
20
21 # The command name may be different from the package name
22 # (i.e. golang => go, rustlang => cargo, ripgrep => rg)
23 # Note: $HOME may contain special characters and should alway be quoted
24 pkg_cmd_name="flutter"
25 #pkg_cmd_name_formatted="flutter $WEBI_VERSION"
26
27 # Some of these directories may be the same
28 pkg_common_opt="$HOME/.local/opt/flutter"
29 pkg_common_bin="$HOME/.local/opt/flutter/bin"
30 pkg_common_cmd="$HOME/.local/opt/flutter/bin/flutter"
31 pkg_new_opt="$HOME/.local/opt/flutter-v$WEBI_VERSION"
32 pkg_new_bin="$HOME/.local/opt/flutter-v$WEBI_VERSION/bin"
33 pkg_new_cmd="$HOME/.local/opt/flutter-v$WEBI_VERSION/bin/flutter"
34 pkg_current_cmd=""
35
36 # The version info should be reduced to a sortable version, without any leading characters
37 # (i.e. v12.8.0 => 12.8.0, go1.14 => 1.14, 1.12.13+hotfix => 1.12.13+hotfix)
38 pkg_get_current_version() {
39     echo "$(flutter --version 2>/dev/null | head -n 1 | cut -d' ' -f2)"
40 }
41
42 # Any version-related directories should be unlinked and relinked to the correct version
43 # (for example: 'go' is special and needs both $HOME/go and $HOME/.local/opt/go)
44 # (others like 'rg', 'hugo', and 'caddy' are single files that just get replaced)
45 pkg_switch_version() {
46     rm -rf "$pkg_common_opt"
47     ln -s "$pkg_new_opt" "$pkg_common_opt"
48 }
49
50 # Different packages represent the version in different ways
51 # ex: node v12.8.0 (leading 'v')
52 # ex: go1.14 (no space, nor trailing '.0's)
53 # ex: flutter 1.17.2 (plain)
54 pkg_format_cmd_version() {
55     my_version=$1
56     echo "$pkg_cmd_name $my_version"
57 }
58
59 pkg_install() {
60     pushd "$WEBI_TMP" 2>&1 >/dev/null
61
62         # simpler for single-binary commands
63         #mv ./example*/bin/example "$HOME/.local/bin"
64
65         # best for packages and toolchains
66         if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
67             rsync -Krl ./flutter*/ "$pkg_new_opt/" 2>/dev/null
68         else
69             cp -Hr ./flutter*/* "$pkg_new_opt/" 2>/dev/null
70             cp -Hr ./flutter*/.* "$pkg_new_opt/" 2>/dev/null
71         fi
72         rm -rf ./flutter*
73     popd 2>&1 >/dev/null
74 }
75
76 pkg_post_install() {
77     webi_path_add "$pkg_common_bin"
78 }
79
80 #
81 # The webi_* functions are defined in webi/template.bash at https://github.com/webinstall/packages
82 #
83
84 # for packages that can have multiple versions
85 webi_check
86 # for packages that can be downloaded via links in ./releases.js
87 webi_download
88 # for single files or packaged directories (compressed or uncompressed)
89 # supported formats: .xz, .tar.*, and .zip
90 webi_extract
91
92 echo "Installing '$pkg_cmd_name' v$WEBI_VERSION as $pkg_new_cmd"
93
94 # for installing the tool
95 pkg_install
96 # for updating paths and installing companion tools
97 pkg_post_install
98 # for re-linking to a previously installed version
99 pkg_switch_version
100
101 echo "Installed '$pkg_cmd_name' v$WEBI_VERSION as $pkg_new_cmd"
102
103 echo ""