Skip to content

Commit 16689b5

Browse files
Merge pull request #59 from pedrox-hs/feat/pubspec-gen/create-package
feat(package): Create pubspec_gen package
2 parents ae32dcc + b669f94 commit 16689b5

18 files changed

+474
-0
lines changed

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ workspace:
99
- auto_route/generator
1010
- error_adapter
1111
- logify
12+
- pubspec_gen
1213
- shared_lints
1314
- shic
1415
- simple_nav

pubspec_gen/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
5+
# Avoid committing pubspec.lock for library packages; see
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
7+
pubspec.lock
8+
9+
# Flutter
10+
.dart_tool/
11+
.flutter-plugins
12+
.flutter-plugins-dependencies

pubspec_gen/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial version.

pubspec_gen/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2025, Pedro Silva
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
3. Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pubspec_gen/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# pubspec_gen
2+
3+
[![pub package](https://img.shields.io/pub/v/pubspec_gen.svg)](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.

pubspec_gen/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:shared_lints/dart.yaml

pubspec_gen/build.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
builders:
2+
pubspec_gen:
3+
import: "package:pubspec_gen/pubspec_gen.dart"
4+
builder_factories: ["packageInfoBuilder"]
5+
build_extensions: { "$package$": ["lib/generated/pubspec_info.dart"] }
6+
auto_apply: root_package
7+
build_to: source
8+
defaults:
9+
options:
10+
output: lib/generated/pubspec_info.dart
11+
class_name: PubspecInfo
12+
header: |
13+
// GENERATED CODE - DO NOT MODIFY BY HAND
14+
fields:
15+
- path: name
16+
default: unknown_package
17+
- path: version
18+
default: 0.0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'package:pubspec_gen_example/main.dart';

pubspec_gen/example/build.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
targets:
2+
$default:
3+
builders:
4+
pubspec_gen|pubspec_gen:
5+
options:
6+
fields:
7+
- path: name
8+
- path: version
9+
- path: description
10+
- path: repository.url
11+
name: repositoryUrl
12+
default: https://example.com/default

pubspec_gen/example/lib/generated/pubspec_info.dart

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)