flowey_lib_common/
get_cargo_crate_version.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Get the version of a cargo crate
5
6use flowey::node::prelude::*;
7
8flowey_request! {
9    pub struct Request {
10        pub path: ReadVar<PathBuf>,
11        pub version: WriteVar<Option<String>>,
12    }
13}
14
15new_simple_flow_node!(struct Node);
16
17impl SimpleFlowNode for Node {
18    type Request = Request;
19
20    fn imports(_ctx: &mut ImportCtx<'_>) {}
21
22    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
23        let Request { path, version } = request;
24
25        ctx.emit_rust_step("get cargo crate version", |ctx| {
26            let path = path.claim(ctx);
27            let write_version = version.claim(ctx);
28
29            move |rt| {
30                let path = rt.read(path).join("Cargo.toml");
31                let toml = fs_err::read_to_string(&path)
32                    .context("failed to read Cargo.toml")?
33                    .parse::<toml_edit::DocumentMut>()
34                    .context("failed to parse Cargo.toml")?;
35                let package = toml.get("package").context("no package section")?;
36                let name = package
37                    .get("name")
38                    .context("missing name")?
39                    .as_str()
40                    .context("invalid name")?
41                    .to_owned();
42                let version = package
43                    .get("version")
44                    .map(|x| {
45                        Ok::<String, anyhow::Error>(
46                            x.as_str().context("invalid version")?.to_owned(),
47                        )
48                    })
49                    .transpose()?;
50                log::info!("package {name} has version {version:?}");
51                rt.write(write_version, &version);
52
53                Ok(())
54            }
55        });
56
57        Ok(())
58    }
59}