Skip to content

Commit

Permalink
Move to our own version type
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed Nov 14, 2024
1 parent d488241 commit 2121742
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 185 deletions.
100 changes: 100 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 @@ -90,6 +90,7 @@ serde = "1.0.164"
serde_json = "1.0.96"
serde_repr = "0.1"
serde-wasm-bindgen = "0.5.0"
serde_with = "3.11.0"
serde_yaml = "0.9.19"
sha-1 = "0.10.0"
sha2 = "0.10.6"
Expand Down
7 changes: 4 additions & 3 deletions cli/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use nickel_lang_core::{
use nickel_lang_package::{
config::Config,
index::{self, PackageIndex},
ManifestFile, SemanticVersion,
version::SemVer,
ManifestFile,
};

use crate::{
Expand Down Expand Up @@ -46,7 +47,7 @@ pub enum Command {
index: PathBuf,

#[arg(long)]
version: SemanticVersion,
version: SemVer,

#[arg(long)]
commit_id: ObjectId,
Expand Down Expand Up @@ -110,7 +111,7 @@ impl PackageCommand {
commit_id,
} => {
let package =
nickel_lang_package::index::fetch_git(package_id, *version, commit_id)?;
nickel_lang_package::index::fetch_git(package_id, version.clone(), commit_id)?;
let config = Config::default().with_index_dir(index.clone());
let mut package_index = PackageIndex::new(config);
package_index.save(package)?;
Expand Down
2 changes: 2 additions & 0 deletions package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ regex.workspace = true
semver = { version = "1.0.23", features = ["serde"] }
serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
tempfile = { workspace = true }
thiserror.workspace = true

[dev-dependencies]
insta = { workspace = true, features = ["filters"] }
Expand Down
4 changes: 2 additions & 2 deletions package/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::path::{Path, PathBuf};
use nickel_lang_core::{
eval::cache::CacheImpl, identifier::Ident, package::ObjectId, program::Program,
};
use pubgrub::version::SemanticVersion;

use crate::{
index::{self},
version::SemVer,
UnversionedPackage,
};

Expand Down Expand Up @@ -69,7 +69,7 @@ pub enum Error {
/// package and version was already present.
DuplicateIndexPackageVersion {
id: index::Id,
version: SemanticVersion,
version: SemVer,
},
}

Expand Down
30 changes: 17 additions & 13 deletions package/src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ use std::{

use nickel_lang_core::{identifier::Ident, package::ObjectId};
use nickel_lang_git::Spec;
use pubgrub::version::SemanticVersion;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tempfile::{tempdir_in, NamedTempFile};

use crate::{
config::Config,
error::{Error, IoResultExt as _},
version::SemVer,
Precise, VersionReq,
};

Expand Down Expand Up @@ -79,7 +79,11 @@ impl PackageCache {
let data = std::fs::read_to_string(&path).with_path(&path)?;
for line in data.lines() {
let package: Package = serde_json::from_str(line).unwrap();
if file.packages.insert(package.vers, package).is_some() {
if file
.packages
.insert(package.vers.clone(), package)
.is_some()
{
panic!("duplicate version, index is corrupt");
}
}
Expand All @@ -97,12 +101,12 @@ impl PackageCache {
/// (Also retains a cached copy in memory.)
pub fn save(&mut self, pkg: Package) -> Result<(), Error> {
let id: Id = pkg.id.clone().into();
let version = pkg.vers;
let version = pkg.vers.clone();
let mut existing = self
.load(&id)?
.cloned()
.unwrap_or(CachedPackageFile::default());
if existing.packages.insert(pkg.vers, pkg).is_some() {
if existing.packages.insert(pkg.vers.clone(), pkg).is_some() {
return Err(Error::DuplicateIndexPackageVersion { id, version });
}
let mut tmp = self.tmp_file(&id);
Expand Down Expand Up @@ -172,7 +176,7 @@ impl PackageIndex {
pub fn available_versions<'a>(
&'a self,
id: &Id,
) -> Result<impl Iterator<Item = SemanticVersion> + 'a, Error> {
) -> Result<impl Iterator<Item = SemVer> + 'a, Error> {
let mut cache = self.cache.borrow_mut();
let pkg_file = cache.load(id)?;
let versions: Vec<_> = pkg_file
Expand All @@ -181,31 +185,31 @@ impl PackageIndex {
Ok(versions.into_iter())
}

pub fn all_versions(&self, id: &Id) -> Result<HashMap<SemanticVersion, Package>, Error> {
pub fn all_versions(&self, id: &Id) -> Result<HashMap<SemVer, Package>, Error> {
let mut cache = self.cache.borrow_mut();
let pkg_file = cache.load(id)?;
Ok(pkg_file
.map(|pkg_file| {
pkg_file
.packages
.iter()
.map(|(v, package)| (*v, package.clone()))
.map(|(v, package)| (v.clone(), package.clone()))
.collect()
})
.unwrap_or_default())
}

pub fn package(&self, id: &Id, v: SemanticVersion) -> Result<Option<Package>, Error> {
pub fn package(&self, id: &Id, v: SemVer) -> Result<Option<Package>, Error> {
Ok(self.all_versions(id)?.get(&v).cloned())
}

pub fn save(&mut self, pkg: Package) -> Result<(), Error> {
self.cache.borrow_mut().save(pkg)
}

pub fn ensure_downloaded(&self, id: &Id, v: SemanticVersion) -> Result<(), Error> {
pub fn ensure_downloaded(&self, id: &Id, v: SemVer) -> Result<(), Error> {
let package = self
.package(id, v)?
.package(id, v.clone())?
.ok_or_else(|| Error::UnknownIndexPackage { id: id.clone() })?;
let precise = Precise::Index {
id: id.clone(),
Expand Down Expand Up @@ -371,15 +375,15 @@ impl From<PreciseId> for Id {

#[derive(Clone, Debug, Default)]
pub struct CachedPackageFile {
pub packages: BTreeMap<SemanticVersion, Package>,
pub packages: BTreeMap<SemVer, Package>,
}

/// A package record in the index.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Package {
pub id: PreciseId,
pub vers: SemanticVersion,
pub nickel_vers: SemanticVersion,
pub vers: SemVer,
pub nickel_vers: SemVer,
pub deps: BTreeMap<Ident, IndexDependency>,

/// Version of the index schema. Currently always zero.
Expand Down
4 changes: 2 additions & 2 deletions package/src/index/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use nickel_lang_core::package::ObjectId;
use nickel_lang_git::Spec;
use pubgrub::version::SemanticVersion;
use tempfile::tempdir;

use crate::{
error::{Error, IoResultExt},
version::SemVer,
ManifestFile,
};

Expand All @@ -19,7 +19,7 @@ use super::{Id, Package, PreciseId};
/// Fetch a package from the specified place, and figure out what its index
/// entry should look like.
/// TODO: allow a subdirectory?
pub fn fetch_git(id: &Id, version: SemanticVersion, commit: &ObjectId) -> Result<Package, Error> {
pub fn fetch_git(id: &Id, version: SemVer, commit: &ObjectId) -> Result<Package, Error> {
// We need to fetch the manifest file to get some metadata out. We're currently shallow-cloning
// the whole repo, but we could use a github API (or maybe some fancier git features) to be more
// efficient.
Expand Down
Loading

0 comments on commit 2121742

Please sign in to comment.