Skip to content

Commit

Permalink
Merge branch 'main' into gbouv/add-latest-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
gbouv authored Jul 5, 2024
2 parents 5a0c9ec + badf7e5 commit 0729eb4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Usage: buffrs <COMMAND>
Commands:
init Initializes a buffrs setup
new Creates a new buffrs package in the current directory
lint Check rule violations for this package
add Adds dependencies to a manifest file
remove Removes dependencies from a manifest file
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
* [buffrs update]()
* [Package Commands](commands/package-commands.md)
* [buffrs init](commands/buffrs-init.md)
* [buffrs new](commands/buffrs-new.md)
* [buffrs lint](commands/buffrs-lint.md)
* [buffrs package](commands/buffrs-package.md)
* [buffrs install](commands/buffrs-install.md)
Expand Down
21 changes: 21 additions & 0 deletions docs/src/commands/buffrs-new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## buffrs init

Initializes a Buffrs project in a new folder created in the current directory.

### Synopsis

`buffrs new <NAME>`

`buffrs new --lib <NAME>`

`buffrs new --api <NAME>`

### Description

This command creates a new Buffrs project with the provided name by creating a
manifest file (`Proto.toml`) as well as `proto` and `proto/vendor` directories
in a new directory created at the current location.

By default, if no package type is provided, `impl` (implementation) will be
used. The meaning of this is described in [Package
Types](../guide/package-types.md).
39 changes: 38 additions & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ use crate::{
use async_recursion::async_recursion;
use miette::{bail, ensure, miette, Context as _, IntoDiagnostic};
use semver::{Version, VersionReq};
use std::{env, path::Path, str::FromStr};
use std::{
env,
path::{Path, PathBuf},
str::FromStr,
};
use tokio::{
fs,
io::{self, AsyncBufReadExt, BufReader},
Expand Down Expand Up @@ -77,6 +81,39 @@ pub async fn init(kind: Option<PackageType>, name: Option<PackageName>) -> miett
Ok(())
}

/// Initializes a project with the given name in the current directory
pub async fn new(kind: Option<PackageType>, name: PackageName) -> miette::Result<()> {
let package_dir = PathBuf::from(name.to_string());
// create_dir fails if the folder already exists
fs::create_dir(&package_dir)
.await
.into_diagnostic()
.wrap_err(miette!(
"failed to create {} directory",
package_dir.display()
))?;

let package = kind
.map(|kind| -> miette::Result<PackageManifest> {
Ok(PackageManifest {
kind,
name,
version: INITIAL_VERSION,
description: None,
})
})
.transpose()?;

let manifest = Manifest::new(package, vec![]);
manifest.write_at(&package_dir).await?;

PackageStore::open(&package_dir)
.await
.wrap_err(miette!("failed to create buffrs `proto` directories"))?;

Ok(())
}

struct DependencyLocator {
repository: String,
package: PackageName,
Expand Down
40 changes: 33 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ enum Command {
package: Option<PackageName>,
},

/// Creates a new buffrs package in the current directory
New {
/// Sets up the package as lib
#[clap(long, conflicts_with = "api")]
#[arg(group = "pkg")]
lib: bool,
/// Sets up the package as api
#[clap(long, conflicts_with = "lib")]
#[arg(group = "pkg")]
api: bool,
/// The package name
#[clap(requires = "pkg")]
package: PackageName,
},

/// Check rule violations for this package.
Lint,

Expand Down Expand Up @@ -177,13 +192,7 @@ async fn main() -> miette::Result<()> {

match cli.command {
Command::Init { lib, api, package } => {
let kind = if lib {
Some(PackageType::Lib)
} else if api {
Some(PackageType::Api)
} else {
None
};
let kind = infer_package_type(lib, api);

command::init(kind, package.to_owned())
.await
Expand All @@ -192,6 +201,13 @@ async fn main() -> miette::Result<()> {
package.map(|p| format!("`{p}`")).unwrap_or_default()
))
}
Command::New { lib, api, package } => {
let kind = infer_package_type(lib, api);

command::new(kind, package.to_owned())
.await
.wrap_err(miette!("failed to initialize {}", format!("`{package}`")))
}
Command::Login { registry } => command::login(registry.to_owned())
.await
.wrap_err(miette!("failed to login to `{registry}`")),
Expand Down Expand Up @@ -255,3 +271,13 @@ async fn main() -> miette::Result<()> {
},
}
}

fn infer_package_type(lib: bool, api: bool) -> Option<PackageType> {
if lib {
Some(PackageType::Lib)
} else if api {
Some(PackageType::Api)
} else {
None
}
}
8 changes: 7 additions & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,20 @@ impl Manifest {

/// Persists the manifest into the current directory
pub async fn write(&self) -> miette::Result<()> {
self.write_at(Path::new(".")).await
}

/// Persists the manifest into the provided directory, which must exist
pub async fn write_at(&self, dir_path: &Path) -> miette::Result<()> {
// hint: create a canary manifest from the current one
let raw = RawManifest::from(Manifest::new(
self.package.clone(),
self.dependencies.clone(),
));

let manifest_file_path = dir_path.join(MANIFEST_FILE);
fs::write(
MANIFEST_FILE,
manifest_file_path,
toml::to_string(&raw)
.into_diagnostic()
.wrap_err(SerializationError(ManagedFile::Manifest))?
Expand Down

0 comments on commit 0729eb4

Please sign in to comment.