blob: b53961c2b5680c17d0cc07020fc2596114ac76f9 [file] [log] [blame]
Michael Goderbauerc234d402017-01-27 23:12:33 -08001@ECHO off
2REM Copyright 2017 The Chromium Authors. All rights reserved.
3REM Use of this source code is governed by a BSD-style license that can be
4REM found in the LICENSE file.
5
Michael Goderbauerf17bb512018-08-15 23:20:13 -07006
7REM ---------------------------------- NOTE ----------------------------------
8REM
9REM Please keep the logic in this file consistent with the logic in the
10REM `flutter` script in the same directory to ensure that Flutter continues to
11REM work across all platforms!
12REM
13REM --------------------------------------------------------------------------
Michael Goderbauerc234d402017-01-27 23:12:33 -080014
15SETLOCAL ENABLEDELAYEDEXPANSION
16
17FOR %%i IN ("%~dp0..") DO SET FLUTTER_ROOT=%%~fi
Michael Goderbauerc234d402017-01-27 23:12:33 -080018
Michael Goderbauerf17bb512018-08-15 23:20:13 -070019SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools
20SET cache_dir=%FLUTTER_ROOT%\bin\cache
21SET snapshot_path=%cache_dir%\flutter_tools.snapshot
22SET stamp_path=%cache_dir%\flutter_tools.stamp
23SET script_path=%flutter_tools_dir%\bin\flutter_tools.dart
24SET dart_sdk_path=%cache_dir%\dart-sdk
25SET engine_stamp=%cache_dir%\engine-dart-sdk.stamp
26SET engine_version_path=%FLUTTER_ROOT%\bin\internal\engine.version
27SET pub_cache_path=%FLUTTER_ROOT%\.pub-cache
28
29SET dart=%dart_sdk_path%\bin\dart.exe
30SET pub=%dart_sdk_path%\bin\pub.bat
31
32REM If available, add location of bundled mingit to PATH
33SET mingit_path=%FLUTTER_ROOT%\bin\mingit\cmd
34IF EXIST "%mingit_path%" SET PATH=%PATH%;%mingit_path%
35
36REM Test if Git is available on the Host
37where /q git || ECHO Error: Unable to find git in your PATH. && EXIT /B 1
38REM Test if the flutter directory is a git clone, otherwise git rev-parse HEAD would fail
39IF 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 Williams1e127002019-02-05 15:16:11 -080043 ECHO git clone -b stable https://github.com/flutter/flutter.git
Michael Goderbauerf17bb512018-08-15 23:20:13 -070044 EXIT /B 1
45)
46
47REM Ensure that bin/cache exists.
Michael Goderbauere01470a2017-02-07 15:02:10 -080048IF NOT EXIST "%cache_dir%" MKDIR "%cache_dir%"
49
Michael Goderbauerf17bb512018-08-15 23:20:13 -070050REM If the cache still doesn't exist, fail with an error that we probably don't have permissions.
51IF 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
62REM To debug the tool, you can uncomment the following lines to enable checked mode and set an observatory port:
63REM SET FLUTTER_TOOL_ARGS="--checked %FLUTTER_TOOL_ARGS%"
64REM SET FLUTTER_TOOL_ARGS="%FLUTTER_TOOL_ARGS% --observe=65432"
65
66:acquire_lock
672>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)
71GOTO :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 Goderbauerf17bb512018-08-15 23:20:13 -070089 SET pubspec_yaml_path=%flutter_tools_dir%\pubspec.yaml
90 SET pubspec_lock_path=%flutter_tools_dir%\pubspec.lock
Michael Goderbauerf17bb512018-08-15 23:20:13 -070091 FOR /F %%i IN ('DIR /B /O:D "%pubspec_yaml_path%" "%pubspec_lock_path%"') DO SET newer_file=%%i
Michael Klimushyn72b6a702018-11-13 09:48:32 -080092 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 Goderbauerf17bb512018-08-15 23:20:13 -070095 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 Goderbauer91dde902018-08-28 09:37:34 -0700105 PowerShell.exe -ExecutionPolicy Bypass -Command "Unblock-File -Path '%update_dart_bin%'; & '%update_dart_bin%'"
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700106 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 Carew88fc38c2019-01-31 08:31:03 -0800139 CALL "%pub%" upgrade "%VERBOSITY%"
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700140 IF "%ERRORLEVEL%" EQU "0" goto :upgrade_succeeded
Chris Brackencccf44f2019-02-28 23:53:32 -0800141 ECHO Error (%ERRORLEVEL%): Unable to 'pub upgrade' flutter tool. Retrying in five seconds... (%remaining_tries% tries left)
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700142 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 Williams0a2175f2019-03-27 15:24:08 -0700155 IF "%FLUTTER_TOOL_ARGS%" == "" (
Jonah Williamsd4f8a7f2019-03-29 13:10:36 -0700156 "%dart%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
Jonah Williams0a2175f2019-03-27 15:24:08 -0700157 ) else (
Jonah Williamsd4f8a7f2019-03-29 13:10:36 -0700158 "%dart%" "%FLUTTER_TOOL_ARGS%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
Jonah Williams0a2175f2019-03-27 15:24:08 -0700159 )
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700160 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 Goderbauer91dde902018-08-28 09:37:34 -0700172REM Chaining the call to 'dart' and 'exit' with an ampersand ensures that
173REM Windows reads both commands into memory once before executing them. This
174REM avoids nasty errors that may otherwise occure when the dart command (e.g. as
175REM part of 'flutter upgrade') modifies this batch script while it is executing.
176REM
177REM Do not use the CALL command in the next line to execute Dart. CALL causes
178REM Windows to re-read the line from disk after the CALL command has finished
179REM regardless of the ampersand chain.
Jonah Williamsfd1291f2019-06-15 10:20:19 -0700180"%dart%" --packages="%flutter_tools_dir%\.packages" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %* & exit /B !ERRORLEVEL!
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700181
182:final_exit
183EXIT /B %exit_code%