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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use std::fs::File;
use std::io::Read;
use std::env;
use std::path::{Path,PathBuf};
use util::{path_exists, is_dir};
use std::fs::{read_dir};
use toml;
macro_rules! otry {
($e:expr) => (match $e { Some(e) => e, None => return None })
}
macro_rules! otry2 {
($e:expr) => (match $e { Ok(e) => e, Err(e) => { error!("ERROR!: {:?} {} {}", e, file!(), line!()); return None } })
}
macro_rules! vectry {
($e:expr) => (match $e { Ok(e) => e, Err(e) => { error!("ERROR!: {:?} {} {}", e, file!(), line!()); return Vec::new() } })
}
fn get_branch_from_source(source: &String) -> Option<&str> {
debug!("get_branch_from_source - Finding branch from {:?}", source);
match source.find("branch=") {
Some(idx) => {
let (_start, branch) = source.split_at(idx+7);
match branch.find("#") {
Some(idx) => {
let (branch, _end) = branch.split_at(idx);
Some(branch)
},
None => Some(branch),
}
},
None => None
}
}
#[test]
fn gets_branch_from_git_source_with_hash() {
let source = "git+https://github.com/phildawes/racer.git?branch=dev#9e04f91f0426c1cf8ec5e5023f74d7261f5a9dd1".to_string();
let branch = get_branch_from_source(&source);
assert_eq!(branch, Some("dev"));
}
#[test]
fn gets_branch_from_git_source_without_hash() {
let source = "git+https://github.com/phildawes/racer.git?branch=dev".to_string();
let branch = get_branch_from_source(&source);
assert_eq!(branch, Some("dev"));
}
#[test]
fn empty_if_no_branch() {
let source = "git+https://github.com/phildawes/racer.git#9e04f91f0426c1cf8ec5e5023f74d7261f5a9dd1".to_string();
let branch = get_branch_from_source(&source);
assert_eq!(branch, None);
}
fn find_src_via_lockfile(kratename: &str, cargofile: &Path) -> Option<PathBuf> {
let mut file = otry2!(File::open(cargofile));
let mut string = String::new();
otry2!(file.read_to_string(&mut string));
let mut parser = toml::Parser::new(&string);
let lock_table = parser.parse().unwrap();
debug!("find_src_via_lockfile found lock table {:?}", lock_table);
let t = match lock_table.get("package") {
Some(&toml::Value::Array(ref t1)) => t1,
_ => return None
};
for item in t {
if let &toml::Value::Table(ref t) = item {
if let Some(&toml::Value::String(ref name)) = t.get("name") {
if name.replace("-", "_") == kratename {
debug!("found matching crate {:?}", t);
let version = otry!(getstr(t, "version"));
let source = otry!(getstr(t, "source"));
if Some("registry") == source.split("+").nth(0) {
return get_versioned_cratefile(name, &version, cargofile);
} else if Some("git") == source.split("+").nth(0) {
let sha1 = otry!(source.split("#").last());
let mut d = otry!(get_cargo_rootdir(cargofile));
let branch = get_branch_from_source(&source);
d.push("git");
d.push("checkouts");
d = otry!(find_git_src_dir(d, name, &sha1, branch));
d.push("src");
d.push("lib.rs");
return Some(d);
}
}
}
}
}
None
}
fn get_cargo_rootdir(cargofile: &Path) -> Option<PathBuf> {
debug!("get_cargo_rootdir. {:?}",cargofile);
match env::var_os("CARGO_HOME") {
Some(cargohome) =>
{
debug!("get_cargo_rootdir. CARGO_HOME is set: {:?}",cargohome);
let d = PathBuf::from(cargohome);
if path_exists(&d) {
return Some(d)
} else {
return None
};
},
None => ()
};
let mut d = otry!(env::home_dir());
d.push(".multirust");
d.push("overrides");
debug!("get_cargo_rootdir: looking for overrides file. {:?}", d);
if let Ok(mut multirust_overides) = File::open(&d) {
debug!("get_cargo_rootdir: found overrides file! {:?}", d);
let mut s = String::new();
otry2!(multirust_overides.read_to_string(&mut s));
for line in s.lines() {
let overridepath = line.split(";").nth(0).unwrap();
if cargofile.starts_with(overridepath) {
let overridepath = line.split(";").nth(1).unwrap();
d.pop();
d.push("toolchains");
d.push(overridepath.trim());
d.push("cargo");
debug!("get_cargo_rootdir override root is {:?}",d);
return Some(d)
}
}
}
d.pop();
d.push("default");
if let Ok(mut multirustdefault) = File::open(&d) {
let mut s = String::new();
otry2!(multirustdefault.read_to_string(&mut s));
d.pop();
d.push("toolchains");
d.push(s.trim());
d.push("cargo");
debug!("get_cargo_rootdir root is {:?}",d);
return Some(d)
}
d.pop();
d.pop();
d.push(".cargo");
if path_exists(&d) {
Some(d)
} else {
None
}
}
fn get_versioned_cratefile(kratename: &str, version: &str, cargofile: &Path) -> Option<PathBuf> {
let mut d = otry!(get_cargo_rootdir(cargofile));
debug!("get_versioned_cratefile: cargo rootdir is {:?}",d);
d.push("registry");
d.push("src");
for mut d in find_cratesio_src_dirs(d) {
if version == "*" {
use std::fs::read_dir;
let mut start_path = d.clone();
start_path.push(kratename);
let start_name = start_path.to_str().unwrap();
if let Ok(reader) = read_dir(d) {
if let Some(path) = reader
.map(|entry| entry.unwrap().path())
.find(|path| path.to_str().unwrap().starts_with(start_name)) {
d = path.clone();
} else {
continue;
}
} else {
continue;
}
} else {
d.push(kratename.to_owned() + "-" + &version);
}
d.push("src");
debug!("crate path {:?}",d);
d.push(kratename);
d.push("lib.rs");
if let Err(_) = File::open(&d) {
d.pop();
d.pop();
d.push("lib.rs");
}
debug!("crate path with lib.rs {:?}",d);
if let Err(_) = File::open(&d) {
d.pop();
d.pop();
d.push("lib.rs");
}
if let Err(_) = File::open(&d) {
continue;
}
return Some(d)
}
None
}
fn find_src_via_tomlfile(kratename: &str, cargofile: &Path) -> Option<PathBuf> {
let mut file = otry2!(File::open(cargofile));
let mut string = String::new();
otry2!(file.read_to_string(&mut string));
let mut parser = toml::Parser::new(&string);
let table = otry!(parser.parse());
if let Some(&toml::Value::Table(ref t)) = table.get("lib") {
if let Some(&toml::Value::String(ref name)) = t.get("name") {
if name == kratename {
debug!("found {} as lib entry in Cargo.toml", kratename);
if let Some(&toml::Value::String(ref pathstr)) = t.get("path") {
let p = Path::new(pathstr);
let libpath = otry!(cargofile.parent()).join(p);
return Some(libpath);
}
}
}
}
let t = match table.get("dependencies") {
Some(&toml::Value::Table(ref t)) => t,
_ => return None
};
let mut name = kratename;
let value = if kratename.contains('_') {
t.iter().find(|&(k, _)| k.replace("-", "_") == name).map(|(k,v)| {
name = k;
v
})
} else {
t.get(kratename)
};
match value {
Some(&toml::Value::Table(ref t)) => {
let relative_path = otry!(getstr(t, "path"));
Some(otry!(cargofile.parent())
.join(relative_path)
.join("src")
.join("lib.rs"))
},
Some(&toml::Value::String(ref version)) => {
get_versioned_cratefile(name, version, cargofile)
}
_ => None
}
}
fn find_cratesio_src_dirs(d: PathBuf) -> Vec<PathBuf> {
let mut out = Vec::new();
for entry in vectry!(read_dir(d)) {
let path = vectry!(entry).path();
if is_dir(path.as_path()) {
if let Some(ref fname) = path.file_name().and_then(|s| s.to_str()) {
if fname.starts_with("github.com-") {
out.push(path.clone());
}
}
}
}
out
}
fn find_git_src_dir(d: PathBuf, name: &str, sha1: &str, branch: Option<&str>) -> Option<PathBuf> {
for entry in otry2!(read_dir(d)) {
let path = otry2!(entry).path();
if is_dir(path.as_path()) {
if let Some(ref fname) = path.file_name().and_then(|s| s.to_str()) {
if fname.starts_with(name) {
let mut d = path.clone();
d.push(sha1);
if !is_dir(d.as_path()) && branch.is_some() {
d.pop();
d.push(branch.unwrap());
}
if !is_dir(d.as_path()) {
d.pop();
d.push("master");
}
let retval = d.clone();
d.push(".git");
d.push("refs");
d.push("heads");
d.push("master");
let mut headref = String::new();
otry2!(otry2!(File::open(d)).read_to_string(&mut headref));
debug!("git headref is {:?}", headref);
if headref.ends_with("\n") {
headref.pop();
}
if sha1 == headref {
return Some(retval);
}
}
}
}
}
None
}
fn getstr(t: &toml::Table, k: &str) -> Option<String> {
match t.get(k) {
Some(&toml::Value::String(ref s)) => Some(s.clone()),
_ => None
}
}
fn find_cargo_tomlfile(currentfile: &Path) -> Option<PathBuf> {
let mut f = currentfile.to_path_buf();
f.push("Cargo.toml");
if path_exists(f.as_path()) {
Some(f)
} else {
if f.pop() && f.pop() {
find_cargo_tomlfile(&f)
} else {
None
}
}
}
pub fn get_crate_file(kratename: &str, from_path: &Path) -> Option<PathBuf> {
if let Some(tomlfile) = find_cargo_tomlfile(from_path) {
debug!("get_crate_file tomlfile is {:?}", tomlfile);
let mut lockfile = tomlfile.clone();
lockfile.pop();
lockfile.push("Cargo.lock");
if path_exists(lockfile.as_path()) {
if let Some(f) = find_src_via_lockfile(kratename, &lockfile) {
return Some(f);
}
}
return find_src_via_tomlfile(kratename, &tomlfile)
}
None
}