pub-package-explorer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pub Package Explorer

Pub包浏览器

Use this workflow to read package source code for Dart or Flutter, including packages not currently installed in the project.
使用该工作流可以阅读Dart或Flutter的包源代码,包括当前项目中未安装的包。

Choose the Source Strategy

选择来源策略

  1. Prefer
    .dart_tool/package_config.json
    when the package is already in the project dependency graph.
  2. Use
    dart pub unpack
    when the package is missing from
    package_config.json
    or when source needs inspection before adding the dependency.
  3. Prefer the installed package path when both are available to match the project-resolved version.
  1. 若包已存在于项目依赖图中,优先使用
    .dart_tool/package_config.json
    获取路径。
  2. package_config.json
    中不存在该包,或是需要在添加依赖前查看源码,使用
    dart pub unpack
    命令。
  3. 若两种路径都可用,优先使用已安装包的路径,以匹配项目解析到的版本。

Resolve a Package Source Path

解析包源码路径

  1. Confirm the project contains
    .dart_tool/package_config.json
    .
  2. Read the package entry from
    packages[]
    by
    name
    .
  3. Extract:
  • rootUri
    (package root, usually in pub cache)
  • packageUri
    (usually
    lib/
    )
  1. Build source path as
    rootUri + packageUri
    .
  2. Convert
    file://
    URI to a filesystem path before reading files.
Use this command pattern:
bash
PACKAGE="analyzer"
CONFIG=".dart_tool/package_config.json"

SOURCE_URI="$(jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | (.rootUri + (if (.rootUri | endswith("/")) then "" else "/" end) + .packageUri)
' "$CONFIG")"
Convert the URI to a local path:
bash
SOURCE_PATH="$(printf '%s\n' "$SOURCE_URI" | sed 's#^file://##')"
Then inspect source files under that directory (
rg
,
ls
,
cat
).
  1. 确认项目中存在
    .dart_tool/package_config.json
    文件。
  2. name
    字段从
    packages[]
    数组中读取对应包的条目。
  3. 提取以下字段:
  • rootUri
    (包根目录,通常位于pub缓存中)
  • packageUri
    (通常为
    lib/
  1. 拼接源码路径为
    rootUri + packageUri
  2. 读取文件前将
    file://
    格式的URI转换为文件系统路径。
使用以下命令模板:
bash
PACKAGE="analyzer"
CONFIG=".dart_tool/package_config.json"

SOURCE_URI="$(jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | (.rootUri + (if (.rootUri | endswith("/")) then "" else "/" end) + .packageUri)
' "$CONFIG")"
将URI转换为本地路径:
bash
SOURCE_PATH="$(printf '%s\n' "$SOURCE_URI" | sed 's#^file://##')"
之后即可查看该目录下的源码文件(可使用
rg
ls
cat
命令)。

Unpack a Package That Is Not Installed

解包未安装的包

Use this path when the package is not in
.dart_tool/package_config.json
or when pre-install inspection is needed.
bash
PACKAGE="analyzer"              # or "analyzer:7.4.0"
OUTPUT_DIR="/tmp/pub-unpack"

mkdir -p "$OUTPUT_DIR"
dart pub unpack "$PACKAGE" --output "$OUTPUT_DIR"
Find the extracted directory and inspect it:
bash
PACKAGE_NAME="${PACKAGE%%:*}"
UNPACKED_DIR="$(find "$OUTPUT_DIR" -maxdepth 1 -type d -name "${PACKAGE_NAME}-*" | sort | tail -n 1)"

ls "$UNPACKED_DIR"
ls "$UNPACKED_DIR/lib"
rg --files "$UNPACKED_DIR/lib"
Notes:
  • dart pub unpack
    downloads and extracts a package even when it is not in the current project's dependencies.
  • Use
    --force
    if the output directory already contains an unpacked copy that must be overwritten.
  • --output
    controls where the extracted
    <package>-<version>
    folder is created.
当包不在
.dart_tool/package_config.json
中,或是需要在安装前查看源码时,使用该方法。
bash
PACKAGE="analyzer"              # 也可指定版本号,如 "analyzer:7.4.0"
OUTPUT_DIR="/tmp/pub-unpack"

mkdir -p "$OUTPUT_DIR"
dart pub unpack "$PACKAGE" --output "$OUTPUT_DIR"
找到解压后的目录并查看:
bash
PACKAGE_NAME="${PACKAGE%%:*}"
UNPACKED_DIR="$(find "$OUTPUT_DIR" -maxdepth 1 -type d -name "${PACKAGE_NAME}-*" | sort | tail -n 1)"

ls "$UNPACKED_DIR"
ls "$UNPACKED_DIR/lib"
rg --files "$UNPACKED_DIR/lib"
注意事项:
  • 即使包不在当前项目的依赖中,
    dart pub unpack
    也可以下载并解压对应包。
  • 如果输出目录中已存在解压后的包副本需要覆盖,可添加
    --force
    参数。
  • --output
    参数用于指定解压后的
    <package>-<version>
    文件夹的存放位置。

Useful Variants

实用变体

  • Return only the package root:
bash
jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | .rootUri
' .dart_tool/package_config.json
  • Return both root and package URI:
bash
jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | "\(.rootUri)\t\(.packageUri)"
' .dart_tool/package_config.json
  • 仅返回包根目录:
bash
jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | .rootUri
' .dart_tool/package_config.json
  • 同时返回根目录和包URI:
bash
jq -r --arg pkg "$PACKAGE" '
  .packages[]
  | select(.name == $pkg)
  | "\(.rootUri)\t\(.packageUri)"
' .dart_tool/package_config.json

Verification and Error Handling

验证与错误处理

  • If no package matches in
    package_config.json
    , switch to
    dart pub unpack
    instead of stopping.
  • If
    .dart_tool/package_config.json
    is missing, run dependency resolution first (
    dart pub get
    or
    flutter pub get
    ) and retry.
  • Prefer reading from
    rootUri + packageUri
    because package APIs are exposed from that subpath, not always from the package root.
  • If
    dart pub unpack
    fails, report the exact failure (for example network access, invalid descriptor, or permissions for output path).
  • 如果在
    package_config.json
    中没有匹配到对应包,切换使用
    dart pub unpack
    而非直接终止流程。
  • 如果缺少
    .dart_tool/package_config.json
    文件,先运行依赖解析命令(
    dart pub get
    flutter pub get
    )后重试。
  • 优先从
    rootUri + packageUri
    路径读取内容,因为包的API是从该子路径暴露的,不一定都在包根目录下。
  • 如果
    dart pub unpack
    执行失败,需返回准确的失败原因(例如网络访问问题、描述符无效、输出路径权限不足等)。