Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow for custom registries urls #1434

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scarb/src/bin/scarb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn cli_main(args: ScarbArgs) -> Result<()> {
.offline(args.offline)
.log_filter_directive(Some(scarb_log))
.profile(args.profile_spec.determine()?)
.with_registry()
.build()?;

commands::run(args.command, &mut config)
Expand Down
24 changes: 23 additions & 1 deletion scarb/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ use camino::{Utf8Path, Utf8PathBuf};
use once_cell::sync::OnceCell;
use tokio::runtime::{Builder, Handle, Runtime};
use tracing::trace;
use url::Url;
use which::which_in;

use scarb_ui::{OutputFormat, Ui, Verbosity};

use crate::compiler::plugin::CairoPluginRepository;
use crate::compiler::{CompilerRepository, Profile};
use crate::core::registry::DEFAULT_REGISTRY_INDEX;
use crate::core::AppDirs;
#[cfg(doc)]
use crate::core::Workspace;
use crate::flock::AdvisoryLock;
use crate::internal::fsx;
use crate::SCARB_ENV;
use crate::{REGISTRY_URL_ENV, SCARB_ENV};

use super::ManifestDependency;

Expand All @@ -46,6 +48,7 @@ pub struct Config {
tokio_handle: OnceCell<Handle>,
profile: Profile,
http_client: OnceCell<reqwest::Client>,
registry_url: Url,
}

impl Config {
Expand Down Expand Up @@ -77,6 +80,7 @@ impl Config {
if let Some(handle) = b.tokio_handle {
tokio_handle.set(handle).unwrap();
}
let registry_url = b.registry_url.unwrap_or(DEFAULT_REGISTRY_INDEX.clone());

Ok(Self {
manifest_path: b.manifest_path,
Expand All @@ -95,6 +99,7 @@ impl Config {
tokio_handle,
profile,
http_client: OnceCell::new(),
registry_url,
})
}

Expand Down Expand Up @@ -285,6 +290,10 @@ impl Config {
);
self.http()
}

pub fn registry_url(&self) -> &Url {
&self.registry_url
}
}

#[derive(Debug)]
Expand All @@ -303,6 +312,7 @@ pub struct ConfigBuilder {
custom_source_patches: Option<Vec<ManifestDependency>>,
tokio_handle: Option<Handle>,
profile: Option<Profile>,
registry_url: Option<Url>,
}

impl ConfigBuilder {
Expand All @@ -322,6 +332,7 @@ impl ConfigBuilder {
custom_source_patches: None,
tokio_handle: None,
profile: None,
registry_url: None,
}
}

Expand Down Expand Up @@ -405,4 +416,15 @@ impl ConfigBuilder {
self.profile = Some(profile);
self
}

pub fn with_registry(mut self) -> Self {
self.registry_url = match env::var(REGISTRY_URL_ENV) {
Ok(value) => match Url::parse(&value) {
Ok(parsed_url) => Some(parsed_url),
Err(_) => panic!("Failed to parse url set by SCARB_REGISTRY_URL env variable"),
},
Err(_) => None,
};
self
}
}
3 changes: 2 additions & 1 deletion scarb/src/core/manifest/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use typed_builder::TypedBuilder;
#[cfg(doc)]
use crate::core::Manifest;
use crate::core::{
Checksum, DepKind, DependencyVersionReq, ManifestDependency, PackageId, PackageName,
Checksum, DepKind, DependencyVersionReq, ManifestDependency, PackageId, PackageName, SourceId,
};

/// Subset of a [`Manifest`] that contains only the most important information about a package.
Expand Down Expand Up @@ -74,6 +74,7 @@ impl Summary {
ManifestDependency::builder()
.name(PackageName::CORE)
.version_req(DependencyVersionReq::exact(&cairo_version))
.source_id(SourceId::for_std())
.build()
});
let mut deps: Vec<&ManifestDependency> = Vec::new();
Expand Down
19 changes: 15 additions & 4 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ impl TomlManifest {
}

