summaryrefslogtreecommitdiff
path: root/scripts/native-utils/src/split_path.rs
blob: 15cac599ae1008cd987709ce51f23d0006026b88 (plain)
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
79
80
81
82
use std::{
    env,
    ffi::{OsStr, OsString},
    io::{self, Write},
    os::unix::prelude::{OsStrExt, OsStringExt},
    path::PathBuf,
};

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[arg(long)]
    automount_root: PathBuf,

    #[arg(long)]
    include_interop: bool,
}

const SINGLE_QUOTE: u8 = b'\'';
const DOUBLE_QUOTE: u8 = b'"';

fn shell_escape(s: &OsStr) -> OsString {
    // a shameless ripoff of the Python algorithm:
    // https://github.com/python/cpython/blob/f1f3af7b8245e61a2e0abef03b2c6c5902ed7df8/Lib/shlex.py#L323
    let mut result = Vec::new();

    result.push(SINGLE_QUOTE);

    for &byte in s.as_bytes() {
        result.push(byte);
        if byte == SINGLE_QUOTE {
            result.push(DOUBLE_QUOTE);
            result.push(SINGLE_QUOTE);
            result.push(DOUBLE_QUOTE);
            result.push(SINGLE_QUOTE);
        }
    }

    result.push(SINGLE_QUOTE);

    OsString::from_vec(result)
}

fn build_export(var: &str, paths: &[PathBuf]) -> OsString {
    let mut result = OsString::new();
    result.push("export ");
    result.push(var);
    result.push("=");
    result.push(shell_escape(
        &env::join_paths(paths).expect("paths must be valid"),
    ));
    result.push("\n");
    result
}

fn main() -> anyhow::Result<()> {
    let args = Args::parse();

    let path = env::var("PATH")?;

    let mut native = vec![];
    let mut interop = vec![];

    for part in env::split_paths(&path) {
        if part.starts_with(&args.automount_root) {
            interop.push(part);
        } else {
            native.push(part);
        }
    }

    if args.include_interop {
        native.extend(interop.clone());
    };

    let mut lock = io::stdout().lock();
    lock.write_all(build_export("PATH", &native).as_bytes())?;
    lock.write_all(build_export("WSLPATH", &interop).as_bytes())?;

    Ok(())
}