mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00

# cargo upload The cargo-upload command will take the local package and upload it to the specified registry. The local package is uploaded as a tarball compressed with gzip under maximum compression. Most of this is done by just delegating to `cargo package` The host to upload to is specified, in order of priority, by a command line `--host` flag, the `registry.host` config key, and then the default registry. The default registry is still `example.com` The registry itself is still a work in progress, but the general plumbing for a command such as this would look like: 1. Ensure the local package has been compressed into an archive. 2. Fetch the relevant registry and login token from config files. 3. Ensure all dependencies for a package are listed as coming from the same registry. 4. Upload the archive to the registry with the login token. 5. The registry will verify the package is under 2MB (configurable). 6. The registry will upload the archive to S3, calculating a checksum in the process. 7. The registry will add an entry to the registry's index (a git repository). The entry will include the name of the package, the version uploaded, the checksum of the upload, and then the list of dependencies (name/version req) 8. The local `cargo upload` command will succeed. # cargo login Uploading requires a token from the api server, and this token follows the same config chain for the host except that there is no fallback. To implement login, the `cargo login` command is used. With 0 arguments, the command will request that a site be visited for a login token, and with an argument it will set the argument as the new login token. The `util::config` module was modified to allow writing configuration as well as reading it. The support is a little lacking in that comments are blown away, but the support is there at least. # RegistrySource An implementation of `RegistrySource` has been created (deleting the old `DummyRegistrySource`). This implementation only needs a URL to be constructed, and it is assumed that the URL is running an instance of the cargo registry. ## RegistrySource::update Currently this will unconditionally update the registry's index (a git repository). Tuning is necessary to prevent updating the index each time (more coming soon). ## RegistrySource::query This is called in the resolve phase of cargo. This function is given a dependency to query for, and the source will simply look into the index to see if any package with the name is present. If found, the package's index file will be loaded and parsed into a list of summaries. The main optimization of this function is to not require the entire registry to ever be resident in memory. Instead, only necessary packages are loaded into memory and parsed. ## RegistrySource::download This is also called during the resolve phase of cargo, but only when a package has been selected to be built (actually resolved). This phase of the source will actually download and unpack the tarball for the package. Currently a configuration file is located in the root of a registry's index describing the root url to download packages from. This function is optimized for two different metrics: 1. If a tarball is downloaded, it is not downloaded again. It is assumed that once a tarball is successfully downloaded it will never change. 2. If the unpacking destination has a `.cargo-ok` file, it is assumed that the unpacking has already occurred and does not need to happen again. With these in place, a rebuild should take almost no time at all. ## RegistrySource::get This function is simply implemented in terms of a PathSource's `get` function by creating a `PathSource` for all unpacked tarballs as part of the `download` stage. ## Filesystem layout There are a few new directories as part of the `.cargo` home folder: * `.cargo/registry/index/$hostname-$hash` - This is the directory containing the actual index of the registry. `$hostname` comes from its url, and `$hash` is the hash of the entire url. * `.cargo/registry/cache/$hostname-$hash/$pkg-$vers.tar.gz` - This is a directory used to cache the downloads of packages from the registry. * `.cargo/registry/src/$hostname-$hash/$pkg-$vers` - This is the location of the unpacked packages. They will be compiled from this location. # New Dependencies Cargo has picked up a new dependency on the `curl-rust` package in order to send HTTP requests to the registry as well as send HTTP requests to download tarballs.
220 lines
6.7 KiB
Rust
220 lines
6.7 KiB
Rust
#![feature(phase, macro_rules)]
|
|
|
|
extern crate serialize;
|
|
#[phase(plugin, link)] extern crate log;
|
|
|
|
extern crate cargo;
|
|
extern crate docopt;
|
|
#[phase(plugin)] extern crate docopt_macros;
|
|
|
|
use std::collections::TreeSet;
|
|
use std::os;
|
|
use std::io;
|
|
use std::io::fs::{mod, PathExtensions};
|
|
use std::io::process::{Command,InheritFd,ExitStatus,ExitSignal};
|
|
use docopt::FlagParser;
|
|
|
|
use cargo::{execute_main_without_stdin, handle_error, shell};
|
|
use cargo::core::MultiShell;
|
|
use cargo::util::{CliError, CliResult};
|
|
|
|
fn main() {
|
|
execute_main_without_stdin(execute, true)
|
|
}
|
|
|
|
docopt!(Flags, "
|
|
Rust's package manager
|
|
|
|
Usage:
|
|
cargo <command> [<args>...]
|
|
cargo [options]
|
|
|
|
Options:
|
|
-h, --help Display this message
|
|
-V, --version Print version info and exit
|
|
--list List installed commands
|
|
-v, --verbose Use verbose output
|
|
|
|
Some common cargo commands are:
|
|
build Compile the current project
|
|
clean Remove the target directory
|
|
doc Build this project's and its dependencies' documentation
|
|
new Create a new cargo project
|
|
run Build and execute src/main.rs
|
|
test Run the tests
|
|
bench Run the benchmarks
|
|
update Update dependencies listed in Cargo.lock
|
|
|
|
See 'cargo help <command>' for more information on a specific command.
|
|
")
|
|
|
|
macro_rules! each_subcommand( ($macro:ident) => ({
|
|
$macro!(bench)
|
|
$macro!(build)
|
|
$macro!(clean)
|
|
$macro!(config_for_key)
|
|
$macro!(config_list)
|
|
$macro!(doc)
|
|
$macro!(generate_lockfile)
|
|
$macro!(git_checkout)
|
|
$macro!(locate_project)
|
|
$macro!(login)
|
|
$macro!(new)
|
|
$macro!(package)
|
|
$macro!(read_manifest)
|
|
$macro!(run)
|
|
$macro!(test)
|
|
$macro!(update)
|
|
$macro!(upload)
|
|
$macro!(verify_project)
|
|
$macro!(version)
|
|
}) )
|
|
|
|
/**
|
|
The top-level `cargo` command handles configuration and project location
|
|
because they are fundamental (and intertwined). Other commands can rely
|
|
on this top-level information.
|
|
*/
|
|
fn execute(flags: Flags, shell: &mut MultiShell) -> CliResult<Option<()>> {
|
|
debug!("executing; cmd=cargo; args={}", os::args());
|
|
shell.set_verbose(flags.flag_verbose);
|
|
|
|
if flags.flag_list {
|
|
println!("Installed Commands:");
|
|
for command in list_commands().move_iter() {
|
|
println!(" {}", command);
|
|
};
|
|
return Ok(None)
|
|
}
|
|
|
|
let (mut args, command) = match flags.arg_command.as_slice() {
|
|
"" | "help" if flags.arg_args.len() == 0 => {
|
|
shell.set_verbose(true);
|
|
let r = cargo::call_main_without_stdin(execute, shell,
|
|
["-h".to_string()], false);
|
|
cargo::process_executed(r, shell);
|
|
return Ok(None)
|
|
}
|
|
"help" => (vec!["-h".to_string()], flags.arg_args[0].as_slice()),
|
|
s => (flags.arg_args.clone(), s),
|
|
};
|
|
args.insert(0, command.to_string());
|
|
|
|
macro_rules! cmd( ($name:ident) => (
|
|
if command.as_slice() == stringify!($name).replace("_", "-").as_slice() {
|
|
mod $name;
|
|
shell.set_verbose(true);
|
|
let r = cargo::call_main_without_stdin($name::execute, shell,
|
|
args.as_slice(),
|
|
false);
|
|
cargo::process_executed(r, shell);
|
|
return Ok(None)
|
|
}
|
|
) )
|
|
each_subcommand!(cmd)
|
|
|
|
execute_subcommand(command.as_slice(), args.as_slice(), shell);
|
|
Ok(None)
|
|
}
|
|
|
|
fn execute_subcommand(cmd: &str, args: &[String], shell: &mut MultiShell) {
|
|
let command = match find_command(cmd) {
|
|
Some(command) => command,
|
|
None => return handle_error(CliError::new("No such subcommand", 127),
|
|
shell)
|
|
};
|
|
let status = Command::new(command)
|
|
.args(args)
|
|
.stdin(InheritFd(0))
|
|
.stdout(InheritFd(1))
|
|
.stderr(InheritFd(2))
|
|
.status();
|
|
|
|
match status {
|
|
Ok(ExitStatus(0)) => (),
|
|
Ok(ExitStatus(i)) => {
|
|
handle_error(CliError::new("", i as uint), shell)
|
|
}
|
|
Ok(ExitSignal(i)) => {
|
|
let msg = format!("subcommand failed with signal: {}", i);
|
|
handle_error(CliError::new(msg, i as uint), shell)
|
|
}
|
|
Err(io::IoError{kind, ..}) if kind == io::FileNotFound =>
|
|
handle_error(CliError::new("No such subcommand", 127), shell),
|
|
Err(err) => handle_error(
|
|
CliError::new(
|
|
format!("Subcommand failed to run: {}", err), 127),
|
|
shell)
|
|
}
|
|
}
|
|
|
|
/// List all runnable commands. find_command should always succeed
|
|
/// if given one of returned command.
|
|
fn list_commands() -> TreeSet<String> {
|
|
let command_prefix = "cargo-";
|
|
let mut commands = TreeSet::new();
|
|
for dir in list_command_directory().iter() {
|
|
let entries = match fs::readdir(dir) {
|
|
Ok(entries) => entries,
|
|
_ => continue
|
|
};
|
|
for entry in entries.iter() {
|
|
let filename = match entry.filename_str() {
|
|
Some(filename) => filename,
|
|
_ => continue
|
|
};
|
|
if filename.starts_with(command_prefix) &&
|
|
filename.ends_with(os::consts::EXE_SUFFIX) &&
|
|
is_executable(entry) {
|
|
let command = filename.slice(
|
|
command_prefix.len(),
|
|
filename.len() - os::consts::EXE_SUFFIX.len());
|
|
commands.insert(String::from_str(command));
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! add_cmd( ($cmd:ident) => ({
|
|
commands.insert(stringify!($cmd).replace("_", "-"));
|
|
}) )
|
|
each_subcommand!(add_cmd);
|
|
commands
|
|
}
|
|
|
|
fn is_executable(path: &Path) -> bool {
|
|
match fs::stat(path) {
|
|
Ok(io::FileStat{ kind: io::TypeFile, perm, ..}) =>
|
|
perm.contains(io::OtherExecute),
|
|
_ => false
|
|
}
|
|
}
|
|
|
|
/// Get `Command` to run given command.
|
|
fn find_command(cmd: &str) -> Option<Path> {
|
|
let command_exe = format!("cargo-{}{}", cmd, os::consts::EXE_SUFFIX);
|
|
let dirs = list_command_directory();
|
|
let mut command_paths = dirs.iter().map(|dir| dir.join(command_exe.as_slice()));
|
|
command_paths.find(|path| path.exists())
|
|
}
|
|
|
|
/// List candidate locations where subcommands might be installed.
|
|
fn list_command_directory() -> Vec<Path> {
|
|
let mut dirs = vec![];
|
|
match os::self_exe_path() {
|
|
Some(path) => {
|
|
dirs.push(path.join("../lib/cargo"));
|
|
dirs.push(path);
|
|
},
|
|
None => {}
|
|
};
|
|
match std::os::getenv("PATH") {
|
|
Some(val) => {
|
|
for dir in os::split_paths(val).iter() {
|
|
dirs.push(Path::new(dir))
|
|
}
|
|
},
|
|
None => {}
|
|
};
|
|
dirs
|
|
}
|