Skip to content

Commit

Permalink
deps(cli): Drop typst-project in favor typst-syntax
Browse files Browse the repository at this point in the history
This drops the `typst-project` dependency in favor of using the PackageManifest
from `typst-syntax` and by downstreaming project root finding.
  • Loading branch information
tingerrr committed Nov 4, 2024
1 parent 71ef6e7 commit c2aa0af
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
16 changes: 1 addition & 15 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ keywords = ["typst", "test"]
readme = "README.md"

[workspace.dependencies]
typst-project = { git = "https://github.com/tingerrr/typst-project", rev = "a71db54" }

typst-test-lib = { path = "crates/typst-test-lib" }
typst-test-stdx = { path = "crates/typst-test-stdx" }

Expand Down Expand Up @@ -63,5 +61,6 @@ typst-assets = "0.12.0"
typst-dev-assets = { git = "https://github.com/typst/typst-dev-assets", rev = "ee8ae61" }
typst-kit = "0.12.0"
typst-render = "0.12.0"
typst-syntax = "0.12.0"
unscanny = "0.1.0"
ureq = { version = "2.9.7", default-features = false }
2 changes: 1 addition & 1 deletion crates/typst-test-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ tracing.workspace = true
tracing-subscriber.workspace = true
tracing-tree.workspace = true
typst.workspace = true
typst-project.workspace = true
typst-kit.workspace = true
typst-syntax.workspace = true
ureq = { workspace = true, default-features = false, features = ["native-tls", "gzip", "json"] }

[features]
Expand Down
5 changes: 3 additions & 2 deletions crates/typst-test-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::{mpsc, Arc};

use chrono::{DateTime, Utc};
use clap::ColorChoice;
use ecow::EcoString;
use termcolor::Color;
use thiserror::Error;
use typst_test_lib::store::vcs::{Git, Vcs};
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Failure for NoProject {
}

#[derive(Debug, Error)]
pub struct ProjectNotInitialized(pub Option<String>);
pub struct ProjectNotInitialized(pub Option<EcoString>);

impl Display for ProjectNotInitialized {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -187,7 +188,7 @@ impl<'a> Context<'a> {
Some(root) => root.to_path_buf(),
None => {
let pwd = std::env::current_dir()?;
match typst_project::try_find_project_root(&pwd)? {
match project::try_find_project_root(&pwd)? {
Some(root) => {
if !root.try_exists()? {
anyhow::bail!(OperationFailure::from(RootNotFound(root.to_path_buf())));
Expand Down
3 changes: 2 additions & 1 deletion crates/typst-test-cli/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Display;
use std::io;
use std::io::Write;

use ecow::EcoString;
use serde::Serialize;
use termcolor::{Color, WriteColor};
use thiserror::Error;
Expand Down Expand Up @@ -35,7 +36,7 @@ pub enum Vcs {
}

#[derive(Debug, Error)]
pub struct ProjectAlreadyIntialized(Option<String>);
pub struct ProjectAlreadyIntialized(Option<EcoString>);

impl Display for ProjectAlreadyIntialized {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
46 changes: 31 additions & 15 deletions crates/typst-test-cli/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use std::str::FromStr;
use std::{fs, io};

use rayon::prelude::*;
use serde::Deserialize;
use tiny_skia::Pixmap;
use toml_edit::DocumentMut;
use tracing::Level;
use typst_project::manifest::Manifest;
use typst_syntax::package::PackageManifest;
use typst_test_lib::config::Config;
use typst_test_lib::store::project::v1::ResolverV1;
use typst_test_lib::store::project::Resolver;
Expand All @@ -23,15 +24,34 @@ use typst_test_stdx::result::ResultEx;

use crate::cli;

const MANIFEST_FILE: &str = "typst.toml";
const MANIFEST_CONFIG_KEY: &str = "typst-test";

const DEFAULT_TEST_INPUT: &str = include_str!("../../../assets/default-test/test.typ");
const DEFAULT_TEST_OUTPUT: &[u8] = include_bytes!("../../../assets/default-test/test.png");

pub fn try_open_manifest(root: &Path) -> anyhow::Result<Option<Manifest>> {
pub fn is_project_root(root: &Path) -> anyhow::Result<bool> {
let manifest_path = root.join(MANIFEST_FILE);
Ok(manifest_path.try_exists()?)
}

pub fn try_find_project_root(root: &Path) -> anyhow::Result<Option<&Path>> {
for ancestor in root.ancestors() {
if is_project_root(ancestor)? {
return Ok(Some(ancestor));
}
}

Ok(None)
}

pub fn try_open_manifest(root: &Path) -> anyhow::Result<Option<PackageManifest>> {
tracing::debug!(?root, "reading manifest");

if typst_project::is_project_root(root)? {
let content = std::fs::read_to_string(root.join(typst_project::heuristics::MANIFEST_FILE))?;
let manifest = Manifest::from_str(&content)?;
if is_project_root(root)? {
let content = std::fs::read_to_string(root.join(MANIFEST_FILE))?;
let manifest = toml::from_str(&content)?;

Ok(Some(manifest))
} else {
Ok(None)
Expand All @@ -41,7 +61,7 @@ pub fn try_open_manifest(root: &Path) -> anyhow::Result<Option<Manifest>> {
#[derive(Debug)]
pub struct Project {
config: Config,
manifest: Option<Manifest>,
manifest: Option<PackageManifest>,
resolver: ResolverV1,
vcs: Option<Box<dyn Vcs + Sync>>,
tests: BTreeMap<Identifier, Test>,
Expand All @@ -53,17 +73,13 @@ impl Project {
pub fn new(
root: PathBuf,
vcs: Option<Box<dyn Vcs + Sync>>,
manifest: Option<Manifest>,
manifest: Option<PackageManifest>,
) -> anyhow::Result<Self> {
let config = manifest
.as_ref()
.and_then(|m| {
m.tool
.as_ref()
.map(|t| t.get_section::<Config>("typst-test"))
})
.and_then(|m| m.tool.sections.get(MANIFEST_CONFIG_KEY))
.map(|c| Config::deserialize(c.clone()))
.transpose()?
.flatten()
.inspect(|config| {
tracing::trace!(?config, "read manifest config");
})
Expand Down Expand Up @@ -108,7 +124,7 @@ impl Project {
&mut self.config
}

pub fn manifest(&self) -> Option<&Manifest> {
pub fn manifest(&self) -> Option<&PackageManifest> {
self.manifest.as_ref()
}

Expand Down Expand Up @@ -312,7 +328,7 @@ impl Project {

#[tracing::instrument(level = Level::DEBUG, skip(self), fields(config = ?self.config))]
pub fn write_config(&self) -> anyhow::Result<()> {
let path = self.root().join(typst_project::heuristics::MANIFEST_FILE);
let path = self.root().join(MANIFEST_FILE);

let content = std::fs::read_to_string(&path)
.ignore_default(|err| err.kind() == io::ErrorKind::NotFound)?;
Expand Down

0 comments on commit c2aa0af

Please sign in to comment.