├── VERSION ├── .gitignore ├── README.md ├── LICENSE └── scripts └── dvm /VERSION: -------------------------------------------------------------------------------- 1 | 1.4.0 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Temp files 2 | *~ 3 | 4 | # DVM 5 | darts 6 | environments/default 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dvm: Dart Version Manager 2 | ========================= 3 | 4 | A minimal tool for managing Dart SDK versions. 5 | 6 | ## Installing 7 | 8 | In your home directory, run: 9 | ```sh 10 | git clone https://github.com/cbracken/dvm.git .dvm 11 | ``` 12 | 13 | Add the following to your `.bashrc` or `.zshrc`: 14 | ```sh 15 | if [[ -f ~/.dvm/scripts/dvm ]]; then 16 | . ~/.dvm/scripts/dvm 17 | fi 18 | ``` 19 | 20 | Optionally, installation in a custom location is supported by setting and 21 | exporting `DVM_ROOT` in your environment prior to the above invocation. 22 | 23 | ## Usage 24 | 25 | Commands: 26 | 27 | * `alias`: manage Dart version aliases 28 | * `help`: display usage 29 | * `implode`: delete dvm and all installed Dart versions 30 | * `install`: install a Dart version 31 | * `list`: list installed Dart versions 32 | * `listall`: list all available Dart versions (`--dev` for dev channel) 33 | * `upgrade`: upgrade dvm to the latest version 34 | * `use`: select a Dart version to use (`--default` to set as default) 35 | * `version`: Display the dvm version number 36 | 37 | ## Uninstalling 38 | 39 | To remove dvm and all installed Dart versions: 40 | 41 | ```sh 42 | dvm implode 43 | ``` 44 | 45 | ## Credits 46 | 47 | Inspired by [gvm](https://github.com/moovweb/gvm). 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /scripts/dvm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Dart Version Manager 4 | # author: Chris Bracken 5 | 6 | DVM_ROOT="${DVM_ROOT:-$HOME/.dvm}" 7 | DVM_VERSION=$(cat "$DVM_ROOT/VERSION") 8 | 9 | _dvm_usage() { 10 | echo "usage: dvm []" 11 | echo "" 12 | echo "Commands:" 13 | echo " alias Manage Dart version aliases" 14 | echo " help Display usage" 15 | echo " implode Delete dvm and all installed Dart versions" 16 | echo " install Install a Dart version" 17 | echo " list List installed Dart versions" 18 | echo " listall List all available Dart versions (--dev for dev channel)" 19 | echo " upgrade Upgrade dvm to the latest version" 20 | echo " use Select a Dart version to use (--default to set as default)" 21 | echo " version Display the dvm version number" 22 | return 1 23 | } 24 | 25 | _dvm_create_dirs() { 26 | mkdir -p "$DVM_ROOT/darts" > /dev/null 2>&1 27 | mkdir -p "$DVM_ROOT/environments" > /dev/null 2>&1 28 | } 29 | 30 | # Removes $1 from PATH. 31 | _dvm_path_remove() { 32 | PATH=$(echo $PATH | sed "s:$1::;s/::/:/;s/:$//") 33 | } 34 | 35 | # Adds or moves $1 to beginning of PATH. 36 | _dvm_path_prepend() { 37 | _dvm_path_remove "$1" 38 | if [ -d "$1" ]; then 39 | export PATH="$1:$PATH" 40 | fi 41 | } 42 | 43 | _dvm_doctor() { 44 | # Check for unzip tool. 45 | unzip_path="$(which unzip)" 46 | if [[ ! -x "$unzip_path" ]]; then 47 | echo "ERROR: dvm requires the 'unzip' tool, which was not found." 48 | echo "To install unzip:" 49 | echo " Debian/Ubuntu: sudo apt install unzip" 50 | echo " RedHat/Fedora: sudo dnf install unzip" 51 | echo " OpenSUSE: sudo zypper install unzip" 52 | echo " Arch/Manjaro: sudo pacman -S unzip" 53 | return 1 54 | fi 55 | } 56 | 57 | _dvm_alias_usage() { 58 | echo "usage: dvm alias []" 59 | } 60 | 61 | dvm_alias_list() { 62 | find "$DVM_ROOT/darts" -maxdepth 1 ! -path "$DVM_ROOT/darts" -type l -exec basename "{}" \; | sort 63 | } 64 | 65 | _dvm_alias_set() { 66 | # ensure target exists 67 | if [[ "$2" == "--path" ]]; then 68 | local abs_path="$3" 69 | if [[ "$abs_path" != /* ]]; then 70 | abs_path="$PWD/$3" 71 | fi 72 | if [[ ! -e "$abs_path" ]]; then 73 | echo "ERROR: target path $3 does not exist." 74 | return 1 75 | fi 76 | rm -f -- "$DVM_ROOT/darts/$1" 77 | ln -s -- "$abs_path" "$DVM_ROOT/darts/$1" 78 | else 79 | if [[ ! -e "$DVM_ROOT/darts/$2" ]]; then 80 | echo "ERROR: target version $2 does not exist." 81 | return 1 82 | fi 83 | rm -f -- "$DVM_ROOT/darts/$1" 84 | ln -s -- "$2" "$DVM_ROOT/darts/$1" 85 | fi 86 | } 87 | 88 | dvm_alias_update() { 89 | if [[ $# < 2 || "$2" == "--path" && $# < 3 ]]; then 90 | echo "usage: dvm alias update [--path] " 91 | return 1 92 | fi 93 | 94 | # check for existing alias/version 95 | if [[ ! -h "$DVM_ROOT/darts/$1" ]]; then 96 | echo "ERROR: no such alias $1." 97 | return 1 98 | fi 99 | 100 | _dvm_alias_set "$@" 101 | } 102 | 103 | dvm_alias_create() { 104 | if [[ $# < 2 || "$2" == "--path" && $# < 3 ]]; then 105 | echo "usage: dvm alias create [--path] " 106 | return 1 107 | fi 108 | 109 | # check for existing alias/version 110 | if [[ -h "$DVM_ROOT/darts/$1" ]]; then 111 | echo "ERROR: alias $1 already exists." 112 | return 1 113 | elif [[ -d "$DVM_ROOT/darts/$1" ]]; then 114 | echo "ERROR: version $1 already exists." 115 | return 1 116 | fi 117 | 118 | _dvm_alias_set "$@" 119 | } 120 | 121 | dvm_alias_delete() { 122 | if [[ $# < 1 ]]; then 123 | echo "usage: dvm alias delete " 124 | return 1 125 | fi 126 | if [[ ! -h "$DVM_ROOT/darts/$1" ]]; then 127 | echo "ERROR: no such alias $1." 128 | return 1 129 | fi 130 | rm -f -- "$DVM_ROOT/darts/$1" 131 | } 132 | 133 | dvm_alias() { 134 | if [[ $# < 1 ]]; then 135 | _dvm_alias_usage 136 | return 1 137 | fi 138 | cmd=$1 139 | shift 140 | case $cmd in 141 | create) 142 | dvm_alias_create "$@" 143 | ;; 144 | delete) 145 | dvm_alias_delete "$@" 146 | ;; 147 | list) 148 | dvm_alias_list "$@" 149 | ;; 150 | update) 151 | dvm_alias_update "$@" 152 | ;; 153 | *) 154 | _dvm_alias_usage 155 | return 1 156 | ;; 157 | esac 158 | } 159 | 160 | dvm_use() { 161 | if [[ $# < 1 ]]; then 162 | echo "usage: dvm use [--default]" 163 | return 1 164 | fi 165 | 166 | local version=$1 167 | local default=$2 168 | shift 169 | 170 | if [[ ! -e "$DVM_ROOT/darts/$version" ]]; then 171 | echo "ERROR: version not found. Try 'dvm install $version'." 172 | return 1 173 | fi 174 | 175 | if [[ "$default" == "--default" ]]; then 176 | local defaults="$DVM_ROOT/environments/default" 177 | echo 'export DVM_ROOT; DVM_ROOT="$DVM_ROOT"' > "$defaults" 178 | echo "export DART_SDK; DART_SDK=\"$DVM_ROOT/darts/$version\"" >> "$defaults" 179 | echo "PATH=\"\$DVM_ROOT/darts/$version/bin:\$PATH\"" >> "$defaults" 180 | fi 181 | 182 | export DART_SDK="$DVM_ROOT/darts/$version" 183 | _dvm_path_prepend "$DVM_ROOT/darts/$version/bin" 184 | } 185 | 186 | dvm_list() { 187 | find "$DVM_ROOT/darts" -maxdepth 1 ! -path "$DVM_ROOT/darts" -type d \ 188 | -exec basename "{}" \; | sort -V 189 | } 190 | 191 | _dvm_list_repo() { 192 | local channel=$1 193 | local api_uri="https://www.googleapis.com/storage/v1/b/dart-archive/o" 194 | local query="prefix=channels/$channel/release/&delimiter=/" 195 | curl -s "$api_uri?$query" | \ 196 | grep "channels/$channel/release/" | \ 197 | sed -e "s@.*/$channel/release/@@;s@/.*@@" | \ 198 | grep -v "^[0-9]*$" | \ 199 | sort -V 200 | } 201 | 202 | dvm_listall() { 203 | if [[ "$1" == "--dev" ]]; then 204 | _dvm_list_repo "dev" 205 | elif [[ "$1" == "--beta" ]]; then 206 | _dvm_list_repo "beta" 207 | else 208 | _dvm_list_repo "stable" 209 | fi 210 | } 211 | 212 | _dvm_download_sdk() { 213 | local channel=$1 214 | local version=$2 215 | local sdk_archive=$3 216 | 217 | local suffix 218 | if [[ "$channel" == "main" ]]; then 219 | suffix="raw" 220 | else 221 | suffix="release" 222 | fi 223 | 224 | local dl_uri="https://storage.googleapis.com/dart-archive/channels/$channel/$suffix" 225 | local base_uri="$dl_uri/$version" 226 | echo "Downloading: $base_uri/sdk/$sdk_archive" 227 | curl -f -O "$base_uri/sdk/$sdk_archive" 228 | } 229 | 230 | _dvm_download_content_shell() { 231 | local channel=$1 232 | local version=$2 233 | local content_shell_archive=$3 234 | 235 | local dl_uri="https://storage.googleapis.com/dart-archive/channels/$channel/release" 236 | local base_uri="$dl_uri/$version" 237 | echo "Downloading: $base_uri/dartium/$content_shell_archive" 238 | curl -f -O "$base_uri/dartium/$content_shell_archive" 239 | } 240 | 241 | _dvm_download_dartium() { 242 | local channel=$1 243 | local version=$2 244 | local dartium_archive=$3 245 | 246 | local dl_uri="https://storage.googleapis.com/dart-archive/channels/$channel/release" 247 | local base_uri="$dl_uri/$version" 248 | echo "Downloading: $base_uri/dartium/$dartium_archive" 249 | curl -f -O "$base_uri/dartium/$dartium_archive" 250 | } 251 | 252 | # Returns the CPU architechture for Dart in the specified SDK version. 253 | # 254 | # Used by Apple Silicon (arm64) based Macs to determine the architecture of the 255 | # Dart SDK archive to download. For SDK 2.14.1 and later, download the arm64 256 | # bundle. Prior to that, only Intel binaries are available. 257 | _macos_arm64_sdk_arch() { 258 | local archBoundary="2.14.1" 259 | if [[ "$1" == "latest" || "$(printf "$1\n$archBoundary\n" | sort -t. -n -k 1,1 -k 2,2 -k 3,3 | head -n1)" == "$archBoundary" ]]; then 260 | echo "arm64" 261 | else 262 | echo "x64" 263 | fi 264 | } 265 | 266 | # Returns the CPU architechture for Dartium in the specified SDK version. 267 | # 268 | # In Dart SDKx 1.19.x and earlier, Dartium shipped only as an ia32 build. In 269 | # SDKs later than 1.20.0, it was produced as an x64 build. Through a happy 270 | # quirk of fate, 1.20.0 never existed. 271 | _macos_dartium_arch() { 272 | local archBoundary="1.20.0" 273 | if [[ "$1" == "latest" || "$(printf "$1\n$archBoundary\n" | sort -t. -n -k 1,1 -k 2,2 -k 3,3 | head -n1)" == "$archBoundary" ]]; then 274 | echo "x64" 275 | else 276 | echo "ia32" 277 | fi 278 | } 279 | 280 | dvm_install() { 281 | if [[ $# < 1 ]]; then 282 | echo "usage: dvm install [channel]" 283 | return 1 284 | fi 285 | 286 | curl=$(which curl) 287 | if [[ ! -x "$curl" ]]; then 288 | echo "ERROR: curl is required but was not found on PATH." 289 | return 1 290 | fi 291 | 292 | local version=$1 293 | local channel=$2 294 | shift 295 | 296 | if [[ -d "$DVM_ROOT/darts/$version" ]]; then 297 | echo "ERROR: version $version is already installed." 298 | return 1 299 | fi 300 | 301 | case $(uname -a) in 302 | Darwin*arm64*) 303 | local arch="$(_macos_arm64_sdk_arch "$version")" 304 | local sdk_archive="dartsdk-macos-$arch-release.zip" 305 | arch="$(_macos_dartium_arch "$version")" 306 | local content_shell_archive="content_shell-macos-$arch-release.zip" 307 | local dartium_archive="dartium-macos-$arch-release.zip" 308 | ;; 309 | Darwin*) 310 | local arch="$(_macos_dartium_arch "$version")" 311 | local sdk_archive="dartsdk-macos-x64-release.zip" 312 | local content_shell_archive="content_shell-macos-$arch-release.zip" 313 | local dartium_archive="dartium-macos-$arch-release.zip" 314 | ;; 315 | Linux*armv7*) 316 | local sdk_archive=dartsdk-linux-arm-release.zip 317 | local content_shell_archive=content_shell-linux-arm-release.zip 318 | local dartium_archive=dartium-linux-arm-release.zip 319 | ;; 320 | Linux*armv8*) 321 | local sdk_archive=dartsdk-linux-arm64-release.zip 322 | local content_shell_archive=content_shell-linux-arm64-release.zip 323 | local dartium_archive=dartium-linux-arm64-release.zip 324 | ;; 325 | Linux*) 326 | local sdk_archive=dartsdk-linux-x64-release.zip 327 | local content_shell_archive=content_shell-linux-x64-release.zip 328 | local dartium_archive=dartium-linux-x64-release.zip 329 | ;; 330 | MINGW*|MSYS*|CYGWIN*|Windows_NT*|*NT-*) 331 | local sdk_archive=dartsdk-windows-x64-release.zip 332 | local content_shell_archive=content_shell-windows-x64-release.zip 333 | local dartium_archive=dartium-windows-x64-release.zip 334 | ;; 335 | *) 336 | echo "ERROR: unable to determine OS." 337 | return 1 338 | ;; 339 | esac 340 | 341 | # Create tmp workspace. 342 | tmpdir=$(mktemp -d) 343 | pushd $tmpdir > /dev/null 344 | 345 | # Download SDK. 346 | # Use channel if defined, otherwise loop through all channels. 347 | if [[ -z "$channel" ]]; then 348 | for channel in "stable" "beta" "dev"; do 349 | _dvm_download_sdk "$channel" "$version" "$sdk_archive" && break 350 | done 351 | else 352 | _dvm_download_sdk "$channel" "$version" "$sdk_archive" 353 | fi 354 | 355 | if [[ $? -ne 0 ]]; then 356 | echo "ERROR: unable to download Dart SDK. Are you sure that version exists?" 357 | popd > /dev/null 358 | rm -rf -- "$tmpdir" 359 | return 1 360 | fi 361 | unzip $sdk_archive 362 | version=$( /dev/null 387 | case $(uname) in 388 | Darwin) 389 | echo "#!/bin/bash" > content_shell 390 | echo "exec \"$DVM_ROOT/darts/$version/content_shell/Content Shell.app/Contents/MacOS/Content Shell\" \"\$@\"" >> content_shell 391 | chmod ugo+x content_shell 392 | ;; 393 | Linux) 394 | ln -s ../content_shell/content_shell content_shell 395 | ;; 396 | esac 397 | popd > /dev/null 398 | fi 399 | 400 | # Download Dartium. 401 | for channel in "stable" "beta" "dev"; do 402 | _dvm_download_dartium "$channel" "$version" "$dartium_archive" && break 403 | done 404 | if [[ $? -ne 0 ]]; then 405 | echo "ERROR: unable to download Dartium. But hey, at least you got the SDK." 406 | else 407 | unzip $dartium_archive 408 | if [[ ! -d "$DVM_ROOT/darts/$version/dartium" ]]; then 409 | local dartium_dir="$(find . -maxdepth 1 ! -path . -type d)" 410 | mv "$dartium_dir" "$DVM_ROOT/darts/$version/dartium" 411 | fi 412 | 413 | # Create Dartium symlink. 414 | pushd "$DVM_ROOT/darts/$version/bin" > /dev/null 415 | case $(uname) in 416 | Darwin) 417 | echo "#!/bin/bash" > dartium 418 | echo "open \"$DVM_ROOT/darts/$version/dartium/Chromium.app\"" >> dartium 419 | chmod ugo+x dartium 420 | ;; 421 | Linux) 422 | ln -s ../dartium/chrome dartium 423 | ;; 424 | esac 425 | popd > /dev/null 426 | fi 427 | fi 428 | 429 | # Clean up. 430 | popd > /dev/null 431 | rm -rf -- "$tmpdir" 432 | } 433 | 434 | # Returns 1 if there are differences against the latest upstream version; 435 | # otherwise returns 0. 436 | _dvm_needsupgrade() { 437 | git -C "$DVM_ROOT" fetch origin 438 | if [[ $? -ne 0 ]]; then 439 | # Failed to fetch; don't try to update. 440 | echo "ERROR: failed to fetch dvm." 441 | return 0 442 | fi 443 | 444 | local local_sha="$(git -C "$DVM_ROOT" rev-parse HEAD)" 445 | local remote_sha="$(git -C "$DVM_ROOT" rev-parse origin/master)" 446 | if [[ "$local_sha" == "$remote_sha" ]]; then 447 | # No diffs vs upstream. 448 | return 0 449 | fi 450 | return 1 451 | } 452 | 453 | dvm_upgrade() { 454 | # Abort if there are local diffs that aren't checked in. 455 | local diffs="$(git -C "$DVM_ROOT" status --porcelain=v1 2>/dev/null)" 456 | if [[ -n "$diffs" ]]; then 457 | echo "ERROR: aborting update. Local changes found: $DVM_ROOT" 458 | echo "Please stash or commit your changes prior to updating." 459 | return 1 460 | fi 461 | 462 | _dvm_needsupgrade 463 | if [[ $? -eq 0 ]]; then 464 | echo "dvm is up to date." 465 | return 0 466 | fi 467 | 468 | git -C "$DVM_ROOT" pull --rebase origin master > /dev/null 469 | if [[ $? -ne 0 ]]; then 470 | git -C "$DVM_ROOT" rebase --abort > /dev/null 471 | echo "ERROR: failed to upgrade dvm." 472 | return 1 473 | fi 474 | 475 | # Re-source the script to replace function definitions. 476 | source "$DVM_ROOT/scripts/dvm" 477 | echo "dvm was upgraded to the latest version." 478 | return 0 479 | } 480 | 481 | dvm_version() { 482 | echo "Dart Version Manager version $DVM_VERSION installed at $DVM_ROOT" 483 | } 484 | 485 | dvm_implode() { 486 | echo "This will delete dvm and all installed versions." 487 | echo -n "Are you sure? " 488 | read yn 489 | yn=$(tr '[:upper:]' '[:lower:]' <<< "$yn") 490 | if [[ "$yn" == "y" || "$yn" == "yes" ]]; then 491 | rm -rf -- "$DVM_ROOT" && echo "DVM successfully removed." || echo "Failed to remove DVM." 492 | else 493 | echo "Cancelled." 494 | fi 495 | } 496 | 497 | dvm() { 498 | # Wrap with subshell to avoid changing the shell's current directory. 499 | (_dvm "$@") 500 | } 501 | 502 | _dvm() { 503 | if [[ -z "$DVM_ROOT" ]]; then 504 | echo "ERROR: DVM_ROOT not set. Please source \$DVM_ROOT/scripts/dvm." 505 | return 1 506 | fi 507 | if [[ ! -d "$DVM_ROOT" ]]; then 508 | echo "ERROR: DVM_ROOT does not exist. Please reinstall dvm." 509 | return 1 510 | fi 511 | 512 | # Verify prerequisites are installed. 513 | _dvm_doctor 514 | if [[ $? -ne 0 ]]; then 515 | return 1 516 | fi 517 | 518 | # Create required directories. 519 | _dvm_create_dirs 520 | 521 | if [[ $# < 1 ]]; then 522 | _dvm_usage 523 | return 1 524 | fi 525 | cmd=$1 526 | shift 527 | case $cmd in 528 | alias) 529 | dvm_alias "$@" 530 | ;; 531 | implode) 532 | dvm_implode "$@" 533 | ;; 534 | install) 535 | dvm_install "$@" 536 | ;; 537 | list) 538 | dvm_list "$@" 539 | ;; 540 | listall) 541 | dvm_listall "$@" 542 | ;; 543 | upgrade) 544 | dvm_upgrade "$@" 545 | ;; 546 | use) 547 | dvm_use "$@" 548 | ;; 549 | version) 550 | dvm_version "$@" 551 | ;; 552 | *) 553 | if [[ $(type -t dvm_$cmd) == "function" ]]; then 554 | dvm_$cmd "$@" 555 | else 556 | _dvm_usage 557 | fi 558 | ;; 559 | esac 560 | } 561 | 562 | if [[ -e "$DVM_ROOT/environments/default" ]]; then 563 | . "$DVM_ROOT/environments/default" 564 | fi 565 | --------------------------------------------------------------------------------