Skip to content

Commit 07db8c8

Browse files
committed
Merge pull request #2454 from killerswan/cargo_may2012
(cargo) fixed a package installation bug, cleaned up usage
2 parents 163b060 + b7393ec commit 07db8c8

File tree

1 file changed

+81
-57
lines changed

1 file changed

+81
-57
lines changed

src/cargo/cargo.rs

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ type options = {
6161
test: bool,
6262
mode: mode,
6363
free: [str],
64+
help: bool,
6465
};
6566

6667
enum mode { system_mode, user_mode, local_mode }
6768

6869
fn opts() -> [getopts::opt] {
69-
[optflag("g"), optflag("G"), optopt("mode"), optflag("test")]
70+
[optflag("g"), optflag("G"), optopt("mode"), optflag("test"),
71+
optflag("h"), optflag("help")]
7072
}
7173

7274
fn info(msg: str) {
@@ -172,7 +174,7 @@ fn rest(s: str, start: uint) -> str {
172174

173175
fn need_dir(s: str) {
174176
if os::path_is_dir(s) { ret; }
175-
if !os::make_dir(s, 0x1c0i32) {
177+
if !os::make_dir(s, 493_i32 /* oct: 755 */) {
176178
fail #fmt["can't make_dir %s", s];
177179
}
178180
}
@@ -337,49 +339,55 @@ fn build_cargo_options(argv: [str]) -> options {
337339
};
338340

339341
let test = opt_present(match, "test");
340-
let G = opt_present(match, "G");
341-
let g = opt_present(match, "g");
342-
let m = opt_present(match, "mode");
342+
let G = opt_present(match, "G");
343+
let g = opt_present(match, "g");
344+
let m = opt_present(match, "mode");
345+
let help = opt_present(match, "h") || opt_present(match, "help");
346+
343347
let is_install = vec::len(match.free) > 1u && match.free[1] == "install";
344348

345349
if G && g { fail "-G and -g both provided"; }
346350
if g && m { fail "--mode and -g both provided"; }
347351
if G && m { fail "--mode and -G both provided"; }
348352

349-
let mode = if is_install {
350-
if G { system_mode }
351-
else if g { user_mode }
352-
else if m {
353+
if !is_install && (g || G || m) {
354+
fail "-g, -G, --mode are only valid for `install`";
355+
}
356+
357+
let mode =
358+
if !is_install || G { system_mode }
359+
else if g { user_mode }
360+
else if !m { local_mode }
361+
else {
353362
alt getopts::opt_str(match, "mode") {
354363
"system" { system_mode }
355-
"user" { user_mode }
356-
"local" { local_mode }
357-
_ { fail "argument to `mode` must be one of `system`" +
358-
", `user`, or `local`";
359-
}
360-
}
361-
} else { local_mode }
362-
} else { system_mode };
364+
"user" { user_mode }
365+
"local" { local_mode }
366+
_ { fail "argument to `mode` must be" +
367+
"one of `system`, `user`, or `local`"; }}
368+
};
363369

364-
{test: test, mode: mode, free: match.free}
370+
{test: test, mode: mode, free: match.free, help: help}
365371
}
366372

367373
fn configure(opts: options) -> cargo {
374+
// NOTE: to make init and sync save into non-root level directories
375+
// simply get rid of syscargo, below
376+
368377
let syscargo = result::get(get_cargo_sysroot());
378+
369379
let get_cargo_dir = alt opts.mode {
370380
system_mode { get_cargo_sysroot }
371381
user_mode { get_cargo_root }
372382
local_mode { get_cargo_root_nearest }
373383
};
374384

375-
let p = alt get_cargo_dir() {
376-
result::ok(p) { p }
377-
result::err(e) { fail e }
378-
};
385+
let p = result::get(get_cargo_dir());
379386

380387
let sources = map::str_hash::<source>();
381388
try_parse_sources(path::connect(syscargo, "sources.json"), sources);
382389
try_parse_sources(path::connect(syscargo, "local-sources.json"), sources);
390+
383391
let mut c = {
384392
pgp: pgp::supported(),
385393
root: p,
@@ -467,17 +475,20 @@ fn install_one_crate(c: cargo, path: str, cf: str) {
467475
let newv = os::list_dir_path(buildpath);
468476
let exec_suffix = os::exe_suffix();
469477
for newv.each {|ct|
478+
// FIXME: What's up with the dual installation? Both `install_to_dir`
479+
// and `install_one_crate_to_sysroot` install the binary files...
480+
470481
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
471482
(exec_suffix == "" && !str::starts_with(path::basename(ct),
472483
"lib")) {
473484
#debug(" bin: %s", ct);
474-
copy_warn(ct, c.bindir);
485+
install_to_dir(ct, c.bindir);
475486
if c.opts.mode == system_mode {
476487
install_one_crate_to_sysroot(ct, "bin");
477488
}
478489
} else {
479490
#debug(" lib: %s", ct);
480-
copy_warn(ct, c.bindir);
491+
install_to_dir(ct, c.libdir);
481492
if c.opts.mode == system_mode {
482493
install_one_crate_to_sysroot(ct, libdir());
483494
}
@@ -491,7 +502,7 @@ fn install_one_crate_to_sysroot(ct: str, target: str) {
491502
let path = [_path, "..", target];
492503
check vec::is_not_empty(path);
493504
let target_dir = path::normalize(path::connect_many(path));
494-
copy_warn(ct, target_dir);
505+
install_to_dir(ct, target_dir);
495506
}
496507
none { }
497508
}
@@ -684,9 +695,10 @@ fn cmd_install(c: cargo) unsafe {
684695

685696
let target = c.opts.free[2];
686697

687-
let wd = alt tempfile::mkdtemp(c.workdir + path::path_sep(), "") {
698+
let wd_base = c.workdir + path::path_sep();
699+
let wd = alt tempfile::mkdtemp(wd_base, "") {
688700
some(_wd) { _wd }
689-
none { fail "needed temp dir"; }
701+
none { fail #fmt("needed temp dir: %s", wd_base); }
690702
};
691703

692704
if str::starts_with(target, "uuid:") {
@@ -802,9 +814,9 @@ fn cmd_init(c: cargo) {
802814

803815
let r = pgp::verify(c.root, srcfile, sigfile, pgp::signing_key_fp());
804816
if !r {
805-
warn(#fmt["signature verification failed for sources.json"]);
817+
warn(#fmt["signature verification failed for '%s'", srcfile]);
806818
} else {
807-
info(#fmt["signature ok for sources.json"]);
819+
info(#fmt["signature ok for '%s'", srcfile]);
808820
}
809821
copy_warn(srcfile, destsrcfile);
810822

@@ -847,44 +859,56 @@ fn cmd_search(c: cargo) {
847859
info(#fmt["Found %d packages.", n]);
848860
}
849861

850-
fn copy_warn(src: str, dest: str) {
851-
if !os::copy_file(src, dest) {
852-
warn(#fmt["Copying %s to %s failed", src, dest]);
862+
fn install_to_dir(srcfile: str, destdir: str) {
863+
let newfile = path::connect(destdir, path::basename(srcfile));
864+
info(#fmt["Installing '%s'...", newfile]);
865+
866+
run::run_program("cp", [srcfile, newfile]);
867+
}
868+
869+
fn copy_warn(srcfile: str, destfile: str) {
870+
if !os::copy_file(srcfile, destfile) {
871+
warn(#fmt["Copying %s to %s failed", srcfile, destfile]);
853872
}
854873
}
855874

875+
// FIXME: decide on either [-g | -G] or [--mode=...] and remove the other
856876
fn cmd_usage() {
857-
print("Usage: cargo <verb> [options] [args...]" +
858-
"
859-
860-
init Set up .cargo
861-
install [options] [source/]package-name Install by name
862-
install [options] uuid:[source/]package-uuid Install by uuid
863-
list [source] List packages
864-
search <name | '*'> [tags...] Search packages
865-
sync Sync all sources
866-
usage This
867-
868-
Options:
869-
870-
cargo install
871-
872-
--mode=[system,user,local] change mode as (system/user/local)
873-
--test run crate tests before installing
874-
-g equivalent to --mode=user
875-
-G equivalent to --mode=system
876-
877-
NOTE:
878-
\"cargo install\" installs bin/libs to local-level .cargo by default.
879-
To install them into user-level .cargo, use option -g/--mode=user.
880-
To install them into bin/lib on sysroot, use option -G/--mode=system.
877+
print("Usage: cargo <verb> [options] [args...]\n" +
878+
" e.g.: cargo [init | sync]\n" +
879+
" e.g.: cargo install [-g | -G | --mode=MODE] ] [PACKAGE...]
880+
881+
Initialization:
882+
init Set up the cargo system near this binary,
883+
for example, at /usr/local/lib/cargo/
884+
sync Sync all package sources
885+
886+
Querying:
887+
list [source] List packages
888+
search <name | '*'> [tags...] Search packages
889+
usage Display this message
890+
891+
Package installation:
892+
[options] [source/]PKGNAME Install a package by name
893+
[options] uuid:[source/]PKGUUID Install a package by uuid
894+
895+
Package installation options:
896+
--mode=MODE Install to one of the following locations:
897+
local (./.cargo/bin/, which is the default),
898+
user (~/.cargo/bin/), or system (/usr/local/lib/cargo/bin/)
899+
--test Run crate tests before installing
900+
-g Equivalent to --mode=user
901+
-G Equivalent to --mode=system
902+
903+
Other:
904+
-h, --help Display this message
881905
");
882906
}
883907

884908
fn main(argv: [str]) {
885909
let o = build_cargo_options(argv);
886910

887-
if vec::len(o.free) < 2u {
911+
if vec::len(o.free) < 2u || o.help {
888912
cmd_usage();
889913
ret;
890914
}

0 commit comments

Comments
 (0)