pub-package-explorer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePub 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
解析包源码路径
- 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 - 根据包名从数组中读取对应的包条目。
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://##')"之后即可查看该目录下的源码文件(可使用、、等命令)。
rglscatUseful 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, stop and report that the package is not in .
package_config.json - 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
- 若未找到匹配的包,需终止操作并提示该包不在中。
package_config.json - 若文件不存在,需先执行依赖解析(
.dart_tool/package_config.json或dart pub get),然后重试。flutter pub get - 建议优先从路径读取,因为包的API通常是从该子路径暴露的,而非总是从包根目录。
rootUri + packageUri