Skip to content

Commit

Permalink
wip: add 'directory' to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Apr 22, 2024
1 parent 94fc6c4 commit 198ab02
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 15 deletions.
1 change: 1 addition & 0 deletions registry/proto/package.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ message Package {
buffrs.package.Type type = 1;
string name = 2;
string version = 3;
optional string directory = 4;
}

message Compressed {
Expand Down
12 changes: 11 additions & 1 deletion registry/src/api/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
types::PackageVersion,
};
use async_trait::async_trait;
use buffrs::package::PackageDirectory;
use buffrs::{manifest::PackageManifest, package::PackageName, package::PackageType};
use semver::{Version, VersionReq};
use tonic::{Code, Request, Response, Status};
Expand Down Expand Up @@ -69,6 +70,14 @@ impl Registry for Context {
let package_name = PackageName::from_str(metadata.name.as_str()).map_err(|error| {
Status::invalid_argument(format!("Package name isn't correct, {:?}", error))
})?;
let package_directory = metadata
.directory
.as_deref()
.map(PackageDirectory::from_str)
.transpose()
.map_err(|error| {
Status::invalid_argument(format!("Package directory isn't correct, {:?}", error))
})?;

let version = Version::from_str(metadata.version.as_str())
.map_err(|_| Status::invalid_argument("version isn't correct"))?;
Expand Down Expand Up @@ -115,7 +124,8 @@ impl Registry for Context {

let package_manifest = PackageManifest {
kind: package_type as PackageType,
name: package_name.clone(),
name: package_name,
directory: package_directory,
version: version.clone(),
description: None,
};
Expand Down
1 change: 1 addition & 0 deletions registry/src/metadata/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl FetchMatching<InMemoryMetadataStorage> for PackageManifest {
.map(|(_version, manifest)| buffrs::manifest::PackageManifest {
kind: manifest.kind,
name: manifest.name.clone(),
directory: manifest.directory.clone(),
version: manifest.version.clone(),
description: manifest.description.clone(),
})
Expand Down
25 changes: 20 additions & 5 deletions registry/src/metadata/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use super::*;

use buffrs::package::PackageType;
use buffrs::package::{PackageDirectory, PackageType};
use semver::Version;
use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_256};
Expand Down Expand Up @@ -108,6 +108,7 @@ impl Publish<PgsqlMetadataStorage> for PackageManifest {
let db_package = sqlx::query_as::<_, PgPackage>(SELECT_OR_INSERT_PACKAGE_QUERY)
.bind(package.name.to_string())
.bind(package_lib)
.bind(package.directory.as_ref().map(|d| d.to_string()))
.fetch_one(&mut *tx)
.await
.map_err(|_| MetadataStorageError::Internal)?;
Expand Down Expand Up @@ -190,8 +191,8 @@ struct PgPackage {

const SELECT_OR_INSERT_PACKAGE_QUERY: &str = r#"
WITH ins AS (
INSERT INTO packages (name, type, created_at, updated_at)
SELECT $1, $2, NOW(), NOW()
INSERT INTO packages (name, type, directory, created_at, updated_at)
SELECT $1, $2, $3, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM packages WHERE name = $1)
RETURNING *
)
Expand All @@ -207,18 +208,19 @@ struct PgPackageVersionQuery {
name: String,
#[sqlx(rename = "type")]
kind: PgPackageType,
directory: Option<String>,
version: String,
}

const SELECT_VERSION_QUERY: &str = r#"
SELECT p.id, p.name, p.type, v.version
SELECT p.id, p.name, p.type, p.directory, v.version
FROM versions v
INNER JOIN packages p ON v.package_id = p.id
WHERE p.name = $1 and v.version = $2;
"#;

const SELECT_VERSION_COLLATE: &str = r#"
SELECT p.id, p.name, p.type, v.version
SELECT p.id, p.name, p.type, p.directory, v.version
FROM versions v
INNER JOIN packages p ON v.package_id = p.id
WHERE p.name = $1
Expand Down Expand Up @@ -288,6 +290,19 @@ impl TryFrom<PgPackageVersionQuery> for PackageManifest {
);
MetadataStorageError::Internal
})?,
directory: value
.directory
.as_deref()
.map(|dir| {
PackageDirectory::from_str(dir).map_err(|x| {
error!(
"Error: {}, packageDirectory: {} couldn't be mapped to a PackageDirectory",
x, dir
);
MetadataStorageError::Internal
})
})
.transpose()?,
version: Version::from_str(&value.version).map_err(|x| {
error!(
"Error: {}, version {} couldn't be mapped to a semver::Version",
Expand Down
1 change: 1 addition & 0 deletions registry/tests/api/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn create_publish_request_sample(version: Option<String>) -> PublishRequest
name: "testing".to_string(),
version: version.unwrap_or("1.0.0".to_string()),
r#type: 0,
directory: None,
}),
tgz: vec![0, 0, 0],
}),
Expand Down
1 change: 1 addition & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn init(kind: Option<PackageType>, name: Option<PackageName>) -> miett
Ok(PackageManifest {
kind,
name,
directory: None,
version: INITIAL_VERSION,
description: None,
})
Expand Down
5 changes: 4 additions & 1 deletion src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use url::Url;

