Skip to content

Commit f8308bc

Browse files
steveklabnikalexcrichton
authored andcommitted
Basic work for cargo install
1 parent 1a6d6c4 commit f8308bc

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

src/bin/cargo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ macro_rules! each_subcommand{ ($mac:ident) => ({
6969
$mac!(generate_lockfile);
7070
$mac!(git_checkout);
7171
$mac!(help);
72+
$mac!(install);
7273
$mac!(locate_project);
7374
$mac!(login);
7475
$mac!(new);

src/bin/install.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use cargo::ops;
2+
use cargo::util::{CliResult, CliError, Config};
3+
use std::path::Path;
4+
5+
#[allow(dead_code)] // for now until all options are implemented
6+
7+
#[derive(RustcDecodable)]
8+
struct Options {
9+
flag_jobs: Option<u32>,
10+
flag_features: Vec<String>,
11+
flag_no_default_features: bool,
12+
flag_debug: bool,
13+
flag_bin: Option<String>,
14+
flag_example: Vec<String>,
15+
flag_package: Vec<String>,
16+
flag_verbose: bool,
17+
flag_root: Option<String>,
18+
}
19+
20+
pub const USAGE: &'static str = "
21+
Install a crate onto the local system
22+
23+
Installing new crates:
24+
cargo install [options]
25+
cargo install [options] [-p CRATE | --package CRATE] [--vers VERS]
26+
cargo install [options] --git URL [--branch BRANCH | --tag TAG | --rev SHA]
27+
cargo install [options] --path PATH
28+
29+
Managing installed crates:
30+
cargo install [options] --list
31+
32+
Options:
33+
-h, --help Print this message
34+
-j N, --jobs N The number of jobs to run in parallel
35+
--features FEATURES Space-separated list of features to activate
36+
--no-default-features Do not build the `default` feature
37+
--debug Build in debug mode instead of release mode
38+
--bin NAME Only install the binary NAME
39+
--example EXAMPLE Install the example EXAMPLE instead of binaries
40+
-p, --package CRATE Install this crate from crates.io or select the
41+
package in a repository/path to install.
42+
-v, --verbose Use verbose output
43+
--root DIR Directory to install packages into
44+
45+
This command manages Cargo's local set of install binary crates. Only packages
46+
which have [[bin]] targets can be installed, and all binaries are installed into
47+
`$HOME/.cargo/bin` by default (or `$CARGO_HOME/bin` if you change the home
48+
directory).
49+
50+
There are multiple methods of installing a new crate onto the system. The
51+
`cargo install` command with no arguments will install the current crate (as
52+
specifed by the current directory). Otherwise the `-p`, `--package`, `--git`,
53+
and `--path` options all specify the source from which a crate is being
54+
installed. The `-p` and `--package` options will download crates from crates.io.
55+
56+
Crates from crates.io can optionally specify the version they wish to install
57+
via the `--vers` flags, and similarly packages from git repositories can
58+
optionally specify the branch, tag, or revision that should be installed. If a
59+
crate has multiple binaries, the `--bin` argument can selectively install only
60+
one of them, and if you'd rather install examples the `--example` argument can
61+
be used as well.
62+
63+
The `--list` option will list all installed packages (and their versions).
64+
";
65+
66+
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
67+
config.shell().set_verbose(options.flag_verbose);
68+
69+
let compile_opts = ops::CompileOptions {
70+
config: config,
71+
jobs: options.flag_jobs,
72+
target: None,
73+
features: &options.flag_features,
74+
no_default_features: options.flag_no_default_features,
75+
spec: None,
76+
exec_engine: None,
77+
mode: ops::CompileMode::Build,
78+
release: true,
79+
filter: ops::CompileFilter::Everything,
80+
target_rustc_args: None,
81+
};
82+
83+
let root = &Path::new("$HOME/.cargo/bin");
84+
85+
ops::install(&root,
86+
&compile_opts).map_err(|err| {
87+
CliError::from_boxed(err, 101)
88+
}).map(|_| None)
89+
}

src/cargo/ops/cargo_install.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use ops;
2+
use util::CargoResult;
3+
use sources::PathSource;
4+
use std::path::Path;
5+
6+
pub fn install(manifest_path: &Path,
7+
opts: &ops::CompileOptions) -> CargoResult<()> {
8+
let config = opts.config;
9+
let src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
10+
config));
11+
let _root = try!(src.root_package());
12+
13+
println!("Compiling");
14+
try!(ops::compile(manifest_path, opts));
15+
16+
Ok(())
17+
}

src/cargo/ops/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub use self::cargo_rustc::{Context, LayoutProxy};
77
pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig};
88
pub use self::cargo_rustc::{CommandType, CommandPrototype, ExecEngine, ProcessEngine};
99
pub use self::cargo_run::run;
10+
pub use self::cargo_install::install;
1011
pub use self::cargo_new::{new, NewOptions, VersionControl};
1112
pub use self::cargo_doc::{doc, DocOptions};
1213
pub use self::cargo_generate_lockfile::{generate_lockfile};
@@ -28,6 +29,7 @@ mod cargo_compile;
2829
mod cargo_doc;
2930
mod cargo_fetch;
3031
mod cargo_generate_lockfile;
32+
mod cargo_install;
3133
mod cargo_new;
3234
mod cargo_package;
3335
mod cargo_pkgid;

tests/support/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,4 @@ pub static DOWNLOADING: &'static str = " Downloading";
563563
pub static UPLOADING: &'static str = " Uploading";
564564
pub static VERIFYING: &'static str = " Verifying";
565565
pub static ARCHIVING: &'static str = " Archiving";
566+
pub static INSTALLED: &'static str = " Installed";

0 commit comments

Comments
 (0)