Michael Goderbauer | c234d40 | 2017-01-27 23:12:33 -0800 | [diff] [blame] | 1 | @ECHO off |
| 2 | REM Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 | REM Use of this source code is governed by a BSD-style license that can be |
| 4 | REM found in the LICENSE file. |
| 5 | |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 6 | |
| 7 | REM ---------------------------------- NOTE ---------------------------------- |
| 8 | REM |
| 9 | REM Please keep the logic in this file consistent with the logic in the |
| 10 | REM `flutter` script in the same directory to ensure that Flutter continues to |
| 11 | REM work across all platforms! |
| 12 | REM |
| 13 | REM -------------------------------------------------------------------------- |
Michael Goderbauer | c234d40 | 2017-01-27 23:12:33 -0800 | [diff] [blame] | 14 | |
| 15 | SETLOCAL ENABLEDELAYEDEXPANSION |
| 16 | |
| 17 | FOR %%i IN ("%~dp0..") DO SET FLUTTER_ROOT=%%~fi |
Michael Goderbauer | c234d40 | 2017-01-27 23:12:33 -0800 | [diff] [blame] | 18 | |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 19 | SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools |
| 20 | SET cache_dir=%FLUTTER_ROOT%\bin\cache |
| 21 | SET snapshot_path=%cache_dir%\flutter_tools.snapshot |
| 22 | SET stamp_path=%cache_dir%\flutter_tools.stamp |
| 23 | SET script_path=%flutter_tools_dir%\bin\flutter_tools.dart |
| 24 | SET dart_sdk_path=%cache_dir%\dart-sdk |
| 25 | SET engine_stamp=%cache_dir%\engine-dart-sdk.stamp |
| 26 | SET engine_version_path=%FLUTTER_ROOT%\bin\internal\engine.version |
| 27 | SET pub_cache_path=%FLUTTER_ROOT%\.pub-cache |
| 28 | |
| 29 | SET dart=%dart_sdk_path%\bin\dart.exe |
| 30 | SET pub=%dart_sdk_path%\bin\pub.bat |
| 31 | |
| 32 | REM If available, add location of bundled mingit to PATH |
| 33 | SET mingit_path=%FLUTTER_ROOT%\bin\mingit\cmd |
| 34 | IF EXIST "%mingit_path%" SET PATH=%PATH%;%mingit_path% |
| 35 | |
| 36 | REM Test if Git is available on the Host |
| 37 | where /q git || ECHO Error: Unable to find git in your PATH. && EXIT /B 1 |
| 38 | REM Test if the flutter directory is a git clone, otherwise git rev-parse HEAD would fail |
| 39 | IF NOT EXIST "%flutter_root%\.git" ( |
| 40 | ECHO Error: The Flutter directory is not a clone of the GitHub project. |
| 41 | ECHO The flutter tool requires Git in order to operate properly; |
| 42 | ECHO to set up Flutter, run the following command: |
Jonah Williams | 1e12700 | 2019-02-05 15:16:11 -0800 | [diff] [blame] | 43 | ECHO git clone -b stable https://github.com/flutter/flutter.git |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 44 | EXIT /B 1 |
| 45 | ) |
| 46 | |
| 47 | REM Ensure that bin/cache exists. |
Michael Goderbauer | e01470a | 2017-02-07 15:02:10 -0800 | [diff] [blame] | 48 | IF NOT EXIST "%cache_dir%" MKDIR "%cache_dir%" |
| 49 | |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 50 | REM If the cache still doesn't exist, fail with an error that we probably don't have permissions. |
| 51 | IF NOT EXIST "%cache_dir%" ( |
| 52 | ECHO Error: Unable to create cache directory at |
| 53 | ECHO %cache_dir% |
| 54 | ECHO. |
| 55 | ECHO This may be because flutter doesn't have write permissions for |
| 56 | ECHO this path. Try moving the flutter directory to a writable location, |
| 57 | ECHO such as within your home directory. |
| 58 | EXIT /B 1 |
| 59 | ) |
| 60 | |
| 61 | |
| 62 | REM To debug the tool, you can uncomment the following lines to enable checked mode and set an observatory port: |
| 63 | REM SET FLUTTER_TOOL_ARGS="--checked %FLUTTER_TOOL_ARGS%" |
| 64 | REM SET FLUTTER_TOOL_ARGS="%FLUTTER_TOOL_ARGS% --observe=65432" |
| 65 | |
| 66 | :acquire_lock |
| 67 | 2>NUL ( |
| 68 | REM "3" is now stderr because of "2>NUL". |
| 69 | CALL :subroutine %* 2>&3 9> "%cache_dir%\flutter.bat.lock" || GOTO acquire_lock |
| 70 | ) |
| 71 | GOTO :after_subroutine |
| 72 | |
| 73 | :subroutine |
| 74 | PUSHD "%flutter_root%" |
| 75 | FOR /f %%r IN ('git rev-parse HEAD') DO SET revision=%%r |
| 76 | POPD |
| 77 | |
| 78 | REM The following IF conditions are all linked with a logical OR. However, |
| 79 | REM there is no OR operator in batch and a GOTO construct is used as replacement. |
| 80 | |
| 81 | IF NOT EXIST "%engine_stamp%" GOTO do_sdk_update_and_snapshot |
| 82 | SET /P dart_required_version=<"%engine_version_path%" |
| 83 | SET /P dart_installed_version=<"%engine_stamp%" |
| 84 | IF !dart_required_version! NEQ !dart_installed_version! GOTO do_sdk_update_and_snapshot |
| 85 | IF NOT EXIST "%snapshot_path%" GOTO do_snapshot |
| 86 | IF NOT EXIST "%stamp_path%" GOTO do_snapshot |
| 87 | SET /P stamp_value=<"%stamp_path%" |
| 88 | IF !stamp_value! NEQ !revision! GOTO do_snapshot |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 89 | SET pubspec_yaml_path=%flutter_tools_dir%\pubspec.yaml |
| 90 | SET pubspec_lock_path=%flutter_tools_dir%\pubspec.lock |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 91 | FOR /F %%i IN ('DIR /B /O:D "%pubspec_yaml_path%" "%pubspec_lock_path%"') DO SET newer_file=%%i |
Michael Klimushyn | 72b6a70 | 2018-11-13 09:48:32 -0800 | [diff] [blame] | 92 | FOR %%i IN (%pubspec_yaml_path%) DO SET pubspec_yaml_timestamp=%%~ti |
| 93 | FOR %%i IN (%pubspec_lock_path%) DO SET pubspec_lock_timestamp=%%~ti |
| 94 | IF "%pubspec_yaml_timestamp%" == "%pubspec_lock_timestamp%" SET newer_file="" |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 95 | IF "%newer_file%" EQU "pubspec.yaml" GOTO do_snapshot |
| 96 | |
| 97 | REM Everything is uptodate - exit subroutine |
| 98 | EXIT /B |
| 99 | |
| 100 | :do_sdk_update_and_snapshot |
| 101 | ECHO Checking Dart SDK version... |
| 102 | SET update_dart_bin=%FLUTTER_ROOT%/bin/internal/update_dart_sdk.ps1 |
| 103 | REM Escape apostrophes from the executable path |
| 104 | SET "update_dart_bin=!update_dart_bin:'=''!" |
Michael Goderbauer | 91dde90 | 2018-08-28 09:37:34 -0700 | [diff] [blame] | 105 | PowerShell.exe -ExecutionPolicy Bypass -Command "Unblock-File -Path '%update_dart_bin%'; & '%update_dart_bin%'" |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 106 | IF "%ERRORLEVEL%" NEQ "0" ( |
| 107 | ECHO Error: Unable to update Dart SDK. Retrying... |
| 108 | timeout /t 5 /nobreak |
| 109 | GOTO :do_sdk_update_and_snapshot |
| 110 | ) |
| 111 | |
| 112 | :do_snapshot |
| 113 | IF EXIST "%FLUTTER_ROOT%\version" DEL "%FLUTTER_ROOT%\version" |
| 114 | ECHO: > "%cache_dir%\.dartignore" |
| 115 | ECHO Building flutter tool... |
| 116 | PUSHD "%flutter_tools_dir%" |
| 117 | |
| 118 | REM Makes changes to PUB_ENVIRONMENT only visible to commands within SETLOCAL/ENDLOCAL |
| 119 | SETLOCAL |
| 120 | SET VERBOSITY=--verbosity=error |
| 121 | IF "%CI%" == "true" GOTO on_bot |
| 122 | IF "%BOT%" == "true" GOTO on_bot |
| 123 | IF "%CONTINUOUS_INTEGRATION%" == "true" GOTO on_bot |
| 124 | IF "%CHROME_HEADLESS%" == "1" GOTO on_bot |
| 125 | GOTO not_on_bot |
| 126 | :on_bot |
| 127 | SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_bot |
| 128 | SET VERBOSITY=--verbosity=normal |
| 129 | :not_on_bot |
| 130 | SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_install |
| 131 | IF "%PUB_CACHE%" == "" ( |
| 132 | IF EXIST "%pub_cache_path%" SET PUB_CACHE=%pub_cache_path% |
| 133 | ) |
| 134 | |
| 135 | SET /A total_tries=10 |
| 136 | SET /A remaining_tries=%total_tries%-1 |
| 137 | :retry_pub_upgrade |
| 138 | ECHO Running pub upgrade... |
Devon Carew | 88fc38c | 2019-01-31 08:31:03 -0800 | [diff] [blame] | 139 | CALL "%pub%" upgrade "%VERBOSITY%" |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 140 | IF "%ERRORLEVEL%" EQU "0" goto :upgrade_succeeded |
Chris Bracken | cccf44f | 2019-02-28 23:53:32 -0800 | [diff] [blame] | 141 | ECHO Error (%ERRORLEVEL%): Unable to 'pub upgrade' flutter tool. Retrying in five seconds... (%remaining_tries% tries left) |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 142 | timeout /t 5 /nobreak 2>NUL |
| 143 | SET /A remaining_tries-=1 |
| 144 | IF "%remaining_tries%" EQU "0" GOTO upgrade_retries_exhausted |
| 145 | GOTO :retry_pub_upgrade |
| 146 | :upgrade_retries_exhausted |
| 147 | SET exit_code=%ERRORLEVEL% |
| 148 | ECHO Error: 'pub upgrade' still failing after %total_tries% tries, giving up. |
| 149 | GOTO final_exit |
| 150 | :upgrade_succeeded |
| 151 | ENDLOCAL |
| 152 | |
| 153 | POPD |
| 154 | |
Jonah Williams | 0a2175f | 2019-03-27 15:24:08 -0700 | [diff] [blame] | 155 | IF "%FLUTTER_TOOL_ARGS%" == "" ( |
Jonah Williams | d4f8a7f | 2019-03-29 13:10:36 -0700 | [diff] [blame] | 156 | "%dart%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%" |
Jonah Williams | 0a2175f | 2019-03-27 15:24:08 -0700 | [diff] [blame] | 157 | ) else ( |
Jonah Williams | d4f8a7f | 2019-03-29 13:10:36 -0700 | [diff] [blame] | 158 | "%dart%" "%FLUTTER_TOOL_ARGS%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%" |
Jonah Williams | 0a2175f | 2019-03-27 15:24:08 -0700 | [diff] [blame] | 159 | ) |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 160 | IF "%ERRORLEVEL%" NEQ "0" ( |
| 161 | ECHO Error: Unable to create dart snapshot for flutter tool. |
| 162 | SET exit_code=%ERRORLEVEL% |
| 163 | GOTO :final_exit |
| 164 | ) |
| 165 | >"%stamp_path%" ECHO %revision% |
| 166 | |
| 167 | REM Exit Subroutine |
| 168 | EXIT /B |
| 169 | |
| 170 | :after_subroutine |
| 171 | |
Michael Goderbauer | 91dde90 | 2018-08-28 09:37:34 -0700 | [diff] [blame] | 172 | REM Chaining the call to 'dart' and 'exit' with an ampersand ensures that |
| 173 | REM Windows reads both commands into memory once before executing them. This |
| 174 | REM avoids nasty errors that may otherwise occure when the dart command (e.g. as |
| 175 | REM part of 'flutter upgrade') modifies this batch script while it is executing. |
| 176 | REM |
| 177 | REM Do not use the CALL command in the next line to execute Dart. CALL causes |
| 178 | REM Windows to re-read the line from disk after the CALL command has finished |
| 179 | REM regardless of the ampersand chain. |
Jonah Williams | fd1291f | 2019-06-15 10:20:19 -0700 | [diff] [blame] | 180 | "%dart%" --packages="%flutter_tools_dir%\.packages" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %* & exit /B !ERRORLEVEL! |
Michael Goderbauer | f17bb51 | 2018-08-15 23:20:13 -0700 | [diff] [blame] | 181 | |
| 182 | :final_exit |
| 183 | EXIT /B %exit_code% |