flutter.bat 5.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@ECHO off
REM Copyright 2017 The Chromium Authors. All rights reserved.
REM Use of this source code is governed by a BSD-style license that can be
REM found in the LICENSE file.


REM ---------------------------------- NOTE ----------------------------------
REM
REM Please keep the logic in this file consistent with the logic in the
REM `flutter` script in the same directory to ensure that Flutter continues to
REM work across all platforms!
REM
REM --------------------------------------------------------------------------

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%i IN ("%~dp0..") DO SET FLUTTER_ROOT=%%~fi

SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools
20
SET cache_dir=%FLUTTER_ROOT%\bin\cache
21 22
SET snapshot_path=%cache_dir%\flutter_tools.snapshot
SET stamp_path=%cache_dir%\flutter_tools.stamp
23
SET script_path=%flutter_tools_dir%\bin\flutter_tools.dart
24 25 26
SET dart_sdk_path=%cache_dir%\dart-sdk
SET dart_stamp_path=%cache_dir%\dart-sdk.stamp
SET dart_version_path=%FLUTTER_ROOT%\bin\internal\dart-sdk.version
27
SET pub_cache_path=%FLUTTER_ROOT%\.pub-cache
28 29 30 31

SET dart=%dart_sdk_path%\bin\dart.exe
SET pub=%dart_sdk_path%\bin\pub.bat

32 33 34 35
REM If available, add location of bundled mingit to PATH
SET mingit_path=%FLUTTER_ROOT%\bin\mingit\cmd
IF EXIST "%mingit_path%" SET PATH=%PATH%;%mingit_path%

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 39 40
REM  Test if the flutter directory is a git clone, otherwise git rev-parse HEAD would fail
IF NOT EXIST "%flutter_root%\.git" (
  ECHO Error: The Flutter directory is not a clone of the GitHub project.
41
  EXIT /B 1
42 43
)

44 45 46
REM Ensure that bin/cache exists.
IF NOT EXIST "%cache_dir%" MKDIR "%cache_dir%"

47 48
REM To debug the tool, you can uncomment the following lines to enable checked mode and set an observatory port:
REM SET FLUTTER_TOOL_ARGS="--checked %FLUTTER_TOOL_ARGS%"
49
REM SET FLUTTER_TOOL_ARGS="%FLUTTER_TOOL_ARGS% --observe=65432"
50

51 52 53 54 55 56 57 58 59 60
:acquire_lock
2>NUL (
  REM "3" is now stderr because of "2>NUL".
  CALL :subroutine %* 2>&3 9> "%cache_dir%\flutter.bat.lock" || GOTO acquire_lock
)
GOTO :after_subroutine

:subroutine
  PUSHD "%flutter_root%"
  FOR /f %%r IN ('git rev-parse HEAD') DO SET revision=%%r
61 62
  POPD

63 64
  REM The following IF conditions are all linked with a logical OR. However,
  REM there is no OR operator in batch and a GOTO construct is used as replacement.
65 66 67 68 69

  IF NOT EXIST "%dart_stamp_path%" GOTO do_sdk_update_and_snapshot
  SET /P dart_required_version=<"%dart_version_path%"
  SET /P dart_installed_version=<"%dart_stamp_path%"
  IF !dart_required_version! NEQ !dart_installed_version! GOTO do_sdk_update_and_snapshot
70 71
  IF NOT EXIST "%snapshot_path%" GOTO do_snapshot
  IF NOT EXIST "%stamp_path%" GOTO do_snapshot
72
  SET /P stamp_value=<"%stamp_path%"
73
  IF !stamp_value! NEQ !revision! GOTO do_snapshot
