Skip to content

Commit

Permalink
feat: add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 4, 2024
1 parent 5b6d1cf commit 223248e
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 145 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ empty_structs_with_brackets = "warn"
rc_buffer = "warn"
rc_mutex = "warn"
same_name_method = "warn"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
module_name_repetitions = "allow"

[lib]
doctest = false
Expand All @@ -41,3 +44,5 @@ toml_edit = { version = "0.22.12", features = ["parse"] }
semver = "1.0.22"
git_cmd = "0.6.4"
crates_io_api = { version = "0.11.0", default-features = false, features = ["rustls"] }
toml = "0.8.13"
serde = "1.0.203"
104 changes: 104 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
fs,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use serde::Deserialize;

use crate::versioning::cargo::CargoWorkspace;

const RELEASE_CONFIG: &str = "oxc_release.toml";

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReleaseConfig {
#[serde(rename = "releases")]
pub release_sets: Vec<ReleaseSet>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReleaseSet {
pub name: String,

versioned_files: Vec<VersionedFile>,
}

impl ReleaseSet {
#[must_use]
pub fn versioned_packages(&self) -> Vec<VersionedPackage> {
self.versioned_files.iter().flat_map(|v| v.content.versioned_packages()).collect::<Vec<_>>()
}

pub fn update_version(&self, version: &str) -> Result<()> {
for versioned_file in &self.versioned_files {
versioned_file.content.update_version(version)?;
}
Ok(())
}
}

#[derive(Debug, Deserialize)]
#[serde(transparent)]
pub struct VersionedFile {
path: PathBuf,

#[serde(skip)]
content: VersionedContent,
}

#[derive(Debug, Default)]
pub enum VersionedContent {
#[default]
None,
Cargo(CargoWorkspace),
}

#[derive(Debug, Clone)]
pub struct VersionedPackage {
pub name: String,
pub dir: PathBuf,
pub path: PathBuf,
}

impl VersionedContent {
fn read(path: &Path) -> Result<Self> {
let file_name =
path.file_name().with_context(|| format!("{path:?} does not have a filename."))?;
let content = match file_name.to_string_lossy().as_ref() {
"Cargo.toml" => Self::Cargo(CargoWorkspace::new(path)?),
_ => anyhow::bail!("{path:?} is not recognized"),
};
Ok(content)
}

#[must_use]
pub fn versioned_packages(&self) -> Vec<VersionedPackage> {
match self {
Self::None => vec![],
Self::Cargo(cargo) => cargo.packages.clone(),
}
}

pub fn update_version(&self, version: &str) -> Result<()> {
match self {
Self::None => Ok(()),
Self::Cargo(cargo) => cargo.update_version(version),
}
}
}

impl ReleaseConfig {
pub fn new(cwd: &Path) -> Result<Self> {
let s =
fs::read_to_string(cwd.join(RELEASE_CONFIG)).context("failed to read release.toml")?;
let mut config: Self = toml::from_str(&s).context("failed to parse release.toml")?;
for release_set in &mut config.release_sets {
for versioned_file in &mut release_set.versioned_files {
versioned_file.content = VersionedContent::read(&cwd.join(&versioned_file.path))?;
}
}
Ok(config)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cargo_command;
pub mod config;
mod publish;
mod update;
mod versioning;
Expand Down
7 changes: 3 additions & 4 deletions src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ pub struct Publish {
}

impl Publish {
/// # Errors
pub fn new(options: &Options) -> Result<Self> {
let metadata = MetadataCommand::new().current_dir(&options.path).no_deps().exec()?;
let cargo = CargoCommand::new(metadata.workspace_root.clone().into_std_path_buf());
let client = SyncClient::new("boshen (boshenc@gmail.com)", Duration::from_millis(1000))
.context("failed to get client")?;
let client =
SyncClient::new("Boshen@users.noreply.github.com", Duration::from_millis(1000))
.context("failed to get client")?;
Ok(Self { metadata, cargo, client })
}

/// # Errors
pub fn run(self) -> Result<()> {
let packages = self.get_packages();

Expand Down
Loading

0 comments on commit 223248e

Please sign in to comment.