impl TomlDependency {
fn resolve(&self) -> Cow<'_, DetailedTomlDependency> {
fn resolve(&self, registry_url: &Url) -> Cow<'_, DetailedTomlDependency> {
match self {
TomlDependency::Simple(version) => Cow::Owned(DetailedTomlDependency {
version: Some(version.clone()),
registry: Some(registry_url.to_owned()),
..Default::default()
}),
TomlDependency::Detailed(detailed) => Cow::Borrowed(detailed),
Expand Down Expand Up @@ -392,6 +393,7 @@ impl TomlManifest {
source_id: SourceId,
profile: Profile,
workspace_manifest: Option<&TomlManifest>,
registry_url: &Url,
) -> Result<Manifest> {
let root = manifest_path
.parent()
Expand Down Expand Up @@ -443,11 +445,18 @@ impl TomlManifest {
.and_then(|deps| deps.get(name.as_str()))
.cloned()
.ok_or_else(|| anyhow!("dependency `{}` not found in workspace", name.clone()))?
.to_dependency(name.clone(), workspace_manifest_path, kind.clone())
.to_dependency(
name.clone(),
workspace_manifest_path,
kind.clone(),
registry_url,
)
};
let toml_dep = toml_dep
.clone()
.map(|dep| dep.to_dependency(name.clone(), manifest_path, kind.clone()))?
.map(|dep| {
dep.to_dependency(name.clone(), manifest_path, kind.clone(), registry_url)
})?
.resolve(name.as_str(), inherit_ws)?;
dependencies.push(toml_dep);
}
Expand Down Expand Up @@ -959,8 +968,10 @@ impl TomlDependency {
name: PackageName,
manifest_path: &Utf8Path,
dep_kind: DepKind,
registry_url: &Url,
) -> Result<ManifestDependency> {
self.resolve().to_dependency(name, manifest_path, dep_kind)
self.resolve(registry_url)
.to_dependency(name, manifest_path, dep_kind)
}
}

Expand Down
2 changes: 1 addition & 1 deletion scarb/src/core/registry/index/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct IndexConfig {
}

impl IndexConfig {
pub const WELL_KNOWN_PATH: &'static str = "config.json";
pub const WELL_KNOWN_PATH: &'static str = "api/v1/index/config.json";
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
7 changes: 6 additions & 1 deletion scarb/src/core/registry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;
use async_trait::async_trait;
use once_cell::sync::Lazy;
use url::Url;

use crate::core::{ManifestDependency, Package, PackageId, Summary};

Expand All @@ -11,7 +13,10 @@ pub mod patch_map;
pub mod patcher;
pub mod source_map;

pub const DEFAULT_REGISTRY_INDEX: &str = "https://there-is-no-default-registry-yet.com";
pub static DEFAULT_REGISTRY_INDEX: Lazy<Url> = Lazy::new(|| {
Url::parse("https://there-is-no-default-registry-yet.com")
.expect("Default registry URL is invalid")
});

#[async_trait(?Send)]
pub trait Registry {
Expand Down
3 changes: 1 addition & 2 deletions scarb/src/core/source/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ impl SourceId {

pub fn default_registry() -> Self {
static CACHE: Lazy<SourceId> = Lazy::new(|| {
let url = Url::parse(DEFAULT_REGISTRY_INDEX).unwrap();
SourceId::new(url, SourceKind::Registry).unwrap()
SourceId::new(DEFAULT_REGISTRY_INDEX.clone(), SourceKind::Registry).unwrap()
});
*CACHE
}
Expand Down
1 change: 1 addition & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ pub const STARKNET_PLUGIN_NAME: &str = "starknet";
pub const TEST_PLUGIN_NAME: &str = "cairo_test";
pub const TEST_ASSERTS_PLUGIN_NAME: &str = "assert_macros";
pub const CAIRO_RUN_PLUGIN_NAME: &str = "cairo_run";
pub const REGISTRY_URL_ENV: &str = "SCARB_REGISTRY_URL";
5 changes: 4 additions & 1 deletion scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ pub fn resolve_workspace_with_opts(
let cairo_version = crate::version::get().cairo.version.parse().unwrap();
let version_req = DependencyVersionReq::exact(&cairo_version);
patch_map.insert(
SourceId::default().canonical_url.clone(),
SourceId::for_registry(ws.config().registry_url())
.expect("Failed to obtain registry url")
.canonical_url
.clone(),
[
ManifestDependency::builder()
.name(PackageName::CORE)
Expand Down
2 changes: 2 additions & 0 deletions scarb/src/ops/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn read_workspace_root<'c>(
source_id,
config.profile(),
Some(&toml_manifest),
config.registry_url(),
)
.with_context(|| format!("failed to parse manifest at: {manifest_path}"))?;
let manifest = Box::new(manifest);
Expand Down Expand Up @@ -118,6 +119,7 @@ fn read_workspace_root<'c>(
source_id,
config.profile(),
Some(&toml_manifest),
config.registry_url(),
)
.with_context(|| format!("failed to parse manifest at: {manifest_path}"))?;
let manifest = Box::new(manifest);
Expand Down
Loading