From a91f0b688dac110cf62e37ea2da5be510d66fa3c Mon Sep 17 00:00:00 2001 From: sunrisepeak Date: Thu, 2 Jan 2025 20:47:15 +0800 Subject: [PATCH] xvm init project --- core/xvm/.gitignore | 15 +++++++ core/xvm/Cargo.toml | 13 ++++++ core/xvm/src/main.rs | 10 +++++ core/xvm/xvmlib/lib.rs | 1 + core/xvm/xvmlib/shims.rs | 86 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 core/xvm/.gitignore create mode 100644 core/xvm/Cargo.toml create mode 100644 core/xvm/src/main.rs create mode 100644 core/xvm/xvmlib/lib.rs create mode 100644 core/xvm/xvmlib/shims.rs diff --git a/core/xvm/.gitignore b/core/xvm/.gitignore new file mode 100644 index 0000000..918fd3f --- /dev/null +++ b/core/xvm/.gitignore @@ -0,0 +1,15 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# Added by cargo + +/target +/Cargo.lock \ No newline at end of file diff --git a/core/xvm/Cargo.toml b/core/xvm/Cargo.toml new file mode 100644 index 0000000..9ab83b4 --- /dev/null +++ b/core/xvm/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "xvmlib" +version = "0.1.0" +edition = "2021" + +[lib] +path = "xvmlib/lib.rs" + +[[bin]] +name = "xvm" +path = "src/main.rs" + +[dependencies] diff --git a/core/xvm/src/main.rs b/core/xvm/src/main.rs new file mode 100644 index 0000000..3294009 --- /dev/null +++ b/core/xvm/src/main.rs @@ -0,0 +1,10 @@ +extern crate xvmlib; + +use xvmlib::shims; + +fn main() { + let mut xim = shims::Program::new("node", "prev-0.0.2"); + //xim.add_env("PATH", "~/.nvm/versions/node/v21.7.3/bin"); + xim.add_args(&["-v", "o"]); + xim.run(); +} \ No newline at end of file diff --git a/core/xvm/xvmlib/lib.rs b/core/xvm/xvmlib/lib.rs new file mode 100644 index 0000000..d1dc796 --- /dev/null +++ b/core/xvm/xvmlib/lib.rs @@ -0,0 +1 @@ +pub mod shims; \ No newline at end of file diff --git a/core/xvm/xvmlib/shims.rs b/core/xvm/xvmlib/shims.rs new file mode 100644 index 0000000..89c3077 --- /dev/null +++ b/core/xvm/xvmlib/shims.rs @@ -0,0 +1,86 @@ +use std::fs; +use std::process::Command; + +// TODO +pub enum Type { + Symlink, // SymlinkWrapper, + XvmRun, // Auto Detect Version +} + +pub struct Program { + name: String, + version: String, + envs: Vec<(String, String)>, + args: Vec, +} + +impl Program { + pub fn new(name: &str, version: &str) -> Self { + Self { + name: name.to_string(), + version: version.to_string(), + envs: Vec::new(), + args: Vec::new(), + } + } + + pub fn save_to(&self, dir: &str) { + println!("Saving Program to {}", dir); + + if !fs::metadata(dir).is_ok() { + fs::create_dir_all(dir).unwrap(); + } + + // create shim-script-file for windows and unix + let filename: String; + let args_placeholder: &str; + if cfg!(target_os = "windows") { + args_placeholder = "%*"; + filename = format!("{}/{}.bat", dir, self.name); + } else { + args_placeholder = "$@"; + filename = format!("{}/{}", dir, self.name); + } + + fs::write(&filename, &format!("echo run {} {}", + self.name, args_placeholder + )).unwrap(); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&filename, PermissionsExt::from_mode(0o755)).unwrap(); + } + } + + pub fn add_env(&mut self, key: &str, value: &str) { + self.envs.push((key.to_string(), value.to_string())); + } + + pub fn add_envs(&mut self, envs: &[(&str, &str)]) { + for (key, value) in envs { + self.envs.push((key.to_string(), value.to_string())); + } + } + + pub fn add_arg(&mut self, arg: &str) { + self.args.push(arg.to_string()); + } + + pub fn add_args(&mut self, args: &[&str]) { + for arg in args { + self.args.push(arg.to_string()); + } + } + + pub fn run(&self) { + println!("Running Program [{}], version {:?}", self.name, self.version); + println!("Args: {:?}", self.args); + Command::new(&self.name) + .args(&self.args) + .envs(self.envs.iter().cloned()) + .status() + .expect("failed to execute process"); + println!("Program [{}] finished", self.name); + } +} \ No newline at end of file