use crate::{
errors::{DeserializationError, FileExistsError, FileNotFound, SerializationError, WriteError},
package::{Package, PackageName},
package::{Package, PackageDirectory, PackageName},
registry::RegistryUri,
ManagedFile,
};
Expand All @@ -41,6 +41,8 @@ pub const LOCKFILE: &str = "Proto.lock";
pub struct LockedPackage {
/// The name of the package
pub name: PackageName,
/// Directory where the package's contents are stored
pub directory: Option<PackageDirectory>,
/// The cryptographic digest of the package contents
pub digest: Digest,
/// The URI of the registry that contains the package
Expand All @@ -67,6 +69,7 @@ impl LockedPackage {
) -> Self {
Self {
name: package.name().to_owned(),
directory: package.directory().cloned(),
registry,
repository,
digest: DigestAlgorithm::SHA256.digest(&package.tgz),
Expand Down
13 changes: 12 additions & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tokio::fs;

use crate::{
errors::{DeserializationError, FileExistsError, SerializationError, WriteError},
package::{PackageName, PackageType},
package::{PackageDirectory, PackageName, PackageType},
registry::RegistryUri,
ManagedFile,
};
Expand Down Expand Up @@ -377,12 +377,23 @@ pub struct PackageManifest {
pub kind: PackageType,
/// Name of the package
pub name: PackageName,
/// Directory in which to put the cache. If unset, defaults to the package name
pub directory: Option<PackageDirectory>,
/// Version of the package
pub version: Version,
/// Description of the api package
pub description: Option<String>,
}

impl PackageManifest {
/// Get the directory where the package contents will be stored.
///
/// This fallbacks to `name` if `directory` is unset.
pub fn directory(&self) -> &str {
self.directory.as_deref().unwrap_or(self.name.as_ref())
}
}

/// Represents a single project dependency
#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
pub struct Dependency {
Expand Down
27 changes: 27 additions & 0 deletions src/package/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use crate::{
ManagedFile,
};

use super::PackageDirectory;

/// An in memory representation of a `buffrs` package
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Package {
Expand Down Expand Up @@ -208,6 +210,31 @@ impl Package {
.name
}

/// The directory of this package
#[inline]
pub fn directory(&self) -> Option<&PackageDirectory> {
assert!(self.manifest.package.is_some());

self.manifest
.package
.as_ref()
.expect("compressed package contains invalid manifest (package section missing)")
.directory
.as_ref()
}

/// Directory for this oackage
#[inline]
pub fn directory_str(&self) -> &str {
assert!(self.manifest.package.is_some());

self.manifest
.package
.as_ref()
.expect("compressed package contains invalid manifest (package section missing)")
.directory()
}

/// The version of this package
#[inline]
pub fn version(&self) -> &Version {
Expand Down
Loading

0 comments on commit 198ab02

Please sign in to comment.