From 55d808f6966b93587cf46f6260d60280083e999b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 15 Jun 2024 21:33:22 +0800 Subject: [PATCH] Export extensions instead of generating them --- .github/workflows/nitrogen.yaml | 14 +-- nitrogen/lib/nitrogen_extension.dart | 1 - nitrogen/test/src/libraries_test.dart | 2 +- nitrogen_flutter_svg/CHANGELOG.md | 4 + .../lib/nitrogen_flutter_svg.dart | 83 +++++++++----- .../lib/src/svg_extension_generator.dart | 85 --------------- nitrogen_flutter_svg/pubspec.yaml | 14 +-- nitrogen_lottie/CHANGELOG.md | 4 + nitrogen_lottie/lib/nitrogen_lottie.dart | 99 ++++++++++++----- .../lib/src/lottie_extension_generator.dart | 101 ------------------ nitrogen_lottie/pubspec.yaml | 10 +- nitrogen_types/CHANGELOG.md | 4 + nitrogen_types/pubspec.yaml | 2 +- 13 files changed, 154 insertions(+), 269 deletions(-) delete mode 100644 nitrogen/lib/nitrogen_extension.dart delete mode 100644 nitrogen_flutter_svg/lib/src/svg_extension_generator.dart delete mode 100644 nitrogen_lottie/lib/src/lottie_extension_generator.dart diff --git a/.github/workflows/nitrogen.yaml b/.github/workflows/nitrogen.yaml index e0ca554..9b1e685 100644 --- a/.github/workflows/nitrogen.yaml +++ b/.github/workflows/nitrogen.yaml @@ -25,16 +25,10 @@ jobs: working-directory: nitrogen steps: - uses: actions/checkout@v4 - - uses: dart-lang/setup-dart@v1 - - run: dart pub get - - run: dart analyze --fatal-warnings - - run: dart run coverage:test_with_coverage - - run: sudo apt-get update -y - - run: sudo apt-get install -y lcov - - run: lcov --remove ./coverage/lcov.info '**.g.dart' -o ./coverage/lcov.info - - uses: codecov/codecov-action@v4 - with: - files: ./nitrogen/coverage/lcov.info + - uses: subosito/flutter-action@v2.16.0 + - run: flutter pub get + - run: flutter analyze --fatal-warnings + - run: flutter test test-nitrogen-flutter-svg: name: Run nitrogen_flutter_svg tests diff --git a/nitrogen/lib/nitrogen_extension.dart b/nitrogen/lib/nitrogen_extension.dart deleted file mode 100644 index d669235..0000000 --- a/nitrogen/lib/nitrogen_extension.dart +++ /dev/null @@ -1 +0,0 @@ -export 'package:nitrogen/src/libraries.dart'; diff --git a/nitrogen/test/src/libraries_test.dart b/nitrogen/test/src/libraries_test.dart index 7cbf6dc..195830c 100644 --- a/nitrogen/test/src/libraries_test.dart +++ b/nitrogen/test/src/libraries_test.dart @@ -1,5 +1,5 @@ import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/nitrogen_extension.dart'; +import 'package:nitrogen/src/libraries.dart'; import 'package:test/test.dart'; void main() { diff --git a/nitrogen_flutter_svg/CHANGELOG.md b/nitrogen_flutter_svg/CHANGELOG.md index d13893b..1a2a33a 100644 --- a/nitrogen_flutter_svg/CHANGELOG.md +++ b/nitrogen_flutter_svg/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.0 + +Export instead of generate extension. + ## 0.2.0+1 * Add dependency on Flutter SDK diff --git a/nitrogen_flutter_svg/lib/nitrogen_flutter_svg.dart b/nitrogen_flutter_svg/lib/nitrogen_flutter_svg.dart index 948f294..e3fdf71 100644 --- a/nitrogen_flutter_svg/lib/nitrogen_flutter_svg.dart +++ b/nitrogen_flutter_svg/lib/nitrogen_flutter_svg.dart @@ -1,28 +1,59 @@ -import 'dart:async'; - -import 'package:build/build.dart'; -import 'package:nitrogen_flutter_svg/src/svg_extension_generator.dart'; -import 'package:path/path.dart'; - -/// Creates a [NitrogenFlutterSvgBuilder]. -Builder nitrogenFlutterSvgBuilder(BuilderOptions options) => NitrogenFlutterSvgBuilder(); - -/// A Nitrogen builder. -class NitrogenFlutterSvgBuilder extends Builder { - - @override - FutureOr build(BuildStep buildStep) async { - await buildStep.writeAsString( - AssetId(buildStep.inputId.package, join('lib', 'src', 'svg_extension.nitrogen.dart')), - SvgExtensionGenerator().generate(), - ); - } - - @override - Map> get buildExtensions => { - r'$package$': [ - 'lib/src/svg_extension.nitrogen.dart', - ], - }; +import 'package:flutter/material.dart'; +import 'package:flutter_svg/svg.dart'; +import 'package:nitrogen_types/nitrogen_types.dart'; +/// Provides functions for converting an [SvgAsset] to a [SvgPicture]. +/// +/// ```dart +/// class Foo extends StatelessWidget { +/// final SvgAsset asset; +/// +/// @override +/// Widget build(BuildContext context) => Container( +/// child: asset( +/// width: 100, +/// height: 100, +/// ), +/// ); +/// } +/// ``` +extension SvgAssetExtension on SvgAsset { + /// Converts an [SvgAsset] to an [SvgPicture]. + SvgPicture call({ + Key? key, + bool matchTextDirection = false, + double? width, + double? height, + BoxFit fit = BoxFit.contain, + AlignmentGeometry alignment = Alignment.center, + bool allowDrawingOutsideViewBox = false, + WidgetBuilder? placeholderBuilder, + String? semanticsLabel, + bool excludeFromSemantics = false, + Clip clipBehavior = Clip.hardEdge, + SvgTheme theme = const SvgTheme(), + ColorFilter? colorFilter, + @Deprecated('Parameter is deprecated in flutter_svg') Color? color, + @Deprecated('Parameter is deprecated in flutter_svg') BlendMode colorBlendMode = BlendMode.srcIn, + @Deprecated('Parameter is deprecated in flutter_svg') bool cacheColorFilter = false, + }) => SvgPicture.asset( + path, + key: key, + matchTextDirection: matchTextDirection, + package: package, + width: width, + height: height, + fit: fit, + alignment: alignment, + allowDrawingOutsideViewBox: allowDrawingOutsideViewBox, + placeholderBuilder: placeholderBuilder, + semanticsLabel: semanticsLabel, + excludeFromSemantics: excludeFromSemantics, + clipBehavior: clipBehavior, + theme: theme, + colorFilter: colorFilter, + color: color, + colorBlendMode: colorBlendMode, + cacheColorFilter: cacheColorFilter, + ); } diff --git a/nitrogen_flutter_svg/lib/src/svg_extension_generator.dart b/nitrogen_flutter_svg/lib/src/svg_extension_generator.dart deleted file mode 100644 index f20ecf0..0000000 --- a/nitrogen_flutter_svg/lib/src/svg_extension_generator.dart +++ /dev/null @@ -1,85 +0,0 @@ -import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/nitrogen_extension.dart'; - -/// A generator for extensions SVG assets. -class SvgExtensionGenerator { - - /// The imports. - static final imports = [ - Directive.import('package:flutter/widgets.dart'), - Directive.import('package:flutter_svg/svg.dart'), - Libraries.importNitrogenTypes, - ]; - - /// The extension. - static const extension = Code(''' -/// Tested against flutter_svg version: 2.0.10+1 -/// -/// Example: -/// ```dart -/// class Foo extends StatelessWidget { -/// final SvgAsset asset; -/// -/// @override -/// Widget build(BuildContext context) => Container( -/// child: asset( -/// width: 100, -/// height: 100, -/// ), -/// ); -/// } -/// ``` -extension SvgAssetExtension on SvgAsset { - - SvgPicture call({ - Key? key, - bool matchTextDirection = false, - double? width, - double? height, - BoxFit fit = BoxFit.contain, - AlignmentGeometry alignment = Alignment.center, - bool allowDrawingOutsideViewBox = false, - WidgetBuilder? placeholderBuilder, - String? semanticsLabel, - bool excludeFromSemantics = false, - Clip clipBehavior = Clip.hardEdge, - SvgTheme theme = const SvgTheme(), - ColorFilter? colorFilter, - @Deprecated('Parameter is deprecated in flutter_svg') Color? color, - @Deprecated('Parameter is deprecated in flutter_svg') BlendMode colorBlendMode = BlendMode.srcIn, - @Deprecated('Parameter is deprecated in flutter_svg') bool cacheColorFilter = false, - }) => SvgPicture.asset( - path, - key: key, - matchTextDirection: matchTextDirection, - package: package, - width: width, - height: height, - fit: fit, - alignment: alignment, - allowDrawingOutsideViewBox: allowDrawingOutsideViewBox, - placeholderBuilder: placeholderBuilder, - semanticsLabel: semanticsLabel, - excludeFromSemantics: excludeFromSemantics, - clipBehavior: clipBehavior, - theme: theme, - colorFilter: colorFilter, - color: color, - colorBlendMode: colorBlendMode, - cacheColorFilter: cacheColorFilter, - ); - -} - '''); - - /// Generates extensions for interfacing between assets and 3rd party libraries. - String generate() { - final library = LibraryBuilder() - ..directives.addAll(imports) - ..body.add(Libraries.header('nitrogen_lottie')) - ..body.add(extension); - - return library.build().format(); - } - -} diff --git a/nitrogen_flutter_svg/pubspec.yaml b/nitrogen_flutter_svg/pubspec.yaml index 81ae93f..4f5db0c 100644 --- a/nitrogen_flutter_svg/pubspec.yaml +++ b/nitrogen_flutter_svg/pubspec.yaml @@ -1,7 +1,7 @@ name: nitrogen_flutter_svg description: Companion library for Nitrogen. Provides flutter_svg bindings for code generated by Nitrogen. -version: 0.2.0+1 +version: 0.3.0+1 homepage: https://github.com/forus-labs/cauldron/nitrogen repository: https://github.com/forus-labs/cauldron/ @@ -9,20 +9,14 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - build: ^2.4.1 - build_runner: ^2.4.6 - code_builder: ^4.10.0 flutter: sdk: flutter - # We depend on flutter_svg so that users installing this extension will get a version compatible with the flutter_svg - # version that they're using. flutter_svg: ^2.0.9 - nitrogen: ^0.2.0 - path: ^1.9.0 + nitrogen_types: ^0.2.0 dev_dependencies: flint: ^2.7.0 dependency_overrides: - nitrogen: - path: ../nitrogen + nitrogen_types: + path: ../nitrogen_types diff --git a/nitrogen_lottie/CHANGELOG.md b/nitrogen_lottie/CHANGELOG.md index d13893b..1a2a33a 100644 --- a/nitrogen_lottie/CHANGELOG.md +++ b/nitrogen_lottie/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.0 + +Export instead of generate extension. + ## 0.2.0+1 * Add dependency on Flutter SDK diff --git a/nitrogen_lottie/lib/nitrogen_lottie.dart b/nitrogen_lottie/lib/nitrogen_lottie.dart index c942668..41cc0d2 100644 --- a/nitrogen_lottie/lib/nitrogen_lottie.dart +++ b/nitrogen_lottie/lib/nitrogen_lottie.dart @@ -1,28 +1,75 @@ -import 'dart:async'; - -import 'package:build/build.dart'; -import 'package:nitrogen_lottie/src/lottie_extension_generator.dart'; -import 'package:path/path.dart'; - -/// Creates a [NitrogenLottieBuilder]. -Builder nitrogenLottieBuilder(BuilderOptions options) => NitrogenLottieBuilder(); - -/// A Nitrogen builder. -class NitrogenLottieBuilder extends Builder { - - @override - FutureOr build(BuildStep buildStep) async { - await buildStep.writeAsString( - AssetId(buildStep.inputId.package, join('lib', 'src', 'lottie_extension.nitrogen.dart')), - LottieExtensionGenerator().generate(), - ); - } - - @override - Map> get buildExtensions => { - r'$package$': [ - 'lib/src/lottie_extension.nitrogen.dart', - ], - }; +import 'package:flutter/material.dart'; +import 'package:lottie/lottie.dart'; +import 'package:nitrogen_types/nitrogen_types.dart'; +/// Provides functions for converting a [LottieAsset] to a [LottieBuilder]. +/// +/// ```dart +/// class Foo extends StatelessWidget { +/// final LottieAsset asset; +/// +/// @override +/// Widget build(BuildContext context) => Container( +/// child: asset( +/// width: 100, +/// height: 100, +/// ), +/// ); +/// } +/// ``` +extension LottieAssetExtension on LottieAsset { + /// Converts a [LottieAsset] to a [LottieBuilder]. + LottieBuilder call({ + Animation? controller, + FrameRate? frameRate, + bool? animate, + bool? reverse, + bool? repeat, + LottieDelegates? delegates, + LottieOptions? options, + LottieImageProviderFactory? imageProviderFactory, + void Function(LottieComposition)? onLoaded, + Key? key, + Widget Function( + BuildContext context, + Widget child, + LottieComposition? composition, + )? frameBuilder, + ImageErrorWidgetBuilder? errorBuilder, + double? width, + double? height, + BoxFit? fit, + Alignment? alignment, + bool? addRepaintBoundary, + FilterQuality? filterQuality, + void Function(String)? onWarning, + LottieDecoder? decoder, + RenderCache? renderCache, + bool? backgroundLoading, + }) => LottieBuilder.asset( + path, + controller: controller, + frameRate: frameRate, + animate: animate, + reverse: reverse, + repeat: repeat, + delegates: delegates, + options: options, + imageProviderFactory: imageProviderFactory, + onLoaded: onLoaded, + key: key, + frameBuilder: frameBuilder, + errorBuilder: errorBuilder, + width: width, + height: height, + fit: fit, + alignment: alignment, + package: package, + addRepaintBoundary: addRepaintBoundary, + filterQuality: filterQuality, + onWarning: onWarning, + decoder: decoder, + renderCache: renderCache, + backgroundLoading: backgroundLoading, + ); } diff --git a/nitrogen_lottie/lib/src/lottie_extension_generator.dart b/nitrogen_lottie/lib/src/lottie_extension_generator.dart deleted file mode 100644 index c606a81..0000000 --- a/nitrogen_lottie/lib/src/lottie_extension_generator.dart +++ /dev/null @@ -1,101 +0,0 @@ -import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/nitrogen_extension.dart'; - -/// A generator for extensions SVG assets. -class LottieExtensionGenerator { - - /// The imports. - static final imports = [ - Directive.import('package:flutter/widgets.dart'), - Directive.import('package:lottie/lottie.dart'), - Libraries.importNitrogenTypes, - ]; - - /// The extension. - static const extension = Code(''' -/// Tested against lottie version: 3.1.1 -/// -/// Example: -/// ```dart -/// class Foo extends StatelessWidget { -/// final LottieAsset asset; -/// -/// @override -/// Widget build(BuildContext context) => Container( -/// child: asset( -/// width: 100, -/// height: 100, -/// ), -/// ); -/// } -/// ``` -extension LottieAssetExtension on LottieAsset { - - LottieBuilder call({ - Animation? controller, - FrameRate? frameRate, - bool? animate, - bool? reverse, - bool? repeat, - LottieDelegates? delegates, - LottieOptions? options, - LottieImageProviderFactory? imageProviderFactory, - void Function(LottieComposition)? onLoaded, - Key? key, - Widget Function( - BuildContext context, - Widget child, - LottieComposition? composition, - )? frameBuilder, - ImageErrorWidgetBuilder? errorBuilder, - double? width, - double? height, - BoxFit? fit, - Alignment? alignment, - bool? addRepaintBoundary, - FilterQuality? filterQuality, - void Function(String)? onWarning, - LottieDecoder? decoder, - RenderCache? renderCache, - bool? backgroundLoading, - }) => LottieBuilder.asset( - path, - controller: controller, - frameRate: frameRate, - animate: animate, - reverse: reverse, - repeat: repeat, - delegates: delegates, - options: options, - imageProviderFactory: imageProviderFactory, - onLoaded: onLoaded, - key: key, - frameBuilder: frameBuilder, - errorBuilder: errorBuilder, - width: width, - height: height, - fit: fit, - alignment: alignment, - package: package, - addRepaintBoundary: addRepaintBoundary, - filterQuality: filterQuality, - onWarning: onWarning, - decoder: decoder, - renderCache: renderCache, - backgroundLoading: backgroundLoading, - ); - -} - '''); - - /// Generates extensions for interfacing between assets and 3rd party libraries. - String generate() { - final library = LibraryBuilder() - ..directives.addAll(imports) - ..body.add(Libraries.header('nitrogen_flutter_svg')) - ..body.add(extension); - - return library.build().format(); - } - -} diff --git a/nitrogen_lottie/pubspec.yaml b/nitrogen_lottie/pubspec.yaml index 70ff7d7..39a5be8 100644 --- a/nitrogen_lottie/pubspec.yaml +++ b/nitrogen_lottie/pubspec.yaml @@ -1,7 +1,7 @@ name: nitrogen_lottie description: Companion library for Nitrogen. Provides lottie bindings for code generated by Nitrogen. -version: 0.2.0+1 +version: 0.3.0+1 homepage: https://github.com/forus-labs/cauldron/nitrogen repository: https://github.com/forus-labs/cauldron/ @@ -10,16 +10,10 @@ environment: flutter: ">=3.3.0" dependencies: - build: ^2.4.1 - build_runner: ^2.4.6 - code_builder: ^4.10.0 flutter: sdk: flutter - # We depend on flutter_svg so that users installing this extension will get a version compatible with the flutter_svg - # version that they're using. lottie: ^3.1.0 - nitrogen: ^0.2.0 - path: ^1.9.0 + nitrogen_types: ^0.2.0 dev_dependencies: flint: ^2.7.0 diff --git a/nitrogen_types/CHANGELOG.md b/nitrogen_types/CHANGELOG.md index d1b2588..2fd725a 100644 --- a/nitrogen_types/CHANGELOG.md +++ b/nitrogen_types/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.0 + +Export `ImageAssetExtension`. + ## 0.2.0+1 * Remove accidental on Flutter SDK diff --git a/nitrogen_types/pubspec.yaml b/nitrogen_types/pubspec.yaml index ed5eb27..1af2ae2 100644 --- a/nitrogen_types/pubspec.yaml +++ b/nitrogen_types/pubspec.yaml @@ -1,7 +1,7 @@ name: nitrogen_types description: Companion library for Nitrogen. Provides shared types that generated bindings use. -version: 0.2.0+1 +version: 0.3.0 homepage: https://github.com/forus-labs/cauldron/tree/master/nitrogen repository: https://github.com/forus-labs/cauldron/