flowey_lib_common/
copy_to_artifact_dir.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Helper to streamline copying an ad-hoc set of files into an artifact
//! directory.

use crate::_util::copy_dir_all;
use flowey::node::prelude::*;

flowey_request! {
    pub struct Request {
        /// Friendly label printed when running the step.
        pub debug_label: String,
        /// Path to the artifact directory
        pub artifact_dir: ReadVar<PathBuf>,
        /// A collection of (dst, src) pairs of files to copy into the artifact dir.
        pub files: ReadVar<Vec<(PathBuf, PathBuf)>>,
        /// Signal that the file copy succeeded.
        pub done: WriteVar<SideEffect>,
    }
}

new_simple_flow_node!(struct Node);

impl SimpleFlowNode for Node {
    type Request = Request;

    fn imports(_ctx: &mut ImportCtx<'_>) {}

    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
        let Request {
            debug_label,
            files,
            artifact_dir,
            done,
        } = request;

        ctx.emit_rust_step(format!("copying {debug_label} to artifact dir"), |ctx| {
            done.claim(ctx);
            let files = files.claim(ctx);
            let artifact_dir = artifact_dir.claim(ctx);
            move |rt| {
                let artifact_dir = rt.read(artifact_dir);
                let files = rt.read(files);

                for (dst, src) in files {
                    // allow passing things like `some/subdir/artifact`
                    if let Some(parent) = dst.parent() {
                        fs_err::create_dir_all(artifact_dir.join(parent))?;
                    }

                    let dst = artifact_dir.join(dst);
                    if src.is_dir() {
                        copy_dir_all(src, dst)?;
                    } else {
                        fs_err::copy(src, dst)?;
                    }
                }

                log::info!("copied files into {}", artifact_dir.display());

                Ok(())
            }
        });

        Ok(())
    }
}