add flutter
[webi-installers/.git] / flutter / flutter.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 # Use the script's first argument or the supplied WEBI_VERSION or ''
18 WEBI_VERSION=${1:-${WEBI_VERSION:-}}
19
20 # Set a temporary directory, if not already set
21 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-flutter.XXXXXXXX)"}
22
23 ###################
24 #  Get WEBI vars  #
25 ###################
26
27 # The WEBI bootstrap will define these
28 # but each script should be testable in its own right
29
30 if [ -z "${WEBI_PKG_URL:-}" ]; then
31   release_tab="${WEBI_HOST}/api/releases/flutter@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&limit=1"
32   WEBI_CSV=$(curl -fsSL "$release_tab" -H "User-Agent: $(uname -a)")
33   WEBI_CHANNEL=$(echo $WEBI_CSV | cut -d ',' -f 3)
34   if [ "error" == "$WEBI_CHANNEL" ]; then
35      echo "could not find release for flutter v${WEBI_VERSION}"
36      exit 1
37   fi
38   # TODO allow EXT ZIP or TAR in bootstrap script
39   WEBI_EXT=$(echo $WEBI_CSV | cut -d ',' -f 8)
40   WEBI_VERSION=$(echo $WEBI_CSV | cut -d ',' -f 1)
41   WEBI_PKG_URL=$(echo $WEBI_CSV | cut -d ',' -f 9)
42   WEBI_PKG_FILE="$WEBI_TMP/$(echo $WEBI_PKG_URL | sed s:.*/::)"
43 fi
44
45 ###################
46 # Install flutter #
47 ###################
48
49 new_flutter_home="${HOME}/.local/opt/flutter-v${WEBI_VERSION}"
50 new_flutter="${HOME}/.local/opt/flutter-v${WEBI_VERSION}/bin/flutter"
51
52 # Test for existing version 
53 set +e
54 cur_flutter="$(command -v flutter)"
55 set -e
56 if [ -n "$cur_flutter" ]; then
57   # TODO this is still sometimes wrong (i.e. 1.14 = 1.14.0)
58   cur_ver=$(flutter --version | head -n 1 | cut -d' ' -f2)
59   if [ "$cur_ver" == "$(echo $WEBI_VERSION)" ]; then
60     echo "flutter v$WEBI_VERSION already installed at $cur_flutter"
61     exit 0
62   elif [ "$cur_flutter" != "$new_flutter" ]; then
63     echo "WARN: possible conflict with flutter v$WEBI_VERSION at $cur_flutter"
64   fi
65 fi
66
67 # TODO move download to the webi bootstrap
68 echo Downloading flutter v"${WEBI_VERSION}" from "${WEBI_PKG_URL}"
69 # TODO use downloads directory because this is big
70 set +e
71 if [ -n "$(command -v wget)" ]; then
72   # better progress bar
73   wget -c "${WEBI_PKG_URL}" -O "${WEBI_PKG_FILE}"
74 else
75   curl -fL "${WEBI_PKG_URL}" -o "${WEBI_PKG_FILE}"
76 fi
77 set -e
78
79 pushd "${WEBI_TMP}" 2>&1 >/dev/null
80         echo Installing flutter v${WEBI_VERSION} as "$new_flutter" 
81         if [ "zip" == "$WEBI_EXT" ]; then
82           unzip "${WEBI_PKG_FILE}"
83         else
84           tar xf "${WEBI_PKG_FILE}"
85         fi
86         rm "${WEBI_PKG_FILE}"
87
88         # simpler for single-binary commands
89         #mv ./example*/bin/example "$HOME/.local/bin"
90
91         # best for packages and toolchains
92         rm -rf "$new_flutter_home"
93         if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
94           rsync -Krl ./flutter*/ "$new_flutter_home/" 2>/dev/null
95         else
96           cp -Hr ./flutter*/* "$new_flutter_home/" 2>/dev/null
97           cp -Hr ./flutter*/.* "$new_flutter_home/" 2>/dev/null
98         fi
99 popd 2>&1 >/dev/null
100
101 ###################
102 #   Update PATH   #
103 ###################
104
105 # TODO get better output from pathman / output the path to add as return to webi bootstrap
106 pathman add "$new_flutter_home/bin"
107 echo "Installed 'flutter'"
108 echo ""