Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: improve crates metadata for publish #159

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4
21 changes: 10 additions & 11 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ rowan = "0.15.15"

which = "5.0.0"

mitex-spec = { version = "0.2.4", path = "crates/mitex-spec" }
mitex-glob = { version = "0.2.4", path = "crates/mitex-glob" }
mitex-lexer = { version = "0.2.4", path = "crates/mitex-lexer" }
mitex-parser = { version = "0.2.4", path = "crates/mitex-parser" }
mitex = { version = "0.2.4", path = "crates/mitex" }
mitex-spec-gen = { version = "0.2.4", path = "crates/mitex-spec-gen" }

clap = { version = "4.4", features = ["derive", "env", "unicode", "wrap_help"] }
clap_builder = { version = "4", features = ["string"] }
clap_complete = "4.4"
Expand Down
1 change: 0 additions & 1 deletion assets/artifacts
Submodule artifacts deleted from 6dd2b1
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
1 change: 1 addition & 0 deletions crates/mitex-spec-gen/assets/artifacts
Submodule artifacts added at 9eb762
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
6 changes: 3 additions & 3 deletions crates/mitex-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ 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

wasm-bindgen = { version = "0.2.74", optional = true }
wasm-bindgen = { version = "0.2.92", optional = true }
wasm-minimal-protocol = { git = "https://github.com/astrale-sharp/wasm-minimal-protocol", optional = true }

[features]
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
8 changes: 8 additions & 0 deletions scripts/publish.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo publish -p mitex-glob
cargo publish -p mitex-spec
cargo publish -p mitex-spec-gen
cargo publish -p mitex-lexer
cargo publish -p mitex-parser
cargo publish -p mitex
# cargo publish -p mitex-wasm
cargo publish -p mitex-cli
Loading