Skip to content

Commit 78f55cc

Browse files
l4lJoshua Nelson
authored and
Joshua Nelson
committed
Support custom indexes
1 parent 14f36e9 commit 78f55cc

File tree

10 files changed

+100
-35
lines changed

10 files changed

+100
-35
lines changed

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ log = "0.4"
2020
regex = "1"
2121
structopt = "0.3"
2222
crates-index = "0.15.1"
23-
crates-index-diff = "7"
23+
crates-index-diff = "7.1"
2424
reqwest = { version = "0.10.6", features = ["blocking", "json"] } # TODO: Remove blocking when async is ready
2525
semver = { version = "0.9", features = ["serde"] }
2626
slug = "=0.1.1"
@@ -40,7 +40,7 @@ schemamama = "0.3"
4040
schemamama_postgres = "0.3"
4141
systemstat = "0.1.4"
4242
prometheus = { version = "0.10.0", default-features = false }
43-
rustwide = "0.10.0"
43+
rustwide = { git = "https://github.com/rust-lang/rustwide.git" , rev = "bcf19b84128c983d02257a0ae9b91e5a6772122b"}
4444
mime_guess = "2"
4545
dotenv = "0.15"
4646
zstd = "0.5"

src/bin/cratesfyi.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::sync::Arc;
66
use docs_rs::db::{self, add_path_into_database, Pool, PoolClient};
77
use docs_rs::utils::{remove_crate_priority, set_crate_priority};
88
use docs_rs::{
9-
BuildQueue, Config, Context, DocBuilder, Index, Metrics, RustwideBuilder, Server, Storage,
9+
BuildQueue, Config, Context, DocBuilder, Index, Metrics, PackageKind, RustwideBuilder, Server,
10+
Storage,
1011
};
1112
use failure::{err_msg, Error, ResultExt};
1213
use once_cell::sync::OnceCell;
@@ -257,8 +258,16 @@ enum BuildSubcommand {
257258
#[structopt(name = "CRATE_VERSION")]
258259
crate_version: Option<String>,
259260

261+
/// Url for registry different from cratesio
262+
#[structopt(name = "CRATE_REGISTRY")]
263+
crate_registry: Option<String>,
264+
260265
/// Build a crate at a specific path
261-
#[structopt(short = "l", long = "local", conflicts_with_all(&["CRATE_NAME", "CRATE_VERSION"]))]
266+
#[structopt(
267+
short = "l",
268+
long = "local",
269+
conflicts_with_all(&["CRATE_NAME", "CRATE_VERSION", "CRATE_REGISTRY"])
270+
)]
262271
local: Option<PathBuf>,
263272
},
264273

