From 9eb157763516ab83077de12ae0d4569e579fe854 Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Fri, 27 Dec 2024 22:10:55 +0900 Subject: [PATCH] feat: add xtask. --- .cargo/config.toml | 2 ++ Cargo.toml | 6 +++--- scripts/publish.sh | 10 ++++++---- xtask/Cargo.toml | 12 ++++++++++++ xtask/src/commands.rs | 1 + xtask/src/commands/update_tags.rs | 22 ++++++++++++++++++++++ xtask/src/main.rs | 18 ++++++++++++++++++ xtask/src/utils.rs | 8 ++++++++ 8 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/commands.rs create mode 100644 xtask/src/commands/update_tags.rs create mode 100644 xtask/src/main.rs create mode 100644 xtask/src/utils.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..8628170 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --bin xtask --" diff --git a/Cargo.toml b/Cargo.toml index 8a6eb5c..1f41945 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["crates/*"] +members = ["crates/*", "xtask"] [workspace.package] edition = "2021" @@ -16,5 +16,5 @@ paste = "^1.0" regex = "^1.6" serde = "^1.0" serde_json = "^1.0" -serde_valid_derive = { version = "1.0.1", path = "crates/serde_valid_derive" } -serde_valid_literal = { version = "1.0.1", path = "crates/serde_valid_literal" } +serde_valid_derive = { path = "crates/serde_valid_derive" } +serde_valid_literal = { path = "crates/serde_valid_literal" } diff --git a/scripts/publish.sh b/scripts/publish.sh index a9e00ee..f0c85d8 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -2,16 +2,18 @@ set -e -cd "$(dirname "$0")" +cd "$(dirname "$0")"/../ -cd ../crates/serde_valid_derive +cargo xtask upgate-tags + +cd "$(dirname "$0")"/../crates/serde_valid_derive cargo publish -cd ../crates/serde_valid_literal +cd "$(dirname "$0")"/../crates/serde_valid_literal cargo publish # wait tarball package publishment sleep 20 -cd ../crates/serde_valid +cd "$(dirname "$0")"/../crates/serde_valid cargo publish diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..c27aae3 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "xtask" +edition.workspace = true +version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true +keywords.workspace = true + +[dependencies] +clap = { version = "4.5.23", features = ["derive"] } +toml_edit = "0.22.22" diff --git a/xtask/src/commands.rs b/xtask/src/commands.rs new file mode 100644 index 0000000..e0cd000 --- /dev/null +++ b/xtask/src/commands.rs @@ -0,0 +1 @@ +pub mod update_tags; diff --git a/xtask/src/commands/update_tags.rs b/xtask/src/commands/update_tags.rs new file mode 100644 index 0000000..76ca6f0 --- /dev/null +++ b/xtask/src/commands/update_tags.rs @@ -0,0 +1,22 @@ +use toml_edit::DocumentMut; + +use crate::utils; + +#[derive(clap::Args, Debug)] +pub struct Args {} + +pub fn run(_args: Args) { + let project_root = utils::project_root(); + + let cargo_toml = std::fs::read_to_string(project_root.join("Cargo.toml")).unwrap(); + let mut doc = cargo_toml.parse::().unwrap(); + + let version = doc["workspace"]["package"]["version"] + .clone() + .into_value() + .unwrap(); + doc["workspace"]["dependencies"]["serde_valid_derive"]["version"] = version.clone().into(); + doc["workspace"]["dependencies"]["serde_valid_literal"]["version"] = version.into(); + + std::fs::write(project_root.join("Cargo.toml"), doc.to_string()).unwrap(); +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..a1f5e69 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,18 @@ +mod commands; +mod utils; + +use clap::Parser; + +#[derive(Debug, clap::Parser)] +enum Args { + UpdateTags(commands::update_tags::Args), +} + +fn main() { + let args = Args::parse(); + match args { + Args::UpdateTags(args) => { + commands::update_tags::run(args); + } + } +} diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs new file mode 100644 index 0000000..2409768 --- /dev/null +++ b/xtask/src/utils.rs @@ -0,0 +1,8 @@ +use std::path::PathBuf; + +/// Returns the path to the root directory of `tombi` project. +pub fn project_root() -> PathBuf { + let dir = std::env::var("CARGO_MANIFEST_DIR") + .unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()); + PathBuf::from(dir).parent().unwrap().to_owned() +}