|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use cargo::ops; |
| 4 | +use cargo::core::{SourceId, GitReference}; |
| 5 | +use cargo::util::{CliResult, Config, ToUrl, human}; |
| 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: Vec<String>, |
| 14 | + flag_example: Vec<String>, |
| 15 | + flag_verbose: bool, |
| 16 | + flag_quiet: bool, |
| 17 | + flag_color: Option<String>, |
| 18 | + flag_root: Option<String>, |
| 19 | + flag_list: bool, |
| 20 | + |
| 21 | + arg_crate: Option<String>, |
| 22 | + flag_vers: Option<String>, |
| 23 | + |
| 24 | + flag_git: Option<String>, |
| 25 | + flag_branch: Option<String>, |
| 26 | + flag_tag: Option<String>, |
| 27 | + flag_rev: Option<String>, |
| 28 | + |
| 29 | + flag_path: Option<String>, |
| 30 | +} |
| 31 | + |
| 32 | +pub const USAGE: &'static str = " |
| 33 | +Install a Rust binary |
| 34 | +
|
| 35 | +Usage: |
| 36 | + cargo install [options] [<crate>] |
| 37 | + cargo install [options] --list |
| 38 | +
|
| 39 | +Specifying what crate to install: |
| 40 | + --vers VERS Specify a version to install from crates.io |
| 41 | + --git URL Git URL to install the specified crate from |
| 42 | + --branch BRANCH Branch to use when installing from git |
| 43 | + --tag TAG Tag to use when installing from git |
| 44 | + --rev SHA Specific commit to use when installing from git |
| 45 | + --path PATH Filesystem path to local crate to install |
| 46 | +
|
| 47 | +Build and install options: |
| 48 | + -h, --help Print this message |
| 49 | + -j N, --jobs N The number of jobs to run in parallel |
| 50 | + --features FEATURES Space-separated list of features to activate |
| 51 | + --no-default-features Do not build the `default` feature |
| 52 | + --debug Build in debug mode instead of release mode |
| 53 | + --bin NAME Only install the binary NAME |
| 54 | + --example EXAMPLE Install the example EXAMPLE instead of binaries |
| 55 | + --root DIR Directory to install packages into |
| 56 | + -v, --verbose Use verbose output |
| 57 | + -q, --quiet Less output printed to stdout |
| 58 | + --color WHEN Coloring: auto, always, never |
| 59 | +
|
| 60 | +This command manages Cargo's local set of install binary crates. Only packages |
| 61 | +which have [[bin]] targets can be installed, and all binaries are installed into |
| 62 | +the installation root's `bin` folder. The installation root is determined, in |
| 63 | +order of precedence, by `--root`, `$CARGO_INSTALL_ROOT`, the `install.root` |
| 64 | +configuration key, and finally the home directory (which is either |
| 65 | +`$CARGO_HOME` if set or `$HOME/.cargo` by default). |
| 66 | +
|
| 67 | +There are multiple sources from which a crate can be installed. The default |
| 68 | +location is crates.io but the `--git` and `--path` flags can change this source. |
| 69 | +If the source contains more than one package (such as crates.io or a git |
| 70 | +repository with multiple crates) the `<crate>` argument is required to indicate |
| 71 | +which crate should be installed. |
| 72 | +
|
| 73 | +Crates from crates.io can optionally specify the version they wish to install |
| 74 | +via the `--vers` flags, and similarly packages from git repositories can |
| 75 | +optionally specify the branch, tag, or revision that should be installed. If a |
| 76 | +crate has multiple binaries, the `--bin` argument can selectively install only |
| 77 | +one of them, and if you'd rather install examples the `--example` argument can |
| 78 | +be used as well. |
| 79 | +
|
| 80 | +The `--list` option will list all installed packages (and their versions). |
| 81 | +"; |
| 82 | + |
| 83 | +pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> { |
| 84 | + try!(config.shell().set_verbosity(options.flag_verbose, options.flag_quiet)); |
| 85 | + try!(config.shell().set_color_config(options.flag_color.as_ref().map(|s| &s[..]))); |
| 86 | + |
| 87 | + let compile_opts = ops::CompileOptions { |
| 88 | + config: config, |
| 89 | + jobs: options.flag_jobs, |
| 90 | + target: None, |
| 91 | + features: &options.flag_features, |
| 92 | + no_default_features: options.flag_no_default_features, |
| 93 | + spec: &[], |
| 94 | + exec_engine: None, |
| 95 | + mode: ops::CompileMode::Build, |
| 96 | + release: !options.flag_debug, |
| 97 | + filter: ops::CompileFilter::new(false, &options.flag_bin, &[], |
| 98 | + &options.flag_example, &[]), |
| 99 | + target_rustc_args: None, |
| 100 | + }; |
| 101 | + |
| 102 | + let source = if let Some(url) = options.flag_git { |
| 103 | + let url = try!(url.to_url().map_err(human)); |
| 104 | + let gitref = if let Some(branch) = options.flag_branch { |
| 105 | + GitReference::Branch(branch) |
| 106 | + } else if let Some(tag) = options.flag_tag { |
| 107 | + GitReference::Tag(tag) |
| 108 | + } else if let Some(rev) = options.flag_rev { |
| 109 | + GitReference::Rev(rev) |
| 110 | + } else { |
| 111 | + GitReference::Branch("master".to_string()) |
| 112 | + }; |
| 113 | + SourceId::for_git(&url, gitref) |
| 114 | + } else if let Some(path) = options.flag_path { |
| 115 | + try!(SourceId::for_path(Path::new(&path))) |
| 116 | + } else { |
| 117 | + try!(SourceId::for_central(config)) |
| 118 | + }; |
| 119 | + |
| 120 | + let krate = options.arg_crate.as_ref().map(|s| &s[..]); |
| 121 | + let vers = options.flag_vers.as_ref().map(|s| &s[..]); |
| 122 | + let root = options.flag_root.as_ref().map(|s| &s[..]); |
| 123 | + |
| 124 | + if options.flag_list { |
| 125 | + try!(ops::install_list(root, config)); |
| 126 | + } else { |
| 127 | + try!(ops::install(root, krate, &source, vers, &compile_opts)); |
| 128 | + } |
| 129 | + Ok(None) |
| 130 | +} |
0 commit comments