搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案