From 033c93590fb859972bc1d6c6e11f860aafa11c19 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Fri, 19 Apr 2024 13:16:19 +0800 Subject: [PATCH] build: improve crates metadata for publish --- .gitmodules | 6 +- Cargo.lock | 1 - crates/mitex-cli/Cargo.toml | 8 +- crates/mitex-lexer/Cargo.toml | 4 +- crates/mitex-parser/Cargo.toml | 8 +- crates/mitex-spec-gen/Cargo.toml | 4 +- .../mitex-spec-gen/assets}/artifacts | 0 crates/mitex-spec-gen/build.rs | 79 ++++++++++++------- crates/mitex-spec-gen/src/lib.rs | 7 +- crates/mitex-spec/Cargo.toml | 4 +- crates/mitex-wasm/Cargo.toml | 4 +- crates/mitex/Cargo.toml | 5 +- 12 files changed, 74 insertions(+), 56 deletions(-) rename {assets => crates/mitex-spec-gen/assets}/artifacts (100%) diff --git a/.gitmodules b/.gitmodules index 477b242..662b7dc 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ -[submodule "assets/artifacts"] - path = assets/artifacts +[submodule "crates/mitex-spec-gen/assets/artifacts"] + path = crates/mitex-spec-gen/assets/artifacts url = https://github.com/mitex-rs/artifacts - branch = v0.1.1 + branch = v0.2.2 diff --git a/Cargo.lock b/Cargo.lock index 0b29ec0..96e71f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -497,7 +497,6 @@ dependencies = [ name = "mitex" version = "0.2.4" dependencies = [ - "bitflags", "divan", "insta", "mitex-parser", diff --git a/crates/mitex-cli/Cargo.toml b/crates/mitex-cli/Cargo.toml index 27f25b7..1b06c5f 100644 --- a/crates/mitex-cli/Cargo.toml +++ b/crates/mitex-cli/Cargo.toml @@ -20,10 +20,10 @@ doc = false [dependencies] -mitex-spec-gen = { path = "../mitex-spec-gen" } -mitex-spec = { path = "../mitex-spec" } -mitex-parser = { path = "../mitex-parser" } -mitex = { path = "../mitex" } +mitex-spec-gen.workspace = true +mitex-spec.workspace = true +mitex-parser.workspace = true +mitex.workspace = true clap.workspace = true clap_builder.workspace = true diff --git a/crates/mitex-lexer/Cargo.toml b/crates/mitex-lexer/Cargo.toml index ee8bba6..c77e65f 100644 --- a/crates/mitex-lexer/Cargo.toml +++ b/crates/mitex-lexer/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true [dependencies] -mitex-spec = { path = "../mitex-spec" } +mitex-spec.workspace = true logos.workspace = true ena.workspace = true @@ -19,7 +19,7 @@ fxhash.workspace = true once_cell.workspace = true [dev-dependencies] -mitex-spec-gen = { path = "../mitex-spec-gen" } +mitex-spec-gen.workspace = true insta.workspace = true divan.workspace = true diff --git a/crates/mitex-parser/Cargo.toml b/crates/mitex-parser/Cargo.toml index 9b33117..d3272ac 100644 --- a/crates/mitex-parser/Cargo.toml +++ b/crates/mitex-parser/Cargo.toml @@ -16,15 +16,15 @@ harness = false [dependencies] -mitex-glob = { path = "../mitex-glob" } -mitex-lexer = { path = "../mitex-lexer" } -mitex-spec = { path = "../mitex-spec" } +mitex-glob.workspace = true +mitex-lexer.workspace = true +mitex-spec.workspace = true rowan.workspace = true [dev-dependencies] -mitex-spec-gen = { path = "../mitex-spec-gen" } +mitex-spec-gen.workspace = true once_cell.workspace = true insta.workspace = true diff --git a/crates/mitex-spec-gen/Cargo.toml b/crates/mitex-spec-gen/Cargo.toml index 4dc18f2..324babd 100644 --- a/crates/mitex-spec-gen/Cargo.toml +++ b/crates/mitex-spec-gen/Cargo.toml @@ -9,12 +9,12 @@ homepage.workspace = true repository.workspace = true [dependencies] -mitex-spec = { path = "../mitex-spec" } +mitex-spec.workspace = true once_cell.workspace = true [build-dependencies] -mitex-spec = { path = "../mitex-spec" } +mitex-spec.workspace = true serde = { workspace = true, features = ["derive"] } serde_json.workspace = true anyhow.workspace = true diff --git a/assets/artifacts b/crates/mitex-spec-gen/assets/artifacts similarity index 100% rename from assets/artifacts rename to crates/mitex-spec-gen/assets/artifacts diff --git a/crates/mitex-spec-gen/build.rs b/crates/mitex-spec-gen/build.rs index ff49240..7b4934d 100644 --- a/crates/mitex-spec-gen/build.rs +++ b/crates/mitex-spec-gen/build.rs @@ -7,9 +7,12 @@ use anyhow::Context; use serde::{Deserialize, Serialize}; fn main() { + let project_root = get_project_root(); + let spec_builder = if cfg!(feature = "prebuilt") { copy_prebuilt - } else if cfg!(feature = "generate") || which::which("typst").is_ok() { + } else if cfg!(feature = "generate") || (which::which("typst").is_ok() && project_root.is_ok()) + { generate } else { // fallback to prebuilt spec @@ -23,25 +26,36 @@ fn main() { fn get_project_root() -> anyhow::Result { let project_root = - std::env::var("CARGO_MANIFEST_DIR").with_context(|| "failed to get project root")?; - Ok(std::path::Path::new(&project_root) - .parent() - .with_context(|| "failed to get project root")? - .parent() - .with_context(|| "failed to get project root")? - .to_owned()) + std::env::var("CARGO_MANIFEST_DIR").with_context(|| "failed to get manifest dir")?; + let mut project_root = std::path::Path::new(&project_root); + Ok(loop { + let parent = project_root + .parent() + .with_context(|| "failed to get project root")?; + if parent.join("Cargo.toml").exists() { + break parent.to_owned(); + } + project_root = parent; + }) } fn copy_prebuilt() -> anyhow::Result<()> { // println!("cargo:warning=copy_prebuilt"); - let project_root = get_project_root()?; + let manifest_dir = + std::env::var("CARGO_MANIFEST_DIR").with_context(|| "failed to get manifest dir")?; + let manifest_dir = std::path::Path::new(&manifest_dir); + let target_spec = + Path::new(&std::env::var("OUT_DIR").unwrap()).join("mitex-artifacts/spec/default.rkyv"); // assets/artifacts/spec/default.rkyv - std::fs::create_dir_all(project_root.join(Path::new("target/mitex-artifacts/spec"))) - .with_context(|| "failed to create target_dir for store spec")?; + std::fs::create_dir_all( + target_spec + .parent() + .context("failed to get dirname of target spec")?, + ) + .with_context(|| "failed to create target_dir for store spec")?; - let prebuilt_spec = project_root.join(Path::new("assets/artifacts/spec/default.rkyv")); - let target_spec = project_root.join(Path::new("target/mitex-artifacts/spec/default.rkyv")); + let prebuilt_spec = manifest_dir.join(Path::new("assets/artifacts/spec/default.rkyv")); println!("cargo:warning=Use prebuilt spec binaries at {prebuilt_spec:?}"); std::fs::copy(prebuilt_spec, target_spec).with_context(|| { @@ -64,21 +78,19 @@ fn generate() -> anyhow::Result<()> { spec_root = spec_root.display() ); - let target_dir = project_root.join("target/mitex-artifacts"); - - let package_specs = std::process::Command::new("typst") - .args([ - "query", - "--root", - project_root.to_str().unwrap(), - project_root - .join("packages/mitex/specs/mod.typ") - .to_str() - .unwrap(), - "", - ]) - .output() - .with_context(|| "failed to query metadata")?; + let target_dir = Path::new(&std::env::var("OUT_DIR").unwrap()).join("mitex-artifacts"); + + let mut package_specs = std::process::Command::new("typst"); + let package_specs = package_specs.args([ + "query", + "--root", + project_root.to_str().unwrap(), + project_root + .join("packages/mitex/specs/mod.typ") + .to_str() + .unwrap(), + "", + ]); #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] struct QueryItem { @@ -88,8 +100,15 @@ fn generate() -> anyhow::Result<()> { type Json = Vec>; let mut json_spec: mitex_spec::JsonCommandSpec = Default::default(); - let json_packages: Json = - serde_json::from_slice(&package_specs.stdout).expect("failed to parse package specs"); + let json_packages: Json = serde_json::from_slice( + &package_specs + .output() + .with_context(|| "failed to query metadata")? + .stdout, + ) + .context(format!( + "failed to parse package specs cmd: {package_specs:?}" + ))?; if json_packages.is_empty() { panic!("no package found"); } diff --git a/crates/mitex-spec-gen/src/lib.rs b/crates/mitex-spec-gen/src/lib.rs index fddc297..d5c1be5 100644 --- a/crates/mitex-spec-gen/src/lib.rs +++ b/crates/mitex-spec-gen/src/lib.rs @@ -9,7 +9,8 @@ use mitex_spec::CommandSpec; /// /// [repro-default]: https://github.com/mitex-rs/artifacts/blob/main/README.md#default-command-specification-since-v011 pub static DEFAULT_SPEC: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { - CommandSpec::from_bytes(include_bytes!( - "../../../target/mitex-artifacts/spec/default.rkyv" - )) + CommandSpec::from_bytes(include_bytes!(concat!( + env!("OUT_DIR"), + "/mitex-artifacts/spec/default.rkyv" + ))) }); diff --git a/crates/mitex-spec/Cargo.toml b/crates/mitex-spec/Cargo.toml index 99e4ec7..6825457 100644 --- a/crates/mitex-spec/Cargo.toml +++ b/crates/mitex-spec/Cargo.toml @@ -25,8 +25,8 @@ rkyv-validation = ["dep:rkyv", "rkyv/validation"] default = ["serde", "rkyv", "rkyv-validation"] [dev-dependencies] -once_cell = "1" -divan = "0.1.7" +once_cell.workspace = true +divan.workspace = true [lints] workspace = true diff --git a/crates/mitex-wasm/Cargo.toml b/crates/mitex-wasm/Cargo.toml index 54fe1d6..0218209 100644 --- a/crates/mitex-wasm/Cargo.toml +++ b/crates/mitex-wasm/Cargo.toml @@ -13,8 +13,8 @@ crate-type = ["cdylib"] [dependencies] -mitex = { path = "../mitex" } -mitex-spec = { path = "../mitex-spec" } +mitex.workspace = true +mitex-spec.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/crates/mitex/Cargo.toml b/crates/mitex/Cargo.toml index 55edb4c..62e984b 100644 --- a/crates/mitex/Cargo.toml +++ b/crates/mitex/Cargo.toml @@ -14,10 +14,9 @@ harness = false [dependencies] -mitex-parser = { path = "../mitex-parser" } -mitex-spec-gen = { path = "../mitex-spec-gen" } +mitex-parser.workspace = true +mitex-spec-gen.workspace = true rowan.workspace = true -bitflags = "2.4.1" [dev-dependencies] insta.workspace = true