markupwright 0.1.0

Template-driven workspace documentation generator
Documentation
use std::{fs, path::Path, process::Command};

use googletest::prelude::*;
use tempfile::TempDir;

pub(super) fn fixture() -> Result<TempDir> {
    let directory = tempfile::tempdir().or_fail()?;

    write_config(
        directory.path(),
        r#"version = 1

[workspace]
name = "fixture"
description = "Fixture workspace"

[render]
templates = ".markupwright/templates"
header = "header.md.j2"
navigation = "README.md"
source-suffix = ".in"

[domains.go]
manifests = ["**/go.mod"]
source-documentation = true

[domains.lua]
manifests = ["**/*.toc"]
source-documentation = true

[domains.node]
manifests = ["package.json", "**/package.json"]
source-documentation = true

[domains.php]
manifests = ["**/composer.json"]
source-documentation = true

[domains.python]
manifests = ["**/pyproject.toml"]
source-documentation = true

[[documents]]
include = ["README.md.in"]
template = "readme-root.md.j2"

[[documents]]
include = ["**/README.md.in"]
domain = "go"
template = "readme-language.md.j2"

[[documents]]
include = ["**/README.md.in"]
domain = "lua"
template = "readme-language.md.j2"

[[documents]]
include = ["**/README.md.in"]
domain = "node"
template = "readme-node.md.j2"

[[documents]]
include = ["**/README.md.in"]
domain = "php"
template = "readme-language.md.j2"

[[documents]]
include = ["**/README.md.in"]
domain = "python"
template = "readme-language.md.j2"

[[documents]]
include = ["**/*.md.in"]
template = "document.md.j2"
"#,
    )?;
    fs::write(
        directory.path().join("README.md.in"),
        "This content came from the input template.\n",
    )
    .or_fail()?;

    Ok(directory)
}

pub(super) fn write_config(root: &Path, contents: &str) -> Result<()> {
    let directory = root.join(".markupwright");

    fs::create_dir_all(&directory).or_fail()?;
    fs::write(directory.join("config.toml"), contents).or_fail()
}

pub(super) fn run(root: &Path, args: &[&str]) -> Result<std::process::Output> {
    Command::new(env!("CARGO_BIN_EXE_markupwright"))
        .args(args)
        .env("MARKUPWRIGHT_ROOT", root)
        .output()
        .or_fail()
}

pub(super) fn stderr(output: &std::process::Output) -> Result<String> {
    String::from_utf8(output.stderr.clone()).or_fail()
}