Skip to content

Commit

Permalink
xvm: optimize workspace file and some prompt
Browse files Browse the repository at this point in the history
Signed-off-by: sunrisepeak <[email protected]>
  • Loading branch information
Sunrisepeak committed Jan 7, 2025
1 parent 50d997e commit 72c7cab
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 28 deletions.
6 changes: 6 additions & 0 deletions core/xvm/.workspace.xvm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
xvm-wmetadata:
name: test
active: false
inherit: true
versions: {}
File renamed without changes.
22 changes: 16 additions & 6 deletions core/xvm/release-build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
LINUX_RELEASE_DIR=`pwd`/target/release
WINDOWS_RELEASE_DIR=`pwd`/target/x86_64-pc-windows-gnu/release
#!/bin/bash

CURRENT_DATE=$(date +"%Y%m%d%H%M%S")

LINUX_RELEASE_DIR=$(pwd)/target/release
WINDOWS_RELEASE_DIR=$(pwd)/target/x86_64-pc-windows-gnu/release

cargo build --release
cargo build --target x86_64-pc-windows-gnu --release
cd $LINUX_RELEASE_DIR
tar -czf xvm-dev-x86_64-linux-gnu.tar.gz xvm
cd $WINDOWS_RELEASE_DIR
zip -r xvm-dev-x86_64-pc-windows-gnu.zip xvm.exe

cd "$LINUX_RELEASE_DIR"
LINUX_ARCHIVE_NAME="xvm-dev-x86_64-linux-gnu-${CURRENT_DATE}.tar.gz"
tar -czf "$LINUX_ARCHIVE_NAME" xvm
echo "✅ Linux archive created: $LINUX_ARCHIVE_NAME"

cd "$WINDOWS_RELEASE_DIR"
WINDOWS_ARCHIVE_NAME="xvm-dev-x86_64-pc-windows-gnu-${CURRENT_DATE}.zip"
zip -r "$WINDOWS_ARCHIVE_NAME" xvm.exe
echo "✅ Windows archive created: $WINDOWS_ARCHIVE_NAME"
4 changes: 3 additions & 1 deletion core/xvm/src/baseinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::env;

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

pub static WORKSPACE_FILE : &str = ".workspace.xvm.yaml";

