Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Improve search and fix custom index deps
Browse files Browse the repository at this point in the history
  - Search functionality has now been improved - we no longer
    hit an API endpoint in order to search packages, and all
    indices specified in configuration are searched.

  - Custom index dependencies now follow the same syntax as is
    specified in the documentation; since this wasn't the case
    before, this is a breaking change.

See #49.
  • Loading branch information
David Cao committed Apr 16, 2019
1 parent 21a555a commit a2c709e
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@

### Changed

- **BREAKING CHANGE**: the syntax for specifying a package from a custom
index has now changed to be consistent with the docs:

```toml
"index/explicit" = { version = ">= 0.1.0", index = "index+dir+../index" }
```

- **BREAKING CHANGE**: the `backend` field in `index.toml` is now
`registry`.

Expand Down
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ serde_derive = "1.0"
serde_json = "1.0"
sha2 = "0.7"
shell-escape = "0.1"
simsearch = "0.1"
slog = { version = "2", features = ["max_level_trace", "release_max_level_warn"] }
slog-async = "2"
slog-term = "2"
Expand Down
14 changes: 3 additions & 11 deletions src/bin/cmds/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{args, get};
use super::get;
use clap::{App, Arg, ArgMatches, SubCommand};
use elba::{
cli::registry,
Expand All @@ -14,21 +14,13 @@ pub fn cli() -> App<'static, 'static> {
.required(true)
.help("The search query."),
)
.arg(args::index())
}

