flowey_lib_common/
copy_to_artifact_dir.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Helper to streamline copying an ad-hoc set of files into an artifact
5//! directory.
6
7use crate::_util::copy_dir_all;
8use flowey::node::prelude::*;
9
10flowey_request! {
11    pub struct Request {
12        /// Friendly label printed when running the step.
13        pub debug_label: String,
14        /// Path to the artifact directory
15        pub artifact_dir: ReadVar<PathBuf>,
16        /// A collection of (dst, src) pairs of files to copy into the artifact dir.
17        pub files: ReadVar<Vec<(PathBuf, PathBuf)>>,
18        /// Signal that the file copy succeeded.
19        pub done: WriteVar<SideEffect>,
20    }
21}
22
23new_simple_flow_node!(struct Node);
24
25impl SimpleFlowNode for Node {
26    type Request = Request;
27
28    fn imports(_ctx: &mut ImportCtx<'_>) {}
29
30    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
31        let Request {
32            debug_label,
33            files,
34            artifact_dir,
35            done,
36        } = request;
37
38        ctx.emit_rust_step(format!("copying {debug_label} to artifact dir"), |ctx| {
39            done.claim(ctx);
40            let files = files.claim(ctx);
41            let artifact_dir = artifact_dir.claim(ctx);
42            move |rt| {
43                let artifact_dir = rt.read(artifact_dir);
44                let files = rt.read(files);
45
46                for (dst, src) in files {
47                    // allow passing things like `some/subdir/artifact`
48                    if let Some(parent) = dst.parent() {
49                        fs_err::create_dir_all(artifact_dir.join(parent))?;
50                    }
51
52                    let dst = artifact_dir.join(dst);
53                    if src.is_dir() {
54                        copy_dir_all(src, dst)?;
55                    } else {
56                        fs_err::copy(src, dst)?;
57                    }
58                }
59
60                log::info!("copied files into {}", artifact_dir.display());
61
62                Ok(())
63            }
64        });
65
66        Ok(())
67    }
68}