@@ -299,6 +308,7 @@ impl BuildSubcommand {
299308
Self::Crate {
300309
crate_name,
301310
crate_version,
311+
crate_registry,
302312
local,
303313
} => {
304314
let mut builder = rustwide_builder()?;
@@ -313,7 +323,10 @@ impl BuildSubcommand {
313323
&crate_name.ok_or_else(|| err_msg("must specify name if not local"))?,
314324
&crate_version
315325
.ok_or_else(|| err_msg("must specify version if not local"))?,
316-
None,
326+
crate_registry
327+
.as_ref()
328+
.map(|s| PackageKind::Registry(s.as_str()))
329+
.unwrap_or(PackageKind::CratesIo),
317330
)
318331
.context("Building documentation failed")?;
319332
}
@@ -593,7 +606,16 @@ impl Context for BinContext {
593606
fn index(&self) -> Result<Arc<Index>, Error> {
594607
Ok(self
595608
.index
596-
.get_or_try_init::<_, Error>(|| Ok(Arc::new(Index::new(&*self.config()?)?)))?
609+
.get_or_try_init::<_, Error>(|| {
610+
let config = self.config()?;
611+
Ok(Arc::new(
612+
if let Some(registry_url) = config.registry_url.clone() {
613+
Index::from_url(config.registry_index_path.clone(), registry_url)
614+
} else {
615+
Index::new(config.registry_index_path.clone())
616+
}?,
617+
))
618+
})?
597619
.clone())
598620
}
599621
}

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct Config {
1212

1313
pub prefix: PathBuf,
1414
pub registry_index_path: PathBuf,
15+
pub registry_url: Option<String>,
1516

1617
// Database connection params
1718
pub(crate) database_url: String,
@@ -56,6 +57,7 @@ impl Config {
5657

5758
prefix: prefix.clone(),
5859
registry_index_path: env("REGISTRY_INDEX_PATH", prefix.join("crates.io-index"))?,
60+
registry_url: maybe_env("REGISTRY_URL")?,
5961

6062
database_url: require_env("CRATESFYI_DATABASE_URL")?,
6163
max_pool_size: env("DOCSRS_MAX_POOL_SIZE", 90)?,

src/docbuilder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod queue;
44
mod rustwide_builder;
55

66
pub(crate) use self::limits::Limits;
7-
pub use self::rustwide_builder::RustwideBuilder;
87
pub(crate) use self::rustwide_builder::{BuildResult, DocCoverage};
8+
pub use self::rustwide_builder::{PackageKind, RustwideBuilder};
99

1010
use crate::db::Pool;
1111
use crate::error::Result;

src/docbuilder/queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Updates registry index and builds new packages
22
3-
use super::{DocBuilder, RustwideBuilder};
3+
use super::{DocBuilder, PackageKind, RustwideBuilder};
44
use crate::error::Result;
55
use crate::utils::get_crate_priority;
66
use crate::Index;
@@ -79,7 +79,7 @@ impl DocBuilder {
7979
queue.process_next_crate(|krate| {
8080
processed = true;
8181

82-
builder.build_package(&krate.name, &krate.version, None)?;
82+
builder.build_package(&krate.name, &krate.version, PackageKind::CratesIo)?;
8383
Ok(())
8484
})?;
8585

src/docbuilder/rustwide_builder.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ const ESSENTIAL_FILES_UNVERSIONED: &[&str] = &[
5656
const DUMMY_CRATE_NAME: &str = "empty-library";
5757
const DUMMY_CRATE_VERSION: &str = "1.0.0";
5858

59+
pub enum PackageKind<'a> {
60+
Local(&'a Path),
61+
CratesIo,
62+
Registry(&'a str),
63+
}
64+
5965
pub struct RustwideBuilder {
6066
workspace: Workspace,
6167
toolchain: Toolchain,
@@ -259,7 +265,12 @@ impl RustwideBuilder {
259265
crates_from_path(
260266
&self.config.registry_index_path.clone(),
261267
&mut |name, version| {
262-
if let Err(err) = self.build_package(name, version, None) {
268+
let registry_url = self.config.registry_url.clone();
269+
let package_kind = registry_url
270+
.as_ref()
271+
.map(|r| PackageKind::Registry(r.as_str()))
272+
.unwrap_or(PackageKind::CratesIo);
273+
if let Err(err) = self.build_package(name, version, package_kind) {
263274
warn!("failed to build package {} {}: {}", name, version, err);
264275
}
265276
},
@@ -273,14 +284,14 @@ impl RustwideBuilder {
273284
err.context(format!("failed to load local package {}", path.display()))
274285
})?;
275286
let package = metadata.root();
276-
self.build_package(&package.name, &package.version, Some(path))
287+
self.build_package(&package.name, &package.version, PackageKind::Local(path))
277288
}
278289

279290
pub fn build_package(
280291
&mut self,
281292
name: &str,
282293
version: &str,
283-
local: Option<&Path>,
294+
kind: PackageKind<'_>,
284295
) -> Result<bool> {
285296
let mut conn = self.db.get()?;
286297

@@ -302,10 +313,10 @@ impl RustwideBuilder {
302313
let mut build_dir = self.workspace.build_dir(&format!("{}-{}", name, version));
303314
build_dir.purge()?;
304315

305-
let krate = if let Some(path) = local {
306-
Crate::local(path)
307-
} else {
308-
Crate::crates_io(name, version)
316+
let krate = match kind {
317+
PackageKind::Local(path) => Crate::local(path),
318+
PackageKind::CratesIo => Crate::crates_io(name, version),
319+
PackageKind::Registry(registry) => Crate::registry(registry, name, version),
309320
};
310321
krate.fetch(&self.workspace)?;
311322

src/index/mod.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{path::PathBuf, process::Command};
33
use url::Url;
44

55
use self::{api::Api, crates::Crates};
6-
use crate::{error::Result, Config};
6+
use crate::error::Result;
77
use failure::ResultExt;
88

99
pub(crate) mod api;
@@ -12,9 +12,10 @@ mod crates;
1212
pub struct Index {
1313
path: PathBuf,
1414
api: Api,
15+
repository_url: Option<String>,
1516
}
1617

17-
#[derive(serde::Deserialize, Clone)]
18+
#[derive(Debug, serde::Deserialize, Clone)]
1819
#[serde(rename_all = "kebab-case")]
1920
struct IndexConfig {
2021
dl: String,
@@ -40,19 +41,44 @@ fn load_config(repo: &git2::Repository) -> Result<IndexConfig> {
4041
}
4142

4243
impl Index {
43-
pub fn new(app_config: &Config) -> Result<Self> {
44-
let path = app_config.registry_index_path.clone();
44+
pub fn from_url(path: PathBuf, repository_url: String) -> Result<Self> {
45+
let url = repository_url.clone();
46+
let diff = crates_index_diff::Index::from_path_or_cloned_with_options(
47+
&path,
48+
crates_index_diff::CloneOptions { repository_url },
49+
)
50+
.context("initialising registry index repository")?;
51+
52+
let config = load_config(diff.repository()).context("loading registry config")?;
53+
let api = Api::new(config.api).context("initialising registry api client")?;
54+
Ok(Self {
55+
path,
56+
api,
57+
repository_url: Some(url),
58+
})
59+
}
60+
61+
pub fn new(path: PathBuf) -> Result<Self> {
4562
// This initializes the repository, then closes it afterwards to avoid leaking file descriptors.
4663
// See https://github.com/rust-lang/docs.rs/pull/847
4764
let diff = crates_index_diff::Index::from_path_or_cloned(&path)
4865
.context("initialising registry index repository")?;
4966
let config = load_config(diff.repository()).context("loading registry config")?;
5067
let api = Api::new(config.api).context("initialising registry api client")?;
51-
Ok(Self { path, api })
68+
Ok(Self {
69+
path,
70+
api,
71+
repository_url: None,
72+
})
5273
}
5374

5475
pub(crate) fn diff(&self) -> Result<crates_index_diff::Index> {
55-
let diff = crates_index_diff::Index::from_path_or_cloned(&self.path)
76+
let options = self
77+
.repository_url
78+
.clone()
79+
.map(|repository_url| crates_index_diff::CloneOptions { repository_url })
80+
.unwrap_or_default();
81+
let diff = crates_index_diff::Index::from_path_or_cloned_with_options(&self.path, options)
5682
.context("re-opening registry index for diff")?;
5783
Ok(diff)
5884
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub use self::build_queue::BuildQueue;
66
pub use self::config::Config;
77
pub use self::context::Context;
88
pub use self::docbuilder::DocBuilder;
9+
pub use self::docbuilder::PackageKind;
910
pub use self::docbuilder::RustwideBuilder;
1011
pub use self::index::Index;
1112
pub use self::metrics::Metrics;

src/test/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ impl TestEnvironment {
197197
pub(crate) fn index(&self) -> Arc<Index> {
198198
self.index
199199
.get_or_init(|| {
200-
Arc::new(Index::new(&*self.config()).expect("failed to initialize the index"))
200+
Arc::new(
201+
Index::new(self.config().registry_index_path.clone())
202+
.expect("failed to initialize the index"),
203+
)
201204
})
202205
.clone()
203206
}

0 commit comments

Comments
 (0)