pub fn exec(c: &mut Config, args: &ArgMatches) -> Res<String> {
let bck = get::index(c, args)?;
let query = args.value_of("query").unwrap();

let bcx = get::build_ctx(c, args);
let bck_text = bck.to_string();
let ctx = registry::RegistryCtx {
index: bck,
data_dir: c.directories.data.clone(),
};

println!("{}", registry::search(&bcx, &ctx, &query)?);
println!("{}", registry::search(&bcx, &query)?);

Ok(format!("search complete in index {}", bck_text))
Ok("search complete".to_string())
}
4 changes: 1 addition & 3 deletions src/lib/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ pub fn compile_lib(

let src_walker = source
.meta()
.list_files(source.path(), &src_path, |x| {
x.path() != &layout.build
})?;
.list_files(source.path(), &src_path, |x| x.path() != layout.build)?;

clear_dir(&layout.build.join("lib"))?;
copy_dir_iter(src_walker, &src_path, &layout.build.join("lib"))?;
Expand Down
39 changes: 25 additions & 14 deletions src/lib/cli/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,28 @@ pub fn yank(bcx: &build::BuildCtx, ctx: &RegistryCtx, name: &Name, ver: &Version
Ok(())
}

// TODO: Local fallback
// TODO: Search from multiple indices
pub fn search(bcx: &build::BuildCtx, ctx: &RegistryCtx, query: &str) -> Res<String> {
let mut cache = Cache::from_disk(&bcx.logger, bcx.global_cache.clone(), bcx.shell)?;
let (indices, registry) = get_registry(&mut cache, ctx.index.res.clone(), false);
let registry = registry?;

let res = registry.search(&indices, query)?;

let mut s = String::new();
for x in res.packages {
s.push_str(&format!("\"{}/{}\" = \"{}\"\n", x.group, x.name, x.version));
pub fn search(bcx: &build::BuildCtx, query: &str) -> Res<String> {
let cache = Cache::from_disk(&bcx.logger, bcx.global_cache.clone(), bcx.shell)?;
let ixs = bcx
.indices
.values()
.cloned()
.map(|x| x.res)
.collect::<Vec<_>>();
let indices = cache.get_indices(&ixs, false, false);

let pkgs = indices.search(query)?;
let mut res = String::new();

for (name, ver, ir) in &pkgs {
if ir.res == ixs[0] {
res.push_str(&format!("\"{}\" = \"{}\"", name, ver));
} else {
res.push_str(&format!("\"{}\" = \"{{ version = {}, index = {} }}\"", name, ver, ir));
}
}

Ok(s)
Ok(res)
}

pub fn publish(bcx: &build::BuildCtx, ctx: &RegistryCtx, project: &Path, verify: bool) -> Res<()> {
Expand Down Expand Up @@ -175,7 +182,11 @@ fn get_token(ctx: &RegistryCtx) -> Res<String> {
})
}

fn get_registry(c: &mut Cache, d: DirectRes, eager: bool) -> (remote::Indices, Res<remote::Registry>) {
fn get_registry(
c: &mut Cache,
d: DirectRes,
eager: bool,
) -> (remote::Indices, Res<remote::Registry>) {
let dt = d.to_string();
let indices = c.get_indices(&[d], eager, false);
let registry = (|| {
Expand Down
14 changes: 7 additions & 7 deletions src/lib/package/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ pub struct PackageInfo {
pub enum DepReq {
Registry(Constraint),
RegLong {
con: Constraint,
registry: String,
version: Constraint,
index: String,
},
Local {
path: PathBuf,
Expand Down Expand Up @@ -202,14 +202,14 @@ impl DepReq {
let pi = PackageId::new(n, def_index.1.clone().into());
Ok((pi, c))
}
DepReq::RegLong { con, registry } => {
if let Some(mapped) = ixmap.get(&registry) {
DepReq::RegLong { version, index } => {
if let Some(mapped) = ixmap.get(&index) {
let pi = PackageId::new(n, mapped.clone().into());
Ok((pi, con))
Ok((pi, version))
} else {
let ix = IndexRes::from_str(&registry)?;
let ix = IndexRes::from_str(&index)?;
let pi = PackageId::new(n, ix.into());
Ok((pi, con))
Ok((pi, version))
}
}
DepReq::Local { path } => {
Expand Down
40 changes: 40 additions & 0 deletions src/lib/remote/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ use semver::Version;
use semver_constraints::Constraint;
use serde_derive::{Deserialize, Serialize};
use serde_json;
use simsearch::SimSearch;
use std::{
fs,
io::{self, prelude::*, BufReader},
str::FromStr,
};
use toml;
use walkdir::WalkDir;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct IndexConfig {
Expand Down Expand Up @@ -179,6 +181,29 @@ impl Indices {
Err(Error::from(ErrorKind::PackageNotFound))
}
}

pub fn search(&self, query: &str) -> Res<Vec<(Name, Version, &IndexRes)>> {
let mut engine: SimSearch<(&IndexRes, &str)> = SimSearch::new();
let x = self
.indices
.iter()
.map(|x| x.1.packages().map(move |p| (x.0, p)))
.flatten()
.collect::<Vec<_>>();

for (ir, pkg) in &x {
engine.insert((ir, pkg), pkg);
}
let pkgs = engine.search(query);

pkgs.iter().map(|(ir, pkg)| {
let name = Name::from_str(pkg).unwrap();
let ix = &self.indices[*ir];
let ver: Version = ix.entries(&name)?.into_iter().map(|x| x.0).last().unwrap();

Ok((name, ver, *ir))
}).collect::<Res<Vec<_>>>()
}
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
Expand Down Expand Up @@ -286,6 +311,21 @@ impl Index {
Ok(res)
}

pub fn packages(&self) -> impl Iterator<Item = String> {
let root_path = self.path.path().to_path_buf();
let git_path = root_path.join(".git");
WalkDir::new(self.path.path())
.min_depth(2)
.max_depth(3)
.into_iter()
.filter_entry(move |x| x.path().parent().unwrap() != git_path)
.filter_map(|x| x.ok())
.map(move |x| {
let stripped = x.path().strip_prefix(&root_path).unwrap();
stripped.to_string_lossy().to_string()
})
}

pub fn depends(&self) -> impl Iterator<Item = &IndexRes> {
self.config.index.dependencies.iter().map(|x| x.1)
}
Expand Down

0 comments on commit a2c709e

Please sign in to comment.