Repository: FlutterGen/flutter_gen

Issue:
Support arbitrary files
Sometimes I have assets that are not on the list of supported extensions but I would still like to have strings generated for them. At the moment flutter_gen just ignores these files and generates no strings for them.

### Example:

For the following snippet from `pubspec.yaml`:

```yaml
flutter:
  assets:
    - CHANGELOG.md
```

I would expect to have a string generated for `CHANGLELOG.md` to avoid using hardcoded strings in my code to reference this asset.


Discussion: @shilangyu 

Thank you for using the FlutterGen.
Recently, We fixed the bug by `1.3.0` and `1.3.1`.

So please upgrade to `1.3.1+`.
``` shell
$ brew upgrade fluttergen/tap/fluttergen

or

$ dart pub global activate flutter_gen
```

And just now I add some arbitrary files to sample settings.

https://github.com/FlutterGen/flutter_gen/commit/3cd380eda1c72a3406f62c91237e61293fb92fac


But this means I need to have my files in a specific folder? For example my changelog.md file is in the root of the project, and the string is not generated for it (as in the example from my initial comment)
The quick workaround using `rootBundle`:

```dart
import 'package:flutter/services.dart' show rootBundle;

FutureBuilder(
  future: rootBundle.loadString('CHANGELOG.md'),
  builder: (context, data) {
    if (!data.hasData) {
      return Container();
    }
    return Markdown(
      data: data.data!,
    );
  }
)
```

Fix patch:
diff --git a/examples/example/lib/gen/assets.gen.dart b/examples/example/lib/gen/assets.gen.dart
index d9597eaff..a545b3d40 100644
--- a/examples/example/lib/gen/assets.gen.dart
+++ b/examples/example/lib/gen/assets.gen.dart
@@ -182,6 +182,7 @@ class $AssetsLottieWrongGen {
 class MyAssets {
   MyAssets._();
 
+  static const String readme = 'README.md';
   static const $AssetsFlareGen flare = $AssetsFlareGen();
   static const $AssetsImagesGen images = $AssetsImagesGen();
   static const $AssetsJsonGen json = $AssetsJsonGen();
@@ -190,6 +191,9 @@ class MyAssets {
   static const $AssetsMovieGen movie = $AssetsMovieGen();
   static const $AssetsRiveGen rive = $AssetsRiveGen();
   static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
+
+  /// List of all assets
+  static List<String> get values => [readme];
 }
 
 class AssetGenImage {
@@ -269,10 +273,12 @@ class AssetGenImage {
 }
 
 class SvgGenImage {
-  const SvgGenImage(this._assetName);
+  const SvgGenImage(this._assetName, {this.size = null});
 
   final String _assetName;
 
+  final Size? size;
+
   SvgPicture svg({
     Key? key,
     bool matchTextDirection = false,
diff --git a/examples/example/pubspec.yaml b/examples/example/pubspec.yaml
index 71f41e679..4365cd909 100644
--- a/examples/example/pubspec.yaml
+++ b/examples/example/pubspec.yaml
@@ -70,6 +70,7 @@ flutter_gen:
 flutter:
   uses-material-design: true
   assets:
+    - README.md
     - assets/images/
     - assets/images/chip3/chip3.jpg
     - assets/images/chip3/chip3.jpg # duplicated
diff --git a/packages/core/lib/generators/assets_generator.dart b/packages/core/lib/generators/assets_generator.dart
index d3222bd58..89e5186a2 100644
--- a/packages/core/lib/generators/assets_generator.dart
+++ b/packages/core/lib/generators/assets_generator.dart
@@ -304,23 +304,33 @@ String _dotDelimiterStyleDefinition(
   AssetsGenConfig config,
   List<Integration> integrations,
 ) {
+  final rootPath = Directory(config.rootPath).absolute.uri.toFilePath();
   final buffer = StringBuffer();
   final className = config.flutterGen.assets.outputs.className;
   final assetRelativePathList = _getAssetRelativePathList(
-    config.rootPath,
+    rootPath,
     config.assets,
     config.exclude,
   );
   final assetsStaticStatements = <_Statement>[];
 
   final assetTypeQueue = ListQueue<AssetType>.from(
-      _constructAssetTree(assetRelativePathList, config.rootPath).children);
+      _constructAssetTree(assetRelativePathList, rootPath).children);
 
   while (assetTypeQueue.isNotEmpty) {
     final assetType = assetTypeQueue.removeFirst();
-    final assetAbsolutePath = join(config.rootPath, assetType.path);
+    String assetPath = join(rootPath, assetType.path);
+    final isDirectory = FileSystemEntity.isDirectorySync(assetPath);
+    if (isDirectory) {
+      assetPath = Directory(assetPath).absolute.uri.toFilePath();
+    } else {
+      assetPath = File(assetPath).absolute.uri.toFilePath();
+    }
 
-    if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) {
+    final isRootAsset = !isDirectory &&
+        File(assetPath).parent.absolute.uri.toFilePath() == rootPath;
+    // Handles directories, and explicitly handles root path assets.
+    if (isDirectory || isRootAsset) {
       final statements = assetType.children
           .mapToIsUniqueWithoutExtension()
           .map(
@@ -339,6 +349,16 @@ String _dotDelimiterStyleDefinition(
 
       if (assetType.isDefaultAssetsDirectory) {
         assetsStaticStatements.addAll(statements);
+      } else if (!isDirectory && isRootAsset) {
+        // Creates explicit statement.
+        assetsStaticStatements.add(
+          _createAssetTypeStatement(
+            config,
+            assetType,
+            integrations,
+            basenameWithoutExtension(assetType.path).camelCase(),
+          )!,
+        );
       } else {
         final className = '\$${assetType.path.camelCase().capitalize()}Gen';
         buffer.writeln(_directoryClassGenDefinition(className, statements));


Test patch:
diff --git a/packages/core/test_resources/CHANGELOG.md b/packages/core/test_resources/CHANGELOG.md
new file mode 100644
index 000000000..24f0add0c
--- /dev/null
+++ b/packages/core/test_resources/CHANGELOG.md
@@ -0,0 +1,1 @@
+This is the CHANGELOG.md
diff --git a/packages/core/test_resources/actual_data/assets.gen.dart b/packages/core/test_resources/actual_data/assets.gen.dart
index 51e5aafe1..d423ae32a 100644
--- a/packages/core/test_resources/actual_data/assets.gen.dart
+++ b/packages/core/test_resources/actual_data/assets.gen.dart
@@ -141,12 +141,16 @@ class $AssetsImagesIconsGen {
 class Assets {
   Assets._();
 
+  static const String changelog = 'CHANGELOG.md';
   static const $AssetsFlareGen flare = $AssetsFlareGen();
   static const $AssetsImagesGen images = $AssetsImagesGen();
   static const $AssetsJsonGen json = $AssetsJsonGen();
   static const $AssetsMovieGen movie = $AssetsMovieGen();
   static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
   static const $PicturesGen pictures = $PicturesGen();
+
+  /// List of all assets
+  static List<String> get values => [changelog];
 }
 
 class AssetGenImage {
diff --git a/packages/core/test_resources/pubspec_assets.yaml b/packages/core/test_resources/pubspec_assets.yaml
index 8439eaf18..b305ccf34 100644
--- a/packages/core/test_resources/pubspec_assets.yaml
+++ b/packages/core/test_resources/pubspec_assets.yaml
@@ -23,3 +23,4 @@ flutter:
     - assets/flare/
     - assets/movie/
     - assets/unknown/unknown_mime_type.bk
+    - CHANGELOG.md