搜索插件、代码仓与 APP 分类

pubspec_generator 封面
插件详情v5.0.2

pubspec_generator

从 pubspec.yaml 生成代码文件 pubspec.yaml.g.dart。只需导入 `pubspec_generator`,然后运行 `dart run build_runner build`

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2060
喜欢数
45
Pub 分数
140

安装插件

flutter pub add pubspec_generator

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,可从项目的 pubspec.yaml 生成 pubspec.yaml.g.dart 文件。它会自动将资产、依赖项和其他元数据提取到 Dart 文件中,从而实现对 pubspec 数据的类型安全访问。只需添加该包并运行 dart run build_runner build 即可生成输出。非常适合需要动态访问 pubspec 信息但又不想手动解析 YAML 的项目。

适合场景

  • 以编程方式访问资产路径
  • 从 pubspec 依赖项生成代码
  • 启用类型安全的 pubspec 元数据
  • 自动化构建中的资产管理
  • 将 pubspec 数据集成到应用逻辑中

主要特点

  • 从 pubspec.yaml 生成 Dart 代码
  • 支持所有 Flutter 平台
  • 使用 build_runner 进行自动化
  • 提供类型安全的 pubspec 访问
  • 所需设置最少

适合对象

  • 具有复杂资产结构的项目
  • 构建时代码生成需要
  • 需要动态 pubspec 访问的 App
  • 希望实现 pubspec 自动化处理的团队
  • 使用 build_runner 工作流的开发者

使用建议

  • 需要设置 build_runner
  • 生成的文件不应手动编辑
  • 可能会略微增加构建时间
  • 简单项目不需要
  • 依赖于正确的 pubspec.yaml 格式

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

pubspec_generator

Code generator pubspec.yaml.g.dart from pubspec.yaml

Pub Checkout Coverage Code size License: MIT Linter GitHub stars

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.

Features

  • Automated code generation: Create a Dart class that includes your pubspec.yaml description and current application version automatically.

  • 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.

Command line utility

You can use the command line utility to generate a pubspec.dart file from a pubspec.yaml file. This is useful for quickly generating the Dart class without needing to set up a full build environment.

dart pub global activate pubspec_generator
dart pub global run pubspec_generator:generate --input pubspec.yaml --output lib/src/pubspec.yaml.g.dart

Better to prefer this approach for quick generation over the fully fledged build runner setup.

Installation

First, add pubspec_info_codegen to your pubspec.yaml:

dev_dependencies:
  build_runner: <VERSION>
  pubspec_generator: <VERSION>

or just run

$ dart pub add --dev build_runner pubspec_generator

Then, run the flutter pub get command to fetch the dependency:

$ dart pub get

Configuration

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

targets:
  $default:
    sources:
      - $package$
      - lib/**
      - pubspec.yaml
    builders:
      pubspec_generator:
        options:
          output: lib/src/constants/pubspec.yaml.g.dart
  • Read more about build.yaml at here.

Usage

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 run build_runner build --delete-conflicting-outputs

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

Generated record for version. A full output example can be found here.

Changelog

Refer to the Changelog to get all release notes.

Maintainers

Funding

If you want to support the development of our library, there are several ways you can do it:

We appreciate any form of support, whether it's a financial donation or just a star on GitHub. It helps us to continue developing and improving our library. Thank you for your support!

License

The MIT 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.

所属分类

依赖

依赖清单 7 项

源码仓库

使用此插件的项目