Skip to content

Commit

Permalink
Streamline nitrogen generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Jun 12, 2024
1 parent 9e10c95 commit f745547
Show file tree
Hide file tree
Showing 27 changed files with 493 additions and 679 deletions.
58 changes: 16 additions & 42 deletions nitrogen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ Install the following:
```yaml
dependencies:
nitrogen_types: <version>
nitrogen_flutter_svg: <version> # Optional: include when using SVG images
nitrogen_lottie: <version> # Optional: include when using Lottie animations

dev_dependencies:
build_runner: <version>
nitrogen: <version>
nitrogen_flutter_svg: <version> # Optional: include when using SVG images
nitrogen_lottie: <version> # Optional: include when using Lottie animations
```
Alternatively:
```shell
dart pub add nitrogen_types

# Include nitrogen_flutter_svg and nitrogen_lottie when using SVG images and Lottie animations respectively.
dart pub add --dev build_runner nitrogen nitrogen_flutter_svg nitrogen_lottie
dart pub add nitrogen_types nitrogen_flutter_svg nitrogen_lottie

dart pub add --dev build_runner nitrogen
```

To generate bindings:
Expand All @@ -72,7 +72,7 @@ dart run build_runner build

## Configuration

Nitrogen's configuration can be set in the `nitrogen` section of your pubspec.yaml. For most cases, the default
Nitrogen's configuration can be set in the `nitrogen` section of your pubspec.yaml. In most cases, the default
configuration works out of the box.

### Example
Expand All @@ -82,12 +82,9 @@ A simple configuration looks like:
nitrogen:
package: true
prefix: 'MyPrefix'
flutter-extension: true
asset-key: file
assets:
theme:
path: assets/themes
fallback: light
key: file
themes:
fallback: assets/themes/light
```
### `package`
Expand All @@ -109,11 +106,7 @@ class MyPrefixAssets {
}
```

### `flutter-extension`

Optional. Defaults to `true`. Controls whether to generate the Flutter extension.

### `asset-key`
### `key`

Optional. Defaults to `file`. Controls the generated assets' key parameters. The following options are supported:

Expand All @@ -123,33 +116,14 @@ Optional. Defaults to `file`. Controls the generated assets' key parameters. The
| grpc-enum | parent directory and file name, without the extension | `assets/images/foo.png` | `IMAGES_FOO` |


### `assets`
### `themes`

Optional. Defaults to `standard`. Controls the structure of generated classes. The following options are supported:
Optional. Defaults to `null`. Controls whether to generate an additional `asset_themes.nitrogen.dart` file. Useful for
working with theme-specific assets.

#### Basic:

Generates bare-bones classes without additional utilities.
```yaml
nitrogen:
generation: basic
```

#### Standard (default):

Generates classes with an additional `contents` map for working with assets in that directory.
```yaml
nitrogen:
generation: standard
```

#### Theme:

Generates an additional `asset_themes.nitrogen.dart` file. Useful for working with theme-specific assets.
```yaml
nitrogen:
assets:
theme:
path: assets/themes # Path to themes, relative to package root. Assumes all themes are directly under assets/themes.
fallback: light # A fallback theme for when an asset is not specified, relative to 'path', i.e. assets/themes/light.
themes:
# Path to fallback theme, relative to package root. Assumes that all themes are under 'assets/themes'.
fallback: assets/themes/light
```
70 changes: 27 additions & 43 deletions nitrogen/lib/nitrogen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import 'dart:async';

import 'package:build/build.dart';
import 'package:glob/glob.dart';
import 'package:nitrogen/src/configuration/assets.dart';
import 'package:nitrogen/src/configuration/configuration.dart';
import 'package:nitrogen/src/configuration/configuration_exception.dart';
import 'package:nitrogen/src/file_system.dart';
import 'package:nitrogen/src/generators/assets/basic_generator.dart';
import 'package:nitrogen/src/generators/assets/standard_generator.dart';
import 'package:nitrogen/src/generators/assets/theme_generator.dart';
import 'package:nitrogen/src/generators/extensions/flutter_extension_generator.dart';
import 'package:nitrogen/src/generators/asset_generator.dart';
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';
Expand All @@ -27,6 +25,8 @@ class NitrogenBuilder extends Builder {
FutureOr<void> 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 walker = Walker(configuration.package, configuration.flutterAssets, configuration.key.call);
Expand All @@ -36,50 +36,35 @@ class NitrogenBuilder extends Builder {
walker.walk(assets, asset);
}

final assetsOutput = AssetId(buildStep.inputId.package, join('lib', 'src', 'assets.nitrogen.dart'));
switch (configuration.assets) {
case NoAssets _:
return;

case BasicAssets _:
await buildStep.writeAsString(
assetsOutput,
BasicGenerator(configuration.prefix, assets).generate(),
);
lintReservedKeyword(assets);

case StandardAssets _:
await buildStep.writeAsString(
assetsOutput,
StandardGenerator(configuration.prefix, assets, {}).generate(),
);

case ThemeAssets(:final path, :final fallback):
var themes = assets;
for (final segment in split(path).skip(1)) {
themes = themes.children[segment]! as AssetDirectory;
}

final fallbackTheme = themes.children[fallback]! as AssetDirectory;
final assetsOutput = AssetId(buildStep.inputId.package, join('lib', 'src', 'assets.nitrogen.dart'));
if (configuration.fallbackTheme case final fallback?) {
var themes = assets;
var fallbackTheme = assets;
for (final segment in split(fallback).skip(1)) {
themes = fallbackTheme;
fallbackTheme = fallbackTheme.children[segment]! as AssetDirectory;
}

await buildStep.writeAsString(
assetsOutput,
StandardGenerator(configuration.prefix, assets, { themes }).generate(),
);
await buildStep.writeAsString(
assetsOutput,
AssetGenerator(configuration.prefix, assets, { themes }).generate(),
);

await buildStep.writeAsString(
AssetId(buildStep.inputId.package, join('lib', 'src', 'asset_themes.nitrogen.dart')),
ThemeGenerator(configuration.prefix, themes, fallbackTheme).generate(),
);
}
await buildStep.writeAsString(
AssetId(buildStep.inputId.package, join('lib', 'src', 'asset_themes.nitrogen.dart')),
ThemeGenerator(configuration.prefix, themes, fallbackTheme).generate(),
);

if (configuration.flutterExtension) {
} else {
await buildStep.writeAsString(
AssetId(buildStep.inputId.package, join('lib', 'src', 'flutter_extension.nitrogen.dart')),
FlutterExtensionGenerator().generate(),
assetsOutput,
AssetGenerator(configuration.prefix, assets, {}).generate(),
);
}

} on ConfigurationException {
} on NitrogenException {
return;
}
}
Expand All @@ -89,7 +74,6 @@ class NitrogenBuilder extends Builder {
r'$package$': [
'lib/src/assets.nitrogen.dart',
'lib/src/asset_themes.nitrogen.dart',
'lib/src/flutter_extension.nitrogen.dart'
],
};

Expand Down
2 changes: 1 addition & 1 deletion nitrogen/lib/nitrogen_extension.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'package:nitrogen/src/generators/libraries.dart';
export 'package:nitrogen/src/libraries.dart';
60 changes: 0 additions & 60 deletions nitrogen/lib/src/configuration/assets.dart

This file was deleted.

Loading

0 comments on commit f745547

Please sign in to comment.