|
| 1 | +# pubspec_gen |
| 2 | + |
| 3 | +[](https://pub.dev/packages/pubspec_gen) |
| 4 | + |
| 5 | +A Dart and Flutter builder that generates a Dart class containing information from `pubspec.yaml`. |
| 6 | + |
| 7 | +This allows you to access information like package name, version, description, repository URL, and more directly from your Dart code. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 🚀 Motivation |
| 12 | + |
| 13 | +Keeping `pubspec.yaml` information accessible in code is useful for: |
| 14 | + |
| 15 | +- Displaying app name, version, or description in UIs or logs. |
| 16 | +- Avoiding hardcoding and duplication between `pubspec.yaml` and Dart code. |
| 17 | +- Building widgets or commands that reflect package metadata. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## ✨ Installation |
| 22 | + |
| 23 | +Add to your project's `pubspec.yaml`: |
| 24 | + |
| 25 | +```yaml |
| 26 | +dev_dependencies: |
| 27 | + pubspec_gen: ^<latest_version> |
| 28 | + build_runner: any |
| 29 | +``` |
| 30 | +
|
| 31 | +--- |
| 32 | +
|
| 33 | +## 🔧 Configuration (optional) |
| 34 | +
|
| 35 | +You can customize which fields to generate by adding a `build.yaml` to your project: |
| 36 | + |
| 37 | +```yaml |
| 38 | +targets: |
| 39 | + $default: |
| 40 | + builders: |
| 41 | + pubspec_gen|pubspec_gen: |
| 42 | + options: |
| 43 | + fields: |
| 44 | + - path: name |
| 45 | + - path: version |
| 46 | + - path: description |
| 47 | + - path: repository.url |
| 48 | + name: repositoryUrl |
| 49 | + default: https://github.com/example/repository |
| 50 | +``` |
| 51 | + |
| 52 | +### 🏗️ Field properties: |
| 53 | + |
| 54 | +| Property | Description | |
| 55 | +| ----------| -----------------------------------------------------------| |
| 56 | +| `path` | Path in YAML, supports nested (`repository.url`) | |
| 57 | +| `name` | (optional) Name of the property in the generated class | |
| 58 | +| `default` | (optional) Default value if not found in pubspec.yaml | |
| 59 | + |
| 60 | +If `name` is not set, it defaults to the normalized camelCase from `path`. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## 🛠️ Usage |
| 65 | + |
| 66 | +Run the builder: |
| 67 | + |
| 68 | +```bash |
| 69 | +dart run build_runner build |
| 70 | +``` |
| 71 | + |
| 72 | +It generates: |
| 73 | + |
| 74 | +``` |
| 75 | +lib/generated/pubspec_info.dart |
| 76 | +``` |
| 77 | + |
| 78 | +Example usage: |
| 79 | + |
| 80 | +```dart |
| 81 | +import 'generated/pubspec_info.dart'; |
| 82 | +
|
| 83 | +void main() { |
| 84 | + print('Package name: ${PubspecInfo.name}'); |
| 85 | + print('Version: ${PubspecInfo.version}'); |
| 86 | + print('Description: ${PubspecInfo.description}'); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## 📜 Example of generated code |
| 93 | + |
| 94 | +```dart |
| 95 | +// GENERATED CODE - DO NOT MODIFY BY HAND |
| 96 | +// ignore_for_file: constant_identifier_names |
| 97 | +
|
| 98 | +/// Package information generated from pubspec.yaml |
| 99 | +class PubspecInfo { |
| 100 | + const PubspecInfo._(); |
| 101 | +
|
| 102 | + static const String name = 'my_package'; |
| 103 | + static const String version = '1.0.0'; |
| 104 | + static const String description = 'My awesome package'; |
| 105 | + static const String repositoryUrl = 'https://github.com/example/repository'; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 🔍 Full example |
| 112 | + |
| 113 | +Check the [`example/`](./example) directory for a working example. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## 💡 Contributing |
| 118 | + |
| 119 | +Contributions are welcome! If you find any issues or have ideas for improvements: |
| 120 | + |
| 121 | +1. Open an issue. |
| 122 | +2. Submit a pull request. |
| 123 | +3. Feel free to suggest new features. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## 📝 License |
| 128 | + |
| 129 | +This project is licensed under the BSD-3-Clause License - see the [LICENSE](LICENSE) file for details. |
0 commit comments