flowey_lib_hvlite/
build_guide.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Build the OpenVMM Guide.

use flowey::node::prelude::*;

flowey_request! {
    pub struct Request {
        pub built_guide: WriteVar<PathBuf>,
    }
}

new_flow_node!(struct Node);

impl FlowNode for Node {
    type Request = Request;

    fn imports(ctx: &mut ImportCtx<'_>) {
        ctx.import::<crate::git_checkout_openvmm_repo::Node>();
        ctx.import::<flowey_lib_common::download_mdbook_admonish::Node>();
        ctx.import::<flowey_lib_common::download_mdbook_mermaid::Node>();
        ctx.import::<flowey_lib_common::download_mdbook::Node>();
        ctx.import::<flowey_lib_common::install_rust::Node>();
    }

    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
        let mdbook_bin = ctx.reqv(flowey_lib_common::download_mdbook::Request::GetMdbook);
        let mdbook_admonish_bin =
            ctx.reqv(flowey_lib_common::download_mdbook_admonish::Request::GetMdbookAdmonish);
        let mdbook_mermaid_bin =
            ctx.reqv(flowey_lib_common::download_mdbook_mermaid::Request::GetMdbookMermaid);

        let guide_source = ctx
            .reqv(crate::git_checkout_openvmm_repo::req::GetRepoDir)
            .map(ctx, |p| p.join("Guide"));

        let rust_is_installed = ctx.reqv(flowey_lib_common::install_rust::Request::EnsureInstalled);

        for Request { built_guide } in requests {
            ctx.emit_rust_step("build OpenVMM guide (mdbook)", |ctx| {
                // rust must be installed to build the `mdbook-openvmm-shim`
                rust_is_installed.clone().claim(ctx);
                let mdbook_bin = mdbook_bin.clone().claim(ctx);
                let mdbook_admonish_bin = mdbook_admonish_bin.clone().claim(ctx);
                let mdbook_mermaid_bin = mdbook_mermaid_bin.clone().claim(ctx);
                let built_guide = built_guide.claim(ctx);
                let guide_source = guide_source.clone().claim(ctx);
                |rt| {
                    let mdbook_bin = rt.read(mdbook_bin);
                    let mdbook_admonish_bin = rt.read(mdbook_admonish_bin);
                    let mdbook_mermaid_bin = rt.read(mdbook_mermaid_bin);

                    let sh = xshell::Shell::new()?;

                    let out_path: PathBuf = sh.current_dir().absolute()?.join("book");
                    let guide_source: PathBuf = rt.read(guide_source);

                    sh.change_dir(&guide_source);
                    xshell::cmd!(
                        sh,
                        "{mdbook_bin} build {guide_source} --dest-dir {out_path}"
                    )
                    // intercepted by the `mdbook-openvmm-shim`
                    .env("SHIM_MDBOOK_ADMONISH", mdbook_admonish_bin)
                    .env("SHIM_MDBOOK_MERMAID", mdbook_mermaid_bin)
                    .run()?;

                    rt.write(built_guide, &out_path);

                    Ok(())
                }
            });
        }

        Ok(())
    }
}