Skip to main content

flowey_lib_common/_util/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4pub use flowey::util::copy_dir_all;
5
6use flowey::node::prelude::FlowPlatformKind;
7use flowey::node::prelude::RustRuntimeServices;
8use std::collections::BTreeMap;
9
10pub mod cargo_install;
11pub mod cargo_output;
12pub mod extract;
13pub mod wslpath;
14
15/// Group values by key, ordering groups deterministically and retaining all
16/// values within each group in input order.
17pub fn group_by<K: Ord, V>(items: impl IntoIterator<Item = (K, V)>) -> BTreeMap<K, Vec<V>> {
18    let mut groups = BTreeMap::<_, Vec<_>>::new();
19    for (key, value) in items {
20        groups.entry(key).or_default().push(value);
21    }
22    groups
23}
24
25// include a "dummy" _rt argument to enforce that this helper should only be
26// used in runtime contexts, and not during flow compile-time.
27pub fn running_in_wsl(_rt: &mut RustRuntimeServices<'_>) -> bool {
28    let Ok(output) = std::process::Command::new("wslpath")
29        .args(["-aw", "/"])
30        .output()
31    else {
32        return false;
33    };
34    String::from_utf8_lossy(&output.stdout).starts_with(r"\\wsl.localhost")
35}
36
37/// Returns the name of the bsdtar binary to use. On Windows, this is just the
38/// inbox tar.exe. Elsewhere, use bsdtar. This will require installing the
39/// libarchive-tools package on Debian-based Linux.
40pub fn bsdtar_name(rt: &mut RustRuntimeServices<'_>) -> &'static str {
41    match rt.platform().kind() {
42        FlowPlatformKind::Windows => "tar.exe",
43        FlowPlatformKind::Unix => "bsdtar",
44    }
45}