Skip to content

Commit

Permalink
build: improve crates metadata for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jun 17, 2024
1 parent c5e9f9b commit 033c935
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion Cargo.lock

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

8 changes: 4 additions & 4 deletions crates/mitex-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/mitex-lexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository.workspace = true

[dependencies]

mitex-spec = { path = "../mitex-spec" }
mitex-spec.workspace = true

logos.workspace = true
ena.workspace = true
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions crates/mitex-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/mitex-spec-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 49 additions & 30 deletions crates/mitex-spec-gen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,25 +26,36 @@ fn main() {

fn get_project_root() -> anyhow::Result<PathBuf> {
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(|| {
Expand All @@ -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(),
"<mitex-packages>",
])
.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(),
"<mitex-packages>",
]);

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
struct QueryItem<T> {
Expand All @@ -88,8 +100,15 @@ fn generate() -> anyhow::Result<()> {
type Json<T> = Vec<QueryItem<T>>;

let mut json_spec: mitex_spec::JsonCommandSpec = Default::default();
let json_packages: Json<mitex_spec::query::PackagesVec> =
serde_json::from_slice(&package_specs.stdout).expect("failed to parse package specs");
let json_packages: Json<mitex_spec::query::PackagesVec> = 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");
}
Expand Down
7 changes: 4 additions & 3 deletions crates/mitex-spec-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandSpec> = 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"
)))
});
4 changes: 2 additions & 2 deletions crates/mitex-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions crates/mitex-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions crates/mitex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 033c935

Please sign in to comment.