[英]How to list a project's source files using the cargo crate?
I'm trying to list the source files of a Rust project using the cargo crate .我正在尝试使用cargo crate列出 Rust 项目的源文件。 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.我不能简单地列出目录中存在的所有.rs
文件,因为我想准确检索编译器在编译期间看到的文件,这些文件可能不是所有.rs
文件。
I'm conducting my experiments on the the Alacritty repository , which has a cargo workspace of 3 projects.我正在Alacritty 存储库上进行我的实验,它有一个包含 3 个项目的货物工作区。 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());
}
}
Here is the output:这是输出:
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?我错过了什么?
Your code works!您的代码有效!
If you run 'cargo vendor' in the alacritty tree, this should solve your issue.如果您在 alacritty 树中运行“cargo vendor”,这应该可以解决您的问题。 Study the 'cargo vendor' command Also, study the --offline switch for the cargo build command.研究“cargo vendor”命令另外,研究cargo build 命令的--offline 开关。 I did not need to use this, but it is very helpful reading.我不需要使用这个,但它是非常有帮助的阅读。
Basically, cargo vendor pulls in all the source.基本上,货物供应商拉入所有来源。
I am not sure exactly why your code is not working.我不确定为什么您的代码不起作用。 I had difficulty recreating this using the /tmp directory.我很难使用 /tmp 目录重新创建它。 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.在剪切和粘贴下面的代码之前,请务必使用您自己的主目录路径更改“/Users/[username]”。
Here is my procedure:这是我的程序:
cd ~
git clone https://github.com/jwilm/alacritty
cargo vendor
This next part is probably not necessary:下一部分可能没有必要:
mkdir /Users/[username]/alacritty/.cargo
Create a file at /Users/[username]/alacritty/.cargo/config and, insert the following:在 /Users/[username]/alacritty/.cargo/config 创建一个文件,并插入以下内容:
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "vendor"
Continuation of necessary part:继续必要的部分:
Modify the path statement to point to the newly created alacritty path:修改 path 语句以指向新创建的 alacritty 路径:
let path = Path::new("/Users/[username]/alacritty/Cargo.toml");
Now, run your code现在,运行你的代码
cargo run
Here is my output:这是我的输出:
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
Search for each element of the needle in the haystack in order.按顺序搜索大海捞针中的每个元素。 Each time you find a matching element, only continue the search in the remaining portion of the haystack.每次找到匹配的元素时,只在大海捞针的剩余部分继续搜索。 You can express this nicely by taking a new subslice of of the haystack each time you match an element.每次匹配一个元素时,你可以通过从干草堆中取一个新的子切片来很好地表达这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.