diff --git a/nitrogen/CHANGELOG.md b/nitrogen/CHANGELOG.md index 6f6c7e5..0ddd2b3 100644 --- a/nitrogen/CHANGELOG.md +++ b/nitrogen/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.3.0 +* Add configuration for output locations. +* Move configuration from `pubspec.yaml` to `build.yaml`. +* Remove generation of `flutter_extension`. + ## 0.2.0 * Add `flutter-extension` option. * Change `file` asset key option to `file-name`. diff --git a/nitrogen/README.md b/nitrogen/README.md index ff95ece..90c12c5 100644 --- a/nitrogen/README.md +++ b/nitrogen/README.md @@ -36,13 +36,13 @@ Widget build(BuildContext context) => Assets.images.foo(); ## Getting started -3rd party packages are supported via 'extension' packages. `extension` packages generate `extension`s in separate files. -These `extension`s provide a `call(...)` function that transforms an `Asset` into a 3rd party type. +3rd party packages are supported via 'extension' packages. `extension` packages contain an `extension` that provide a +`call(...)` function that transforms an `Asset` into a 3rd party type. -| Type | Package | Extension Package | Version | Default generated file | -|-------------------|---------------|------------------------|----------------------------------------------------------------------------------------------------------------|----------------------------------| -| SVG images | `flutter_svg` | `nitrogen_flutter_svg` | [![Pub Dev](https://img.shields.io/pub/v/nitrogen_flutter_svg)](https://pub.dev/packages/nitrogen_flutter_svg) | `svg_extension.nitrogen.dart` | -| Lottie animations | `lottie` | `nitrogen_lottie` | [![Pub Dev](https://img.shields.io/pub/v/nitrogen_lottie)](https://pub.dev/packages/nitrogen_lottie) | `lottie_extension.nitrogen.dart` | +| Type | Package | Extension Package | Version | +|-------------------|---------------|------------------------|----------------------------------------------------------------------------------------------------------------| +| SVG images | `flutter_svg` | `nitrogen_flutter_svg` | [![Pub Dev](https://img.shields.io/pub/v/nitrogen_flutter_svg)](https://pub.dev/packages/nitrogen_flutter_svg) | +| Lottie animations | `lottie` | `nitrogen_lottie` | [![Pub Dev](https://img.shields.io/pub/v/nitrogen_lottie)](https://pub.dev/packages/nitrogen_lottie) | Install the following: @@ -72,19 +72,26 @@ dart run build_runner build ## Configuration -Nitrogen's configuration can be set in the `nitrogen` section of your pubspec.yaml. In most cases, the default +Nitrogen's configuration can be set in the `nitrogen` section of your build.yaml. In most cases, the default configuration works out of the box. ### Example A simple configuration looks like: ```yaml -nitrogen: - package: true - prefix: 'MyPrefix' - key: file - themes: - fallback: assets/themes/light +targets: + $default: + builders: + nitrogen: + options: + package: true + prefix: 'MyPrefix' + key: file-name + assets: + output: 'lib/src/assets.nitrogen.dart' + themes: + output: 'lib/src/asset_themes.nitrogen.dart' + fallback: assets/themes/light ``` ### `package` @@ -115,6 +122,15 @@ Optional. Defaults to `file`. Controls the generated assets' key parameters. The | file-name | file name, without the extension | `assets/images/foo.png` | `foo` | | grpc-enum | parent directory and file name, without the extension | `assets/images/foo.png` | `IMAGES_FOO` | +### `assets` + +Optional. Controls the location of the generated assets file. + +```yaml +assets: + # Path to the generated assets file. Relative to the project root. Defaults to `lib/src/assets.nitrogen.dart`. + output: lib/src/my/path/assets.nitrogen.dart +``` ### `themes` @@ -122,8 +138,9 @@ Optional. Defaults to `null`. Controls whether to generate an additional `asset_ working with theme-specific assets. ```yaml -nitrogen: - themes: - # Path to fallback theme, relative to package root. Assumes that all themes are under 'assets/themes'. - fallback: assets/themes/light +themes: + # Path to the generated themes file. Relative to the project root. Defaults to `lib/src/asset_themes.nitrogen.dart`. + output: lib/src/my/path/assets_themes.nitrogen.dart + # Path to fallback theme, relative to package root. Assumes that all themes are under 'assets/themes'. + fallback: assets/themes/light ``` diff --git a/nitrogen/build.yaml b/nitrogen/build.yaml index 3cf1f7e..5207ac9 100644 --- a/nitrogen/build.yaml +++ b/nitrogen/build.yaml @@ -12,6 +12,15 @@ builders: nitrogen: import: "package:nitrogen/nitrogen.dart" builder_factories: [ "nitrogenBuilder" ] - build_extensions: { "$package$": [ "lib/src/assets.nitrogen.dart", "lib/src/asset_themes.nitrogen.dart", "lib/src/flutter_extension.nitrogen.dart"] } + build_extensions: { "$package$": [ "lib/src/assets.nitrogen.dart", "lib/src/asset_themes.nitrogen.dart" ] } + defaults: + options: + package: false + prefix: "" + key: "file-name" + assets: + output: "lib/src/assets.nitrogen.dart" + themes: + output: "lib/src/asset_themes.nitrogen.dart" auto_apply: dependents build_to: source \ No newline at end of file diff --git a/nitrogen/lib/nitrogen.dart b/nitrogen/lib/nitrogen.dart index 9068afe..780b69c 100644 --- a/nitrogen/lib/nitrogen.dart +++ b/nitrogen/lib/nitrogen.dart @@ -2,6 +2,10 @@ import 'dart:async'; import 'package:build/build.dart'; import 'package:glob/glob.dart'; +import 'package:path/path.dart'; +import 'package:yaml/yaml.dart'; + +import 'package:nitrogen/src/configuration/build_configuration.dart'; import 'package:nitrogen/src/configuration/configuration.dart'; import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen/src/generators/asset_generator.dart'; @@ -9,25 +13,34 @@ import 'package:nitrogen/src/generators/theme_generator.dart'; import 'package:nitrogen/src/lints/reserved_keyword_lint.dart'; import 'package:nitrogen/src/nitrogen_exception.dart'; import 'package:nitrogen/src/walker.dart'; -import 'package:path/path.dart'; -import 'package:yaml/yaml.dart'; /// Creates a [NitrogenBuilder]. -Builder nitrogenBuilder(BuilderOptions options) => NitrogenBuilder(); +Builder nitrogenBuilder(BuilderOptions options) { + try { + BuildConfiguration.lint(options.config); + return NitrogenBuilder(BuildConfiguration.parse(options.config)); + + } on NitrogenException { + return EmptyBuilder(); + } +} /// A Nitrogen builder. class NitrogenBuilder extends Builder { static final _pubspec = Glob('pubspec.yaml'); static final _assets = Glob('assets/**'); + + final BuildConfiguration _configuration; + + /// Creates a [NitrogenBuilder]. + NitrogenBuilder(this._configuration); @override FutureOr build(BuildStep buildStep) async { try { final pubspec = loadYaml(await buildStep.readAsString(await buildStep.findAssets(_pubspec).first)); - - Configuration.lint(pubspec); - final configuration = Configuration.parse(pubspec); + final configuration = Configuration.merge(_configuration, pubspec); final walker = Walker(configuration.package, configuration.flutterAssets, configuration.key.call); @@ -38,8 +51,8 @@ class NitrogenBuilder extends Builder { lintReservedKeyword(assets); - final assetsOutput = AssetId(buildStep.inputId.package, join('lib', 'src', 'assets.nitrogen.dart')); - if (configuration.fallbackTheme case final fallback?) { + final assetsOutput = AssetId(buildStep.inputId.package, configuration.assets.output); + if (configuration.themes case (:final fallback, :final output)) { var themes = assets; var fallbackTheme = assets; for (final segment in split(fallback).skip(1)) { @@ -53,7 +66,7 @@ class NitrogenBuilder extends Builder { ); await buildStep.writeAsString( - AssetId(buildStep.inputId.package, join('lib', 'src', 'asset_themes.nitrogen.dart')), + AssetId(buildStep.inputId.package, output), ThemeGenerator(configuration.prefix, themes, fallbackTheme).generate(), ); @@ -72,9 +85,19 @@ class NitrogenBuilder extends Builder { @override Map> get buildExtensions => { r'$package$': [ - 'lib/src/assets.nitrogen.dart', - 'lib/src/asset_themes.nitrogen.dart', + _configuration.assets.output, + if (_configuration.themes != null) + _configuration.themes!.output, ], }; } + +/// A stub builder for when Nitrogen fails to build. +class EmptyBuilder extends Builder { + @override + void build(BuildStep buildStep) {} + + @override + Map> get buildExtensions => {}; +} diff --git a/nitrogen/lib/src/configuration/build_configuration.dart b/nitrogen/lib/src/configuration/build_configuration.dart new file mode 100644 index 0000000..46e870f --- /dev/null +++ b/nitrogen/lib/src/configuration/build_configuration.dart @@ -0,0 +1,92 @@ +import 'package:build/build.dart'; +import 'package:meta/meta.dart'; + +import 'package:nitrogen/src/configuration/key.dart'; +import 'package:nitrogen/src/nitrogen_exception.dart'; + + /// Nitrogen's build.yaml configuration. +final class BuildConfiguration { + + /// The Nitrogen configuration's valid keys. + static const keys = { 'package', 'prefix', 'key', 'assets', 'themes' }; + + /// Lints the pubspec. + static void lint(Map configuration) { + for (final key in {...configuration.keys }..removeAll(keys)) { + log.warning('Unknown key, "$key", in build.yaml\'s nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#configuration for valid configuration options.'); + } + + final assets = configuration['assets'] as Map; + for (final key in {...assets.keys }..removeAll({ 'output' })) { + log.warning('Unknown key, "$key", in build.yaml\'s nitrogen assets configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#assets for valid configuration options.'); + } + + final themes = configuration['themes'] as Map; + for (final key in {...themes.keys }..removeAll({ 'fallback', 'output' })) { + log.warning('Unknown key, "$key", in build.yaml\'s nitrogen themes configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes for valid configuration options.'); + } + } + + /// Parses the assets configuration. + @visibleForTesting + static ({String output,}) parseAssets(Map configuration) { + switch (configuration) { + case { 'output': final String output }: + return (output: output); + + default: + log.severe("Unable to read assets configuration in build.yaml's nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#assets."); + throw NitrogenException(); + } + } + + /// Parses the themes configuration. + @visibleForTesting + static ({String fallback, String output})? parseThemes(Map configuration) { + switch (configuration) { + case { 'fallback': final String fallback, 'output': final String output }: + return (fallback: fallback, output: output); + + case { 'output': String _ }: + return null; + + default: + log.severe("Unable to read themes configuration in build.yaml's nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes."); + throw NitrogenException(); + } + } + + /// Whether to generate assets for a package. + final bool package; + + /// The prefix for generated classes. + final String prefix; + + /// The function for generating asset keys. + final String Function(List) key; + + /// The output location of the generated assets. + final ({String output,}) assets; + + /// The fallback theme and output location of the generated themes. + final ({String fallback, String output})? themes; + + /// Parses the build configuration. + factory BuildConfiguration.parse(Map configuration) => BuildConfiguration( + package: configuration['package'], + prefix: configuration['prefix'], + key: Key.parse(configuration['key']), + assets: parseAssets(configuration['assets']), + themes: parseThemes(configuration['themes']), + ); + + /// Creates a [BuildConfiguration]. + BuildConfiguration({ + required this.package, + required this.prefix, + required this.key, + required this.assets, + required this.themes, + }); + +} diff --git a/nitrogen/lib/src/configuration/configuration.dart b/nitrogen/lib/src/configuration/configuration.dart index ceb4aab..0f3b7c1 100644 --- a/nitrogen/lib/src/configuration/configuration.dart +++ b/nitrogen/lib/src/configuration/configuration.dart @@ -1,46 +1,27 @@ import 'package:build/build.dart'; import 'package:meta/meta.dart'; -import 'package:nitrogen/src/nitrogen_exception.dart'; -import 'package:nitrogen/src/configuration/key.dart'; import 'package:yaml/yaml.dart'; +import 'package:nitrogen/src/configuration/build_configuration.dart'; +import 'package:nitrogen/src/nitrogen_exception.dart'; + /// Nitrogen's configuration. final class Configuration { - /// The Nitrogen section's valid keys. - static const keys = { 'package', 'prefix', 'key', 'themes' }; - - /// Lints the pubspec. - static void lint(YamlMap? pubspec) { - final nitrogen = pubspec?.nodes['nitrogen'].as(); - - for (final key in {...?nitrogen?.keys }..removeAll(keys)) { - log.warning(nitrogen?.nodes[key]!.span.message('Unknown key, "$key", in pubspec.yaml\'s nitrogen section. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#configuration for valid configuration options.')); - } - - final themes = nitrogen?.nodes['themes'].as(); - for (final key in {...?themes?.keys }..removeAll({ 'fallback' })) { - log.warning(themes?.nodes[key]!.span.message('Unknown key, "$key", in pubspec.yaml\'s nitrogen section. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes for valid configuration options.')); - } - } - - /// Parses the configuration from the project's pubspec.yaml. - factory Configuration.parse(YamlMap pubspec) { - final nitrogen = pubspec.nodes['nitrogen'].as(); - return Configuration( - package: parsePackage(pubspec.nodes['name'], nitrogen?.nodes['package']), - prefix: parsePrefix(nitrogen?.nodes['prefix']), - key: Key.parse(nitrogen?.nodes['key']), - fallbackTheme: parseFallbackTheme(nitrogen?.nodes['themes']), - flutterAssets: parseFlutterAssets(pubspec.nodes['flutter'].as()?.nodes['assets']), - ); - } + factory Configuration.merge(BuildConfiguration configuration, YamlMap pubspec) => Configuration( + package: parsePackage(pubspec.nodes['name'], enabled: configuration.package), + prefix: configuration.prefix, + key: configuration.key, + assets: configuration.assets, + themes: configuration.themes, + flutterAssets: parseFlutterAssets(pubspec.nodes['flutter'].as()?.nodes['assets']), + ); /// Parses the package name from the project's pubspec.yaml. @visibleForTesting - static String? parsePackage(YamlNode? name, YamlNode? enabled) { - switch ((name?.value, enabled?.value)) { + static String? parsePackage(YamlNode? name, {required bool enabled}) { + switch ((name?.value, enabled)) { case (final String name, true): return name; @@ -50,39 +31,6 @@ final class Configuration { case (_, bool? _): return null; - - default: - log.severe(enabled?.span.message('Unable to read package name. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#package.')); - throw NitrogenException(); - } - } - - /// Parses the class prefix from the nitrogen section in the project's pubspec.yaml. - @visibleForTesting - static String parsePrefix(YamlNode? node) { - switch (node?.value) { - case final String? prefix: - return prefix ?? ''; - - default: - log.severe(node!.span.message('Unable to read prefix. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#prefix.')); - throw NitrogenException(); - } - } - - /// Parses the path to the fallback theme. - @visibleForTesting - static String? parseFallbackTheme(YamlNode? node) { - switch (node?.value) { - case null: - return null; - - case { 'fallback': final String fallback }: - return fallback; - - case _: - log.severe(node!.span.message('Unable to read themes. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes.')); - throw NitrogenException(); } } @@ -111,8 +59,11 @@ final class Configuration { /// The function for generating asset keys. final String Function(List) key; - /// The path to the fallback theme. - final String? fallbackTheme; + /// The output location of the generated assets. + final ({String output,}) assets; + + /// The fallback theme and output location of the generated themes. + final ({String fallback, String output})? themes; /// The flutter assets. final Set flutterAssets; @@ -122,7 +73,8 @@ final class Configuration { required this.package, required this.prefix, required this.key, - required this.fallbackTheme, + required this.assets, + required this.themes, required this.flutterAssets, }); diff --git a/nitrogen/lib/src/configuration/key.dart b/nitrogen/lib/src/configuration/key.dart index 2dec369..0b773eb 100644 --- a/nitrogen/lib/src/configuration/key.dart +++ b/nitrogen/lib/src/configuration/key.dart @@ -1,23 +1,23 @@ import 'package:build/build.dart'; -import 'package:nitrogen/src/nitrogen_exception.dart'; import 'package:path/path.dart'; import 'package:sugar/sugar.dart'; -import 'package:yaml/yaml.dart'; + +import 'package:nitrogen/src/nitrogen_exception.dart'; /// Provides functions for generating asset keys. extension Key on Never { /// Parses the `asset-key` node if valid. - static String Function(List) parse(YamlNode? key) { - switch (key?.value) { + static String Function(List) parse(String key) { + switch (key) { case 'grpc-enum': return grpcEnum; - case 'file-name' || null: + case 'file-name': return fileName; default: - log.severe(key!.span.message('Unable to read asset key. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#key.')); + log.severe('Unknown asset key: "$key". See https://github.com/forus-labs/cauldron/tree/master/nitrogen#key.'); throw NitrogenException(); } } diff --git a/nitrogen/lib/src/generators/asset_generator.dart b/nitrogen/lib/src/generators/asset_generator.dart index 253b719..1094d6a 100644 --- a/nitrogen/lib/src/generators/asset_generator.dart +++ b/nitrogen/lib/src/generators/asset_generator.dart @@ -1,9 +1,9 @@ import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/src/file_system.dart'; -import 'package:nitrogen/src/libraries.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; import 'package:sugar/sugar.dart'; +import 'package:nitrogen/src/file_system.dart'; +import 'package:nitrogen/src/libraries.dart'; /// A generator for standard class representations of asset directories. class AssetGenerator { diff --git a/nitrogen/lib/src/generators/theme_generator.dart b/nitrogen/lib/src/generators/theme_generator.dart index 428f8ec..c170111 100644 --- a/nitrogen/lib/src/generators/theme_generator.dart +++ b/nitrogen/lib/src/generators/theme_generator.dart @@ -1,11 +1,11 @@ import 'dart:collection'; import 'package:code_builder/code_builder.dart'; +import 'package:sugar/sugar.dart'; + import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen/src/generators/asset_generator.dart'; import 'package:nitrogen/src/libraries.dart'; -import 'package:sugar/sugar.dart'; - /// A generator for theme class representations of asset directories. class ThemeGenerator { diff --git a/nitrogen/lib/src/lints/reserved_keyword_lint.dart b/nitrogen/lib/src/lints/reserved_keyword_lint.dart index 8990fa1..0ddd03e 100644 --- a/nitrogen/lib/src/lints/reserved_keyword_lint.dart +++ b/nitrogen/lib/src/lints/reserved_keyword_lint.dart @@ -1,4 +1,5 @@ import 'package:build/build.dart'; + import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen/src/nitrogen_exception.dart'; diff --git a/nitrogen/lib/src/walker.dart b/nitrogen/lib/src/walker.dart index f758a29..d371d27 100644 --- a/nitrogen/lib/src/walker.dart +++ b/nitrogen/lib/src/walker.dart @@ -1,9 +1,10 @@ import 'dart:collection'; import 'package:build/build.dart'; -import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; +import 'package:nitrogen/src/file_system.dart'; + /// A walker that recursively walks through a directory and converts all entities into [Entity]s. final class Walker { diff --git a/nitrogen/pubspec.yaml b/nitrogen/pubspec.yaml index d5714e4..653ae12 100644 --- a/nitrogen/pubspec.yaml +++ b/nitrogen/pubspec.yaml @@ -27,8 +27,12 @@ dependencies: dev_dependencies: build_test: ^2.2.2 flint: ^2.7.0 + import_sorter: ^4.6.0 test: ^1.25.2 dependency_overrides: nitrogen_types: path: ../nitrogen_types + +import_sorter: + comments: false diff --git a/nitrogen/test/src/configuration/build_configuration_test.dart b/nitrogen/test/src/configuration/build_configuration_test.dart new file mode 100644 index 0000000..a0ac110 --- /dev/null +++ b/nitrogen/test/src/configuration/build_configuration_test.dart @@ -0,0 +1,93 @@ +import 'package:build/build.dart'; +import 'package:build_test/build_test.dart'; +import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; + +import 'package:nitrogen/src/configuration/build_configuration.dart'; +import 'package:nitrogen/src/configuration/key.dart'; +import 'package:nitrogen/src/nitrogen_exception.dart'; + +const valid = ''' +package: true +prefix: 'MyPrefix' +key: grpc-enum +assets: + output: foo-output +themes: + fallback: assets/path/to/first/fallback + output: bar-output +'''; + +const invalid = ''' +package: true +prefix: 'MyPrefix' +key: grpc-enum +invalid-nitrogen: true +assets: + invalid-assets: true +themes: + fallback: true + invalid-theme: true +'''; + +void main() { + group('lint(...)', () { + test('invalid pubspec', () { + expectLater( + log.onRecord, + emitsInOrder([ + warningLogOf(contains('Unknown key, "invalid-nitrogen", in build.yaml\'s nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#configuration for valid configuration options.')), + warningLogOf(contains('Unknown key, "invalid-assets", in build.yaml\'s nitrogen assets configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#assets for valid configuration options.')), + warningLogOf(contains('Unknown key, "invalid-theme", in build.yaml\'s nitrogen themes configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes for valid configuration options.')), + ]), + ); + + BuildConfiguration.lint(loadYaml(invalid)); + }); + }); + + group('parseAssets(...)', () { + test('valid configuration', () => expect(BuildConfiguration.parseAssets({ 'output': 'valid' }).output, 'valid')); + + test('invalid configuration', () { + expectLater( + log.onRecord, + emits(severeLogOf(contains("Unable to read assets configuration in build.yaml's nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#assets."))), + ); + + expect(() => BuildConfiguration.parseAssets({}), throwsA(isA())); + }); + }); + + group('parseThemes(...)', () { + test('valid configuration', () { + final configuration = BuildConfiguration.parseThemes({ 'fallback': 'valid-fallback', 'output': 'valid-output' }); + expect(configuration?.fallback, 'valid-fallback'); + expect(configuration?.output, 'valid-output'); + }); + + test('no fallback', () => expect(BuildConfiguration.parseThemes({ 'output': 'valid-output' }), null)); + + test('invalid configuration', () { + expectLater( + log.onRecord, + emits(severeLogOf(contains("Unable to read themes configuration in build.yaml's nitrogen configuration. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes."))), + ); + + expect(() => BuildConfiguration.parseThemes({}), throwsA(isA())); + }); + }); + + group('parse(...)', () { + test('valid configuration', () { + final configuration = BuildConfiguration.parse(loadYaml(valid)); + + expect(configuration.package, true); + expect(configuration.prefix, 'MyPrefix'); + expect(configuration.key, Key.grpcEnum); + expect(configuration.assets.output, 'foo-output'); + expect(configuration.themes?.fallback, 'assets/path/to/first/fallback'); + expect(configuration.themes?.output, 'bar-output'); + }); + }); +} diff --git a/nitrogen/test/src/configuration/configuration_test.dart b/nitrogen/test/src/configuration/configuration_test.dart index fcca49d..6549e0f 100644 --- a/nitrogen/test/src/configuration/configuration_test.dart +++ b/nitrogen/test/src/configuration/configuration_test.dart @@ -1,46 +1,17 @@ import 'package:build/build.dart'; import 'package:build_test/build_test.dart'; +import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; + +import 'package:nitrogen/src/configuration/build_configuration.dart'; import 'package:nitrogen/src/configuration/configuration.dart'; import 'package:nitrogen/src/configuration/key.dart'; import 'package:nitrogen/src/nitrogen_exception.dart'; -import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; // ignore_for_file: avoid_dynamic_calls -const _invalid = ''' -name: hi -flutter: - assets: - - path/to/first/ - - path/to/second/ -nitrogen: - package: true - prefix: 'MyPrefix' - key: grpc-enum - invalid-nitrogen: true - themes: - fallback: true - invalid-theme: true -'''; - - const _pubspec = ''' name: hi -flutter: - assets: - - path/to/first/ - - path/to/second/ -nitrogen: - package: true - prefix: 'MyPrefix' - key: grpc-enum - themes: - fallback: assets/path/to/first/fallback -'''; - -const _noNitrogen = ''' -name: hi flutter: assets: - path/to/first/ @@ -54,44 +25,29 @@ assets: '''; void main() { - group('lint(...)', () { - test('invalid pubspec', () { - expectLater( - log.onRecord, - emitsInOrder([ - warningLogOf(contains('Unknown key, "invalid-nitrogen", in pubspec.yaml\'s nitrogen section. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#configuration for valid configuration options.')), - warningLogOf(contains('Unknown key, "invalid-theme", in pubspec.yaml\'s nitrogen section. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes for valid configuration options.')), - ]), - ); - - Configuration.lint(loadYaml(_invalid)); - }); - }); - - group('parse(...)', () { + final buildConfiguration = BuildConfiguration( + package: true, + prefix: 'MyPrefix', + key: Key.grpcEnum, + assets: (output: 'foo-output'), + themes: (fallback: 'some-path', output: 'bar-output'), + ); + + group('merge(...)', () { test('valid pubspec', () { - final configuration = Configuration.parse(loadYaml(_pubspec)); + final configuration = Configuration.merge(buildConfiguration, loadYaml(_pubspec)); expect(configuration.package, 'hi'); - expect(configuration.prefix, 'MyPrefix'); - expect(configuration.key, Key.grpcEnum); - expect(configuration.fallbackTheme, 'assets/path/to/first/fallback'); - expect(configuration.flutterAssets, { 'path/to/first/', 'path/to/second/' }); - }); - - test('no nitrogen section', () { - final configuration = Configuration.parse(loadYaml(_noNitrogen)); - - expect(configuration.package, null); - expect(configuration.prefix, ''); - expect(configuration.key, Key.fileName); - expect(configuration.fallbackTheme, null); + expect(configuration.prefix, buildConfiguration.prefix); + expect(configuration.key, buildConfiguration.key); + expect(configuration.assets, buildConfiguration.assets); + expect(configuration.themes, buildConfiguration.themes); expect(configuration.flutterAssets, { 'path/to/first/', 'path/to/second/' }); }); }); group('parsePackage(...)', () { - test('use package name', () => expect(Configuration.parsePackage(loadYamlNode('nitrogen'), loadYamlNode('true')), 'nitrogen')); + test('use package name', () => expect(Configuration.parsePackage(loadYamlNode('nitrogen'), enabled: true), 'nitrogen')); test('use package name & invalid name', () { expectLater( @@ -99,53 +55,12 @@ void main() { emits(severeLogOf(contains('Unable to read package name in pubspec.yaml. See https://dart.dev/tools/pub/pubspec#name.'))), ); expect( - () => Configuration.parsePackage(loadYamlNode('true'), loadYamlNode('true')), + () => Configuration.parsePackage(loadYamlNode('true'), enabled: true), throwsA(isA()), ); }); - test('do not use package name & invalid name', () => expect(Configuration.parsePackage(loadYamlNode('true'), loadYamlNode('false')), null)); - - test('null & invalid name', () => expect(Configuration.parsePackage(loadYamlNode('true'), null), null)); - - test('invalid package usage', () { - expectLater( - log.onRecord, - emits(severeLogOf(contains('Unable to read package name. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#package.'))), - ); - expect( - () => Configuration.parsePackage(loadYamlNode('nitrogen'), loadYamlNode('invalid')), - throwsA(isA()), - ); - }); - }); - - group('parsePrefix(...)', () { - test('prefix', () => expect(Configuration.parsePrefix(loadYamlNode('something')), 'something')); - - test('null', () => expect(Configuration.parsePrefix(null), '')); - - test('invalid', () { - expectLater( - log.onRecord, - emits(severeLogOf(contains('Unable to read prefix. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#prefix.'))), - ); - expect(() => Configuration.parsePrefix(loadYamlNode('true')), throwsA(isA())); - }); - }); - - group('parseFallbackTheme(...)', () { - test('fallback theme', () => expect(Configuration.parseFallbackTheme(loadYamlNode('fallback: assets/path/to/fallback')), 'assets/path/to/fallback')); - - test('null', () => expect(Configuration.parseFallbackTheme(null), null)); - - test('invalid', () { - expectLater( - log.onRecord, - emits(severeLogOf(contains('Unable to read themes. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#themes.'))), - ); - expect(() => Configuration.parseFallbackTheme(loadYamlNode('true')), throwsA(isA())); - }); + test('do not use package name & invalid name', () => expect(Configuration.parsePackage(loadYamlNode('true'), enabled: false), null)); }); group('parseFlutterAssets(...)', () { diff --git a/nitrogen/test/src/configuration/key_test.dart b/nitrogen/test/src/configuration/key_test.dart index f5d7149..3f68204 100644 --- a/nitrogen/test/src/configuration/key_test.dart +++ b/nitrogen/test/src/configuration/key_test.dart @@ -1,35 +1,31 @@ import 'package:build/build.dart'; import 'package:build_test/build_test.dart'; -import 'package:nitrogen/src/nitrogen_exception.dart'; -import 'package:nitrogen/src/configuration/key.dart'; import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; +import 'package:nitrogen/src/configuration/key.dart'; +import 'package:nitrogen/src/nitrogen_exception.dart'; + // ignore_for_file: avoid_dynamic_calls void main() { group('parse', () { test('grpc-enum', () { - final key = Key.parse(loadYaml('key: grpc-enum').nodes['key']); + final key = Key.parse(loadYaml('key: grpc-enum')['key']); expect(key(['path', 'to', 'file.png']), 'TO_FILE'); }); test('file-name', () { - final key = Key.parse(loadYaml('key: file-name').nodes['key']); - expect(key(['path', 'to', 'file.png']), 'file'); - }); - - test('null', () { - final key = Key.parse(null); + final key = Key.parse(loadYaml('key: file-name')['key']); expect(key(['path', 'to', 'file.png']), 'file'); }); test('invalid', () { expectLater( log.onRecord, - emits(severeLogOf(contains('Unable to read asset key. See https://github.com/forus-labs/cauldron/tree/master/nitrogen#key.'))), + emits(severeLogOf(contains('Unknown asset key: "invalid". See https://github.com/forus-labs/cauldron/tree/master/nitrogen#key.'))), ); - expect(() => Key.parse(loadYaml('key: invalid').nodes['key']), throwsA(isA())); + expect(() => Key.parse(loadYaml('key: invalid')['key']), throwsA(isA())); }); }); } diff --git a/nitrogen/test/src/file_system_test.dart b/nitrogen/test/src/file_system_test.dart index 7a1b1d5..cc5aeb6 100644 --- a/nitrogen/test/src/file_system_test.dart +++ b/nitrogen/test/src/file_system_test.dart @@ -1,7 +1,8 @@ -import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; import 'package:test/test.dart'; +import 'package:nitrogen/src/file_system.dart'; + void main() { group('AssetDirectory', () { test('empty path', () { diff --git a/nitrogen/test/src/generators/asset_generator_test.dart b/nitrogen/test/src/generators/asset_generator_test.dart index f66f75c..042a352 100644 --- a/nitrogen/test/src/generators/asset_generator_test.dart +++ b/nitrogen/test/src/generators/asset_generator_test.dart @@ -1,10 +1,11 @@ import 'package:code_builder/code_builder.dart'; import 'package:dart_style/dart_style.dart'; -import 'package:nitrogen/src/file_system.dart'; -import 'package:nitrogen/src/generators/asset_generator.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; import 'package:test/test.dart'; +import 'package:nitrogen/src/file_system.dart'; +import 'package:nitrogen/src/generators/asset_generator.dart'; + const _classes = r''' import 'package:nitrogen_types/nitrogen_types.dart'; diff --git a/nitrogen/test/src/generators/theme_generator_test.dart b/nitrogen/test/src/generators/theme_generator_test.dart index d605e29..5f221f7 100644 --- a/nitrogen/test/src/generators/theme_generator_test.dart +++ b/nitrogen/test/src/generators/theme_generator_test.dart @@ -1,9 +1,10 @@ import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/src/file_system.dart'; -import 'package:nitrogen/src/generators/theme_generator.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; import 'package:test/test.dart'; +import 'package:nitrogen/src/file_system.dart'; +import 'package:nitrogen/src/generators/theme_generator.dart'; + const _classes = r''' import 'package:nitrogen_types/nitrogen_types.dart'; diff --git a/nitrogen/test/src/libraries_test.dart b/nitrogen/test/src/libraries_test.dart index 195830c..8575b5d 100644 --- a/nitrogen/test/src/libraries_test.dart +++ b/nitrogen/test/src/libraries_test.dart @@ -1,7 +1,8 @@ import 'package:code_builder/code_builder.dart'; -import 'package:nitrogen/src/libraries.dart'; import 'package:test/test.dart'; +import 'package:nitrogen/src/libraries.dart'; + void main() { test('format()', () { final library = Library((b) => b.body.addAll([ diff --git a/nitrogen/test/src/lints/reserved_keyword_lint_test.dart b/nitrogen/test/src/lints/reserved_keyword_lint_test.dart index 4714c58..f853963 100644 --- a/nitrogen/test/src/lints/reserved_keyword_lint_test.dart +++ b/nitrogen/test/src/lints/reserved_keyword_lint_test.dart @@ -1,10 +1,11 @@ import 'package:build/build.dart'; import 'package:build_test/build_test.dart'; +import 'package:nitrogen_types/nitrogen_types.dart'; +import 'package:test/test.dart'; + import 'package:nitrogen/src/file_system.dart'; import 'package:nitrogen/src/lints/reserved_keyword_lint.dart'; import 'package:nitrogen/src/nitrogen_exception.dart'; -import 'package:nitrogen_types/nitrogen_types.dart'; -import 'package:test/test.dart'; void main() { group('lintReservedKeyword', () { diff --git a/nitrogen/test/src/walker_test.dart b/nitrogen/test/src/walker_test.dart index 85518a2..be491d3 100644 --- a/nitrogen/test/src/walker_test.dart +++ b/nitrogen/test/src/walker_test.dart @@ -1,9 +1,10 @@ import 'package:build/build.dart'; -import 'package:nitrogen/src/file_system.dart'; -import 'package:nitrogen/src/walker.dart'; import 'package:nitrogen_types/nitrogen_types.dart'; import 'package:test/test.dart'; +import 'package:nitrogen/src/file_system.dart'; +import 'package:nitrogen/src/walker.dart'; + void main() { group('walk(...)', () { test('ancestor asset directory is allowed', () {