feat(json-edit): added json-edit to update some json content with next global version
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
57
crates/cuddle-please-release-strategy/src/json_edit.rs
Normal file
57
crates/cuddle-please-release-strategy/src/json_edit.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct JsonEditOptions {
|
||||
pub jq: String,
|
||||
}
|
||||
|
||||
impl JsonEditOptions {
|
||||
pub fn execute(&self, path: &Path, next_version: impl AsRef<str>) -> anyhow::Result<()> {
|
||||
let next_version = next_version.as_ref();
|
||||
|
||||
let jq_query = self.jq.replace("%%version%%", next_version);
|
||||
|
||||
if !path.exists() {
|
||||
anyhow::bail!("could not find file at: {}", path.display());
|
||||
}
|
||||
|
||||
if let Ok(metadata) = path.metadata() {
|
||||
if !metadata.is_file() {
|
||||
anyhow::bail!("{} is not a file", path.display());
|
||||
}
|
||||
}
|
||||
|
||||
let abs_path = path.canonicalize().context(anyhow::anyhow!(
|
||||
"could not get absolute path from {}",
|
||||
path.display()
|
||||
))?;
|
||||
|
||||
let output = std::process::Command::new("jq")
|
||||
.arg(format!("{}", jq_query))
|
||||
.arg(
|
||||
abs_path
|
||||
.to_str()
|
||||
.ok_or(anyhow::anyhow!("path contains non utf-8 chars"))?,
|
||||
)
|
||||
.output()
|
||||
.context(anyhow::anyhow!("failed to run jq on file"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let err_content = std::str::from_utf8(output.stderr.as_slice())?;
|
||||
anyhow::bail!("failed to run jq with output: {}", err_content);
|
||||
}
|
||||
|
||||
let edited_json_content = std::str::from_utf8(output.stdout.as_slice())?;
|
||||
tracing::trace!(
|
||||
new_content = edited_json_content,
|
||||
file = &abs_path.display().to_string(),
|
||||
"applied jq to file"
|
||||
);
|
||||
std::fs::write(abs_path, edited_json_content)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
6
crates/cuddle-please-release-strategy/src/lib.rs
Normal file
6
crates/cuddle-please-release-strategy/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#[cfg(feature = "json-edit")]
|
||||
mod json_edit;
|
||||
mod strategy;
|
||||
|
||||
#[cfg(feature = "json-edit")]
|
||||
pub use json_edit::JsonEditOptions;
|
59
crates/cuddle-please-release-strategy/src/strategy.rs
Normal file
59
crates/cuddle-please-release-strategy/src/strategy.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct UpdateOptions {
|
||||
next_version: String,
|
||||
global_changelog: String,
|
||||
}
|
||||
|
||||
pub type Projects = Vec<Project>;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Project {
|
||||
path: Option<PathBuf>,
|
||||
r#type: ProjectType,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum ProjectType {
|
||||
#[cfg(feature = "rust-workspace")]
|
||||
#[serde(alias = "rust_workspace")]
|
||||
RustWorkspace,
|
||||
#[cfg(feature = "rust-crate")]
|
||||
#[serde(alias = "json_edit")]
|
||||
RustCrate,
|
||||
#[cfg(feature = "toml-edit")]
|
||||
#[serde(alias = "toml_edit")]
|
||||
TomlEdit,
|
||||
#[cfg(feature = "yaml-edit")]
|
||||
#[serde(alias = "yaml_edit")]
|
||||
YamlEdit,
|
||||
#[cfg(feature = "json-edit")]
|
||||
#[serde(alias = "json_edit")]
|
||||
JsonEdit,
|
||||
}
|
||||
|
||||
impl Project {
|
||||
pub fn new(path: Option<PathBuf>, r#type: ProjectType) -> Self {
|
||||
Self { path, r#type }
|
||||
}
|
||||
|
||||
pub fn execute(&self, options: &UpdateOptions) -> anyhow::Result<()> {
|
||||
match self.r#type {
|
||||
#[cfg(feature = "rust-workspace")]
|
||||
ProjectType::RustWorkspace => todo!(),
|
||||
#[cfg(feature = "rust-crate")]
|
||||
ProjectType::RustCrate => todo!(),
|
||||
#[cfg(feature = "toml-edit")]
|
||||
ProjectType::TomlEdit => todo!(),
|
||||
#[cfg(feature = "yaml-edit")]
|
||||
ProjectType::YamlEdit => todo!(),
|
||||
#[cfg(feature = "json-edit")]
|
||||
ProjectType::JsonEdit => todo!(),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user