Flutter许可证信息收集插件license_gatherer的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter许可证信息收集插件license_gatherer的使用

license_gatherer

license_gatherer 可以将你项目中所有依赖项的许可证信息收集到一个格式化的文件中。

license_gatherer 读取你的 pubspec.yaml 文件来收集所有依赖项(托管、Git、路径和SDK支持)的许可证信息。

示例

你可以在 license_gatherer 的自己的 NOTICES 文件中找到由 license_gatherer 生成的一些示例输出。你可以在这里查看:NOTICES

前提条件

请注意,为了使 license_gatherer 能正常工作,你必须已经在项目中运行了 pub get 命令,并且该命令已经创建了一个 .dart_tool/package_config.json 文件。

安装

要安装 license_gatherer,请运行以下命令:

dart pub global activate license_gatherer

使用

运行 license_gatherer

如果你的环境变量路径设置正确,可以使用以下命令:

license_gatherer -h

否则,请使用以下命令:

dart pub global run license_gatherer

参数

$ license_gatherer -h
license_gatherer v1.1.0 - Copyright Rubin Raithel
-i, --pubspec=<path>          Path to pubspec.yaml to extract licenses from (mandatory)
-o, --notices=<path>          Path to file that notices are saved to
                              (defaults to "NOTICES")

=== Format
-j, --formatfile=<path>       File that contains JSON representation of format to use (see README)
                              (defaults to "")

=== Miscellaneous
-f, --[no-]flutter-version    Whether to dynamically determine flutter version if in dependencies 
                              (defaults to on)
-p, --pedantic                Fail at the slightest sign of trouble (warning)
-c, --color                   Colorize output (CLI)
-h, --help                    Display this usage description
-v, --version                 Display current version

自定义格式

你可以在 JSON 语法中指定自定义的格式。创建一个包含你的格式的文件,并使用 -j--formatfile 提供给 license_gatherer

以下是一个使用默认格式的示例,并附有每个配置选项的文档说明。

// notices_format.json
{
    // 文件顶部的头部文本
    "header": "",

    // 每个单独许可证的格式
    //
    // 变量: {{NAME}}, {{VERSION}}, {{LICENSE}}
    "license": "{{NAME}} ({{VERSION}}):\n\n{{LICENSE}}\n\n",

    // 在每个许可证之间插入的分隔符
    "licenseSeparator": "-----------------------------------------------------------------------------\n\n",

    // 文件底部的尾部文本
    "footer": "",

    // 如果版本不可用时使用什么
    "nullVersion": "?",

    // 如果许可证文本不可用时使用什么
    "nullText": "?",

    // 从前后修剪空白字符和换行符
    //
    // 为了更好的控制,修剪应用于添加头部和尾部之前(仅适用于主体部分)。
    "trim": true,

    // 在文件末尾添加一个换行符(`\n`)
    //
    // 结合此选项与 `[trim]` 可以在末尾实现单个空行。
    "trailingNewline": true
}

许可证

BSD 3-Clause “New” 或 “Revised” 许可证


完整示例 DEMO

假设我们有一个简单的 Flutter 项目结构如下:

my_flutter_project/
├── lib/
│   └── main.dart
├── pubspec.yaml
└── .dart_tool/

步骤 1: 配置 pubspec.yaml

首先确保你的 pubspec.yaml 中包含一些依赖项:

name: my_flutter_project
dependencies:
  flutter:
    sdk: flutter
  http: ^0.13.3

步骤 2: 安装 license_gatherer

在终端中运行以下命令来安装 license_gatherer

dart pub global activate license_gatherer

步骤 3: 创建自定义格式文件

创建一个名为 notices_format.json 的文件,并将其放在项目的根目录下:

// notices_format.json
{
    "header": "This file contains all the licenses for the dependencies used in this project.\n",
    "license": "{{NAME}} ({{VERSION}}):\n\n{{LICENSE}}\n\n",
    "licenseSeparator": "-----------------------------------------------------------------------------\n\n",
    "footer": "\nPlease refer to the LICENSE files provided by the respective packages for more details.",
    "nullVersion": "?",
    "nullText": "?",
    "trim": true,
    "trailingNewline": true
}

步骤 4: 运行 license_gatherer

在终端中运行以下命令来生成 NOTICES 文件:

license_gatherer -i pubspec.yaml -o NOTICES -j notices_format.json

更多关于Flutter许可证信息收集插件license_gatherer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter许可证信息收集插件license_gatherer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用license_gatherer插件来收集许可证信息的代码示例。license_gatherer插件可以帮助你自动生成并收集第三方依赖的许可证信息,这对于开源项目尤其重要。

步骤 1: 添加依赖

首先,你需要在你的pubspec.yaml文件中添加license_gatherer依赖:

dependencies:
  flutter:
    sdk: flutter
  license_gatherer: ^最新版本号 # 请替换为实际的最新版本号

然后运行flutter pub get来获取依赖。

步骤 2: 配置插件

在你的Flutter项目中,你需要创建一个脚本来运行license_gatherer。通常,你可以在tools目录下创建一个脚本文件,例如gather_licenses.dart

// tools/gather_licenses.dart
import 'package:license_gatherer/license_gatherer.dart';
import 'dart:io';

void main() async {
  // 指定你的pubspec.yaml文件路径
  final pubspecPath = File('pubspec.yaml').absolute.path;

  // 收集许可证信息
  final licenses = await collectLicenses(pubspecPath);

  // 输出许可证信息到一个文件
  final outputFile = File('licenses.txt');
  await outputFile.writeAsString(licenses);

  print('许可证信息已收集并保存到 licenses.txt');
}

步骤 3: 运行脚本

你可以使用Dart命令行工具来运行这个脚本。在终端中导航到你的项目根目录,然后运行:

dart tools/gather_licenses.dart

这个命令会读取你的pubspec.yaml文件,收集所有依赖的许可证信息,并将它们保存到licenses.txt文件中。

步骤 4: 检查生成的许可证文件

运行完脚本后,你应该会在项目根目录下看到一个名为licenses.txt的文件。这个文件包含了所有第三方依赖的许可证信息。

完整示例

假设你的项目结构如下:

my_flutter_app/
├── pubspec.yaml
├── tools/
│   └── gather_licenses.dart
└── ...

gather_licenses.dart的内容如之前所示,运行脚本后,你会在my_flutter_app/目录下得到一个licenses.txt文件。

注意事项

  • 确保你的Dart和Flutter环境已经正确安装和配置。
  • 根据需要调整脚本中的路径和输出文件名。
  • 定期检查并更新license_gatherer插件到最新版本,以确保兼容性和功能完整性。

通过上述步骤,你就可以在Flutter项目中使用license_gatherer插件来自动收集和生成第三方依赖的许可证信息了。

回到顶部