Skip to content

Commit

Permalink
XVM Base - minimal implement (#60)
Browse files Browse the repository at this point in the history
* init xvm branch

* xvm init project

* add config

* xvmlib: base

* base-impl

* update

* xvm -> xvm-rs
  • Loading branch information
Sunrisepeak authored Jan 4, 2025
1 parent 43bcfa0 commit 32b73ac
Show file tree
Hide file tree
Showing 18 changed files with 1,193 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
20 changes: 20 additions & 0 deletions core/xvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "xvm-rs"
version = "0.1.0"
edition = "2021"

[lib]
name = "xvmlib"
path = "xvmlib/lib.rs"

[[bin]]
name = "xvm"
path = "src/main.rs"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
indexmap = { version = "1.9", features = ["serde"] }
clap = { version = "4.3", features = ["derive"] }
anyhow = "1.0"
colored = "2.0"
1 change: 1 addition & 0 deletions core/xvm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# XVM | Xlings Version Manager
13 changes: 13 additions & 0 deletions core/xvm/config/cmd.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
xvm add target version --path xxx
xvm add target version --command xxx
xvm add target version --env name=value
xvm remove target version

xvm use target version
xvm current target
xvm run target version --args xxx
xvm list target

xvm workspace target
xvm workspace target --enable
xvm workspace target --disable
15 changes: 15 additions & 0 deletions core/xvm/config/config.xvm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bindir: xxx

xvm add target version --path xxx
xvm add target version --command xxx
xvm add target version --env name=value
xvm remove target version

xvm use target version
xvm current target
xvm run target version --args xxx
xvm list target

xvm workspace target
xvm workspace target --enable
xvm workspace target --disable
26 changes: 26 additions & 0 deletions core/xvm/config/versions.xvm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
python:
3.12.3:
command: python3
path: /usr/bin
2.7.18:
path: /usr/bin
2.4.18:
path: /usr/bin
"3":
command: python3
path: ""
java:
8.0.0:
path: /usr/local/java/8
envs:
JAVA_HOME: /usr/local/java/8
CLASSPATH: /usr/local/java/8/lib
node:
22.12.0:
path: ~/.nvm/versions/node/v22.12.0/bin
21.7.3:
path: ~/.nvm/versions/node/v21.7.3/bin
test:
3.12.1:
path: /usr/bin
9 changes: 9 additions & 0 deletions core/xvm/config/workspace.xvm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
xvm-wmetadata:
name: global
active: true
versions:
python: 2.7.18
java: 8.0.0
node: 21.7.3
mytest: 3.12.2
76 changes: 76 additions & 0 deletions core/xvm/src/baseinfo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::sync::OnceLock;
use std::path::PathBuf;
use std::env;

static RUNDIR: OnceLock<PathBuf> = OnceLock::new();

#[allow(dead_code)]
pub fn rundir() {
RUNDIR.get_or_init(|| {
// get current runtime directory
env::current_dir().expect("Failed to get current directory")
});
}

pub fn versiondb_file() -> String {
format!("{}/versions.xvm.yaml", platform::xvm_homedir())
}

pub fn workspace_file() -> String {
format!("{}/workspace.xvm.yaml", platform::xvm_homedir())
}

#[allow(dead_code)]
pub fn bindir() -> String {
platform::bindir()
}

#[allow(dead_code)]
pub fn shimdir() -> String {
format!("{}/shims", platform::xvm_homedir())
}

#[allow(dead_code)]
pub fn print_baseinfo() {
println!("XVM Home: {}", platform::xvm_homedir());
println!("XVM Bindir: {}", bindir());
println!("XVM VersionDB: {}", versiondb_file());
println!("XVM Workspace: {}", workspace_file());
}

pub mod platform {
//use std::env;
use super::*;

static HOMEDIR: OnceLock<String> = OnceLock::new();

pub fn homedir() -> String {
HOMEDIR.get_or_init(|| {
#[cfg(target_os = "windows")]
{
env::var("USERPROFILE").expect("Failed to get USERPROFILE environment variable")
}

#[cfg(not(target_os = "windows"))]
{
env::var("HOME").expect("Failed to get HOME environment variable")
}
}).clone()
}

pub fn bindir() -> String {
if cfg!(target_os = "windows") {
"C:/users/public/.xlings_data/bin".to_string()
} else {
format!("{}/.xlings_data/bin", homedir())
}
}

pub fn xvm_homedir() -> String {
if cfg!(target_os = "windows") {
"C:/users/public/.xlings_data/xvm".to_string()
} else {
format!("{}/.xlings_data/xvm", homedir())
}
}
}
165 changes: 165 additions & 0 deletions core/xvm/src/cmdprocessor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
use clap::{Arg, ArgAction, Command, ArgMatches};
use anyhow::Result;

use crate::handler;

pub fn run(matches: &ArgMatches) -> Result<()> {

match matches.subcommand() {
Some(("add", sub_matches)) => handler::xvm_add(sub_matches)?,
Some(("remove", sub_matches)) => handler::xvm_remove(sub_matches)?,
Some(("use", sub_matches)) => handler::xvm_use(sub_matches)?,
Some(("current", sub_matches)) => handler::xvm_current(sub_matches)?,
Some(("run", sub_matches)) => handler::xvm_run(sub_matches)?,
Some(("list", sub_matches)) => handler::xvm_list(sub_matches)?,
Some(("workspace", sub_matches)) => handler::xvm_workspace(sub_matches)?,
_ => println!("Unknown command. Use --help for usage information."),
}

Ok(())
}

////////////

pub fn parse_from_command_line() -> ArgMatches {
build_command().get_matches()
}

pub fn parse_from_string(args: &[&str]) -> ArgMatches {
build_command()
.try_get_matches_from(args)
.expect("Failed to parse arguments from string")
}

fn build_command() -> Command {
Command::new("xvm")
.version("prev-0.0.2")
.author("d2learn <[email protected]>")
.about("a simple and generic version management tool")
.subcommand(
Command::new("add")
.about("Add a target")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
)
.arg(
Arg::new("version")
.required(true)
.help("The version of the target"),
)
.arg(
Arg::new("path")
.long("path")
.value_name("PATH")
.action(ArgAction::Set)
.help("Specify the installation path for the target"),
)
.arg(
Arg::new("command")
.long("command")
.value_name("COMMAND")
.action(ArgAction::Set)
.help("Specify a command for the target"),
)
.arg(
Arg::new("env")
.long("env")
.value_name("ENV")
.action(ArgAction::Append)
.help("Set environment variables for the target (format: name=value)"),
),
)
.subcommand(
Command::new("remove")
.about("Remove a target")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
)
.arg(
Arg::new("version")
//.required(true)
.help("The version of the target"),
),
)
.subcommand(
Command::new("use")
.about("Use a specific target and version")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
)
.arg(
Arg::new("version")
//.required(true)
.help("The version of the target"),
),
)
.subcommand(
Command::new("current")
.about("Show the current target's version")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
),
)
.subcommand(
Command::new("list")
.about("List all versions for a target")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
),
)
.subcommand(
Command::new("run")
.about("Run a target program")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
)
.arg(
Arg::new("version")
//.required(true)
.help("The version of the target"),
)
.arg(
Arg::new("args")
//.required(true)
.long("args")
//.value_name("ARGS")
.action(ArgAction::Set)
.num_args(0..)
.allow_hyphen_values(true)
.help("Arguments to pass to the command")
),
)
.subcommand(
Command::new("workspace")
.about("Manage xvm's workspaces")
.arg(
Arg::new("target")
.required(true)
.help("The name of the target"),
)
.arg(
Arg::new("enable")
.long("enable")
.action(ArgAction::SetTrue)
.help("Enable the workspace"),
)
.arg(
Arg::new("disable")
.long("disable")
.action(ArgAction::SetTrue)
.help("Disable the workspace"),
),
)
}
Loading

0 comments on commit 32b73ac

Please sign in to comment.