| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:async'; |
| 6 | |
| 7 | import 'package:flutter_tools/src/build_info.dart'; |
| 8 | import 'package:flutter_tools/src/build_system/build_system.dart'; |
| 9 | |
| 10 | class TestBuildSystem implements BuildSystem { |
| 11 | /// Create a [BuildSystem] instance that returns the provided results in order. |
| 12 | TestBuildSystem.list(this._results, [this._onRun]) : _exception = null, _singleResult = null; |
| 13 | |
| 14 | /// Create a [BuildSystem] instance that returns the provided result for every build |
| 15 | /// and buildIncremental request. |
| 16 | TestBuildSystem.all(this._singleResult, [this._onRun]) |
| 17 | : _exception = null, |
| 18 | _results = <BuildResult>[]; |
| 19 | |
| 20 | /// Create a [BuildSystem] instance that always throws the provided error for every build |
| 21 | /// and buildIncremental request. |
| 22 | TestBuildSystem.error(this._exception) |
| 23 | : _singleResult = null, |
| 24 | _results = <BuildResult>[], |
| 25 | _onRun = null; |
| 26 | |
| 27 | final List<BuildResult> _results; |
| 28 | final BuildResult? _singleResult; |
| 29 | final Exception? _exception; |
| 30 | final void Function(Target target, Environment environment)? _onRun; |
| 31 | var _nextResult = 0; |
| 32 | |
| 33 | @override |
| 34 | Future<BuildResult> build( |
| 35 | Target target, |
| 36 | Environment environment, { |
| 37 | BuildSystemConfig buildSystemConfig = const BuildSystemConfig(), |
| 38 | }) async { |
| 39 | if (_onRun != null) { |
| 40 | _onRun.call(target, environment); |
| 41 | } |
| 42 | if (_exception != null) { |
| 43 | throw _exception; |
| 44 | } |
| 45 | if (_singleResult != null) { |
| 46 | return _singleResult; |
| 47 | } |
| 48 | if (_nextResult >= _results.length) { |
| 49 | throw StateError('Unexpected build request of ${target.name}' ); |
| 50 | } |
| 51 | return _results[_nextResult++]; |
| 52 | } |
| 53 | |
| 54 | @override |
| 55 | Future<BuildResult> buildIncremental( |
| 56 | Target target, |
| 57 | Environment environment, |
| 58 | BuildResult? previousBuild, |
| 59 | ) async { |
| 60 | if (_onRun != null) { |
| 61 | _onRun.call(target, environment); |
| 62 | } |
| 63 | if (_exception != null) { |
| 64 | throw _exception; |
| 65 | } |
| 66 | if (_singleResult != null) { |
| 67 | return _singleResult; |
| 68 | } |
| 69 | if (_nextResult >= _results.length) { |
| 70 | throw StateError('Unexpected buildIncremental request of ${target.name}' ); |
| 71 | } |
| 72 | return _results[_nextResult++]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Encodes a map of key-value pairs into a comma-separated base64 encoded string. |
| 77 | /// |
| 78 | /// ## Example |
| 79 | /// |
| 80 | /// ```dart |
| 81 | /// print(encodeDartDefines({'FLUTTER_WEB': 'true', 'FLUTTER_WEB_CANVASKIT_URL': 'https://example.com'})); |
| 82 | /// // RkxVVFRFUl9XRUI9dHJ1ZQo=,RkxVVFRFUl9XRUJfQ0FOVkFTS0lUX1VSTD1odHRwczovL2V4YW1wbGUuY29t |
| 83 | /// ``` |
| 84 | String encodeDartDefinesMap(Map<String, String> defines) { |
| 85 | final flattened = <String>[ |
| 86 | for (final MapEntry<String, String> entry in defines.entries) ' ${entry.key}= ${entry.value}' , |
| 87 | ]; |
| 88 | return encodeDartDefines(flattened); |
| 89 | } |
| 90 | |