From edc7c8870136f83adc6a6e831f42e3cd5b9b17fe Mon Sep 17 00:00:00 2001 From: Christopher Swindle Date: Fri, 5 Oct 2018 06:11:51 +0100 Subject: [PATCH 1/4] Add support for registry to new and init --- src/bin/cargo/command_prelude.rs | 1 + src/bin/cargo/commands/init.rs | 1 + src/bin/cargo/commands/new.rs | 1 + src/cargo/ops/cargo_new.rs | 11 +++++++++++ 4 files changed, 14 insertions(+) diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index 5785c7baf81..8d06d7799f8 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -347,6 +347,7 @@ pub trait ArgMatchesExt { self.value_of_path("path", config).unwrap(), self._value_of("name").map(|s| s.to_string()), self._value_of("edition").map(|s| s.to_string()), + self.registry(config)?, ) } diff --git a/src/bin/cargo/commands/init.rs b/src/bin/cargo/commands/init.rs index 073f545c1d6..bc4bf424954 100644 --- a/src/bin/cargo/commands/init.rs +++ b/src/bin/cargo/commands/init.rs @@ -6,6 +6,7 @@ pub fn cli() -> App { subcommand("init") .about("Create a new cargo package in an existing directory") .arg(Arg::with_name("path").default_value(".")) + .arg(opt("registry", "Registry to use").value_name("REGISTRY")) .arg_new_opts() } diff --git a/src/bin/cargo/commands/new.rs b/src/bin/cargo/commands/new.rs index 644df685f52..417cebc1b22 100644 --- a/src/bin/cargo/commands/new.rs +++ b/src/bin/cargo/commands/new.rs @@ -6,6 +6,7 @@ pub fn cli() -> App { subcommand("new") .about("Create a new cargo package at ") .arg(Arg::with_name("path").required(true)) + .arg(opt("registry", "Registry to use").value_name("REGISTRY")) .arg_new_opts() } diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index bf296c33287..adc60829de1 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -31,6 +31,7 @@ pub struct NewOptions { pub path: PathBuf, pub name: Option, pub edition: Option, + pub registry: Option, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -67,6 +68,7 @@ struct MkOptions<'a> { source_files: Vec, bin: bool, edition: Option<&'a str>, + registry: Option<&'a str>, } impl NewOptions { @@ -77,6 +79,7 @@ impl NewOptions { path: PathBuf, name: Option, edition: Option, + registry: Option, ) -> CargoResult { let kind = match (bin, lib) { (true, true) => bail!("can't specify both lib and binary outputs"), @@ -91,6 +94,7 @@ impl NewOptions { path, name, edition, + registry, }; Ok(opts) } @@ -326,6 +330,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> { source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())], bin: opts.kind.is_bin(), edition: opts.edition.as_ref().map(|s| &**s), + registry: opts.registry.as_ref().map(|s| &**s), }; mk(config, &mkopts).chain_err(|| { @@ -403,6 +408,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> { bin: src_paths_types.iter().any(|x| x.bin), source_files: src_paths_types, edition: opts.edition.as_ref().map(|s| &**s), + registry: opts.registry.as_ref().map(|s| &**s), }; mk(config, &mkopts).chain_err(|| { @@ -537,6 +543,7 @@ name = "{}" version = "0.1.0" authors = [{}] edition = {} +publish = {} [dependencies] {}"#, @@ -546,6 +553,10 @@ edition = {} Some(edition) => toml::Value::String(edition.to_string()), None => toml::Value::String("2018".to_string()), }, + match opts.registry { + Some(registry) => toml::Value::Array(vec!(toml::Value::String(registry.to_string()))), + None => toml::Value::Boolean(true), + }, cargotoml_path_specifier ).as_bytes(), )?; From e992cebe557f9ad386a0f18f8598037f102f54e3 Mon Sep 17 00:00:00 2001 From: Christopher Swindle Date: Fri, 5 Oct 2018 06:58:56 +0100 Subject: [PATCH 2/4] Add support for providing a default registry --- src/bin/cargo/command_prelude.rs | 17 +++++++++++++++-- src/cargo/util/config.rs | 10 ++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index 8d06d7799f8..e7f8d1f4351 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -360,9 +360,22 @@ pub trait ArgMatchesExt { requires -Zunstable-options to use." )); } - Ok(Some(registry.to_string())) + + if registry == "crates.io" { + // If "crates.io" is specified then we just need to return None + // as that will cause cargo to use crates.io. This is required + // for the case where a default alterative registry is used + // but the user wants to switch back to crates.io for a single + // command. + Ok(None) + } + else { + Ok(Some(registry.to_string())) + } + } + None => { + config.default_registry() } - None => Ok(None), } } diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index fb242d220bc..99689e40cf4 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -174,6 +174,16 @@ impl Config { self.home_path.join("registry").join("src") } + /// The default cargo registry (`alternative-registry`) + pub fn default_registry(&self) -> CargoResult> { + Ok( + match self.get_string("registry.default")? { + Some(registry) => Some(registry.val), + None => None, + } + ) + } + /// Get a reference to the shell, for e.g. writing error messages pub fn shell(&self) -> RefMut { self.shell.borrow_mut() From abd9ac4feb60e335ba27f124001d22393f4e345d Mon Sep 17 00:00:00 2001 From: Christopher Swindle Date: Fri, 5 Oct 2018 07:01:34 +0100 Subject: [PATCH 3/4] Add default in config document --- src/doc/src/reference/config.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md index 129c7f5eb7d..6499c3c4c8a 100644 --- a/src/doc/src/reference/config.md +++ b/src/doc/src/reference/config.md @@ -93,6 +93,7 @@ runner = ".." [registry] index = "..." # URL of the registry index (defaults to the central repository) token = "..." # Access token (found on the central repo’s website) +default = "..." # Default alternative registry to use (can be overriden with --registry) [http] proxy = "host:port" # HTTP proxy to use for HTTP requests (defaults to none) From 1fd3f1b6f1c38a217f36da7f2536f5300ae32c4d Mon Sep 17 00:00:00 2001 From: Christopher Swindle Date: Mon, 8 Oct 2018 07:28:37 +0100 Subject: [PATCH 4/4] Switch to use crates-io as the registry name and don't include publish when registry is not specified --- src/bin/cargo/command_prelude.rs | 3 ++- src/cargo/core/package_id.rs | 4 ++-- src/cargo/core/source/source_id.rs | 6 +++--- src/cargo/ops/cargo_new.rs | 11 +++++++---- src/cargo/sources/config.rs | 4 ++-- src/cargo/sources/mod.rs | 2 +- src/cargo/sources/registry/mod.rs | 3 ++- src/cargo/util/toml/mod.rs | 6 +++--- 8 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index e7f8d1f4351..c8f2fc824b0 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -6,6 +6,7 @@ use cargo::CargoResult; use cargo::core::Workspace; use cargo::core::compiler::{BuildConfig, MessageFormat}; use cargo::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl}; +use cargo::sources::CRATES_IO_REGISTRY; use cargo::util::paths; use cargo::util::important_paths::find_root_manifest_for_wd; @@ -361,7 +362,7 @@ pub trait ArgMatchesExt { )); } - if registry == "crates.io" { + if registry == CRATES_IO_REGISTRY { // If "crates.io" is specified then we just need to return None // as that will cause cargo to use crates.io. This is required // for the case where a default alterative registry is used diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index 7bb64e8a112..568bef5b6c4 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -182,12 +182,12 @@ impl fmt::Debug for PackageId { mod tests { use super::PackageId; use core::source::SourceId; - use sources::CRATES_IO; + use sources::CRATES_IO_INDEX; use util::ToUrl; #[test] fn invalid_version_handled_nicely() { - let loc = CRATES_IO.to_url().unwrap(); + let loc = CRATES_IO_INDEX.to_url().unwrap(); let repo = SourceId::for_registry(&loc).unwrap(); assert!(PackageId::new("foo", "1.0", &repo).is_err()); diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index ae037e7a640..f30d18ffd05 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -12,7 +12,7 @@ use url::Url; use ops; use sources::git; -use sources::{GitSource, PathSource, RegistrySource, CRATES_IO}; +use sources::{GitSource, PathSource, RegistrySource, CRATES_IO_INDEX}; use sources::DirectorySource; use util::{CargoResult, Config, ToUrl}; @@ -184,7 +184,7 @@ impl SourceId { } &index[..] } else { - CRATES_IO + CRATES_IO_INDEX }; let url = url.to_url()?; SourceId::for_registry(&url) @@ -302,7 +302,7 @@ impl SourceId { Kind::Registry => {} _ => return false, } - self.inner.url.to_string() == CRATES_IO + self.inner.url.to_string() == CRATES_IO_INDEX } /// Hash `self` diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index adc60829de1..bd2bc4073cc 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -543,8 +543,7 @@ name = "{}" version = "0.1.0" authors = [{}] edition = {} -publish = {} - +{} [dependencies] {}"#, name, @@ -554,8 +553,12 @@ publish = {} None => toml::Value::String("2018".to_string()), }, match opts.registry { - Some(registry) => toml::Value::Array(vec!(toml::Value::String(registry.to_string()))), - None => toml::Value::Boolean(true), + Some(registry) => { + format!("publish = {}\n", + toml::Value::Array(vec!(toml::Value::String(registry.to_string()))) + ) + } + None => "".to_string(), }, cargotoml_path_specifier ).as_bytes(), diff --git a/src/cargo/sources/config.rs b/src/cargo/sources/config.rs index 991c3c631cc..ca52bc4d2b5 100644 --- a/src/cargo/sources/config.rs +++ b/src/cargo/sources/config.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; use url::Url; use core::{GitReference, Source, SourceId}; -use sources::ReplacedSource; +use sources::{ReplacedSource, CRATES_IO_REGISTRY}; use util::{Config, ToUrl}; use util::config::ConfigValue; use util::errors::{CargoResult, CargoResultExt}; @@ -59,7 +59,7 @@ impl<'cfg> SourceConfigMap<'cfg> { config, }; base.add( - "crates-io", + CRATES_IO_REGISTRY, SourceConfig { id: SourceId::crates_io(config)?, replace_with: None, diff --git a/src/cargo/sources/mod.rs b/src/cargo/sources/mod.rs index ed784e95ab4..d96a05639ef 100644 --- a/src/cargo/sources/mod.rs +++ b/src/cargo/sources/mod.rs @@ -2,7 +2,7 @@ pub use self::config::SourceConfigMap; pub use self::directory::DirectorySource; pub use self::git::GitSource; pub use self::path::PathSource; -pub use self::registry::{RegistrySource, CRATES_IO}; +pub use self::registry::{RegistrySource, CRATES_IO_INDEX, CRATES_IO_REGISTRY}; pub use self::replaced::ReplacedSource; pub mod config; diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index 76afec1c427..b061716f8f9 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -179,7 +179,8 @@ use util::to_url::ToUrl; use util::{internal, CargoResult, Config, FileLock, Filesystem}; const INDEX_LOCK: &str = ".cargo-index-lock"; -pub const CRATES_IO: &str = "https://github.com/rust-lang/crates.io-index"; +pub const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index"; +pub const CRATES_IO_REGISTRY: &str = "crates-io"; const CRATE_TEMPLATE: &str = "{crate}"; const VERSION_TEMPLATE: &str = "{version}"; diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 79b68189a2d..4f42a7335c4 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -18,7 +18,7 @@ use core::profiles::Profiles; use core::{Dependency, Manifest, PackageId, Summary, Target}; use core::{Edition, EitherManifest, Feature, Features, VirtualManifest}; use core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig}; -use sources::CRATES_IO; +use sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY}; use util::errors::{CargoError, CargoResult, CargoResultExt}; use util::paths; use util::{self, Config, ToUrl}; @@ -1140,7 +1140,7 @@ impl TomlManifest { ) })?; if spec.url().is_none() { - spec.set_url(CRATES_IO.parse().unwrap()); + spec.set_url(CRATES_IO_INDEX.parse().unwrap()); } let version_specified = match *replacement { @@ -1175,7 +1175,7 @@ impl TomlManifest { let mut patch = HashMap::new(); for (url, deps) in self.patch.iter().flat_map(|x| x) { let url = match &url[..] { - "crates-io" => CRATES_IO.parse().unwrap(), + CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(), _ => url.to_url()?, }; patch.insert(