pub-package-explorer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pub Package Explorer

Pub包浏览器

Use this workflow to read source code for a dependency package in a Dart or Flutter project.
使用此工作流可查看Dart或Flutter项目中依赖包的源码。

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. 根据包名从
    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
等命令)。

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, stop and report that the package is not in
    package_config.json
    .
  • 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.
  • 若未找到匹配的包,需终止操作并提示该包不在
    package_config.json
    中。
  • .dart_tool/package_config.json
    文件不存在,需先执行依赖解析(
    dart pub get
    flutter pub get
    ),然后重试。
  • 建议优先从
    rootUri + packageUri
    路径读取,因为包的API通常是从该子路径暴露的,而非总是从包根目录。