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