Skip to content

Commit

Permalink
4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jul 3, 2023
1 parent 5ac6581 commit f396a08
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 170 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
## 4.0.0-pre.1 - 2023-06-30
## 4.0.0 - 2023-07-03

### Changed

- Dart v3
- Refactoring
- Sealed class instead of global variables
- More properties
- Comments
- Tests
- Update README.md

## 3.0.1 - 2021-05-14

Expand Down
195 changes: 71 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,44 @@
[![Linter](https://img.shields.io/badge/style-linter-40c4ff.svg)](https://pub.dev/packages/linter)
[![GitHub stars](https://img.shields.io/github/stars/plugfox/pubspec_generator?style=social)](https://github.com/plugfox/pubspec_generator/)

<!--
[![Coverage](https://codecov.io/gh/PlugFox/pubspec_generator/branch/master/graph/badge.svg)](https://codecov.io/gh/PlugFox/pubspec_generator)
-->
Pubspec Generator is a Dart library that auto-generates a class containing the information from `pubspec.yaml` description, dependencies and the current app version for all possible platforms. This is an efficient way to keep the application metadata up-to-date across your entire project.

## Setup
## Features

Add the following lines to the pubspec.yaml:
- **Automated code generation:** Create a Dart class that includes your `pubspec.yaml` description and current application version automatically.

```yaml
- **Cross-platform compatibility:** Supports all platforms where Dart is available.

**Continually updated:** The generated class updates as your pubspec.yaml changes, so your application's metadata remains current at all times.

## Installation

First, add pubspec_info_codegen to your `pubspec.yaml`:

```dart
dev_dependencies:
build_runner: ^1.12.2
pubspec_generator: ^3.0.0
build_runner: <VERSION>
pubspec_generator: <VERSION>
```

or just run

```bash
$ dart pub add --dev build_runner pubspec_generator
```

and then execute in the console:
Then, run the flutter pub get command to fetch the dependency:

```bash
dart run build_runner build
$ dart pub get
```

## Path of creation
## Configuration

Create `build.yaml` at project root (near with `pubspec.yaml`).
And set output path:

```yaml
# Read about `build.yaml` at https://pub.dev/packages/build_config
targets:
$default:
sources:
Expand All @@ -49,129 +60,46 @@ targets:
output: lib/src/constants/pubspec.yaml.g.dart
```

## Result example
- Read more about `build.yaml` at [here](https://pub.dev/packages/build_config).

![](version.png)
Generated record for version.
## Usage

By default, at project path `lib/src/constants/pubspec.yaml.g.dart`:
To use the library, you need to run the provided script which triggers the code generation. Below is an example command to do so:

```dart
sealed class Pubspec {
static const PubspecVersion version = (
representation: r'4.0.0-pre.1',
canonical: r'4.0.0-pre.1',
major: 4,
minor: 0,
patch: 0,
preRelease: <String>[r'pre', r'1'],
build: <String>[],
);
static const String name = r'pubspec_generator';
static const String description =
r'Code generator pubspec.yaml.g.dart from pubspec.yaml. Just import `pubspec_generator` and then run `dart run build_runner build`';
static const String homepage =
r'https://github.com/PlugFox/pubspec_generator';
static const String repository =
r'https://github.com/PlugFox/pubspec_generator/tree/master';
static const String issueTracker =
r'https://github.com/PlugFox/pubspec_generator/issues';
static const String documentation = r'';
static const String publishTo = r'https://pub.dev/';
static const List<Object> funding = <Object>[
r'https://www.buymeacoffee.com/plugfox',
r'https://www.patreon.com/plugfox',
r'https://boosty.to/plugfox',
];
static const List<Object> falseSecrets = <Object>[];
static const List<Object> screenshots = <Object>[
<String, Object>{
'description': r'Icon',
'path': r'.img/icon.png',
},
];
static const List<Object> topics = <Object>[
r'yaml',
r'pubspec',
r'generator',
r'build',
r'build_runner',
r'codegeneration',
];
static const Map<String, String> environment = <String, String>{
'sdk': '>=3.0.0 <4.0.0',
};
static const Map<String, Object> platforms = <String, Object>{
'android': r'',
'ios': r'',
'linux': r'',
'macos': r'',
'web': r'',
'windows': r'',
};
static const Map<String, Object> dependencies = <String, Object>{
'analyzer': r'^6.0.0',
'build': r'^2.3.1',
'pub_semver': r'^2.1.4',
'yaml': r'^3.1.2',
};
static const Map<String, Object> devDependencies = <String, Object>{
'build_runner': r'^2.3.3',
'build_runner_core': r'^7.2.10',
'build_test': r'^2.1.5',
'test': r'^1.21.0',
};
static const Map<String, Object> dependencyOverrides = <String, Object>{};
static const Map<String, Object> executables = <String, Object>{};
static const Map<String, Object> source = <String, Object>{
'name': name,
'description': description,
'repository': repository,
'issue_tracker': issueTracker,
'homepage': homepage,
'documentation': documentation,
'publish_to': publishTo,
'version': version,
'funding': funding,
'false_secrets': falseSecrets,
'screenshots': screenshots,
'topics': topics,
'platforms': platforms,
'environment': environment,
'dependencies': dependencies,
'dev_dependencies': devDependencies,
'dependency_overrides': dependencyOverrides,
};
}
```bash
$ dart run build_runner build --delete-conflicting-outputs
```

- Example provided without comments.
This command generates a `pubspec.yaml.g.dart` file in your `lib` directory, which contains a class with your `pubspec.yaml` description and the current app version. You can import this file in your Dart code and use the class to access your app's metadata.

## Use Cases

1. **Consistent app metadata across multiple platforms:** This library ensures that the application metadata (description, version) is consistent across all platforms, reducing the chances of discrepancies.

2. **Reduced manual errors:** Automated code generation eliminates the need for manual updates and thereby reduces the potential for errors.

3. **Faster development process:** Less time spent managing app metadata means more time for feature development.

4. **Dynamic display of app information:** You can use the generated class to dynamically display the app's description and version in the UI, which would automatically stay updated with changes in `pubspec.yaml`.

5. **Automated Version Migration:** With the library's functionality, you can also handle automated version migration. If your app has different behavior or functionalities across different versions, you can use the auto-generated class to identify the current version and execute the necessary migration logic. This ensures a smooth and seamless user experience across updates.

6. **Backend Request Headers:** The generated class can also be used to add meta information from `pubspec.yaml` into the headers of your backend requests. This can help your backend service identify the app version that is making the request, which can be useful for debugging, tracking, or version-specific behaviors on the backend.

7. **App Analytics Configuration:** The metadata from `pubspec.yaml` (like app version) can be sent to your analytics tool (such as Firebase Analytics) to track the usage of different app versions. This can provide valuable insights into your user base, helping you understand how different versions of your app are being used, which versions are most popular, and how updates affect user behavior. It can also assist in identifying any issues or crashes specific to a certain version.

## Result

![](version.png)
Generated record for version. A full output example can be found [here](https://pub.dev/packages/pubspec_generator/example).

## Changelog

Refer to the [Changelog](https://github.com/plugfox/pubspec_generator/blob/master/CHANGELOG.md) to get all release notes.

## Maintainers

[Plague Fox](https://plugfox.dev)
- Matiunin Mikhail aka [Plague Fox](https://plugfox.dev)

## Funding

Expand All @@ -185,4 +113,23 @@ We appreciate any form of support, whether it's a financial donation or just a s

## License

[MIT](https://github.com/plugfox/pubspec_generator/blob/master/LICENSE)
The [MIT](https://github.com/plugfox/pubspec_generator/blob/master/LICENSE) License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT). It's a simple license that has minimal restrictions on what end-users can do with the software. Here's a summary of the permissions, conditions, and limitations under the MIT License:

**Permissions:**

- **Commercial Use:** You are free to use the software for commercial purposes.
- **Modification:** You can make changes to the software and distribute your modifications.
- **Distribution:** You can distribute the original or modified (derivative) software.
- **Private Use:** You are free to use the software for private purposes.

**Conditions:**

- **Include Copyright:** You must include the original copyright notice in any copy of the software/source code.
- **Include License:** You must include a copy of the license in any copy of the software/source code.

**Limitations:**

- **No Liability:** The software is provided "as is". The authors or copyright holders cannot be held liable for any damages or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
- **No Warranty:** The authors or copyright holders provide no warranty of any kind.

It's important to note that this is a simplified explanation of the MIT License. If you're using MIT-licensed software in a manner that could expose you to legal risk, it's always a good idea to consult with a qualified legal professional.
58 changes: 34 additions & 24 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,46 @@ sealed class Pubspec {
/// When you select a version,
/// follow [semantic versioning](https://semver.org/).
static const PubspecVersion version = (
// Non-canonical string representation of the version.
representation: r'4.0.0-pre.1',
// Canonicalized string representation of the version.
canonical: r'4.0.0-pre.1',
// MAJOR version when you make incompatible API changes.
// The major version number: 1 in "1.2.3".
/// Non-canonical string representation of the version as provided
/// in the pubspec.yaml file.
representation: r'4.0.0',

/// Returns a 'canonicalized' representation
/// of the application version.
/// This represents the version string in accordance with
/// Semantic Versioning (SemVer) standards.
canonical: r'4.0.0',

/// MAJOR version when you make incompatible API changes.
/// The major version number: 1 in "1.2.3".
major: 4,
// MINOR version when you add functionality
// in a backward compatible manner.
// The minor version number: 2 in "1.2.3".

/// MINOR version when you add functionality
/// in a backward compatible manner.
/// The minor version number: 2 in "1.2.3".
minor: 0,
// PATCH version when you make backward compatible bug fixes.
// The patch version number: 3 in "1.2.3".

/// PATCH version when you make backward compatible bug fixes.
/// The patch version number: 3 in "1.2.3".
patch: 0,
// The pre-release identifier: "foo" in "1.2.3-foo".
preRelease: <String>[r'pre', r'1'],
// The build identifier: "foo" in "1.2.3+foo".

/// The pre-release identifier: "foo" in "1.2.3-foo".
preRelease: <String>[],

/// The build identifier: "foo" in "1.2.3+foo".
build: <String>[],
);

/// Build date
static final DateTime timestamp = DateTime(
/// Build date and time (UTC)
static final DateTime timestamp = DateTime.utc(
2023,
6,
30,
14,
6,
45,
702,
693,
7,
3,
9,
49,
54,
100,
370,
);

/// Name
Expand Down Expand Up @@ -347,7 +357,7 @@ sealed class Pubspec {
r'yaml',
r'generator',
r'build',
r'codegen',
r'version',
];

/// Environment
Expand Down
6 changes: 3 additions & 3 deletions lib/src/generator/timestamp_generator_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:pubspec_generator/src/generator/pubspec_generator.dart';
mixin TimestampGeneratorMixin on PubspecGenerator {
@override
Iterable<String> generate(Map<String, Object> pubspec) sync* {
final now = DateTime.now();
final now = DateTime.now().toUtc();
final builder = StringBuffer()
..writeln('/// Build date')
..writeln('static final DateTime timestamp = DateTime(')
..writeln('/// Build date and time (UTC)')
..writeln('static final DateTime timestamp = DateTime.utc(')
..writeln(' ${now.year},')
..writeln(' ${now.month},')
..writeln(' ${now.day},')
Expand Down
Loading

0 comments on commit f396a08

Please sign in to comment.