make Prettier
[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
9 set -e
10 set -u
11
12 FLUTTER_VER=${WEBI_VERSION:-}
13 FLUTTER_VER="${FLUTTER_VER:-v}"
14 EXT="tar.xz"
15 FLUTTER_PATH=""
16
17 FLUTTER_OS="${WEBI_OS}" # linux or darwin
18 if [ "darwin" == "$FLUTTER_OS" ]; then
19   FLUTTER_OS="macos"
20   EXT="zip"
21 fi
22
23 my_tmp="$WEBI_TMP"
24
25 #########
26 # BEGIN #
27 #########
28
29 get_flutter_version() {
30   my_char="."
31   my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${FLUTTER_VER}")
32   # get the latest version if partial
33   if [ $my_count -ne 2 ]; then
34     if [ "$FLUTTER_VER" != "v" ]; then
35       FLUTTER_VER="$FLUTTER_VER\\."
36     fi
37     get_http=""
38     if [ -n "$(type -p curl)" ]; then
39       get_http="curl -fsL"
40     elif [ -n "$(type -p wget)" ]; then
41       get_http="wget --quiet -O -"
42     else
43       echo "Found neither 'curl' nor 'wget'. Can't Continue."
44       exit 1
45     fi
46   fi
47   FLUTTER_PATH=$($get_http "https://storage.googleapis.com/flutter_infra/releases/releases_${FLUTTER_OS}.json" | grep ${FLUTTER_OS} | grep ${FLUTTER_VER} | grep stable | head -n 1 | cut -d '"' -f 4) \
48         || echo 'error automatically determining current Flutter version'
49   FLUTTER_VER=$(echo $FLUTTER_PATH | sed 's/.*flutter_.*_v//' | sed 's/-stable.*//')
50 }
51
52 get_flutter_version
53
54 #
55 # flutter
56 #
57 flutter_install_path=$HOME/.local/opt/flutter_${FLUTTER_VER}
58 mkdir -p "$flutter_install_path"
59
60 # TODO warn if existing flutter in path my take precedence
61 if [ -e "$flutter_install_path/bin/flutter" ]; then
62   # flutter of some version is already installed
63   if [ "${FLUTTER_VER}" == "$($flutter_install_path/bin/flutter --version | head -n 1 | cut -d ' ' -f2 2>/dev/null)" ]; then
64     echo flutter_${FLUTTER_VER} already installed at $flutter_install_path
65     exit 0
66   fi
67 fi
68
69 # flutter_linux_v0.9.0-dev # flutter_linux_v0.9.0-dev.tar.xz
70 FLUTTER_PRE="flutter_${FLUTTER_OS}_${FLUTTER_VER}-stable"
71 FLUTTER_REMOTE="https://storage.googleapis.com/flutter_infra/releases/${FLUTTER_PATH}"
72 FLUTTER_LOCAL="$my_tmp/${FLUTTER_PRE}.${EXT}"
73 FLUTTER_UNTAR="$my_tmp/${FLUTTER_PRE}"
74
75 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
76   curl -fSL ${FLUTTER_REMOTE} -o ${FLUTTER_LOCAL} || echo 'error downloading flutter'
77 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
78   wget ${FLUTTER_REMOTE} -O ${FLUTTER_LOCAL} || echo 'error downloading flutter'
79 else
80   echo "'wget' and 'curl' are missing. Please run the following command and try again"
81   echo "    sudo apt-get install --yes curl wget"
82   exit 1
83 fi
84
85 mkdir -p ${FLUTTER_UNTAR}/
86 # --strip-components isn't portable, switch to portable version by performing move step after untar
87 if [ "zip" == "$EXT" ]; then
88   pushd ${FLUTTER_UNTAR}/
89     unzip ${FLUTTER_LOCAL}
90   popd
91 else
92   tar xf ${FLUTTER_LOCAL} -C ${FLUTTER_UNTAR}/ #--strip-components=1
93 fi
94 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
95   echo rsync -Krl "${FLUTTER_UNTAR}"/flutter/ "$flutter_install_path/"
96   rsync -Krl "${FLUTTER_UNTAR}/flutter/" "$flutter_install_path/"
97 else
98   echo cp -Hr "${FLUTTER_UNTAR}/"flutter/* "${FLUTTER_UNTAR}/"flutter/.* "$flutter_install_path/"
99   cp -Hr "${FLUTTER_UNTAR}/"flutter/* "${FLUTTER_UNTAR}/"flutter/.* "$flutter_install_path/"
100 fi
101 rm -rf "${FLUTTER_UNTAR}"
102
103 #######
104 # END #
105 #######
106
107 # TODO add more than one at a time
108 pathman add $flutter_install_path/bin