Skip to content

Commit

Permalink
Merge pull request #1 from helsing-ai/mara/libs
Browse files Browse the repository at this point in the history
Introduce protobuf libraries
  • Loading branch information
mara-schulke authored Jul 10, 2023
2 parents 99949f1 + 3fec81f commit d07b643
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 95 deletions.
49 changes: 48 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "buffrs"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "An opinionated protobuf package manager"
authors = ["Mara Schulke <[email protected]>"]
Expand All @@ -24,6 +24,7 @@ eyre = "0.6"
flate2 = "1"
futures = "0.3"
home = "0.5.5"
human-panic = "1"
keyring = "2"
reqwest = "0.11"
serde = { version = "1", features = ["derive"] }
Expand All @@ -33,5 +34,6 @@ toml = "0.7"
toml_edit = "0.19"
tracing = "0.1"
tracing-subscriber = "0.3"
serde_typename = "0.1"
url = { version = "2.4", features = ["serde"] }
walkdir = "2"
58 changes: 37 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Copyright 2023 Helsing GmbH. All rights reserved.

use buffrs::config::Config;
use buffrs::package::PackageId;
use buffrs::{config::Config, package::PackageType};
use clap::{Parser, Subcommand};

#[derive(Parser)]
Expand All @@ -16,9 +16,12 @@ struct Cli {
enum Command {
/// Initializes a buffrs setup
Init {
/// Sets up the repository as api package
#[clap(long)]
api: Option<PackageId>,
/// Sets up the package as lib
#[clap(long, conflicts_with = "api")]
lib: bool,
/// Sets up the package as api
#[clap(long, conflicts_with = "lib")]
api: bool,
},

/// Adds dependencies to a manifest file
Expand Down Expand Up @@ -61,6 +64,8 @@ enum Command {

#[tokio::main]
async fn main() -> eyre::Result<()> {
human_panic::setup_panic!();

color_eyre::install()?;

tracing_subscriber::fmt()
Expand All @@ -78,7 +83,16 @@ async fn main() -> eyre::Result<()> {
let config = Config::load().await?;

match cli.command {
Command::Init { api } => cmd::init(api).await?,
Command::Init { lib, api } => {
cmd::init(if lib {
Some(PackageType::Lib)
} else if api {
Some(PackageType::Api)
} else {
None
})
.await?
}
Command::Add { dependency } => cmd::add(dependency).await?,
Command::Remove { package } => cmd::remove(package).await?,
Command::Publish { repository } => cmd::publish(config, repository).await?,
Expand All @@ -94,19 +108,27 @@ async fn main() -> eyre::Result<()> {
mod cmd {
use buffrs::{
config::Config,
manifest::{ApiManifest, Dependency, Manifest},
package::{Package, PackageId, PackageStore},
manifest::{Dependency, Manifest, PackageManifest},
package::{PackageId, PackageStore, PackageType},
registry::{Artifactory, ArtifactoryConfig, Registry},
};
use eyre::{ensure, Context, ContextCompat};
use futures::future::try_join_all;

/// Initializes the project
pub async fn init(api: Option<PackageId>) -> eyre::Result<()> {
pub async fn init(r#type: Option<PackageType>) -> eyre::Result<()> {
let mut manifest = Manifest::default();

if let Some(name) = api {
manifest.api = Some(ApiManifest {
if let Some(r#type) = r#type {
let name = std::env::current_dir()?
.file_name()
.wrap_err("Failed to read current directory name")?
.to_str()
.wrap_err("Failed to read current directory name")?
.parse()?;

manifest.package = Some(PackageManifest {
r#type,
name,
version: "0.0.1".to_owned(),
description: None,
Expand All @@ -118,7 +140,9 @@ mod cmd {
"Cant initialize existing project"
);

manifest.write().await
manifest.write().await?;

PackageStore::create(r#type).await
}

/// Adds a dependency to this project
Expand Down Expand Up @@ -213,18 +237,10 @@ mod cmd {

let manifest = Manifest::read().await?;

let mut packages = Vec::with_capacity(manifest.dependencies.len());

for dep in manifest.dependencies {
packages.push(artifactory.download(dep));
}

let packages: Vec<Package> = try_join_all(packages).await?;

let mut install = Vec::new();

for package in packages {
install.push(PackageStore::install(package));
for dependency in manifest.dependencies {
install.push(PackageStore::install(dependency, artifactory.clone()));
}

try_join_all(install).await?;
Expand Down
20 changes: 11 additions & 9 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
use tokio::fs;

use crate::package::PackageId;
use crate::package::{PackageId, PackageType};

pub const MANIFEST_FILE: &str = "Proto.toml";

Expand All @@ -15,7 +15,7 @@ pub const MANIFEST_FILE: &str = "Proto.toml";
/// empty fields.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RawManifest {
pub api: Option<ApiManifest>,
pub package: Option<PackageManifest>,
pub dependencies: Option<DependencyMap>,
}

Expand All @@ -30,7 +30,7 @@ impl From<Manifest> for RawManifest {
let dependencies = (!dependencies.is_empty()).then_some(dependencies);

Self {
api: manifest.api,
package: manifest.package,
dependencies,
}
}
Expand All @@ -43,7 +43,7 @@ pub type DependencyMap = HashMap<PackageId, DependencyManifest>;
/// version of the `RawManifest` for easier use.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Manifest {
pub api: Option<ApiManifest>,
pub package: Option<PackageManifest>,
pub dependencies: Vec<Dependency>,
}

Expand Down Expand Up @@ -86,18 +86,20 @@ impl From<RawManifest> for Manifest {
.collect();

Self {
api: raw.api,
package: raw.package,
dependencies,
}
}
}

/// Manifest format for api packages
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ApiManifest {
/// Name of the api package
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct PackageManifest {
/// Type of the package
pub r#type: PackageType,
/// Name of the package
pub name: PackageId,
/// Version of the api package
/// Version of the package
pub version: String,
/// Description of the api package
pub description: Option<String>,
Expand Down
Loading

0 comments on commit d07b643

Please sign in to comment.