blob: efe6c9c38f173e2f7f780b2a47b00997d0c87799 [file] [log] [blame]
Michael Goderbauerc234d402017-01-27 23:12:33 -08001@ECHO off
Ian Hickson449f4a62019-11-27 15:04:02 -08002REM Copyright 2014 The Flutter Authors. All rights reserved.
Michael Goderbauerc234d402017-01-27 23:12:33 -08003REM 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 -07006REM ---------------------------------- NOTE ----------------------------------
7REM
8REM Please keep the logic in this file consistent with the logic in the
9REM `flutter` script in the same directory to ensure that Flutter continues to
10REM work across all platforms!
11REM
12REM --------------------------------------------------------------------------
Michael Goderbauerc234d402017-01-27 23:12:33 -080013
14SETLOCAL ENABLEDELAYEDEXPANSION
15
16FOR %%i IN ("%~dp0..") DO SET FLUTTER_ROOT=%%~fi
Michael Goderbauerc234d402017-01-27 23:12:33 -080017
Michael Goderbauerf17bb512018-08-15 23:20:13 -070018SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools
19SET cache_dir=%FLUTTER_ROOT%\bin\cache
20SET snapshot_path=%cache_dir%\flutter_tools.snapshot
21SET stamp_path=%cache_dir%\flutter_tools.stamp
22SET script_path=%flutter_tools_dir%\bin\flutter_tools.dart
23SET dart_sdk_path=%cache_dir%\dart-sdk
24SET engine_stamp=%cache_dir%\engine-dart-sdk.stamp
25SET engine_version_path=%FLUTTER_ROOT%\bin\internal\engine.version
26SET pub_cache_path=%FLUTTER_ROOT%\.pub-cache
27
28SET dart=%dart_sdk_path%\bin\dart.exe
29SET pub=%dart_sdk_path%\bin\pub.bat
30
31REM If available, add location of bundled mingit to PATH
32SET mingit_path=%FLUTTER_ROOT%\bin\mingit\cmd
33IF EXIST "%mingit_path%" SET PATH=%PATH%;%mingit_path%
34
35REM Test if Git is available on the Host
36where /q git || ECHO Error: Unable to find git in your PATH. && EXIT /B 1
37REM Test if the flutter directory is a git clone, otherwise git rev-parse HEAD would fail
38IF NOT EXIST "%flutter_root%\.git" (
39 ECHO Error: The Flutter directory is not a clone of the GitHub project.
40 ECHO The flutter tool requires Git in order to operate properly;
41 ECHO to set up Flutter, run the following command:
Jonah Williams1e127002019-02-05 15:16:11 -080042 ECHO git clone -b stable https://github.com/flutter/flutter.git
Michael Goderbauerf17bb512018-08-15 23:20:13 -070043 EXIT /B 1
44)
45
46REM Ensure that bin/cache exists.
Michael Goderbauere01470a2017-02-07 15:02:10 -080047IF NOT EXIST "%cache_dir%" MKDIR "%cache_dir%"
48
Michael Goderbauerf17bb512018-08-15 23:20:13 -070049REM If the cache still doesn't exist, fail with an error that we probably don't have permissions.
50IF NOT EXIST "%cache_dir%" (
51 ECHO Error: Unable to create cache directory at
52 ECHO %cache_dir%
53 ECHO.
54 ECHO This may be because flutter doesn't have write permissions for
55 ECHO this path. Try moving the flutter directory to a writable location,
56 ECHO such as within your home directory.
57 EXIT /B 1
58)
59
60
61REM To debug the tool, you can uncomment the following lines to enable checked mode and set an observatory port:
Ian Hickson8c6d60b2019-10-02 12:46:51 -070062REM SET FLUTTER_TOOL_ARGS="--enable-asserts %FLUTTER_TOOL_ARGS%"
Michael Goderbauerf17bb512018-08-15 23:20:13 -070063REM SET FLUTTER_TOOL_ARGS="%FLUTTER_TOOL_ARGS% --observe=65432"
64
65:acquire_lock
662>NUL (
67 REM "3" is now stderr because of "2>NUL".
68 CALL :subroutine %* 2>&3 9> "%cache_dir%\flutter.bat.lock" || GOTO acquire_lock
69)
70GOTO :after_subroutine
71
72:subroutine
73 PUSHD "%flutter_root%"
74 FOR /f %%r IN ('git rev-parse HEAD') DO SET revision=%%r
75 POPD
76
77 REM The following IF conditions are all linked with a logical OR. However,
78 REM there is no OR operator in batch and a GOTO construct is used as replacement.
79
80 IF NOT EXIST "%engine_stamp%" GOTO do_sdk_update_and_snapshot
81 SET /P dart_required_version=<"%engine_version_path%"
82 SET /P dart_installed_version=<"%engine_stamp%"
83 IF !dart_required_version! NEQ !dart_installed_version! GOTO do_sdk_update_and_snapshot
84 IF NOT EXIST "%snapshot_path%" GOTO do_snapshot
85 IF NOT EXIST "%stamp_path%" GOTO do_snapshot
86 SET /P stamp_value=<"%stamp_path%"
87 IF !stamp_value! NEQ !revision! GOTO do_snapshot
Michael Goderbauerf17bb512018-08-15 23:20:13 -070088 SET pubspec_yaml_path=%flutter_tools_dir%\pubspec.yaml
89 SET pubspec_lock_path=%flutter_tools_dir%\pubspec.lock
Michael Goderbauerf17bb512018-08-15 23:20:13 -070090 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 -080091 FOR %%i IN (%pubspec_yaml_path%) DO SET pubspec_yaml_timestamp=%%~ti
92 FOR %%i IN (%pubspec_lock_path%) DO SET pubspec_lock_timestamp=%%~ti
93 IF "%pubspec_yaml_timestamp%" == "%pubspec_lock_timestamp%" SET newer_file=""
Michael Goderbauerf17bb512018-08-15 23:20:13 -070094 IF "%newer_file%" EQU "pubspec.yaml" GOTO do_snapshot
95
96 REM Everything is uptodate - exit subroutine
97 EXIT /B
98
99 :do_sdk_update_and_snapshot
100 ECHO Checking Dart SDK version...
101 SET update_dart_bin=%FLUTTER_ROOT%/bin/internal/update_dart_sdk.ps1
102 REM Escape apostrophes from the executable path
103 SET "update_dart_bin=!update_dart_bin:'=''!"
Michael Goderbauer91dde902018-08-28 09:37:34 -0700104 PowerShell.exe -ExecutionPolicy Bypass -Command "Unblock-File -Path '%update_dart_bin%'; & '%update_dart_bin%'"
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700105 IF "%ERRORLEVEL%" NEQ "0" (
106 ECHO Error: Unable to update Dart SDK. Retrying...
107 timeout /t 5 /nobreak
108 GOTO :do_sdk_update_and_snapshot
109 )
110
111 :do_snapshot
112 IF EXIST "%FLUTTER_ROOT%\version" DEL "%FLUTTER_ROOT%\version"
113 ECHO: > "%cache_dir%\.dartignore"
114 ECHO Building flutter tool...
115 PUSHD "%flutter_tools_dir%"
116
117 REM Makes changes to PUB_ENVIRONMENT only visible to commands within SETLOCAL/ENDLOCAL
118 SETLOCAL
119 SET VERBOSITY=--verbosity=error
120 IF "%CI%" == "true" GOTO on_bot
121 IF "%BOT%" == "true" GOTO on_bot
122 IF "%CONTINUOUS_INTEGRATION%" == "true" GOTO on_bot
123 IF "%CHROME_HEADLESS%" == "1" GOTO on_bot
124 GOTO not_on_bot
125 :on_bot
126 SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_bot
127 SET VERBOSITY=--verbosity=normal
128 :not_on_bot
129 SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_install
130 IF "%PUB_CACHE%" == "" (
131 IF EXIST "%pub_cache_path%" SET PUB_CACHE=%pub_cache_path%
132 )
133
134 SET /A total_tries=10
135 SET /A remaining_tries=%total_tries%-1
136 :retry_pub_upgrade
137 ECHO Running pub upgrade...
Jacob MacDonald3f1f22c2019-10-15 13:34:04 -0700138 CALL "%pub%" upgrade "%VERBOSITY%" --no-precompile
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700139 IF "%ERRORLEVEL%" EQU "0" goto :upgrade_succeeded
Chris Brackencccf44f2019-02-28 23:53:32 -0800140 ECHO Error (%ERRORLEVEL%): Unable to 'pub upgrade' flutter tool. Retrying in five seconds... (%remaining_tries% tries left)
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700141 timeout /t 5 /nobreak 2>NUL
142 SET /A remaining_tries-=1
143 IF "%remaining_tries%" EQU "0" GOTO upgrade_retries_exhausted
144 GOTO :retry_pub_upgrade
145 :upgrade_retries_exhausted
146 SET exit_code=%ERRORLEVEL%
147 ECHO Error: 'pub upgrade' still failing after %total_tries% tries, giving up.
148 GOTO final_exit
149 :upgrade_succeeded
150 ENDLOCAL
151
152 POPD
153
Jonah Williams0a2175f2019-03-27 15:24:08 -0700154 IF "%FLUTTER_TOOL_ARGS%" == "" (
Jonah Williamsd4f8a7f2019-03-29 13:10:36 -0700155 "%dart%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
Jonah Williams0a2175f2019-03-27 15:24:08 -0700156 ) else (
Jonah Williamsd4f8a7f2019-03-29 13:10:36 -0700157 "%dart%" "%FLUTTER_TOOL_ARGS%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
Jonah Williams0a2175f2019-03-27 15:24:08 -0700158 )
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700159 IF "%ERRORLEVEL%" NEQ "0" (
160 ECHO Error: Unable to create dart snapshot for flutter tool.
161 SET exit_code=%ERRORLEVEL%
162 GOTO :final_exit
163 )
164 >"%stamp_path%" ECHO %revision%
165
166 REM Exit Subroutine
167 EXIT /B
168
169:after_subroutine
170
Michael Goderbauer91dde902018-08-28 09:37:34 -0700171REM Chaining the call to 'dart' and 'exit' with an ampersand ensures that
172REM Windows reads both commands into memory once before executing them. This
173REM avoids nasty errors that may otherwise occure when the dart command (e.g. as
174REM part of 'flutter upgrade') modifies this batch script while it is executing.
175REM
176REM Do not use the CALL command in the next line to execute Dart. CALL causes
177REM Windows to re-read the line from disk after the CALL command has finished
178REM regardless of the ampersand chain.
Jonah Williamsfd1291f2019-06-15 10:20:19 -0700179"%dart%" --packages="%flutter_tools_dir%\.packages" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %* & exit /B !ERRORLEVEL!
Michael Goderbauerf17bb512018-08-15 23:20:13 -0700180
181:final_exit
182EXIT /B %exit_code%