|
| 1 | +// Copyright 2013 The Flutter Authors |
| 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 'package:hooks/hooks.dart'; |
| 6 | + |
| 7 | +/// The key under which Flutter-specific configuration is stored inside the hook |
| 8 | +/// config's `extensions` map. |
| 9 | +const String _flutterExtensionKey = 'flutter'; |
| 10 | + |
| 11 | +const String _impellercKey = 'impellerc'; |
| 12 | +const String _libtessellatorKey = 'libtessellator'; |
| 13 | + |
| 14 | +/// Flutter-specific configuration supplied to a build or link hook. |
| 15 | +/// |
| 16 | +/// Obtained via [HookConfigFlutterConfig.flutter]. Only available when the hook |
| 17 | +/// is invoked by a Flutter SDK that supports this extension; check |
| 18 | +/// [HookConfigFlutterConfig.buildForFlutter] before accessing it. |
| 19 | +final class FlutterConfig { |
| 20 | + FlutterConfig._(this._json); |
| 21 | + |
| 22 | + final Map<String, Object?> _json; |
| 23 | + |
| 24 | + /// The absolute path to the `impellerc` offline shader compiler that ships |
| 25 | + /// with the engine artifacts of the invoking Flutter SDK. |
| 26 | + /// |
| 27 | + /// When the SDK is invoked with `--local-engine`, this points at the locally |
| 28 | + /// built `impellerc`, matching the engine the app is being built against. |
| 29 | + Uri get impellerc => _filePath(_impellercKey); |
| 30 | + |
| 31 | + /// The absolute path to the `libtessellator` dynamic library that ships with |
| 32 | + /// the engine artifacts of the invoking Flutter SDK. |
| 33 | + /// |
| 34 | + /// When the SDK is invoked with `--local-engine`, this points at the locally |
| 35 | + /// built `libtessellator`. |
| 36 | + Uri get libtessellator => _filePath(_libtessellatorKey); |
| 37 | + |
| 38 | + Uri _filePath(String key) { |
| 39 | + final Object? value = _json[key]; |
| 40 | + if (value is! String) { |
| 41 | + throw FormatException( |
| 42 | + "Expected a String at 'config.extensions.$_flutterExtensionKey.$key' " |
| 43 | + 'in the hook input, got: $value', |
| 44 | + ); |
| 45 | + } |
| 46 | + return Uri.file(value); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Extension on [HookConfig] providing access to Flutter-specific |
| 51 | +/// configuration. |
| 52 | +extension HookConfigFlutterConfig on HookConfig { |
| 53 | + /// Whether this hook is being invoked as part of a Flutter build. |
| 54 | + /// |
| 55 | + /// When `false`, the hook is being invoked by a plain `dart` build, or by a |
| 56 | + /// Flutter SDK that predates the `flutter_hook_config` extension, and |
| 57 | + /// [flutter] must not be accessed. Hooks that need Flutter-supplied |
| 58 | + /// configuration should fall back to their own discovery logic in that case. |
| 59 | + bool get buildForFlutter => _flutterExtensionJson != null; |
| 60 | + |
| 61 | + /// The Flutter-specific configuration for this hook invocation. |
| 62 | + /// |
| 63 | + /// Only valid when [buildForFlutter] is `true`; throws a [StateError] |
| 64 | + /// otherwise. |
| 65 | + FlutterConfig get flutter { |
| 66 | + final Map<String, Object?>? extensionJson = _flutterExtensionJson; |
| 67 | + if (extensionJson == null) { |
| 68 | + throw StateError( |
| 69 | + 'No Flutter-specific hook configuration is available. The hook is not ' |
| 70 | + 'being invoked by a Flutter SDK, or the Flutter SDK predates ' |
| 71 | + 'flutter_hook_config support. Check `config.buildForFlutter` first.', |
| 72 | + ); |
| 73 | + } |
| 74 | + return FlutterConfig._(extensionJson); |
| 75 | + } |
| 76 | + |
| 77 | + Map<String, Object?>? get _flutterExtensionJson { |
| 78 | + final Object? extensions = json['extensions']; |
| 79 | + if (extensions is! Map<String, Object?>) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + final Object? flutterJson = extensions[_flutterExtensionKey]; |
| 83 | + return flutterJson is Map<String, Object?> ? flutterJson : null; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Extension on [HookConfigBuilder] for writing Flutter-specific configuration |
| 88 | +/// onto a hook input. |
| 89 | +/// |
| 90 | +/// This is intended for use by the Flutter SDK (`flutter_tools`) via |
| 91 | +/// [FlutterExtension]; ordinary hook authors do not call this directly. |
| 92 | +extension HookConfigBuilderFlutterConfig on HookConfigBuilder { |
| 93 | + /// Records the Flutter-specific configuration on a build or link hook input. |
| 94 | + /// |
| 95 | + /// [impellerc] and [libtessellator] must be absolute file URIs. |
| 96 | + void setupFlutter({required Uri impellerc, required Uri libtessellator}) { |
| 97 | + assert(impellerc.isAbsolute, 'impellerc must be an absolute URI'); |
| 98 | + assert(libtessellator.isAbsolute, 'libtessellator must be an absolute URI'); |
| 99 | + final extensions = |
| 100 | + (json['extensions'] ??= <String, Object?>{}) as Map<String, Object?>; |
| 101 | + extensions[_flutterExtensionKey] = <String, Object?>{ |
| 102 | + _impellercKey: impellerc.toFilePath(), |
| 103 | + _libtessellatorKey: libtessellator.toFilePath(), |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
0 commit comments