From d3a333b8733a6283b686dcd20bd4fb02a73a13f1 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 21 Jun 2023 19:06:34 +0200 Subject: [PATCH] Tidy up xtask --- Cargo.lock | 17 ++++++++++++++++- crates/xtask/Cargo.toml | 3 ++- crates/xtask/src/cli.rs | 23 +++++++++++++++++++++++ crates/xtask/src/main.rs | 28 +++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4df64bfac7..3f6ebf306f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,7 +1150,7 @@ checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", "bitflags", - "clap_derive", + "clap_derive 3.2.25", "clap_lex 0.2.4", "indexmap", "once_cell", @@ -1166,6 +1166,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2686c4115cb0810d9a984776e197823d08ec94f176549a89a9efded477c456dc" dependencies = [ "clap_builder", + "clap_derive 4.3.2", + "once_cell", ] [[package]] @@ -1194,6 +1196,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "clap_derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn 2.0.18", +] + [[package]] name = "clap_lex" version = "0.2.4" @@ -8843,6 +8857,7 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" name = "xtask" version = "0.1.0" dependencies = [ + "anyhow", "clap 4.3.5", "schemars", "serde_json", diff --git a/crates/xtask/Cargo.toml b/crates/xtask/Cargo.toml index 2f4ba75ca7..3d3f7d4357 100644 --- a/crates/xtask/Cargo.toml +++ b/crates/xtask/Cargo.toml @@ -6,7 +6,8 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "4.0" +anyhow = "1.0" +clap = {version = "4.0", features = ["derive"]} theme = {path = "../theme"} serde_json.workspace = true schemars.workspace = true diff --git a/crates/xtask/src/cli.rs b/crates/xtask/src/cli.rs index e69de29bb2..bffda1bc16 100644 --- a/crates/xtask/src/cli.rs +++ b/crates/xtask/src/cli.rs @@ -0,0 +1,23 @@ +use clap::{Parser, Subcommand}; +use std::path::PathBuf; +/// Common utilities for Zed developers. +// For more information, see [matklad's repository README](https://github.com/matklad/cargo-xtask/) +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +#[command(propagate_version = true)] +pub struct Cli { + #[command(subcommand)] + pub command: Commands, +} + +/// Command to run. +#[derive(Subcommand)] +pub enum Commands { + /// Builds theme types for interop with Typescript. + BuildThemeTypes { + #[clap(short, long, default_value = "schemas")] + out_dir: PathBuf, + #[clap(short, long, default_value = "theme.json")] + file_name: PathBuf, + }, +} diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index 03e342fa4a..881a65fd38 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -1,11 +1,29 @@ mod cli; -use schemars::schema_for; +use std::path::PathBuf; +use anyhow::Result; +use clap::Parser; +use schemars::schema_for; use theme::Theme; -fn main() { + +fn build_themes(mut out_dir: PathBuf, file_name: PathBuf) -> Result<()> { let theme = schema_for!(Theme); - let output = serde_json::to_string_pretty(&theme).unwrap(); - std::fs::create_dir("schemas").ok(); - std::fs::write("schemas/theme.json", output).ok(); + let output = serde_json::to_string_pretty(&theme)?; + + std::fs::create_dir(&out_dir)?; + + let mut file_path = out_dir; + out_dir.push(file_name); + + std::fs::write(file_path, output)?; + + Ok(()) +} + +fn main() -> Result<()> { + let args = cli::Cli::parse(); + match args.command { + cli::Commands::BuildThemeTypes { out_dir, file_name } => build_themes(out_dir, file_name), + } }