74 75 76 77 78 79 80 81
  REM Compare "last modified" timestamps
  SET pubspec_yaml_path=%flutter_tools_dir%\pubspec.yaml
  SET pubspec_lock_path=%flutter_tools_dir%\pubspec.lock
  FOR %%i IN ("%pubspec_yaml_path%") DO SET yaml_timestamp=%%~ti
  FOR %%i IN ("%pubspec_lock_path%") DO SET lock_timestamp=%%~ti
  IF !yaml_timestamp! EQU !lock_timestamp! GOTO do_snapshot
  FOR /F %%i IN ('DIR /B /O:D "%pubspec_yaml_path%" "%pubspec_lock_path%"') DO SET newer_file=%%i
  IF "%newer_file%" EQU "pubspec.yaml" GOTO do_snapshot
82 83 84 85

  REM Everything is uptodate - exit subroutine
  EXIT /B

86
  :do_sdk_update_and_snapshot
87 88
    ECHO Checking Dart SDK version...
    CALL PowerShell.exe -ExecutionPolicy Bypass -Command "& '%FLUTTER_ROOT%/bin/internal/update_dart_sdk.ps1'"
89 90 91
    IF "%ERRORLEVEL%" NEQ "0" (
      ECHO Error: Unable to update Dart SDK. Retrying...
      timeout /t 5 /nobreak
92
      GOTO :do_sdk_update_and_snapshot
93
    )
94

95
  :do_snapshot
96
    IF EXIST "%FLUTTER_ROOT%\version" DEL "%FLUTTER_ROOT%\version"
97
    ECHO: > "%cache_dir%\.dartignore"
98 99
    ECHO Updating flutter tool...
    PUSHD "%flutter_tools_dir%"
100 101 102

    REM Makes changes to PUB_ENVIRONMENT only visible to commands within SETLOCAL/ENDLOCAL
    SETLOCAL
103 104 105 106 107 108 109 110 111 112 113
      IF "%TRAVIS%" == "true" GOTO on_bot
      IF "%BOT%" == "true" GOTO on_bot
      IF "%CONTINUOUS_INTEGRATION%" == "true" GOTO on_bot
      IF "%CHROME_HEADLESS%" == "1" GOTO on_bot
      IF "%APPVEYOR%" == "true" GOTO on_bot
      IF "%CI%" == "true" GOTO on_bot
      GOTO not_on_bot
      :on_bot
        SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_bot
      :not_on_bot
      SET PUB_ENVIRONMENT=%PUB_ENVIRONMENT%:flutter_install
114 115 116
      IF "%PUB_CACHE%" == "" (
       IF EXIST "%pub_cache_path%" SET PUB_CACHE=%pub_cache_path%
      )
117 118 119
      :retry_pub_upgrade
      CALL "%pub%" upgrade --verbosity=error --no-packages-dir
      IF "%ERRORLEVEL%" NEQ "0" (
120
        ECHO Error: Unable to 'pub upgrade' flutter tool. Retrying in five seconds...
121 122 123
        timeout /t 5 /nobreak
        GOTO :retry_pub_upgrade
      )
124 125
    ENDLOCAL

126
    POPD
127 128

    :retry_dart_snapshot
129
    CALL "%dart%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
130 131 132 133 134
    IF "%ERRORLEVEL%" NEQ "0" (
      ECHO Error: Unable to create dart snapshot for flutter tool. Retrying...
      timeout /t 5 /nobreak
      GOTO :retry_dart_snapshot
    )
135 136 137 138 139 140
    >"%stamp_path%" ECHO %revision%

  REM Exit Subroutine
  EXIT /B

:after_subroutine
141

142
CALL "%dart%" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %*
143
SET exit_code=%ERRORLEVEL%
144 145

REM The VM exits with code 253 if the snapshot version is out-of-date.
146
IF "%exit_code%" EQU "253" (
147
  CALL "%dart%" --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
148 149 150 151 152
  SET exit_code=%ERRORLEVEL%
  IF "%exit_code%" EQU "253" (
    ECHO Error: Unable to create dart snapshot for flutter tool.
    EXIT /B %exit_code%
  )
153 154
  CALL "%dart%" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %*
  SET exit_code=%ERRORLEVEL%
155
)
156

157
EXIT /B %exit_code%