Skip to content

Commit

Permalink
xvm init project
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisepeak committed Jan 2, 2025
1 parent ad65948 commit a91f0b6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/xvm/.gitignore
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions core/xvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 10 additions & 0 deletions core/xvm/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions core/xvm/xvmlib/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod shims;
86 changes: 86 additions & 0 deletions core/xvm/xvmlib/shims.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

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);
}
}

0 comments on commit a91f0b6

Please sign in to comment.