1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::{
util::{cargo_package, cksum, extract_crate},
IndexDependency, IndexPackage,
};
use failure::{bail, format_err, Error, ResultExt};
use same_file::is_same_file;
use std::{
env,
path::{Path, PathBuf},
};
use url::Url;
pub(crate) struct MetaInfo {
pub(crate) index_pkg: IndexPackage,
pub(crate) crate_path: PathBuf,
}
pub fn metadata_from_crate(
index_url: &str,
crate_path: impl AsRef<Path>,
) -> Result<IndexPackage, Error> {
let crate_path = crate_path.as_ref();
let (_tmp_dir, pkg_path) = extract_crate(crate_path)?;
Ok(metadata_reg(
index_url,
Some(&pkg_path.join("Cargo.toml")),
Some(crate_path),
None,
)?
.index_pkg)
}
pub fn metadata(
index_url: &str,
manifest_path: Option<&Path>,
package_args: Option<&Vec<String>>,
) -> Result<IndexPackage, Error> {
Ok(metadata_reg(index_url, manifest_path, None, package_args)?.index_pkg)
}
pub(crate) fn metadata_reg(
index_url: &str,
manifest_path: Option<&Path>,
crate_path: Option<&Path>,
package_args: Option<&Vec<String>>,
) -> Result<MetaInfo, Error> {
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = manifest_path {
if let Some(parent) = path.parent() {
cmd.current_dir(parent);
} else {
cmd.manifest_path(path);
}
}
let metadata = cmd
.exec()
.map_err(|e| format_err!("{}", e))
.with_context(|_| match manifest_path {
Some(path) => format_err!("Failed to read manifest at `{}`.", path.display()),
None => format_err!("Failed to read manifest from current directory."),
})?;
let cwd = env::current_dir()?;
let actual_manifest_path = match manifest_path {
Some(path) => path.to_path_buf(),
None => cwd
.ancestors()
.map(|p| p.join("Cargo.toml"))
.find(|p| p.exists())
.ok_or_else(|| {
format_err!(
"Could not find `Cargo.toml` in `{}` or any parent.",
cwd.display()
)
})?,
};
let pkg = metadata
.packages
.iter()
.find(|p| is_same_file(&p.manifest_path, &actual_manifest_path).unwrap_or(false))
.ok_or_else(|| {
format_err!(
"Could not find package at `{}`.",
actual_manifest_path.display()
)
})?;
let crate_path = match crate_path {
Some(path) => {
if !path.exists() {
bail!("Crate file not found at `{}`", path.display());
}
path.to_path_buf()
}
None => cargo_package(
&actual_manifest_path,
metadata.target_directory.as_ref(),
pkg,
package_args,
)?,
};
let cksum = cksum(&crate_path)?;
let deps: Vec<IndexDependency> = pkg
.dependencies
.iter()
.map(|dep| {
let (name, package) = match &dep.rename {
Some(new_name) => (new_name.clone(), Some(dep.name.clone())),
None => (dep.name.clone(), None),
};
println!("dep ={:?} index_url={:?}", dep.registry, index_url);
let registry = dep
.registry
.as_ref()
.map(|s| s.as_ref())
.or_else(|| {
Some("https://github.com/rust-lang/crates.io-index")
})
.and_then(|r| {
if r == index_url {
None
} else {
Some(Url::parse(r).unwrap())
}
});
IndexDependency {
name,
req: dep.req.clone(),
features: dep.features.clone(),
optional: dep.optional,
default_features: dep.uses_default_features,
target: dep.target.as_ref().map(|t| format!("{}", t)),
kind: dep.kind,
registry,
package,
__nonexhaustive: (),
}
})
.collect();
let index_pkg = IndexPackage {
name: pkg.name.clone(),
vers: pkg.version.clone(),
deps,
features: pkg.features.clone().into_iter().collect(),
cksum,
yanked: false,
links: pkg.links.clone(),
__nonexhaustive: (),
};
let info = MetaInfo {
index_pkg,
crate_path,
};
Ok(info)
}