From b9209577d35c4ad36bcd897b24096559fc2eb4e7 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 7 Nov 2020 21:28:15 +0100 Subject: [PATCH 1/2] [package_info_plus] add missing kIsWeb guard --- packages/package_info_plus/lib/package_info_plus.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/package_info_plus/lib/package_info_plus.dart b/packages/package_info_plus/lib/package_info_plus.dart index de45a20873..a51fdc8040 100644 --- a/packages/package_info_plus/lib/package_info_plus.dart +++ b/packages/package_info_plus/lib/package_info_plus.dart @@ -5,7 +5,7 @@ import 'dart:async'; import 'dart:io' show Platform; -import 'package:flutter/foundation.dart' show visibleForTesting; +import 'package:flutter/foundation.dart' show kIsWeb, visibleForTesting; import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart'; import 'package:package_info_plus_windows/package_info_plus_windows.dart'; @@ -40,7 +40,7 @@ class PackageInfo { // of dart plugins is implemented. // See https://github.com/flutter/flutter/issues/52267 for more details. static PackageInfoPlatform get _platform { - __platform ??= Platform.isWindows && !_disablePlatformOverride + __platform ??= !kIsWeb && Platform.isWindows && !_disablePlatformOverride ? PackageInfoWindows() : PackageInfoPlatform.instance; return __platform; From 3de01ec6c75fb01bc7d28f62bdc782fde919ae6f Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 7 Nov 2020 21:30:25 +0100 Subject: [PATCH 2/2] [package_info_plus_windows] move FFI behind a conditional import Same as for path_provider_windows: https://github.com/flutter/plugins/commit/25957033132ac17094e82bec58e429fac073a483 Moves the real implementation of package_info_plus_windows behind a conditional export, instead exporting a stub on platforms that don't support dart:ffi. This avoids build breakage in web projects that have transitive dependencies on package_info_plus (and thus package_info_plus__windows due to manual endorsement). This will no longer be necessary once https://github.com/flutter/flutter/issues/52267 is fixed, since only Windows builds will ever need to have code-level dependency on package_info_plus_windows. --- .../lib/package_info_plus_windows.dart | 41 +++---------------- .../src/package_info_plus_windows_real.dart | 36 ++++++++++++++++ .../src/package_info_plus_windows_stub.dart | 21 ++++++++++ 3 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 packages/package_info_plus_windows/lib/src/package_info_plus_windows_real.dart create mode 100644 packages/package_info_plus_windows/lib/src/package_info_plus_windows_stub.dart diff --git a/packages/package_info_plus_windows/lib/package_info_plus_windows.dart b/packages/package_info_plus_windows/lib/package_info_plus_windows.dart index 666f9cbc4b..e31a0ec651 100644 --- a/packages/package_info_plus_windows/lib/package_info_plus_windows.dart +++ b/packages/package_info_plus_windows/lib/package_info_plus_windows.dart @@ -1,36 +1,5 @@ -/// The Windows implementation of `package_info_plus`. -library package_info_plus_windows; - -import 'dart:io'; -import 'dart:ffi'; - -import 'package:ffi/ffi.dart'; -import 'package:package_info_plus_platform_interface/package_info_data.dart'; -import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart'; -import 'package:win32/win32.dart'; - -part 'src/file_version_info.dart'; - -/// The Windows implementation of [PackageInfoPlatform]. -class PackageInfoWindows extends PackageInfoPlatform { - /// Returns a map with the following keys: - /// appName, packageName, version, buildNumber - @override - Future getAll() { - final info = _FileVersionInfo(Platform.resolvedExecutable); - final versions = info.productVersion.split('+'); - final data = PackageInfoData( - appName: info.productName, - packageName: info.internalName, - version: versions.getOrNull(0), - buildNumber: versions.getOrNull(1), - ); - info.dispose(); - return Future.value(data); - } -} - -extension _GetOrNull on List { - T getOrNull(int index) => _checkIndex(index) ? this[index] : null; - bool _checkIndex(int index) => index >= 0 && index < length; -} +// package_info_plus_windows is implemented using FFI; export a stub for +// platforms that don't support FFI (e.g., web) to avoid having transitive +// dependencies break web compilation. +export 'src/package_info_plus_windows_stub.dart' + if (dart.library.ffi) 'src/package_info_plus_windows_real.dart'; diff --git a/packages/package_info_plus_windows/lib/src/package_info_plus_windows_real.dart b/packages/package_info_plus_windows/lib/src/package_info_plus_windows_real.dart new file mode 100644 index 0000000000..67957762c3 --- /dev/null +++ b/packages/package_info_plus_windows/lib/src/package_info_plus_windows_real.dart @@ -0,0 +1,36 @@ +/// The Windows implementation of `package_info_plus`. +library package_info_plus_windows; + +import 'dart:io'; +import 'dart:ffi'; + +import 'package:ffi/ffi.dart'; +import 'package:package_info_plus_platform_interface/package_info_data.dart'; +import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart'; +import 'package:win32/win32.dart'; + +part 'file_version_info.dart'; + +/// The Windows implementation of [PackageInfoPlatform]. +class PackageInfoWindows extends PackageInfoPlatform { + /// Returns a map with the following keys: + /// appName, packageName, version, buildNumber + @override + Future getAll() { + final info = _FileVersionInfo(Platform.resolvedExecutable); + final versions = info.productVersion.split('+'); + final data = PackageInfoData( + appName: info.productName, + packageName: info.internalName, + version: versions.getOrNull(0), + buildNumber: versions.getOrNull(1), + ); + info.dispose(); + return Future.value(data); + } +} + +extension _GetOrNull on List { + T getOrNull(int index) => _checkIndex(index) ? this[index] : null; + bool _checkIndex(int index) => index >= 0 && index < length; +} diff --git a/packages/package_info_plus_windows/lib/src/package_info_plus_windows_stub.dart b/packages/package_info_plus_windows/lib/src/package_info_plus_windows_stub.dart new file mode 100644 index 0000000000..349c386f50 --- /dev/null +++ b/packages/package_info_plus_windows/lib/src/package_info_plus_windows_stub.dart @@ -0,0 +1,21 @@ +import 'package:package_info_plus_platform_interface/package_info_data.dart'; +import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart'; + +/// A stub implementation to satisfy compilation of multi-platform packages that +/// depend on package_info_plus_windows. This should never actually be created. +/// +/// Notably, because package_info_plus needs to manually register +/// package_info_plus_windows, anything with a transitive dependency on +/// package_info_plus will also depend on package_info_plus_windows, not just at +/// the pubspec level but the code level. +class PackageInfoWindows extends PackageInfoPlatform { + /// Errors on attempted instantiation of the stub. It exists only to satisfy + /// compile-time dependencies, and should never actually be created. + PackageInfoWindows() { + assert(false); + } + + /// Stub + @override + Future getAll() => null; +}