#[allow(dead_code)]
pub fn rundir() {
RUNDIR.get_or_init(|| {
Expand All @@ -17,7 +19,7 @@ pub fn versiondb_file() -> String {
}

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

#[allow(dead_code)]
Expand Down
15 changes: 11 additions & 4 deletions core/xvm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pub fn xvm_use(matches: &ArgMatches) -> Result<()> {
let vdb = xvmlib::get_versiondb();

if !vdb.has_target(target) {
println!("Target [{}] is missing from the xvm database. Please add it before proceeding.", target);
println!("Target [{}] is missing from the xvm database", target.bold().red());
println!("\n\t{}\n", "xvm add [target] [version] ...".yellow());
std::process::exit(1);
}

Expand Down Expand Up @@ -251,11 +252,16 @@ pub fn xvm_workspace(matches: &ArgMatches) -> Result<()> {
let mut workspace = if target == "global" {
xvmlib::get_global_workspace().clone()
} else {
if fs::metadata("workspace.xvm.yaml").is_ok() {
if fs::metadata(baseinfo::WORKSPACE_FILE).is_ok() {
helper::load_local_workspace()
} else {
need_save = true;
Workspace::new("workspace.xvm.yaml", target)
println!("\n\t[ {} ]\n", target.bright_purple().bold());
if helper::prompt("create workspace? (y/n): ", "y") {
need_save = true;
Workspace::new(baseinfo::WORKSPACE_FILE, target)
} else {
return Ok(());
}
}
};

Expand Down Expand Up @@ -305,6 +311,7 @@ pub fn xvm_workspace(matches: &ArgMatches) -> Result<()> {

if need_save {
workspace.save_to_local().context("Failed to save Workspace")?;
println!("update workspace [{}] - ok", target.bold().bright_purple());
}

Ok(())
Expand Down
54 changes: 51 additions & 3 deletions core/xvm/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// helper
use std::io::{self, Write};
use std::fs;
use std::env;

use colored::Colorize;
use xvmlib::Workspace;

use crate::baseinfo;

// try to load local workspace, if not found, load global workspace
pub fn load_workspace() -> Workspace {
let mut workspace;
if fs::metadata("workspace.xvm.yaml").is_ok() {
if fs::metadata(baseinfo::WORKSPACE_FILE).is_ok() {
workspace = load_local_workspace();
if !workspace.active() {
workspace = xvmlib::get_global_workspace().clone()
Expand All @@ -19,12 +23,12 @@ pub fn load_workspace() -> Workspace {
}

pub fn load_local_workspace() -> Workspace {
Workspace::from("workspace.xvm.yaml").expect("Failed to load Workspace")
Workspace::from(baseinfo::WORKSPACE_FILE).expect("Failed to load Workspace")
}

pub fn load_workspace_and_merge() -> Workspace {
let mut workspace = xvmlib::get_global_workspace().clone();
if fs::metadata("workspace.xvm.yaml").is_ok() {
if fs::metadata(baseinfo::WORKSPACE_FILE).is_ok() {
let local_workspace = load_local_workspace();
if local_workspace.active() {
if local_workspace.inherit() {
Expand All @@ -50,4 +54,48 @@ pub fn prompt(prompt: &str, value: &str) -> bool {
let input = input.trim().to_lowercase();

input == value
}

pub fn runtime_check_and_tips() -> bool {
let path_to_check = baseinfo::bindir();
let path_var = env::var("PATH").unwrap_or_default();
let separator = if cfg!(target_os = "windows") { ";" } else { ":" };
let first_path = path_var.split(separator).next();

if let Some(first) = first_path {
if first == path_to_check {
true
} else {
print_commands(&path_to_check);
false
}
} else {
print_commands(&path_to_check);
false
}
}

fn print_commands(path_to_check: &str) {

println!("\n\t\t{}", "[Runtime Tips]".bold());

if cfg!(target_os = "windows") {
println!("\n# For PowerShell:");
println!(r#"[System.Environment]::SetEnvironmentVariable("PATH", "{}" + $env:PATH, "User")"#, path_to_check);

println!("\n# For cmd:");
println!(r#"setx PATH "{};%PATH%""#, path_to_check);

println!("\n-- {}", "run this command in cmd or PowerShell.".yellow());
} else {
println!("\n# For bash/zsh:");
println!(r#"export PATH="{}:$PATH""#, path_to_check);

println!("\n# For fish:");
println!(r#"set -Ux PATH {} $PATH"#, path_to_check);

println!("\n-- {}", "add it to your configuration file.".yellow());
}

println!("\n👉 Don't forget to refresh environment variable\n")
}
12 changes: 7 additions & 5 deletions core/xvm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ mod handler;

fn main() {
//baseinfo::print_baseinfo();
xvmlib::init_versiondb(baseinfo::versiondb_file().as_str());
xvmlib::init_global_workspace(baseinfo::workspace_file().as_str());
xvmlib::init_shims(baseinfo::bindir().as_str());
let matches = cmdprocessor::parse_from_command_line();
let _ = cmdprocessor::run(&matches);
if helper::runtime_check_and_tips() {
xvmlib::init_versiondb(baseinfo::versiondb_file().as_str());
xvmlib::init_global_workspace(baseinfo::workspace_file().as_str());
xvmlib::init_shims(baseinfo::bindir().as_str());
let matches = cmdprocessor::parse_from_command_line();
let _ = cmdprocessor::run(&matches);
}
}
9 changes: 0 additions & 9 deletions core/xvm/workspace.xvm.yaml

This file was deleted.

0 comments on commit 72c7cab

Please sign in to comment.