pub-package-explorer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePub 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
选择来源策略
- Prefer when the package is already in the project dependency graph.
.dart_tool/package_config.json - Use when the package is missing from
dart pub unpackor when source needs inspection before adding the dependency.package_config.json - Prefer the installed package path when both are available to match the project-resolved version.
- 若包已存在于项目依赖图中,优先使用获取路径。
.dart_tool/package_config.json - 若中不存在该包,或是需要在添加依赖前查看源码,使用
package_config.json命令。dart pub unpack - 若两种路径都可用,优先使用已安装包的路径,以匹配项目解析到的版本。
Resolve a Package Source Path
解析包源码路径
- Confirm the project contains .
.dart_tool/package_config.json - Read the package entry from by
packages[].name - Extract:
- (package root, usually in pub cache)
rootUri - (usually
packageUri)lib/
- Build source path as .
rootUri + packageUri - Convert URI to a filesystem path before reading files.
file://
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 (, , ).
rglscat- 确认项目中存在文件。
.dart_tool/package_config.json - 按字段从
name数组中读取对应包的条目。packages[] - 提取以下字段:
- (包根目录,通常位于pub缓存中)
rootUri - (通常为
packageUri)lib/
- 拼接源码路径为。
rootUri + packageUri - 读取文件前将格式的URI转换为文件系统路径。
file://
使用以下命令模板:
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://##')"之后即可查看该目录下的源码文件(可使用、、命令)。
rglscatUnpack a Package That Is Not Installed
解包未安装的包
Use this path when the package is not in or when pre-install inspection is needed.
.dart_tool/package_config.jsonbash
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:
- downloads and extracts a package even when it is not in the current project's dependencies.
dart pub unpack - Use if the output directory already contains an unpacked copy that must be overwritten.
--force - controls where the extracted
--outputfolder is created.<package>-<version>
当包不在中,或是需要在安装前查看源码时,使用该方法。
.dart_tool/package_config.jsonbash
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.jsonVerification and Error Handling
验证与错误处理
- If no package matches in , switch to
package_config.jsoninstead of stopping.dart pub unpack - If is missing, run dependency resolution first (
.dart_tool/package_config.jsonordart pub get) and retry.flutter pub get - Prefer reading from because package APIs are exposed from that subpath, not always from the package root.
rootUri + packageUri - If fails, report the exact failure (for example network access, invalid descriptor, or permissions for output path).
dart pub unpack
- 如果在中没有匹配到对应包,切换使用
package_config.json而非直接终止流程。dart pub unpack - 如果缺少文件,先运行依赖解析命令(
.dart_tool/package_config.json或dart pub get)后重试。flutter pub get - 优先从路径读取内容,因为包的API是从该子路径暴露的,不一定都在包根目录下。
rootUri + packageUri - 如果执行失败,需返回准确的失败原因(例如网络访问问题、描述符无效、输出路径权限不足等)。
dart pub unpack