From 72c7cabc115437ca24b4cf5c3681b6e3ff96d8de Mon Sep 17 00:00:00 2001 From: sunrisepeak Date: Tue, 7 Jan 2025 18:54:07 +0800 Subject: [PATCH] xvm: optimize workspace file and some prompt Signed-off-by: sunrisepeak --- core/xvm/.workspace.xvm.yaml | 6 +++ ...workspace.xvm.yaml => .workspace.xvm.yaml} | 0 core/xvm/release-build.sh | 22 +++++--- core/xvm/src/baseinfo.rs | 4 +- core/xvm/src/handler.rs | 15 ++++-- core/xvm/src/helper.rs | 54 +++++++++++++++++-- core/xvm/src/main.rs | 12 +++-- core/xvm/workspace.xvm.yaml | 9 ---- 8 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 core/xvm/.workspace.xvm.yaml rename core/xvm/config/{workspace.xvm.yaml => .workspace.xvm.yaml} (100%) delete mode 100644 core/xvm/workspace.xvm.yaml diff --git a/core/xvm/.workspace.xvm.yaml b/core/xvm/.workspace.xvm.yaml new file mode 100644 index 0000000..be6a2c3 --- /dev/null +++ b/core/xvm/.workspace.xvm.yaml @@ -0,0 +1,6 @@ +--- +xvm-wmetadata: + name: test + active: false + inherit: true +versions: {} diff --git a/core/xvm/config/workspace.xvm.yaml b/core/xvm/config/.workspace.xvm.yaml similarity index 100% rename from core/xvm/config/workspace.xvm.yaml rename to core/xvm/config/.workspace.xvm.yaml diff --git a/core/xvm/release-build.sh b/core/xvm/release-build.sh index b01201d..db10d02 100755 --- a/core/xvm/release-build.sh +++ b/core/xvm/release-build.sh @@ -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 \ No newline at end of file + +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" \ No newline at end of file diff --git a/core/xvm/src/baseinfo.rs b/core/xvm/src/baseinfo.rs index a47eae5..4441a0c 100644 --- a/core/xvm/src/baseinfo.rs +++ b/core/xvm/src/baseinfo.rs @@ -4,6 +4,8 @@ use std::env; static RUNDIR: OnceLock = OnceLock::new(); +pub static WORKSPACE_FILE : &str = ".workspace.xvm.yaml"; + #[allow(dead_code)] pub fn rundir() { RUNDIR.get_or_init(|| { @@ -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)] diff --git a/core/xvm/src/handler.rs b/core/xvm/src/handler.rs index f63b168..c2d34e3 100644 --- a/core/xvm/src/handler.rs +++ b/core/xvm/src/handler.rs @@ -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); } @@ -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(()); + } } }; @@ -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(()) diff --git a/core/xvm/src/helper.rs b/core/xvm/src/helper.rs index c2c9cab..35ac506 100644 --- a/core/xvm/src/helper.rs +++ b/core/xvm/src/helper.rs @@ -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() @@ -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() { @@ -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") } \ No newline at end of file diff --git a/core/xvm/src/main.rs b/core/xvm/src/main.rs index 0ce482f..909ef51 100644 --- a/core/xvm/src/main.rs +++ b/core/xvm/src/main.rs @@ -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); + } } diff --git a/core/xvm/workspace.xvm.yaml b/core/xvm/workspace.xvm.yaml deleted file mode 100644 index 04944d9..0000000 --- a/core/xvm/workspace.xvm.yaml +++ /dev/null @@ -1,9 +0,0 @@ ---- -xvm-wmetadata: - name: test - active: true - inherit: true -versions: - test: 1.1.1 - python: 2.4.18 - py: 0.0.2