如何使用cargo crate列出项目的源文件? [英] How to list a project's source files using the cargo crate?
问题描述
我正在尝试使用 cargo crate<列出 Rust 项目的源文件/a>.我不能简单地列出目录中存在的所有 .rs
文件,因为我想准确地检索编译器在编译过程中看到的文件,这可能不是全部.rs
文件.
I'm trying to list the source files of a Rust project using the cargo crate. I can not just simply list all the .rs
files present in a directory as I want to retrieve exactly the files that the compiler sees during the compilation, which may not be all the .rs
files.
我正在Alacritty 存储库上进行我的实验,它有一个货物工作区,共 3 个项目.到目前为止,这是我的代码:
I'm conducting my experiments on the the Alacritty repository, which has a cargo workspace of 3 projects. Here is my code so far:
extern crate cargo;
use std::path::Path;
use cargo::core::Source;
fn main() {
let path = Path::new("/tmp/alacritty/Cargo.toml");
let config = cargo::util::config::Config::default().unwrap();
let ws = cargo::core::Workspace::new(&path, &config).unwrap();
for pkg in ws.members() {
println!("found package {}", pkg);
let config = ws.config();
let mut src = cargo::sources::PathSource::new(pkg.root(), pkg.package_id().source_id(), config);
src.update().unwrap();
let src_files = src.list_files(pkg).unwrap();
println!("found {} source files", src_files.len());
}
}
输出如下:
found package alacritty v0.5.0-dev (/tmp/alacritty/alacritty)
found 0 source files
found package alacritty_terminal v0.5.0-dev (/tmp/alacritty/alacritty_terminal)
found 0 source files
found package font v0.1.0 (/tmp/alacritty/font)
found 0 source files
工作区的成员已正确找到,但我无法检索每个成员的源文件.我错过了什么?
The members of the workspace are correctly found but I fail to retrieve the source files for each of these members. What am I missing?
推荐答案
您的代码有效!
如果您在 alacritty 树中运行cargo vendor",这应该可以解决您的问题.研究 'cargo vendor' 命令 另外,研究 -- 货物构建命令的离线开关.我不需要使用它,但它对阅读很有帮助.
If you run 'cargo vendor' in the alacritty tree, this should solve your issue. Study the 'cargo vendor' command Also, study the --offline switch for the cargo build command. I did not need to use this, but it is very helpful reading.
基本上,货物供应商会提供所有来源.
Basically, cargo vendor pulls in all the source.
我不确定为什么您的代码不起作用.我很难使用/tmp 目录重新创建它.然后我使用了一个普通目录,并结合了对货物供应商"的调用,它起作用了.在剪切和粘贴下面的代码之前,请务必将/Users/[username]"更改为您自己的主目录路径.
I am not sure exactly why your code is not working. I had difficulty recreating this using the /tmp directory. I then used a normal directory combined with a call to 'cargo vendor', and it worked. Before cutting and pasting my code below, be sure to change '/Users/[username]' with your own path to your home directory.
这是我的程序:
cd ~
git clone https://github.com/jwilm/alacritty
cargo vendor
下一部分可能没有必要:
This next part is probably not necessary:
mkdir /Users/[username]/alacritty/.cargo
在/Users/[username]/alacritty/.cargo/config 创建一个文件并插入以下内容:
Create a file at /Users/[username]/alacritty/.cargo/config and, insert the following:
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "vendor"
继续必要的部分:
修改path语句指向新创建的alacritty路径:
Modify the path statement to point to the newly created alacritty path:
let path = Path::new("/Users/[username]/alacritty/Cargo.toml");
现在,运行你的代码
cargo run
这是我的输出:
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.27s
Running `target/debug/test3`
found package alacritty v0.5.0-dev (/Users/jmurray/alacritty/alacritty)
found 18 source files
found package alacritty_terminal v0.5.0-dev
(/Users/[username]/alacritty/alacritty_terminal)
found 172 source files
found package font v0.1.0 (/Users/jmurray/alacritty/font)
found 12 source files
这篇关于如何使用cargo crate列出项目的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!