feat: add CLI compile command and bench script (#1193)

This commit is contained in:
Myriad-Dreamin 2025-01-19 18:48:19 +08:00 committed by GitHub
parent 1f01ec1f6c
commit 3577ed3b2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 3 deletions

View file

@ -32,6 +32,9 @@ pub enum Commands {
#[cfg(feature = "preview")]
Preview(tinymist::tool::preview::PreviewCliArgs),
/// Runs compile commands
#[clap(hide(true))] // still in development
Compile(CompileArgs),
/// Runs language query
#[clap(hide(true))] // still in development
#[clap(subcommand)]
@ -143,6 +146,17 @@ pub struct TraceLspArgs {
pub compile: CompileOnceArgs,
}
/// Common arguments of compile, watch, and query.
#[derive(Debug, Clone, Default, clap::Parser)]
pub struct CompileArgs {
#[clap(flatten)]
pub compile: CompileOnceArgs,
/// Path to output file
#[clap(value_name = "OUTPUT")]
pub output: Option<String>,
}
#[derive(Debug, Clone, Default, clap::Parser)]
pub struct LspArgs {
#[clap(flatten)]

View file

@ -9,7 +9,7 @@ use std::{
sync::Arc,
};
use anyhow::bail;
use anyhow::{bail, Context};
use clap::Parser;
use clap_builder::CommandFactory;
use clap_complete::generate;
@ -17,18 +17,18 @@ use futures::future::MaybeDone;
use lsp_server::RequestId;
use once_cell::sync::Lazy;
use reflexo::ImmutPath;
use reflexo_typst::{package::PackageSpec, TypstDict};
use reflexo_typst::{package::PackageSpec, Compiler, TypstDict};
use serde_json::Value as JsonValue;
use sync_lsp::{
internal_error,
transport::{with_stdio_transport, MirrorArgs},
LspBuilder, LspClientRoot, LspResult,
};
use tinymist::world::TaskInputs;
use tinymist::{
tool::project::{project_main, task_main},
CompileConfig, Config, LanguageState, RegularInit, SuperInit, UserActionTask,
};
use tinymist::{world::TaskInputs, WorldProvider};
use tinymist_core::LONG_VERSION;
use tinymist_query::{package::PackageInfo, EntryResolver};
use typst::foundations::IntoValue;
@ -84,6 +84,7 @@ fn main() -> anyhow::Result<()> {
match args.command.unwrap_or_default() {
Commands::Completion(args) => completion(args),
Commands::Compile(args) => compile(args),
Commands::Query(query_cmds) => query_main(query_cmds),
Commands::Lsp(args) => lsp_main(args),
Commands::TraceLsp(args) => trace_lsp_main(args),
@ -112,6 +113,41 @@ pub fn completion(args: ShellCompletionArgs) -> anyhow::Result<()> {
Ok(())
}
/// Runs compilation
pub fn compile(args: CompileArgs) -> anyhow::Result<()> {
use std::io::Write;
let input = args
.compile
.input
.as_ref()
.context("Missing required argument: INPUT")?;
let output = match args.output {
Some(stdout_path) if stdout_path == "-" => None,
Some(output_path) => Some(PathBuf::from(output_path)),
None => Some(Path::new(input).with_extension("pdf")),
};
let universe = args.compile.resolve()?;
let world = universe.snapshot();
let converter = std::marker::PhantomData.compile(&world, &mut Default::default());
let pdf = typst_pdf::pdf(&converter.unwrap().output, &Default::default());
match (pdf, output) {
(Ok(pdf), None) => {
std::io::stdout().write_all(&pdf).unwrap();
}
(Ok(pdf), Some(output)) => std::fs::write(output, pdf).unwrap(),
(Err(err), ..) => {
eprintln!("{err:?}");
std::process::exit(1);
}
}
Ok(())
}
/// The main entry point for the language server.
pub fn lsp_main(args: LspArgs) -> anyhow::Result<()> {
let pairs = LONG_VERSION.trim().split('\n');