From 5464d4ebe9976d5b1bec4ffe88b2af74800ab230 Mon Sep 17 00:00:00 2001 From: anusikh Date: Thu, 4 Jul 2024 09:54:06 +0530 Subject: [PATCH 01/24] refactor cmd parser and ls fix --- src/main.rs | 88 ++++++++++++++++++++++++++++------------- src/utils/file_utils.rs | 13 +++++- 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0fd8247..8171ddb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::sync::{ Arc, }; -use clap::Parser; +use clap::{Parser, Subcommand, ValueEnum}; use tokio::signal; use crate::commands::{ @@ -20,49 +20,72 @@ use crate::commands::{ #[command(version, about, long_about = None)] struct Cli { #[clap(subcommand)] - cmd: Command, + cmd: Lang, } -#[derive(Parser, Debug)] +#[derive(Debug, Clone, Subcommand)] +enum Lang { + #[clap(about = "java version management")] + Java { + // #[arg(short, long, value_enum, help = "the Java action to perform")] + #[clap(help = "the java action to be performed")] + action: Option, + param: Option, + }, +} + +#[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] enum Command { - #[clap(about = "Install the specific JDK")] - Install(Arg), - #[clap(about = "Uninstall the specified JDK version")] - Uninstall(Arg), - #[clap(about = "Use a specific JDK version after installation")] - Usev(Arg), - #[clap(about = "Clean empty folders in the .jvem directory")] + #[clap(help = "install the specific JDK")] + Install, + #[clap(help = "uninstall the specified JDK version")] + Uninstall, + #[clap(help = "use a specific JDK version after installation")] + Usev, + #[clap(help = "clean empty folders in the .jvem directory")] Clean, - #[clap(about = "Find the currently active JDK version")] + #[clap(help = "find the currently active JDK version")] Current, - #[clap(about = "List all JDK versions available for install")] + #[clap(help = "list all JDK versions available for install")] Lsrem, - #[clap(about = "List locally installed JDK versions")] + #[clap(help = "list locally installed JDK versions")] Ls, - #[clap(about = "Deactivate the currently active JDK")] + #[clap(help = "deactivate the currently active JDK")] Deactivate, } -#[derive(Parser, Debug)] -struct Arg { - #[clap(index = 1)] - arg: String, +impl std::str::FromStr for Command { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "install" => Ok(Command::Install), + "lsrem" => Ok(Command::Lsrem), + "ls" => Ok(Command::Ls), + "current" => Ok(Command::Current), + "uninstall" => Ok(Command::Uninstall), + "usev" => Ok(Command::Usev), + "deactivate" => Ok(Command::Deactivate), + "clean" => Ok(Command::Clean), + _ => Err(format!("Invalid Java action: {}", s)), + } + } } -async fn logic(running: Arc) { - while running.load(Ordering::Relaxed) { - match Cli::parse().cmd { - Command::Install(arg) => { - let jdk = arg.arg; +async fn handle_java_action(action: Option, param: Option) { + if let Some(action) = action { + match action { + Command::Install => { + let jdk = param.unwrap(); install(jdk); } Command::Current => current(), - Command::Uninstall(arg) => { - let jdk = arg.arg; + Command::Uninstall => { + let jdk = param.unwrap(); uninstall(jdk); } - Command::Usev(arg) => { - let jdk = arg.arg; + Command::Usev => { + let jdk = param.unwrap(); usev(jdk).await; } Command::Lsrem => { @@ -72,6 +95,17 @@ async fn logic(running: Arc) { Command::Deactivate => deactivate(), Command::Clean => clean(), } + } else { + println!("enter valid action"); + } +} + +async fn logic(running: Arc) { + while running.load(Ordering::Relaxed) { + match Cli::parse().cmd { + Lang::Java { action, param } => handle_java_action(action, param).await, + } + // the below step is important to prevent infinite loop on failure running.store(false, Ordering::Relaxed); } diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index a45bb20..21fdb8c 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -77,16 +77,25 @@ pub fn check_list_locally() { let path_dir = Path::new(&jvem_dir); match path_dir.exists() { true => { + let mut res: Vec = Vec::new(); + for entry in fs::read_dir(path_dir).unwrap() { let entry = entry.unwrap(); // .DS_Store is macos specific if entry.file_name() != "java" && entry.file_name() != ".DS_Store" { - println!("{}", entry.file_name().to_str().unwrap()); + res.push(String::from(entry.file_name().to_str().unwrap())); + } + } + if res.len() > 0 { + for item in res { + println!("{}", item); } + } else { + println!("no jdk installations found locally"); } } false => { - println!("{}", "no jdk installations found locally"); + println!("no jdk installations found locally"); } } } From 97693eef6944c56d8f950b1bd394b11a340438f4 Mon Sep 17 00:00:00 2001 From: anusikh Date: Thu, 4 Jul 2024 15:30:01 +0530 Subject: [PATCH 02/24] refactor java version management --- Cargo.lock | 2 +- src/commands/{ => java}/clean.rs | 0 src/commands/{ => java}/current.rs | 0 src/commands/{ => java}/deactivate.rs | 0 src/commands/{ => java}/install.rs | 0 src/commands/{ => java}/ls.rs | 0 src/commands/{ => java}/lsrem.rs | 0 src/commands/java/mod.rs | 8 ++++++++ src/commands/{ => java}/uninstall.rs | 0 src/commands/{ => java}/usev.rs | 0 src/commands/mod.rs | 9 +-------- src/constants/{versions.rs => java_versions.rs} | 0 src/constants/mod.rs | 2 +- src/main.rs | 7 +++---- src/utils/env_ops.rs | 2 +- src/utils/file_utils.rs | 12 ++++++------ 16 files changed, 21 insertions(+), 21 deletions(-) rename src/commands/{ => java}/clean.rs (100%) rename src/commands/{ => java}/current.rs (100%) rename src/commands/{ => java}/deactivate.rs (100%) rename src/commands/{ => java}/install.rs (100%) rename src/commands/{ => java}/ls.rs (100%) rename src/commands/{ => java}/lsrem.rs (100%) create mode 100644 src/commands/java/mod.rs rename src/commands/{ => java}/uninstall.rs (100%) rename src/commands/{ => java}/usev.rs (100%) rename src/constants/{versions.rs => java_versions.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index a1df872..25bed99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,7 +187,7 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "jvem" -version = "0.2.2" +version = "0.2.3" dependencies = [ "clap", "lazy_static", diff --git a/src/commands/clean.rs b/src/commands/java/clean.rs similarity index 100% rename from src/commands/clean.rs rename to src/commands/java/clean.rs diff --git a/src/commands/current.rs b/src/commands/java/current.rs similarity index 100% rename from src/commands/current.rs rename to src/commands/java/current.rs diff --git a/src/commands/deactivate.rs b/src/commands/java/deactivate.rs similarity index 100% rename from src/commands/deactivate.rs rename to src/commands/java/deactivate.rs diff --git a/src/commands/install.rs b/src/commands/java/install.rs similarity index 100% rename from src/commands/install.rs rename to src/commands/java/install.rs diff --git a/src/commands/ls.rs b/src/commands/java/ls.rs similarity index 100% rename from src/commands/ls.rs rename to src/commands/java/ls.rs diff --git a/src/commands/lsrem.rs b/src/commands/java/lsrem.rs similarity index 100% rename from src/commands/lsrem.rs rename to src/commands/java/lsrem.rs diff --git a/src/commands/java/mod.rs b/src/commands/java/mod.rs new file mode 100644 index 0000000..c2375fe --- /dev/null +++ b/src/commands/java/mod.rs @@ -0,0 +1,8 @@ +pub mod deactivate; +pub mod install; +pub mod usev; +pub mod lsrem; +pub mod current; +pub mod ls; +pub mod uninstall; +pub mod clean; diff --git a/src/commands/uninstall.rs b/src/commands/java/uninstall.rs similarity index 100% rename from src/commands/uninstall.rs rename to src/commands/java/uninstall.rs diff --git a/src/commands/usev.rs b/src/commands/java/usev.rs similarity index 100% rename from src/commands/usev.rs rename to src/commands/java/usev.rs diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d5bba2e..b8d0181 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,8 +1 @@ -pub mod clean; -pub mod current; -pub mod deactivate; -pub mod install; -pub mod ls; -pub mod lsrem; -pub mod uninstall; -pub mod usev; +pub mod java; diff --git a/src/constants/versions.rs b/src/constants/java_versions.rs similarity index 100% rename from src/constants/versions.rs rename to src/constants/java_versions.rs diff --git a/src/constants/mod.rs b/src/constants/mod.rs index 36fa96e..b99510f 100644 --- a/src/constants/mod.rs +++ b/src/constants/mod.rs @@ -1 +1 @@ -pub mod versions; \ No newline at end of file +pub mod java_versions; diff --git a/src/main.rs b/src/main.rs index 8171ddb..6c13525 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,12 +9,11 @@ use std::sync::{ }; use clap::{Parser, Subcommand, ValueEnum}; -use tokio::signal; - -use crate::commands::{ +use commands::java::{ clean::clean, current::current, deactivate::deactivate, install::install, ls::ls, lsrem::lsrem, uninstall::uninstall, usev::usev, }; +use tokio::signal; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -96,7 +95,7 @@ async fn handle_java_action(action: Option, param: Option) { Command::Clean => clean(), } } else { - println!("enter valid action"); + println!("enter valid action. for more details use --help or -h"); } } diff --git a/src/utils/env_ops.rs b/src/utils/env_ops.rs index eef60aa..83a7c2c 100644 --- a/src/utils/env_ops.rs +++ b/src/utils/env_ops.rs @@ -1,4 +1,4 @@ -use crate::constants::versions::constants; +use crate::constants::java_versions::constants; pub fn read_versions() -> Result<(), Box> { let available_versions = constants::AVAILABLE_VERSIONS; diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 21fdb8c..21753ee 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -49,7 +49,7 @@ pub fn get_home_dir() -> String { } pub fn get_installation_dir(name: &str) -> String { - format!("{}/.jvem/{}", get_home_dir(), name) + format!("{}/.jvem/java_versions/{}", get_home_dir(), name) } pub fn check_jdk_exists(name: &str) -> bool { @@ -68,12 +68,12 @@ pub fn check_path_exists(path: &str) -> bool { } pub fn create_java_dir(name: &str) { - let new_dir_path = format!("{}/.jvem/{}", *HOME_DIR, name); + let new_dir_path = format!("{}/.jvem/java_versions/{}", *HOME_DIR, name); fs::create_dir_all(new_dir_path).unwrap(); } pub fn check_list_locally() { - let jvem_dir = format!("{}/.jvem/", get_home_dir()); + let jvem_dir = format!("{}/.jvem/java_versions/", get_home_dir()); let path_dir = Path::new(&jvem_dir); match path_dir.exists() { true => { @@ -105,7 +105,7 @@ pub fn is_empty_dir(path: &std::path::Path) -> io::Result { } pub fn clean_jvem() { - for entry in fs::read_dir(format!("{}/.jvem", get_home_dir())).unwrap() { + for entry in fs::read_dir(format!("{}/.jvem/java_versions", get_home_dir())).unwrap() { let entry = entry.unwrap(); let path = entry.path(); @@ -123,7 +123,7 @@ pub fn extract_tarball_linux(name: String) { &find_file_in_dir("/tmp/", &name), "--strip-components=1", "-C", - &format!("{}/.jvem/{}", get_home_dir(), name), + &format!("{}/.jvem/java_versions/{}", get_home_dir(), name), ], ); @@ -160,7 +160,7 @@ pub fn extract_tarball_macos(name: &str) { vec![ "-c", &format!( - "mv $(find /tmp/{} -mindepth 1 -maxdepth 1 -type d | head -n 1)/* {}/.jvem/{}", + "mv $(find /tmp/{} -mindepth 1 -maxdepth 1 -type d | head -n 1)/* {}/.jvem/java_versions/{}", temp_folder_name, get_home_dir(), name From 9c86374563ab3445fb38bff328dd37e548745ebf Mon Sep 17 00:00:00 2001 From: anusikh Date: Thu, 4 Jul 2024 23:04:45 +0530 Subject: [PATCH 03/24] added maven management for linux --- src/commands/java/install.rs | 4 +-- src/commands/maven/install.rs | 42 ++++++++++++++++++++++++++++++++ src/commands/maven/mod.rs | 2 ++ src/commands/maven/uninstall.rs | 12 +++++++++ src/commands/mod.rs | 1 + src/main.rs | 43 ++++++++++++++++++++++++++++++--- src/utils/file_utils.rs | 28 ++++++++++++++++++--- 7 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 src/commands/maven/install.rs create mode 100644 src/commands/maven/mod.rs create mode 100644 src/commands/maven/uninstall.rs diff --git a/src/commands/java/install.rs b/src/commands/java/install.rs index ec7ab2b..531a8a3 100644 --- a/src/commands/java/install.rs +++ b/src/commands/java/install.rs @@ -60,7 +60,7 @@ fn install_util(name: String, link: String) { if x.ends_with(".gz") { println!("fetching tarball from cache successful"); - extract_tarball_linux(name); + extract_tarball_linux(name, String::from("java")); } else { let output = run_command( "/usr/bin/wget", @@ -72,7 +72,7 @@ fn install_util(name: String, link: String) { ); if output.status.success() { println!("fetching tarball successful "); - extract_tarball_linux(name); + extract_tarball_linux(name, String::from("java")); } else { println!("fetching tarball failed "); } diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs new file mode 100644 index 0000000..9d0388e --- /dev/null +++ b/src/commands/maven/install.rs @@ -0,0 +1,42 @@ +use lazy_static::lazy_static; + +use crate::utils::file_utils::{check_maven_exists, find_file_in_dir, run_command}; + +lazy_static! { + static ref MAVEN_DOWN_URL: String = String::from( + "https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz" + ); +} + +#[cfg(target_os = "linux")] +fn install_util() { + use crate::utils::file_utils::{extract_tarball_linux, get_home_dir}; + + match check_maven_exists() { + false => { + let maven_path = format!("{}/.jvem/maven", get_home_dir()); + std::fs::create_dir_all(maven_path).unwrap(); + let x = find_file_in_dir("/tmp/", "maven"); + + if x.ends_with(".gz") { + println!("fetching maven from cache successful"); + extract_tarball_linux(String::new(), String::from("maven")); + } else { + let download = + run_command("/usr/bin/wget", vec![&MAVEN_DOWN_URL, "-O", "/tmp/maven.tar.gz"]); + + if download.status.success() { + println!("maven download successful"); + extract_tarball_linux(String::new(), String::from("maven")); + } else { + println!("fetching maven failed"); + } + } + } + true => println!("maven already exists, if it doesn't please run the uninstall command and try re-installing it"), + } +} + +pub fn install() { + let _ = install_util(); +} diff --git a/src/commands/maven/mod.rs b/src/commands/maven/mod.rs new file mode 100644 index 0000000..aea399a --- /dev/null +++ b/src/commands/maven/mod.rs @@ -0,0 +1,2 @@ +pub mod install; +pub mod uninstall; diff --git a/src/commands/maven/uninstall.rs b/src/commands/maven/uninstall.rs new file mode 100644 index 0000000..0ee70e4 --- /dev/null +++ b/src/commands/maven/uninstall.rs @@ -0,0 +1,12 @@ +use std::fs; + +use crate::utils::file_utils::get_home_dir; + +pub fn uninstall() { + let path = format!("{}/.jvem/maven", get_home_dir()); + let res = fs::remove_dir_all(path); + match res { + Ok(_) => println!("maven uninstall successful"), + Err(_) => println!("something went wrong uninstalling maven"), + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b8d0181..85fd9aa 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1 +1,2 @@ pub mod java; +pub mod maven; diff --git a/src/main.rs b/src/main.rs index 6c13525..80048a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,10 @@ use std::sync::{ }; use clap::{Parser, Subcommand, ValueEnum}; -use commands::java::{ +use commands::{java::{ clean::clean, current::current, deactivate::deactivate, install::install, ls::ls, lsrem::lsrem, uninstall::uninstall, usev::usev, -}; +}, maven}; use tokio::signal; #[derive(Parser, Debug)] @@ -31,6 +31,11 @@ enum Lang { action: Option, param: Option, }, + #[clap(about = "maven management")] + Maven { + #[clap(help = "the maven action to be performed")] + action: Option, + }, } #[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] @@ -53,6 +58,14 @@ enum Command { Deactivate, } +#[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] +enum MavenCommand { + #[clap(help = "install maven to system")] + Install, + #[clap(help = "uninstall maven from system")] + Uninstall, +} + impl std::str::FromStr for Command { type Err = String; @@ -66,7 +79,19 @@ impl std::str::FromStr for Command { "usev" => Ok(Command::Usev), "deactivate" => Ok(Command::Deactivate), "clean" => Ok(Command::Clean), - _ => Err(format!("Invalid Java action: {}", s)), + _ => Err(format!("invalid Java action: {}", s)), + } + } +} + +impl std::str::FromStr for MavenCommand { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "install" => Ok(MavenCommand::Install), + "uninstall" => Ok(MavenCommand::Uninstall), + _ => Err(format!("invalid Java action: {}", s)), } } } @@ -99,10 +124,22 @@ async fn handle_java_action(action: Option, param: Option) { } } +async fn handle_maven_action(action: Option) { + if let Some(action) = action { + match action { + MavenCommand::Install => maven::install::install(), + MavenCommand::Uninstall => maven::uninstall::uninstall() + } + } else { + println!("enter valid action, for more details use --help or -h"); + } +} + async fn logic(running: Arc) { while running.load(Ordering::Relaxed) { match Cli::parse().cmd { Lang::Java { action, param } => handle_java_action(action, param).await, + Lang::Maven { action } => handle_maven_action(action).await , } // the below step is important to prevent infinite loop on failure diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 21753ee..5df561b 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -60,6 +60,14 @@ pub fn check_jdk_exists(name: &str) -> bool { } } +pub fn check_maven_exists() -> bool { + let m_path = format!("{}/.jvem/maven/bin", get_home_dir()); + match Path::new(&m_path).exists() { + true => true, + false => false, + } +} + pub fn check_path_exists(path: &str) -> bool { match Path::new(&path).exists() { true => true, @@ -115,20 +123,32 @@ pub fn clean_jvem() { } } -pub fn extract_tarball_linux(name: String) { +pub fn extract_tarball_linux(name: String, command: String) { + let tar_location = match command.as_str() { + "java" => &find_file_in_dir("/tmp", &name), + "maven" => "/tmp/maven.tar.gz", + _ => "", + }; + + let ext_location = match command.as_str() { + "java" => &format!("{}/.jvem/java_versions/{}", get_home_dir(), name), + "maven" => &format!("{}/.jvem/maven", get_home_dir()), + _ => "", + }; + let tarball_status = run_command( "/usr/bin/tar", vec![ "xvzf", - &find_file_in_dir("/tmp/", &name), + tar_location, "--strip-components=1", "-C", - &format!("{}/.jvem/java_versions/{}", get_home_dir(), name), + ext_location ], ); if tarball_status.status.success() { - println!("tarball extraction successful "); + println!("tarball extraction successful"); } else { println!( "tarball extraction failed: {:?} ", From 1d25366b5c244db6ed401320c6c233c767d782e6 Mon Sep 17 00:00:00 2001 From: anusikh Date: Thu, 4 Jul 2024 23:39:20 +0530 Subject: [PATCH 04/24] import fixes --- src/commands/maven/install.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs index 9d0388e..dcc2195 100644 --- a/src/commands/maven/install.rs +++ b/src/commands/maven/install.rs @@ -1,6 +1,8 @@ use lazy_static::lazy_static; -use crate::utils::file_utils::{check_maven_exists, find_file_in_dir, run_command}; +use crate::utils::file_utils::{ + check_maven_exists, extract_tarball_linux, find_file_in_dir, get_home_dir, run_command, +}; lazy_static! { static ref MAVEN_DOWN_URL: String = String::from( @@ -10,8 +12,6 @@ lazy_static! { #[cfg(target_os = "linux")] fn install_util() { - use crate::utils::file_utils::{extract_tarball_linux, get_home_dir}; - match check_maven_exists() { false => { let maven_path = format!("{}/.jvem/maven", get_home_dir()); From c2d9578d642eaf867f6ec0dfe9a2b566461d1ed2 Mon Sep 17 00:00:00 2001 From: anusikh Date: Fri, 5 Jul 2024 01:09:52 +0530 Subject: [PATCH 05/24] added maven manag. for windows --- src/commands/java/install.rs | 6 ++--- src/commands/maven/install.rs | 49 +++++++++++++++++++++++++++++++++-- src/utils/file_utils.rs | 20 ++++++++++---- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/commands/java/install.rs b/src/commands/java/install.rs index 531a8a3..72f1e19 100644 --- a/src/commands/java/install.rs +++ b/src/commands/java/install.rs @@ -17,7 +17,7 @@ fn install_util(name: String, link: String) { if std::path::Path::new(&temp_directory).exists() { println!("fetching tarball from cache successful"); - extract_zip(&temp_directory, &name); + extract_zip(&temp_directory, &name, String::from("java")); } else { println!("fetching zip..."); let output = run_command( @@ -29,13 +29,13 @@ fn install_util(name: String, link: String) { "-outf", &temp_directory, "-Uri", - &format!("{}", link), + &link ], ); if output.status.success() { println!("fetching zip successful "); - extract_zip(&temp_directory, &name); + extract_zip(&temp_directory, &name, String::from("java")); } else { println!( "fetching zip failed: {} ", diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs index dcc2195..46f0820 100644 --- a/src/commands/maven/install.rs +++ b/src/commands/maven/install.rs @@ -2,20 +2,65 @@ use lazy_static::lazy_static; use crate::utils::file_utils::{ check_maven_exists, extract_tarball_linux, find_file_in_dir, get_home_dir, run_command, + create_maven_dir }; lazy_static! { static ref MAVEN_DOWN_URL: String = String::from( "https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz" ); + static ref MAVEN_DOWN_URL_WIN: String = String::from( + "https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.zip" + ); +} + +#[cfg(target_os = "windows")] +fn install_util() { + use crate::utils::file_utils::extract_zip; + + match check_maven_exists() { + false => { + create_maven_dir(); + let temp_directory = format!("{}/AppData/Local/Temp/maven.zip", get_home_dir()); + if std::path::Path::new(&temp_directory).exists() { + println!("fetched maven zip from cache"); + extract_zip(&temp_directory, "", String::from("maven")); + }else { + println!("fetching zip..."); + + let output = run_command( + "powershell", + vec![ + "-Command", + "Set-Variable ProgressPreference SilentlyContinue ;", + "Invoke-WebRequest", + "-outf", + &temp_directory, + "-Uri", + &MAVEN_DOWN_URL_WIN + ], + ); + + if output.status.success() { + println!("fetching zip successful "); + extract_zip(&temp_directory, "", String::from("maven")); + } else { + println!( + "fetching zip failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } + }, + true => println!("maven already exists, if it doesn't please run the uninstall command and try re-installing it"), + } } #[cfg(target_os = "linux")] fn install_util() { match check_maven_exists() { false => { - let maven_path = format!("{}/.jvem/maven", get_home_dir()); - std::fs::create_dir_all(maven_path).unwrap(); + create_maven_dir(); let x = find_file_in_dir("/tmp/", "maven"); if x.ends_with(".gz") { diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 5df561b..ec0e931 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -80,6 +80,11 @@ pub fn create_java_dir(name: &str) { fs::create_dir_all(new_dir_path).unwrap(); } +pub fn create_maven_dir() { + let new_dir = format!("{}/.jvem/maven", *HOME_DIR); + fs::create_dir_all(new_dir).unwrap(); +} + pub fn check_list_locally() { let jvem_dir = format!("{}/.jvem/java_versions/", get_home_dir()); let path_dir = Path::new(&jvem_dir); @@ -143,7 +148,7 @@ pub fn extract_tarball_linux(name: String, command: String) { tar_location, "--strip-components=1", "-C", - ext_location + ext_location, ], ); @@ -209,15 +214,20 @@ pub fn extract_tarball_macos(name: &str) { } } -pub fn extract_zip(temp_dir: &str, name: &str) { +pub fn extract_zip(temp_dir: &str, name: &str, command: String) { + let output_path = match command.as_str() { + "java" => &get_installation_dir(name), + "maven" => &format!("{}/.jvem/maven", get_home_dir()), + _ => "", + }; + let unzip_output = run_command( "powershell", vec![ "-Command", &format!( - "Expand-Archive -Path {0} -DestinationPath {1}; mv {1}\\*\\* {1}", - temp_dir, - get_installation_dir(name), + "$ProgressPreference = 'SilentlyContinue';Expand-Archive -Path {0} -DestinationPath {1}; mv {1}\\*\\* {1};$ProgressPreference = 'Continue'", + temp_dir, output_path, ), ], ); From f81cb50a93e948b82d39d67fb2f321f47d42970c Mon Sep 17 00:00:00 2001 From: anusikh Date: Fri, 5 Jul 2024 12:11:07 +0530 Subject: [PATCH 06/24] added maven support macos --- src/commands/java/install.rs | 11 +++---- src/commands/maven/install.rs | 40 +++++++++++++++++++++--- src/utils/file_utils.rs | 59 ++++++++++++++++++++--------------- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/src/commands/java/install.rs b/src/commands/java/install.rs index 72f1e19..649e9a0 100644 --- a/src/commands/java/install.rs +++ b/src/commands/java/install.rs @@ -29,7 +29,7 @@ fn install_util(name: String, link: String) { "-outf", &temp_directory, "-Uri", - &link + &link, ], ); @@ -92,17 +92,14 @@ fn install_util(name: String, link: String) { if std::path::Path::new(&temp_directory).exists() { println!("fetching tarball from cache successful"); - extract_tarball_macos(&name); + extract_tarball_macos(&name, "java"); } else { println!("fetching tarball..."); - let output = run_command( - "/usr/bin/curl", - vec!["-o", &temp_directory, &format!("{}", link)], - ); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); if output.status.success() { println!("fetching tarball successful "); - extract_tarball_macos(&name); + extract_tarball_macos(&name, "java"); } else { println!( "fetching tarball failed: {} ", diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs index 46f0820..edccfa8 100644 --- a/src/commands/maven/install.rs +++ b/src/commands/maven/install.rs @@ -1,8 +1,8 @@ use lazy_static::lazy_static; use crate::utils::file_utils::{ - check_maven_exists, extract_tarball_linux, find_file_in_dir, get_home_dir, run_command, - create_maven_dir + check_maven_exists, create_maven_dir, extract_tarball_linux, find_file_in_dir, get_home_dir, + run_command, }; lazy_static! { @@ -27,7 +27,6 @@ fn install_util() { extract_zip(&temp_directory, "", String::from("maven")); }else { println!("fetching zip..."); - let output = run_command( "powershell", vec![ @@ -37,7 +36,7 @@ fn install_util() { "-outf", &temp_directory, "-Uri", - &MAVEN_DOWN_URL_WIN + &MAVEN_DOWN_URL_WIN ], ); @@ -82,6 +81,39 @@ fn install_util() { } } +#[cfg(target_os = "macos")] +fn install_util() { + use crate::utils::file_utils::extract_tarball_macos; + + match check_maven_exists() { + false => { + create_maven_dir(); + let temp_directory = "/tmp/maven.tar.gz"; + if std::path::Path::new(temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_macos("", "maven"); + } else { + println!("fetching tarball"); + let output = run_command( + "/usr/bin/curl", + vec!["-o", temp_directory,&MAVEN_DOWN_URL] + ); + + if output.status.success() { + println!("fetching tarball successful"); + extract_tarball_macos("", "maven"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } + }, + true => println!("maven already exists, if it doesn't please run the uninstall command and try re-installing it"), + } +} + pub fn install() { let _ = install_util(); } diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index ec0e931..6948b8f 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -162,44 +162,51 @@ pub fn extract_tarball_linux(name: String, command: String) { } } -pub fn extract_tarball_macos(name: &str) { +pub fn extract_tarball_macos(name: &str, command: &str) { let temp_folder_name = format!("{}", rand::thread_rng().gen::()); + let tar_path = match command { + "java" => &find_file_in_dir("/tmp/", &name), + "maven" => "/tmp/maven.tar.gz", + _ => "", + }; + + let output_path = match command { + "java" => &format!("/tmp/{}", temp_folder_name), + "maven" => &format!("{}/.jvem/maven", get_home_dir()), + _ => "", + }; + let res = fs::create_dir_all(format!("/tmp/{}", temp_folder_name)); match res { Ok(_) => { let tarball_status = run_command( "/usr/bin/tar", - vec![ - "xvzf", - &find_file_in_dir("/tmp/", &name), - "--strip-components=1", - "-C", - &format!("/tmp/{}", temp_folder_name), - ], + vec!["xvzf", tar_path, "--strip-components=1", "-C", output_path], ); if tarball_status.status.success() { println!("tarball extraction successful, trying to move some files around..."); - let mv_status = run_command( - "sh", - vec![ - "-c", - &format!( - "mv $(find /tmp/{} -mindepth 1 -maxdepth 1 -type d | head -n 1)/* {}/.jvem/java_versions/{}", + if command.eq("java") { + let mv_status = run_command( + "sh", + vec![ + "-c", + &format!( + "mv $(find /tmp/{} -mindepth 1 -maxdepth 1 -type d | head -n 1)/* {}", temp_folder_name, - get_home_dir(), - name - ) - ], - ); - - if mv_status.status.success() { - println!("done moving files around..."); - } else { - println!( - "moving files failed: {:?} ", - String::from_utf8_lossy(&mv_status.stderr) + &format!("{}/.jvem/java_versions/{}", get_home_dir(), name) + ), + ], ); + + if mv_status.status.success() { + println!("done moving files around..."); + } else { + println!( + "moving files failed: {:?} ", + String::from_utf8_lossy(&mv_status.stderr) + ); + } } } else { println!( From e839c1cfddd93ce1879ee0357922643a8bc3ef03 Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 7 Jul 2024 01:32:47 +0530 Subject: [PATCH 07/24] general refactor --- src/commands/java/install.rs | 8 ++++---- src/commands/maven/install.rs | 14 ++++++-------- src/utils/file_utils.rs | 14 +++++++------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/commands/java/install.rs b/src/commands/java/install.rs index 649e9a0..625cd10 100644 --- a/src/commands/java/install.rs +++ b/src/commands/java/install.rs @@ -17,7 +17,7 @@ fn install_util(name: String, link: String) { if std::path::Path::new(&temp_directory).exists() { println!("fetching tarball from cache successful"); - extract_zip(&temp_directory, &name, String::from("java")); + extract_zip(&temp_directory, &name, "java"); } else { println!("fetching zip..."); let output = run_command( @@ -35,7 +35,7 @@ fn install_util(name: String, link: String) { if output.status.success() { println!("fetching zip successful "); - extract_zip(&temp_directory, &name, String::from("java")); + extract_zip(&temp_directory, &name, "java"); } else { println!( "fetching zip failed: {} ", @@ -60,7 +60,7 @@ fn install_util(name: String, link: String) { if x.ends_with(".gz") { println!("fetching tarball from cache successful"); - extract_tarball_linux(name, String::from("java")); + extract_tarball_linux(&name, "java"); } else { let output = run_command( "/usr/bin/wget", @@ -72,7 +72,7 @@ fn install_util(name: String, link: String) { ); if output.status.success() { println!("fetching tarball successful "); - extract_tarball_linux(name, String::from("java")); + extract_tarball_linux(&name, "java"); } else { println!("fetching tarball failed "); } diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs index edccfa8..4d35ab2 100644 --- a/src/commands/maven/install.rs +++ b/src/commands/maven/install.rs @@ -1,8 +1,8 @@ use lazy_static::lazy_static; use crate::utils::file_utils::{ - check_maven_exists, create_maven_dir, extract_tarball_linux, find_file_in_dir, get_home_dir, - run_command, + check_maven_exists, create_maven_dir, extract_tarball_linux, extract_tarball_macos, + find_file_in_dir, get_home_dir, run_command, }; lazy_static! { @@ -24,7 +24,7 @@ fn install_util() { let temp_directory = format!("{}/AppData/Local/Temp/maven.zip", get_home_dir()); if std::path::Path::new(&temp_directory).exists() { println!("fetched maven zip from cache"); - extract_zip(&temp_directory, "", String::from("maven")); + extract_zip(&temp_directory, "", "maven"); }else { println!("fetching zip..."); let output = run_command( @@ -42,7 +42,7 @@ fn install_util() { if output.status.success() { println!("fetching zip successful "); - extract_zip(&temp_directory, "", String::from("maven")); + extract_zip(&temp_directory, "", "maven"); } else { println!( "fetching zip failed: {} ", @@ -64,14 +64,14 @@ fn install_util() { if x.ends_with(".gz") { println!("fetching maven from cache successful"); - extract_tarball_linux(String::new(), String::from("maven")); + extract_tarball_linux("", "maven"); } else { let download = run_command("/usr/bin/wget", vec![&MAVEN_DOWN_URL, "-O", "/tmp/maven.tar.gz"]); if download.status.success() { println!("maven download successful"); - extract_tarball_linux(String::new(), String::from("maven")); + extract_tarball_linux("", "maven"); } else { println!("fetching maven failed"); } @@ -83,8 +83,6 @@ fn install_util() { #[cfg(target_os = "macos")] fn install_util() { - use crate::utils::file_utils::extract_tarball_macos; - match check_maven_exists() { false => { create_maven_dir(); diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 6948b8f..e353cad 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -128,15 +128,15 @@ pub fn clean_jvem() { } } -pub fn extract_tarball_linux(name: String, command: String) { - let tar_location = match command.as_str() { - "java" => &find_file_in_dir("/tmp", &name), +pub fn extract_tarball_linux(name: &str, command: &str) { + let tar_location = match command { + "java" => &find_file_in_dir("/tmp", name), "maven" => "/tmp/maven.tar.gz", _ => "", }; - let ext_location = match command.as_str() { - "java" => &format!("{}/.jvem/java_versions/{}", get_home_dir(), name), + let ext_location = match command { + "java" => &format!("{}/.jvem/java_versions/{}", get_home_dir(), String::from(name)), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; @@ -221,8 +221,8 @@ pub fn extract_tarball_macos(name: &str, command: &str) { } } -pub fn extract_zip(temp_dir: &str, name: &str, command: String) { - let output_path = match command.as_str() { +pub fn extract_zip(temp_dir: &str, name: &str, command: &str) { + let output_path = match command { "java" => &get_installation_dir(name), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", From 6233692bae681b981206fb112f4577f9820e69e5 Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 7 Jul 2024 05:45:15 +0530 Subject: [PATCH 08/24] (node) added commands for node, except install, uninstall and general refactor --- src/commands/java/clean.rs | 2 +- src/commands/java/current.rs | 15 +++--- src/commands/java/ls.rs | 2 +- src/commands/java/lsrem.rs | 7 +-- src/commands/java/uninstall.rs | 2 - src/commands/java/usev.rs | 6 +-- src/commands/mod.rs | 1 + src/commands/node/clean.rs | 5 ++ src/commands/node/current.rs | 22 +++++++++ src/commands/node/deactivate.rs | 41 ++++++++++++++++ src/commands/node/ls.rs | 5 ++ src/commands/node/lsrem.rs | 5 ++ src/commands/node/mod.rs | 5 ++ src/main.rs | 84 ++++++++++++++++++++++++++++----- src/utils/env_ops.rs | 74 ++++++++++++++++++++++++++++- src/utils/file_utils.rs | 74 ++++++++++++++++++----------- 16 files changed, 288 insertions(+), 62 deletions(-) create mode 100644 src/commands/node/clean.rs create mode 100644 src/commands/node/current.rs create mode 100644 src/commands/node/deactivate.rs create mode 100644 src/commands/node/ls.rs create mode 100644 src/commands/node/lsrem.rs create mode 100644 src/commands/node/mod.rs diff --git a/src/commands/java/clean.rs b/src/commands/java/clean.rs index 55c5a40..7c5a729 100644 --- a/src/commands/java/clean.rs +++ b/src/commands/java/clean.rs @@ -1,5 +1,5 @@ use crate::utils::file_utils::clean_jvem; pub fn clean() { - let _ = clean_jvem(); + let _ = clean_jvem("java"); } diff --git a/src/commands/java/current.rs b/src/commands/java/current.rs index 8a7cc9e..9e37669 100644 --- a/src/commands/java/current.rs +++ b/src/commands/java/current.rs @@ -1,13 +1,10 @@ -use std::process::Command; +use crate::utils::file_utils::run_command; pub fn current() { - let output_res = Command::new("java").args(vec!["--version"]).output(); - match output_res { - Ok(output) => { - println!("{}", String::from_utf8_lossy(&output.stdout)); - } - Err(_) => { - println!("failed: jdk not set"); - } + let res = run_command("java", vec!["--version"]); + if res.status.success() { + println!("node version: {}", String::from_utf8_lossy(&res.stdout)); + } else { + println!("failed: node not set"); } } diff --git a/src/commands/java/ls.rs b/src/commands/java/ls.rs index dd9578b..e9ae5db 100644 --- a/src/commands/java/ls.rs +++ b/src/commands/java/ls.rs @@ -1,5 +1,5 @@ use crate::utils::file_utils::check_list_locally; pub fn ls() { - let _ = check_list_locally(); + let _ = check_list_locally("java"); } diff --git a/src/commands/java/lsrem.rs b/src/commands/java/lsrem.rs index 6f48f03..92aec14 100644 --- a/src/commands/java/lsrem.rs +++ b/src/commands/java/lsrem.rs @@ -1,8 +1,5 @@ -use std::error::Error; - use crate::utils::env_ops::read_versions; -pub fn lsrem() -> Result<(), Box> { - read_versions()?; - Ok(()) +pub fn lsrem() { + let _ = read_versions(); } diff --git a/src/commands/java/uninstall.rs b/src/commands/java/uninstall.rs index 66c7159..4a281b3 100644 --- a/src/commands/java/uninstall.rs +++ b/src/commands/java/uninstall.rs @@ -31,8 +31,6 @@ fn uninstall_util(name: &str) { #[cfg(target_os = "macos")] fn uninstall_util(name: &str) { - // ToDo - let output = run_command( "rm", vec!["-rf", &format!("{}", get_installation_dir(&name))], diff --git a/src/commands/java/usev.rs b/src/commands/java/usev.rs index 5e6a6b5..2dbde41 100644 --- a/src/commands/java/usev.rs +++ b/src/commands/java/usev.rs @@ -1,4 +1,6 @@ -use crate::utils::file_utils::{check_jdk_exists, get_home_dir, get_installation_dir, run_command}; +use crate::utils::file_utils::{ + check_jdk_exists, check_path_exists, get_home_dir, get_installation_dir, run_command, +}; #[cfg(target_os = "windows")] async fn usev_util(name: String) { @@ -86,8 +88,6 @@ pub async fn usev_util(name: String) { #[cfg(target_os = "macos")] pub async fn usev_util(name: String) { - use crate::utils::file_utils::check_path_exists; - let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); // check if Contents folder present inside extracted files (required for Graal VM support) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 85fd9aa..04f83c6 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod java; pub mod maven; +pub mod node; diff --git a/src/commands/node/clean.rs b/src/commands/node/clean.rs new file mode 100644 index 0000000..f4e268e --- /dev/null +++ b/src/commands/node/clean.rs @@ -0,0 +1,5 @@ +use crate::utils::file_utils::clean_jvem; + +pub fn clean() { + let _ = clean_jvem("node"); +} diff --git a/src/commands/node/current.rs b/src/commands/node/current.rs new file mode 100644 index 0000000..e84e165 --- /dev/null +++ b/src/commands/node/current.rs @@ -0,0 +1,22 @@ +use crate::utils::file_utils::run_command; + +pub async fn current() { + let node_task = tokio::spawn(async { + let res = run_command("node", vec!["--version"]); + if res.status.success() { + println!("node version: {}", String::from_utf8_lossy(&res.stdout)); + } else { + println!("failed: node not set"); + } + }); + let npm_task = tokio::spawn(async { + let res = run_command("npm", vec!["--version"]); + if res.status.success() { + println!("npm version: {}", String::from_utf8_lossy(&res.stdout)); + } else { + println!("failed: npm not set"); + } + }); + + let _ = tokio::join!(node_task, npm_task); +} diff --git a/src/commands/node/deactivate.rs b/src/commands/node/deactivate.rs new file mode 100644 index 0000000..f667d2f --- /dev/null +++ b/src/commands/node/deactivate.rs @@ -0,0 +1,41 @@ +use crate::utils::file_utils::{get_home_dir, run_command}; + +#[cfg(target_os = "windows")] +fn deactivate_util() { + let output = run_command( + "powershell", + vec![ + "-Command", + &format!("rm -r {}\\.jvem\\node", get_home_dir()), + ], + ); + if output.status.success() { + println!("deactivation successful"); + } else { + println!("deactivation failed"); + } +} + +#[cfg(target_os = "linux")] +fn deactivate_util() { + let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + if output.status.success() { + println!("deactivation successful "); + } else { + println!("deactivation failed"); + } +} + +#[cfg(target_os = "macos")] +fn deactivate_util() { + let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + if output.status.success() { + println!("deactivation successful"); + } else { + println!("deactivation failed"); + } +} + +pub fn deactivate() { + let _ = deactivate_util(); +} diff --git a/src/commands/node/ls.rs b/src/commands/node/ls.rs new file mode 100644 index 0000000..88f08a8 --- /dev/null +++ b/src/commands/node/ls.rs @@ -0,0 +1,5 @@ +use crate::utils::file_utils::check_list_locally; + +pub fn ls() { + let _ = check_list_locally("node"); +} diff --git a/src/commands/node/lsrem.rs b/src/commands/node/lsrem.rs new file mode 100644 index 0000000..238ddeb --- /dev/null +++ b/src/commands/node/lsrem.rs @@ -0,0 +1,5 @@ +use crate::utils::env_ops::read_versions_nodejs; + +pub fn lsrem() { + let _ = read_versions_nodejs(); +} diff --git a/src/commands/node/mod.rs b/src/commands/node/mod.rs new file mode 100644 index 0000000..1a81017 --- /dev/null +++ b/src/commands/node/mod.rs @@ -0,0 +1,5 @@ +pub mod lsrem; +pub mod ls; +pub mod clean; +pub mod current; +pub mod deactivate; diff --git a/src/main.rs b/src/main.rs index 80048a1..1551f3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,13 @@ use std::sync::{ }; use clap::{Parser, Subcommand, ValueEnum}; -use commands::{java::{ - clean::clean, current::current, deactivate::deactivate, install::install, ls::ls, lsrem::lsrem, - uninstall::uninstall, usev::usev, -}, maven}; +use commands::{ + java::{ + clean::clean, current::current, deactivate::deactivate, install::install, ls::ls, + lsrem::lsrem, uninstall::uninstall, usev::usev, + }, + maven, node, +}; use tokio::signal; #[derive(Parser, Debug)] @@ -36,6 +39,11 @@ enum Lang { #[clap(help = "the maven action to be performed")] action: Option, }, + #[clap(about = "node version management")] + Node { + action: Option, + param: Option, + }, } #[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] @@ -46,7 +54,7 @@ enum Command { Uninstall, #[clap(help = "use a specific JDK version after installation")] Usev, - #[clap(help = "clean empty folders in the .jvem directory")] + #[clap(help = "clean empty folders in the .jvem/java_versions directory")] Clean, #[clap(help = "find the currently active JDK version")] Current, @@ -58,6 +66,26 @@ enum Command { Deactivate, } +#[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] +enum NodeCommand { + #[clap(help = "install the specific node version")] + Install, + #[clap(help = "uninstall the specified node version")] + Uninstall, + #[clap(help = "use a specific node version after installation")] + Usev, + #[clap(help = "clean empty folders in the .jvem/node_versions directory")] + Clean, + #[clap(help = "find the currently active node version")] + Current, + #[clap(help = "list all node versions available for install")] + Lsrem, + #[clap(help = "list locally installed node versions")] + Ls, + #[clap(help = "deactivate the currently active node version")] + Deactivate, +} + #[derive(Copy, Debug, Clone, PartialEq, Eq, ValueEnum)] enum MavenCommand { #[clap(help = "install maven to system")] @@ -84,6 +112,24 @@ impl std::str::FromStr for Command { } } +impl std::str::FromStr for NodeCommand { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "install" => Ok(NodeCommand::Install), + "lsrem" => Ok(NodeCommand::Lsrem), + "ls" => Ok(NodeCommand::Ls), + "current" => Ok(NodeCommand::Current), + "uninstall" => Ok(NodeCommand::Uninstall), + "usev" => Ok(NodeCommand::Usev), + "deactivate" => Ok(NodeCommand::Deactivate), + "clean" => Ok(NodeCommand::Clean), + _ => Err(format!("invalid Node action: {}", s)), + } + } +} + impl std::str::FromStr for MavenCommand { type Err = String; @@ -91,7 +137,7 @@ impl std::str::FromStr for MavenCommand { match s.to_lowercase().as_str() { "install" => Ok(MavenCommand::Install), "uninstall" => Ok(MavenCommand::Uninstall), - _ => Err(format!("invalid Java action: {}", s)), + _ => Err(format!("invalid Maven action: {}", s)), } } } @@ -112,9 +158,7 @@ async fn handle_java_action(action: Option, param: Option) { let jdk = param.unwrap(); usev(jdk).await; } - Command::Lsrem => { - let _ = lsrem(); - } + Command::Lsrem => lsrem(), Command::Ls => ls(), Command::Deactivate => deactivate(), Command::Clean => clean(), @@ -128,7 +172,22 @@ async fn handle_maven_action(action: Option) { if let Some(action) = action { match action { MavenCommand::Install => maven::install::install(), - MavenCommand::Uninstall => maven::uninstall::uninstall() + MavenCommand::Uninstall => maven::uninstall::uninstall(), + } + } else { + println!("enter valid action, for more details use --help or -h"); + } +} + +async fn handle_nodejs_action(action: Option, param: Option) { + if let Some(action) = action { + match action { + NodeCommand::Lsrem => node::lsrem::lsrem(), + NodeCommand::Ls => node::ls::ls(), + NodeCommand::Clean => node::clean::clean(), + NodeCommand::Current => node::current::current().await, + NodeCommand::Deactivate => node::deactivate::deactivate(), + _ => println!("not implemented yet"), } } else { println!("enter valid action, for more details use --help or -h"); @@ -139,7 +198,8 @@ async fn logic(running: Arc) { while running.load(Ordering::Relaxed) { match Cli::parse().cmd { Lang::Java { action, param } => handle_java_action(action, param).await, - Lang::Maven { action } => handle_maven_action(action).await , + Lang::Maven { action } => handle_maven_action(action).await, + Lang::Node { action, param } => handle_nodejs_action(action, param).await, } // the below step is important to prevent infinite loop on failure @@ -149,7 +209,7 @@ async fn logic(running: Arc) { #[tokio::main] async fn main() { - // the below logic is used for ctrl+c handling + // NOTE: the below logic is used for ctrl+c handling // the idea here is to set a boolean atomic value, when program is running // on ctrl+c, the value is set to false and it stops the process diff --git a/src/utils/env_ops.rs b/src/utils/env_ops.rs index 83a7c2c..7e5ca90 100644 --- a/src/utils/env_ops.rs +++ b/src/utils/env_ops.rs @@ -1,4 +1,6 @@ -use crate::constants::java_versions::constants; +use core::panic; + +use crate::{constants::java_versions::constants, utils::file_utils::run_command}; pub fn read_versions() -> Result<(), Box> { let available_versions = constants::AVAILABLE_VERSIONS; @@ -35,3 +37,73 @@ pub fn get_download_link( ) } } + +pub fn read_versions_nodejs() -> Result<(), Box> { + let output = run_command("curl", vec!["https://nodejs.org/dist/"]); + let mut curr = 16; + let mut display_vec = Vec::new(); + if output.status.success() { + let body = String::from_utf8_lossy(&output.stdout); + for line in body.lines() { + if line.contains(">v") && line.contains("/<") { + if let Some(version) = parse_version(&line) { + let num_ver: u32 = version.split('.').next().unwrap().parse().unwrap(); + // NOTE: We are supporting versions above 16 + if num_ver >= 16 { + if !num_ver.eq(&curr) { + println!("nodejs v{}", curr.to_string()); + println!("{:?}", display_vec); + curr = num_ver; + display_vec.clear(); + } + display_vec.push(version); + } + } + } + } + } else { + println!("couldn't connect to nodejs.org: {}", String::from_utf8_lossy(&output.stderr)); + } + + Ok(()) +} + +fn parse_version(line: &str) -> Option<&str> { + let start = line.find(">v")? + 2; + let end = line.find("/<")?; + line.get(start..end) +} + +pub fn get_download_link_nodejs(version: &str, os: &str, arch: &str) { + let os_mapped = match os { + "windows" => "win", + "macos" => "darwin", + "linux" => "linux", + _ => panic!("unsupported operating system"), + }; + + let arch_mapped = match arch { + "aarch64" => "arm64", + "x86_64" => "x64", + _ => panic!("unsupported architecture"), + }; + + let format_mapped = match os { + "linux" | "macos" => "tar.gz", + "windows" => "zip", + _ => panic!("unsupported operating system"), + }; + + let x: Vec<&str> = version.split(".").collect(); + + let link = format!( + "https://nodejs.org/dist/latest-v{}.x/node-v{}-{}-{}.{}", + x.get(0).unwrap(), + version, + os_mapped, + arch_mapped, + format_mapped + ); + + println!("{}", link); +} diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index e353cad..464e863 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -85,30 +85,37 @@ pub fn create_maven_dir() { fs::create_dir_all(new_dir).unwrap(); } -pub fn check_list_locally() { - let jvem_dir = format!("{}/.jvem/java_versions/", get_home_dir()); - let path_dir = Path::new(&jvem_dir); - match path_dir.exists() { - true => { - let mut res: Vec = Vec::new(); - - for entry in fs::read_dir(path_dir).unwrap() { - let entry = entry.unwrap(); - // .DS_Store is macos specific - if entry.file_name() != "java" && entry.file_name() != ".DS_Store" { - res.push(String::from(entry.file_name().to_str().unwrap())); +pub fn check_list_locally(command: &str) { + let dir = match command { + "java" => "java_versions", + "node" => "node_versions", + _ => "", + }; + if !dir.is_empty() { + let jvem_dir = format!("{}/.jvem/{}/", get_home_dir(), dir); + let path_dir = Path::new(&jvem_dir); + match path_dir.exists() { + true => { + let mut res: Vec = Vec::new(); + + for entry in fs::read_dir(path_dir).unwrap() { + let entry = entry.unwrap(); + // .DS_Store is macos specific + if entry.file_name() != ".DS_Store" { + res.push(String::from(entry.file_name().to_str().unwrap())); + } } - } - if res.len() > 0 { - for item in res { - println!("{}", item); + if res.len() > 0 { + for item in res { + println!("{}", item); + } + } else { + println!("no installations found locally"); } - } else { - println!("no jdk installations found locally"); } - } - false => { - println!("no jdk installations found locally"); + false => { + println!("no installations found locally"); + } } } } @@ -117,13 +124,20 @@ pub fn is_empty_dir(path: &std::path::Path) -> io::Result { Ok(fs::read_dir(path)?.next().is_none()) } -pub fn clean_jvem() { - for entry in fs::read_dir(format!("{}/.jvem/java_versions", get_home_dir())).unwrap() { - let entry = entry.unwrap(); - let path = entry.path(); +pub fn clean_jvem(command: &str) { + let dir = match command { + "java" => "java_versions", + "node" => "node_versions", + _ => "", + }; + if !dir.is_empty() { + for entry in fs::read_dir(format!("{}/.jvem/{}", get_home_dir(), dir)).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); - if path.is_dir() && is_empty_dir(&path).unwrap() { - fs::remove_dir(&path).unwrap(); + if path.is_dir() && is_empty_dir(&path).unwrap() { + fs::remove_dir(&path).unwrap(); + } } } } @@ -136,7 +150,11 @@ pub fn extract_tarball_linux(name: &str, command: &str) { }; let ext_location = match command { - "java" => &format!("{}/.jvem/java_versions/{}", get_home_dir(), String::from(name)), + "java" => &format!( + "{}/.jvem/java_versions/{}", + get_home_dir(), + String::from(name) + ), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; From 23163b4f5356e3e9c51d1a6d3faf693928a0c69f Mon Sep 17 00:00:00 2001 From: anusikh Date: Tue, 9 Jul 2024 03:38:15 +0530 Subject: [PATCH 09/24] (node) all done, remaing install (windows, linux), removed serde, general refactor --- Cargo.lock | 21 -------- Cargo.toml | 1 - src/commands/java/current.rs | 2 +- src/commands/java/install.rs | 41 +++++++++------- src/commands/java/uninstall.rs | 6 +-- src/commands/java/usev.rs | 33 +++++++------ src/commands/node/install.rs | 35 +++++++++++++ src/commands/node/lsrem.rs | 4 +- src/commands/node/mod.rs | 3 ++ src/commands/node/uninstall.rs | 53 ++++++++++++++++++++ src/commands/node/usev.rs | 90 ++++++++++++++++++++++++++++++++++ src/main.rs | 37 +++++--------- src/utils/env_ops.rs | 21 ++++---- src/utils/file_utils.rs | 63 +++++++++++++++++++++--- 14 files changed, 305 insertions(+), 105 deletions(-) create mode 100644 src/commands/node/install.rs create mode 100644 src/commands/node/uninstall.rs create mode 100644 src/commands/node/usev.rs diff --git a/Cargo.lock b/Cargo.lock index 25bed99..af5f79e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,7 +192,6 @@ dependencies = [ "clap", "lazy_static", "rand", - "serde", "tokio", ] @@ -367,26 +366,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "serde" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/Cargo.toml b/Cargo.toml index 6193b97..e7d4fbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ edition = "2021" clap = { version = "4.5.1", features = ["derive"] } lazy_static = "1.4.0" rand = "0.8.5" -serde = { version = "1.0.197", features = ["serde_derive"] } tokio = { version = "1.36.0", features = ["full"] } [profile.release] diff --git a/src/commands/java/current.rs b/src/commands/java/current.rs index 9e37669..a84fee2 100644 --- a/src/commands/java/current.rs +++ b/src/commands/java/current.rs @@ -3,7 +3,7 @@ use crate::utils::file_utils::run_command; pub fn current() { let res = run_command("java", vec!["--version"]); if res.status.success() { - println!("node version: {}", String::from_utf8_lossy(&res.stdout)); + println!("java version: {}", String::from_utf8_lossy(&res.stdout)); } else { println!("failed: node not set"); } diff --git a/src/commands/java/install.rs b/src/commands/java/install.rs index 625cd10..da8bc13 100644 --- a/src/commands/java/install.rs +++ b/src/commands/java/install.rs @@ -86,25 +86,32 @@ fn install_util(name: String, link: String) { #[cfg(target_os = "macos")] fn install_util(name: String, link: String) { - create_java_dir(&name); + match check_jdk_exists(&name) { + false => { + create_java_dir(&name); - let temp_directory = format!("/tmp/{}.tar.gz", name); + let temp_directory = format!("/tmp/{}.tar.gz", name); - if std::path::Path::new(&temp_directory).exists() { - println!("fetching tarball from cache successful"); - extract_tarball_macos(&name, "java"); - } else { - println!("fetching tarball..."); - let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_macos(&name, "java"); + } else { + println!("fetching tarball..."); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); - if output.status.success() { - println!("fetching tarball successful "); - extract_tarball_macos(&name, "java"); - } else { - println!( - "fetching tarball failed: {} ", - String::from_utf8_lossy(&output.stderr) - ); + if output.status.success() { + println!("fetching tarball successful "); + extract_tarball_macos(&name, "java"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } + } + true => { + println!("jdk already exists in fs, if it doesn't run the clean command"); } } } @@ -117,7 +124,7 @@ pub fn install(name: String) { install_util(name, x); } Err(e) => { - println!("{} ", e.to_string()); + println!("{}", e.to_string()); } } } diff --git a/src/commands/java/uninstall.rs b/src/commands/java/uninstall.rs index 4a281b3..21db12f 100644 --- a/src/commands/java/uninstall.rs +++ b/src/commands/java/uninstall.rs @@ -6,7 +6,7 @@ fn uninstall_util(name: &str) { "powershell", vec![ "-Command", - &format!("rm -r {}", get_installation_dir(&name)), + &format!("rm -r {}", get_installation_dir(&name, "java")), ], ); if output.status.success() { @@ -20,7 +20,7 @@ fn uninstall_util(name: &str) { fn uninstall_util(name: &str) { let output = run_command( "rm", - vec!["-rf", &format!("{}", get_installation_dir(&name))], + vec!["-rf", &format!("{}", get_installation_dir(&name, "java"))], ); if output.status.success() { println!("uninstall successful "); @@ -33,7 +33,7 @@ fn uninstall_util(name: &str) { fn uninstall_util(name: &str) { let output = run_command( "rm", - vec!["-rf", &format!("{}", get_installation_dir(&name))], + vec!["-rf", &format!("{}", get_installation_dir(&name, "java"))], ); if output.status.success() { println!("uninstall successful "); diff --git a/src/commands/java/usev.rs b/src/commands/java/usev.rs index 2dbde41..ad11a28 100644 --- a/src/commands/java/usev.rs +++ b/src/commands/java/usev.rs @@ -4,7 +4,7 @@ use crate::utils::file_utils::{ #[cfg(target_os = "windows")] async fn usev_util(name: String) { - let java_path = get_installation_dir(&name); + let java_path = get_installation_dir(&name, "java"); let java_home_future = tokio::spawn(async move { println!("setting JAVA_HOME..."); @@ -14,7 +14,7 @@ async fn usev_util(name: String) { vec![ "-Command", &format!("[System.Environment]::SetEnvironmentVariable('JAVA_HOME','{}',[System.EnvironmentVariableTarget]::User) - ", get_installation_dir(&name)) + ", get_installation_dir(&name, "java")) ], ); @@ -57,13 +57,18 @@ async fn usev_util(name: String) { } }); - let _ = tokio::join!(alias_future, java_home_future); + let (alias_task, java_home_task) = tokio::join!(alias_future, java_home_future); - println!("set jdk version successfully"); + match (alias_task, java_home_task) { + (Ok(alias_task), Ok(java_home_task)) => { + println!("set jdk version successfully"); + } + _ => println!("something went wrong"), + } } #[cfg(target_os = "linux")] -pub async fn usev_util(name: String) { +async fn usev_util(name: String) { // remove the previously linked folder let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); @@ -73,7 +78,7 @@ pub async fn usev_util(name: String) { "-c", &format!( "ln --symbolic {} {}/.jvem/java", - get_installation_dir(&name), + get_installation_dir(&name, "java"), get_home_dir() ), ], @@ -87,18 +92,18 @@ pub async fn usev_util(name: String) { } #[cfg(target_os = "macos")] -pub async fn usev_util(name: String) { +async fn usev_util(name: String) { let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); // check if Contents folder present inside extracted files (required for Graal VM support) - let con_path = format!("{}/Contents", get_installation_dir(&name)); + let con_path = format!("{}/Contents", get_installation_dir(&name, "java")); let if_contents_exists = check_path_exists(&con_path); let final_path; if if_contents_exists == true { - final_path = format!("{}/Contents/Home", get_installation_dir(&name)); + final_path = format!("{}/Contents/Home", get_installation_dir(&name, "java")); } else { - final_path = format!("{}/Home", get_installation_dir(&name)); + final_path = format!("{}/Home", get_installation_dir(&name, "java")); }; let output = run_command( @@ -118,11 +123,7 @@ pub async fn usev_util(name: String) { pub async fn usev(name: String) { match check_jdk_exists(&name) { - true => { - usev_util(name).await; - } - false => { - println!("install the jdk first") - } + true => usev_util(name).await, + false => println!("install the jdk first"), } } diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs new file mode 100644 index 0000000..d10f668 --- /dev/null +++ b/src/commands/node/install.rs @@ -0,0 +1,35 @@ +use crate::utils::env_ops::get_download_link_node; +use crate::utils::file_utils::{create_node_dir, extract_tarball_macos, run_command}; + +#[cfg(target_os = "macos")] +fn install_util(version: String, link: String) { + create_node_dir(); + + let temp_directory = format!("/tmp/{}.tar.gz", version); + + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_macos(&version, "node"); + } else { + println!("fetching tarball..."); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); + + if output.status.success() { + println!("fetching tarball successful "); + extract_tarball_macos(&version, "node"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } +} + +pub fn install(version: String) { + let link = get_download_link_node(&version, std::env::consts::OS, std::env::consts::ARCH); + match link { + Ok(l) => install_util(version, l), + Err(e) => println!("{}", e.to_string()), + } +} diff --git a/src/commands/node/lsrem.rs b/src/commands/node/lsrem.rs index 238ddeb..5df4943 100644 --- a/src/commands/node/lsrem.rs +++ b/src/commands/node/lsrem.rs @@ -1,5 +1,5 @@ -use crate::utils::env_ops::read_versions_nodejs; +use crate::utils::env_ops::read_versions_node; pub fn lsrem() { - let _ = read_versions_nodejs(); + let _ = read_versions_node(); } diff --git a/src/commands/node/mod.rs b/src/commands/node/mod.rs index 1a81017..ad273ad 100644 --- a/src/commands/node/mod.rs +++ b/src/commands/node/mod.rs @@ -3,3 +3,6 @@ pub mod ls; pub mod clean; pub mod current; pub mod deactivate; +pub mod install; +pub mod uninstall; +pub mod usev; diff --git a/src/commands/node/uninstall.rs b/src/commands/node/uninstall.rs new file mode 100644 index 0000000..9b76011 --- /dev/null +++ b/src/commands/node/uninstall.rs @@ -0,0 +1,53 @@ +use crate::utils::file_utils::{get_installation_dir, run_command}; + +#[cfg(target_os = "windows")] +fn uninstall_util(version: &str) { + let output = run_command( + "powershell", + vec![ + "-Command", + &format!("rm -r {}", get_installation_dir(&version, "node")), + ], + ); + if output.status.success() { + println!("uninstall successful"); + } else { + println!("uninstall failed: maybe the mentioned node version is not installed locally"); + } +} + +#[cfg(target_os = "linux")] +fn uninstall_util(version: &str) { + let output = run_command( + "rm", + vec![ + "-rf", + &format!("{}", get_installation_dir(&version, "node")), + ], + ); + if output.status.success() { + println!("uninstall successful "); + } else { + println!("uninstall failed: maybe the mentioned node version is not installed locally"); + } +} + +#[cfg(target_os = "macos")] +fn uninstall_util(version: &str) { + let output = run_command( + "rm", + vec![ + "-rf", + &format!("{}", get_installation_dir(&version, "node")), + ], + ); + if output.status.success() { + println!("uninstall successful "); + } else { + println!("uninstall failed: maybe the mentioned node version is not installed locally"); + } +} + +pub fn uninstall(version: String) { + let _ = uninstall_util(&version); +} diff --git a/src/commands/node/usev.rs b/src/commands/node/usev.rs new file mode 100644 index 0000000..e74e7ff --- /dev/null +++ b/src/commands/node/usev.rs @@ -0,0 +1,90 @@ +use crate::utils::file_utils::{ + check_node_exists, get_home_dir, get_installation_dir, run_command, +}; + +#[cfg(target_os = "windows")] +fn usev_util(version: String) { + let node_path = get_installation_dir(&name, "node"); + + println!("creating symlink..."); + + // remove the previously linked folder + let _ = run_command( + "powershell", + vec![ + "-Command", + &format!("rm -r {}\\.jvem\\node", get_home_dir()), + ], + ); + let output = run_command( + "powershell", + vec![ + "-Command", + &format!( + "New-Item -Path {}\\.jvem\\node -ItemType Junction -Value {}", + get_home_dir(), + node_path + ), + ], + ); + if output.status.success() { + println!("done!"); + println!("set node version successfully"); + } else { + println!("failed: {}", String::from_utf8_lossy(&output.stderr)); + } +} + +#[cfg(target_os = "linux")] +fn usev_util(version: String) { + // remove the previously linked folder + let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + + let output = run_command( + "sh", + vec![ + "-c", + &format!( + "ln --symbolic {} {}/.jvem/node", + get_installation_dir(&version, "node"), + get_home_dir() + ), + ], + ); + + if output.status.success() { + println!("set node version successfully"); + } else { + println!("failed: {}", String::from_utf8_lossy(&output.stderr)) + } +} + +#[cfg(target_os = "macos")] +fn usev_util(version: String) { + let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + + let output = run_command( + "sh", + vec![ + "-c", + &format!( + "ln -s {} {}/.jvem/node", + get_installation_dir(&version, "node"), + get_home_dir() + ), + ], + ); + + if output.status.success() { + println!("set node version successfully"); + } else { + println!("failed: {}", String::from_utf8_lossy(&output.stderr)) + } +} + +pub fn usev(version: String) { + match check_node_exists(&version) { + true => usev_util(version), + false => println!("install the node version first"), + } +} diff --git a/src/main.rs b/src/main.rs index 1551f3f..4e58f74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,13 +9,7 @@ use std::sync::{ }; use clap::{Parser, Subcommand, ValueEnum}; -use commands::{ - java::{ - clean::clean, current::current, deactivate::deactivate, install::install, ls::ls, - lsrem::lsrem, uninstall::uninstall, usev::usev, - }, - maven, node, -}; +use commands::{java, maven, node}; use tokio::signal; #[derive(Parser, Debug)] @@ -145,23 +139,14 @@ impl std::str::FromStr for MavenCommand { async fn handle_java_action(action: Option, param: Option) { if let Some(action) = action { match action { - Command::Install => { - let jdk = param.unwrap(); - install(jdk); - } - Command::Current => current(), - Command::Uninstall => { - let jdk = param.unwrap(); - uninstall(jdk); - } - Command::Usev => { - let jdk = param.unwrap(); - usev(jdk).await; - } - Command::Lsrem => lsrem(), - Command::Ls => ls(), - Command::Deactivate => deactivate(), - Command::Clean => clean(), + Command::Install => java::install::install(param.unwrap()), + Command::Current => java::current::current(), + Command::Uninstall => java::uninstall::uninstall(param.unwrap()), + Command::Usev => java::usev::usev(param.unwrap()).await, + Command::Lsrem => java::lsrem::lsrem(), + Command::Ls => java::ls::ls(), + Command::Deactivate => java::deactivate::deactivate(), + Command::Clean => java::clean::clean(), } } else { println!("enter valid action. for more details use --help or -h"); @@ -187,7 +172,9 @@ async fn handle_nodejs_action(action: Option, param: Option NodeCommand::Clean => node::clean::clean(), NodeCommand::Current => node::current::current().await, NodeCommand::Deactivate => node::deactivate::deactivate(), - _ => println!("not implemented yet"), + NodeCommand::Install => node::install::install(param.unwrap()), + NodeCommand::Uninstall => node::uninstall::uninstall(param.unwrap()), + NodeCommand::Usev => node::usev::usev(param.unwrap()), } } else { println!("enter valid action, for more details use --help or -h"); diff --git a/src/utils/env_ops.rs b/src/utils/env_ops.rs index 7e5ca90..d5adaeb 100644 --- a/src/utils/env_ops.rs +++ b/src/utils/env_ops.rs @@ -38,7 +38,7 @@ pub fn get_download_link( } } -pub fn read_versions_nodejs() -> Result<(), Box> { +pub fn read_versions_node() -> Result<(), Box> { let output = run_command("curl", vec!["https://nodejs.org/dist/"]); let mut curr = 16; let mut display_vec = Vec::new(); @@ -62,7 +62,10 @@ pub fn read_versions_nodejs() -> Result<(), Box> { } } } else { - println!("couldn't connect to nodejs.org: {}", String::from_utf8_lossy(&output.stderr)); + println!( + "couldn't connect to nodejs.org: {}", + String::from_utf8_lossy(&output.stderr) + ); } Ok(()) @@ -74,7 +77,7 @@ fn parse_version(line: &str) -> Option<&str> { line.get(start..end) } -pub fn get_download_link_nodejs(version: &str, os: &str, arch: &str) { +pub fn get_download_link_node(version: &str, os: &str, arch: &str) -> Result> { let os_mapped = match os { "windows" => "win", "macos" => "darwin", @@ -94,16 +97,10 @@ pub fn get_download_link_nodejs(version: &str, os: &str, arch: &str) { _ => panic!("unsupported operating system"), }; - let x: Vec<&str> = version.split(".").collect(); - let link = format!( - "https://nodejs.org/dist/latest-v{}.x/node-v{}-{}-{}.{}", - x.get(0).unwrap(), - version, - os_mapped, - arch_mapped, - format_mapped + "https://nodejs.org/dist/v{0}/node-v{0}-{1}-{2}.{3}", + version, os_mapped, arch_mapped, format_mapped ); - println!("{}", link); + Ok(link) } diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 464e863..4adaac7 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -48,12 +48,18 @@ pub fn get_home_dir() -> String { res.to_string() } -pub fn get_installation_dir(name: &str) -> String { - format!("{}/.jvem/java_versions/{}", get_home_dir(), name) +pub fn get_installation_dir(name: &str, command: &str) -> String { + let res = match command { + "java" => &format!("{}/.jvem/java_versions/{}", get_home_dir(), name), + "node" => &format!("{}/.jvem/node_versions/{}", get_home_dir(), name), + "maven" => &format!("{}/.jvem/maven/bin", get_home_dir()), + _ => "", + }; + String::from(res) } pub fn check_jdk_exists(name: &str) -> bool { - let d_path = get_installation_dir(name); + let d_path = get_installation_dir(name, "java"); match Path::new(&d_path).exists() { true => true, false => false, @@ -61,13 +67,21 @@ pub fn check_jdk_exists(name: &str) -> bool { } pub fn check_maven_exists() -> bool { - let m_path = format!("{}/.jvem/maven/bin", get_home_dir()); + let m_path = get_installation_dir("", "maven"); match Path::new(&m_path).exists() { true => true, false => false, } } +pub fn check_node_exists(version: &str) -> bool { + let n_path = get_installation_dir(version, "node"); + match Path::new(&n_path).exists() { + true => true, + false => false, + } +} + pub fn check_path_exists(path: &str) -> bool { match Path::new(&path).exists() { true => true, @@ -85,6 +99,11 @@ pub fn create_maven_dir() { fs::create_dir_all(new_dir).unwrap(); } +pub fn create_node_dir() { + let new_dir = format!("{}/.jvem/node_versions", *HOME_DIR); + fs::create_dir_all(new_dir).unwrap(); +} + pub fn check_list_locally(command: &str) { let dir = match command { "java" => "java_versions", @@ -183,18 +202,26 @@ pub fn extract_tarball_linux(name: &str, command: &str) { pub fn extract_tarball_macos(name: &str, command: &str) { let temp_folder_name = format!("{}", rand::thread_rng().gen::()); let tar_path = match command { - "java" => &find_file_in_dir("/tmp/", &name), + "java" | "node" => &find_file_in_dir("/tmp/", &name), "maven" => "/tmp/maven.tar.gz", _ => "", }; let output_path = match command { "java" => &format!("/tmp/{}", temp_folder_name), + "node" => &format!("/tmp/{}", name), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; - let res = fs::create_dir_all(format!("/tmp/{}", temp_folder_name)); + let tmp_path = match command { + "java" | "maven" => &format!("/tmp/{}", temp_folder_name), + "node" => &format!("/tmp/{}", name), + _ => "", + }; + + let res = fs::create_dir_all(tmp_path); + println!("{}", output_path); match res { Ok(_) => { let tarball_status = run_command( @@ -225,6 +252,28 @@ pub fn extract_tarball_macos(name: &str, command: &str) { String::from_utf8_lossy(&mv_status.stderr) ); } + } else if command.eq("node") { + // NOTE: extracted into /tmp/{version} and moving it in ~/.jvem/node_versions + let mv_status = run_command( + "sh", + vec![ + "-c", + &format!( + "mv {} {}", + tmp_path, + &format!("{}/.jvem/node_versions/{}", get_home_dir(), name) + ), + ], + ); + + if mv_status.status.success() { + println!("done moving files around..."); + } else { + println!( + "moving files failed: {:?}", + String::from_utf8_lossy(&mv_status.stderr) + ); + } } } else { println!( @@ -241,7 +290,7 @@ pub fn extract_tarball_macos(name: &str, command: &str) { pub fn extract_zip(temp_dir: &str, name: &str, command: &str) { let output_path = match command { - "java" => &get_installation_dir(name), + "java" => &get_installation_dir(name, "java"), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; From 431b774c0889f3c8e711e4468b8a68e2d8a6b504 Mon Sep 17 00:00:00 2001 From: anusikh Date: Tue, 9 Jul 2024 03:46:01 +0530 Subject: [PATCH 10/24] (node) pipeline failure fix --- src/commands/node/install.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index d10f668..623a57d 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -1,6 +1,16 @@ use crate::utils::env_ops::get_download_link_node; use crate::utils::file_utils::{create_node_dir, extract_tarball_macos, run_command}; +#[cfg(target_os = "linux")] +fn install_util(version: String, link: String) { + println!("not implemented yet"); +} + +#[cfg(target_os = "windows")] +fn install_util(version: String, link: String) { + println!("not implemented yet"); +} + #[cfg(target_os = "macos")] fn install_util(version: String, link: String) { create_node_dir(); From e0fc7527808700058702e60a3693637f1b1b1da1 Mon Sep 17 00:00:00 2001 From: anusikh Date: Tue, 9 Jul 2024 03:50:27 +0530 Subject: [PATCH 11/24] (node) wrong param passed --- src/commands/node/usev.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/node/usev.rs b/src/commands/node/usev.rs index e74e7ff..c256c38 100644 --- a/src/commands/node/usev.rs +++ b/src/commands/node/usev.rs @@ -4,7 +4,7 @@ use crate::utils::file_utils::{ #[cfg(target_os = "windows")] fn usev_util(version: String) { - let node_path = get_installation_dir(&name, "node"); + let node_path = get_installation_dir(&version, "node"); println!("creating symlink..."); From 0223fd7a2ed19faf5a5f443dbbb3f6abc463e2fd Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 14 Jul 2024 04:36:26 +0530 Subject: [PATCH 12/24] (node) use fs symlink instead of run command to create symlinks --- src/commands/java/usev.rs | 78 ++++++++++++------------------------- src/commands/node/usev.rs | 81 +++++++++++---------------------------- src/utils/file_utils.rs | 2 +- 3 files changed, 48 insertions(+), 113 deletions(-) diff --git a/src/commands/java/usev.rs b/src/commands/java/usev.rs index ad11a28..de648ee 100644 --- a/src/commands/java/usev.rs +++ b/src/commands/java/usev.rs @@ -31,30 +31,15 @@ async fn usev_util(name: String) { let alias_future = tokio::spawn(async move { println!("creating symlink..."); - // remove the previously linked folder - let _ = run_command( - "powershell", - vec![ - "-Command", - &format!("rm -r {}\\.jvem\\java", get_home_dir()), - ], - ); - let output = run_command( - "powershell", - vec![ - "-Command", - &format!( - "New-Item -Path {}\\.jvem\\java -ItemType Junction -Value {}", - get_home_dir(), - java_path - ), - ], - ); - if output.status.success() { - println!("done!"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)); - } + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/java", get_home_dir())); + + let res = + std::os::windows::fs::symlink_file(java_path, format!("{}/.jvem/java", get_home_dir())); + + match res { + Ok(_) => println!("set jdk version successfully"), + Err(e) => println!("failed: {}", e.to_string()), + }; }); let (alias_task, java_home_task) = tokio::join!(alias_future, java_home_future); @@ -70,30 +55,22 @@ async fn usev_util(name: String) { #[cfg(target_os = "linux")] async fn usev_util(name: String) { // remove the previously linked folder - let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); - - let output = run_command( - "sh", - vec![ - "-c", - &format!( - "ln --symbolic {} {}/.jvem/java", - get_installation_dir(&name, "java"), - get_home_dir() - ), - ], + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/java", get_home_dir())); + + let res = std::os::unix::fs::symlink( + get_installation_dir(&name, "java"), + format!("{}/.jvem/java", get_home_dir()), ); - if output.status.success() { - println!("set jdk version successfully"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)) - } + match res { + Ok(_) => println!("set jdk version successfully"), + Err(e) => println!("failed: {}", e.to_string()), + }; } #[cfg(target_os = "macos")] async fn usev_util(name: String) { - let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/java", get_home_dir())); // check if Contents folder present inside extracted files (required for Graal VM support) let con_path = format!("{}/Contents", get_installation_dir(&name, "java")); @@ -106,19 +83,12 @@ async fn usev_util(name: String) { final_path = format!("{}/Home", get_installation_dir(&name, "java")); }; - let output = run_command( - "sh", - vec![ - "-c", - &format!("ln -s {} {}/.jvem/java", final_path, get_home_dir()), - ], - ); + let res = std::os::unix::fs::symlink(final_path, format!("{}/.jvem/java", get_home_dir())); - if output.status.success() { - println!("set jdk version successfully"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)) - } + match res { + Ok(_) => println!("set jdk version successfully"), + Err(e) => println!("failed: {}", e.to_string()), + }; } pub async fn usev(name: String) { diff --git a/src/commands/node/usev.rs b/src/commands/node/usev.rs index c256c38..aa3f30c 100644 --- a/src/commands/node/usev.rs +++ b/src/commands/node/usev.rs @@ -1,84 +1,49 @@ -use crate::utils::file_utils::{ - check_node_exists, get_home_dir, get_installation_dir, run_command, -}; +use crate::utils::file_utils::{check_node_exists, get_home_dir, get_installation_dir}; #[cfg(target_os = "windows")] fn usev_util(version: String) { let node_path = get_installation_dir(&version, "node"); println!("creating symlink..."); + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/node", get_home_dir())); - // remove the previously linked folder - let _ = run_command( - "powershell", - vec![ - "-Command", - &format!("rm -r {}\\.jvem\\node", get_home_dir()), - ], - ); - let output = run_command( - "powershell", - vec![ - "-Command", - &format!( - "New-Item -Path {}\\.jvem\\node -ItemType Junction -Value {}", - get_home_dir(), - node_path - ), - ], - ); - if output.status.success() { - println!("done!"); - println!("set node version successfully"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)); - } + let res = + std::os::windows::fs::symlink_file(node_path, format!("{}/.jvem/node", get_home_dir())); + + match res { + Ok(_) => println!("set node version successfully"), + Err(e) => println!("failed: {}", e.to_string()), + }; } #[cfg(target_os = "linux")] fn usev_util(version: String) { // remove the previously linked folder - let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/node", get_home_dir())); - let output = run_command( - "sh", - vec![ - "-c", - &format!( - "ln --symbolic {} {}/.jvem/node", - get_installation_dir(&version, "node"), - get_home_dir() - ), - ], + let res = std::os::unix::fs::symlink( + get_installation_dir(&version, "node"), + format!("{}/.jvem/node", get_home_dir()), ); - if output.status.success() { - println!("set node version successfully"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)) + match res { + Ok(_) => println!("set node version successfully"), + Err(e) => println!("failed: {}", e.to_string()), } } #[cfg(target_os = "macos")] fn usev_util(version: String) { - let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); + let _ = std::fs::remove_dir_all(&format!("{}/.jvem/node", get_home_dir())); - let output = run_command( - "sh", - vec![ - "-c", - &format!( - "ln -s {} {}/.jvem/node", - get_installation_dir(&version, "node"), - get_home_dir() - ), - ], + let res = std::os::unix::fs::symlink( + get_installation_dir(&version, "node"), + format!("{}/.jvem/node", get_home_dir()), ); - if output.status.success() { - println!("set node version successfully"); - } else { - println!("failed: {}", String::from_utf8_lossy(&output.stderr)) + match res { + Ok(_) => println!("set node version successfully"), + Err(e) => println!("failed: {}", e.to_string()), } } diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 4adaac7..b5ab420 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -300,7 +300,7 @@ pub fn extract_zip(temp_dir: &str, name: &str, command: &str) { vec![ "-Command", &format!( - "$ProgressPreference = 'SilentlyContinue';Expand-Archive -Path {0} -DestinationPath {1}; mv {1}\\*\\* {1};$ProgressPreference = 'Continue'", + "Set-Variable ProgressPreference = 'SilentlyContinue';Expand-Archive -Path {0} -DestinationPath {1}; mv {1}\\*\\* {1};", temp_dir, output_path, ), ], From a16a4c13952fd0237b30827002f663425cfa258e Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 14 Jul 2024 21:36:05 +0530 Subject: [PATCH 13/24] (node) check node exists (macos) --- src/commands/node/install.rs | 65 ++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index 623a57d..6ed6e42 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -1,37 +1,60 @@ use crate::utils::env_ops::get_download_link_node; -use crate::utils::file_utils::{create_node_dir, extract_tarball_macos, run_command}; +use crate::utils::file_utils::{ + check_node_exists, create_node_dir, extract_tarball_macos, run_command, +}; -#[cfg(target_os = "linux")] +#[cfg(target_os = "windows")] fn install_util(version: String, link: String) { - println!("not implemented yet"); + match check_node_exists(&version) { + false => { + println!(link); + } + true => { + println!("node version exists already, if it doesn't run the clean command") + } + } } -#[cfg(target_os = "windows")] +#[cfg(target_os = "linux")] fn install_util(version: String, link: String) { - println!("not implemented yet"); + match check_node_exists(&version) { + false => { + println!(link); + } + true => { + println!("node version exists already, if it doesn't run the clean command") + } + } } #[cfg(target_os = "macos")] fn install_util(version: String, link: String) { - create_node_dir(); + match check_node_exists(&version) { + false => { + create_node_dir(); - let temp_directory = format!("/tmp/{}.tar.gz", version); + let temp_directory = format!("/tmp/{}.tar.gz", version); - if std::path::Path::new(&temp_directory).exists() { - println!("fetching tarball from cache successful"); - extract_tarball_macos(&version, "node"); - } else { - println!("fetching tarball..."); - let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_macos(&version, "node"); + } else { + println!("fetching tarball..."); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); - if output.status.success() { - println!("fetching tarball successful "); - extract_tarball_macos(&version, "node"); - } else { - println!( - "fetching tarball failed: {} ", - String::from_utf8_lossy(&output.stderr) - ); + if output.status.success() { + println!("fetching tarball successful "); + extract_tarball_macos(&version, "node"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } + } + true => { + println!("node version already exists in fs, if it doesn't run the clean command") } } } From 43a25cede2fceb739cda89d370e968c8614db46c Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 14 Jul 2024 21:45:12 +0530 Subject: [PATCH 14/24] (node) fixing println error --- src/commands/node/install.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index 6ed6e42..5128084 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -7,7 +7,7 @@ use crate::utils::file_utils::{ fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - println!(link); + println!("{}", link); } true => { println!("node version exists already, if it doesn't run the clean command") @@ -19,7 +19,7 @@ fn install_util(version: String, link: String) { fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - println!(link); + println!("{}", link); } true => { println!("node version exists already, if it doesn't run the clean command") From be3a86e6b3c292e95c05f5d010bbfe6cef8f56b0 Mon Sep 17 00:00:00 2001 From: anusikh Date: Wed, 17 Jul 2024 11:34:01 +0530 Subject: [PATCH 15/24] (node) node version install and usev in linux --- src/commands/node/install.rs | 26 +++++++++++++++++++++++--- src/utils/env_ops.rs | 21 ++++++++++++++++----- src/utils/file_utils.rs | 12 ++++++++---- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index 5128084..24c3a28 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -1,6 +1,6 @@ use crate::utils::env_ops::get_download_link_node; use crate::utils::file_utils::{ - check_node_exists, create_node_dir, extract_tarball_macos, run_command, + check_node_exists, create_node_dir, extract_tarball_linux, extract_tarball_macos, run_command, }; #[cfg(target_os = "windows")] @@ -19,7 +19,27 @@ fn install_util(version: String, link: String) { fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - println!("{}", link); + create_node_dir(&version); + + let temp_directory = format!("/tmp/{}.tar.gz", version); + + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_linux(&version, "node"); + } else { + println!("fetching tarball..."); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); + + if output.status.success() { + println!("fetching tarball successful "); + extract_tarball_linux(&version, "node"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } } true => { println!("node version exists already, if it doesn't run the clean command") @@ -31,7 +51,7 @@ fn install_util(version: String, link: String) { fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - create_node_dir(); + create_node_dir(&version); let temp_directory = format!("/tmp/{}.tar.gz", version); diff --git a/src/utils/env_ops.rs b/src/utils/env_ops.rs index d5adaeb..87fe56f 100644 --- a/src/utils/env_ops.rs +++ b/src/utils/env_ops.rs @@ -6,7 +6,7 @@ pub fn read_versions() -> Result<(), Box> { let available_versions = constants::AVAILABLE_VERSIONS; let versions: Vec<&str> = available_versions.split(',').collect(); - println!("Availaible Versions:"); + println!("available versions:"); for version in versions { println!("{}", version.to_ascii_lowercase()); } @@ -42,10 +42,12 @@ pub fn read_versions_node() -> Result<(), Box> { let output = run_command("curl", vec!["https://nodejs.org/dist/"]); let mut curr = 16; let mut display_vec = Vec::new(); + + println!("available versions:"); if output.status.success() { let body = String::from_utf8_lossy(&output.stdout); for line in body.lines() { - if line.contains(">v") && line.contains("/<") { + if line.contains(" Result<(), Box> { } } } + + if !display_vec.is_empty() { + println!("nodejs v{}", curr.to_string()); + println!("{:?}", display_vec); + } } else { println!( "couldn't connect to nodejs.org: {}", @@ -72,12 +79,16 @@ pub fn read_versions_node() -> Result<(), Box> { } fn parse_version(line: &str) -> Option<&str> { - let start = line.find(">v")? + 2; - let end = line.find("/<")?; + let start = line.find("v")? + 1; + let end = line.find("/\">")?; line.get(start..end) } -pub fn get_download_link_node(version: &str, os: &str, arch: &str) -> Result> { +pub fn get_download_link_node( + version: &str, + os: &str, + arch: &str, +) -> Result> { let os_mapped = match os { "windows" => "win", "macos" => "darwin", diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index b5ab420..a6df6d4 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -99,8 +99,8 @@ pub fn create_maven_dir() { fs::create_dir_all(new_dir).unwrap(); } -pub fn create_node_dir() { - let new_dir = format!("{}/.jvem/node_versions", *HOME_DIR); +pub fn create_node_dir(name: &str) { + let new_dir = format!("{}/.jvem/node_versions/{}", *HOME_DIR, name); fs::create_dir_all(new_dir).unwrap(); } @@ -163,7 +163,7 @@ pub fn clean_jvem(command: &str) { pub fn extract_tarball_linux(name: &str, command: &str) { let tar_location = match command { - "java" => &find_file_in_dir("/tmp", name), + "java" | "node" => &find_file_in_dir("/tmp", name), "maven" => "/tmp/maven.tar.gz", _ => "", }; @@ -174,6 +174,11 @@ pub fn extract_tarball_linux(name: &str, command: &str) { get_home_dir(), String::from(name) ), + "node" => &format!( + "{}/.jvem/node_versions/{}", + get_home_dir(), + String::from(name) + ), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; @@ -221,7 +226,6 @@ pub fn extract_tarball_macos(name: &str, command: &str) { }; let res = fs::create_dir_all(tmp_path); - println!("{}", output_path); match res { Ok(_) => { let tarball_status = run_command( From 5ecb412b75095eee8ed0f4f0042192fb019eceb4 Mon Sep 17 00:00:00 2001 From: anusikh Date: Wed, 17 Jul 2024 15:28:11 +0530 Subject: [PATCH 16/24] (node) node version install (windows) --- src/commands/java/usev.rs | 23 ++++++++++++++++------ src/commands/node/current.rs | 29 +++++++++++++++++++++++++++- src/commands/node/install.rs | 37 ++++++++++++++++++++++++++++++++++-- src/commands/node/usev.rs | 20 ++++++++++++------- src/utils/file_utils.rs | 1 + 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/src/commands/java/usev.rs b/src/commands/java/usev.rs index de648ee..70a89f4 100644 --- a/src/commands/java/usev.rs +++ b/src/commands/java/usev.rs @@ -31,15 +31,26 @@ async fn usev_util(name: String) { let alias_future = tokio::spawn(async move { println!("creating symlink..."); + // remove previous symlink let _ = std::fs::remove_dir_all(&format!("{}/.jvem/java", get_home_dir())); - let res = - std::os::windows::fs::symlink_file(java_path, format!("{}/.jvem/java", get_home_dir())); + let res = run_command( + "powershell", + vec![ + "-Command", + &format!( + "New-Item -Path {}\\.jvem\\java -ItemType Junction -Value {}", + get_home_dir(), + java_path + ), + ], + ); - match res { - Ok(_) => println!("set jdk version successfully"), - Err(e) => println!("failed: {}", e.to_string()), - }; + if res.status.success() { + println!("done!"); + } else { + println!("failed: {}", String::from_utf8_lossy(&res.stderr)); + } }); let (alias_task, java_home_task) = tokio::join!(alias_future, java_home_future); diff --git a/src/commands/node/current.rs b/src/commands/node/current.rs index e84e165..496288e 100644 --- a/src/commands/node/current.rs +++ b/src/commands/node/current.rs @@ -1,6 +1,29 @@ use crate::utils::file_utils::run_command; -pub async fn current() { +#[cfg(target_os = "windows")] +pub async fn current_util() { + let node_task = tokio::spawn(async { + let res = run_command("powershell", vec!["-C", "node --version"]); + if res.status.success() { + println!("node version: {}", String::from_utf8_lossy(&res.stdout)); + } else { + println!("failed: node not set"); + } + }); + let npm_task = tokio::spawn(async { + let res = run_command("powershell", vec!["-C", "npm --version"]); + if res.status.success() { + println!("npm version: {}", String::from_utf8_lossy(&res.stdout)); + } else { + println!("failed: node not set"); + } + }); + + let _ = tokio::join!(node_task, npm_task); +} + +#[cfg(unix)] +pub async fn current_util() { let node_task = tokio::spawn(async { let res = run_command("node", vec!["--version"]); if res.status.success() { @@ -20,3 +43,7 @@ pub async fn current() { let _ = tokio::join!(node_task, npm_task); } + +pub async fn current() { + let _ = current_util().await; +} diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index 24c3a28..ffbb565 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -1,13 +1,46 @@ use crate::utils::env_ops::get_download_link_node; use crate::utils::file_utils::{ - check_node_exists, create_node_dir, extract_tarball_linux, extract_tarball_macos, run_command, + check_node_exists, create_node_dir, extract_tarball_linux, extract_tarball_macos, extract_zip, + get_home_dir, run_command, }; #[cfg(target_os = "windows")] fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - println!("{}", link); + create_node_dir(&version); + + let temp_directory = format!("{}/AppData/Local/Temp/{}.zip", get_home_dir(), version); + + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_zip(&temp_directory, &version, "node"); + } else { + println!("fetching zip..."); + + let output = run_command( + "powershell", + vec![ + "-Command", + "Set-Variable ProgressPreference SilentlyContinue ;", + "Invoke-WebRequest", + "-outf", + &temp_directory, + "-Uri", + &link, + ], + ); + + if output.status.success() { + println!("fetching zip successful "); + extract_zip(&temp_directory, &version, "node"); + } else { + println!( + "fetching zip failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } } true => { println!("node version exists already, if it doesn't run the clean command") diff --git a/src/commands/node/usev.rs b/src/commands/node/usev.rs index aa3f30c..40bd6e0 100644 --- a/src/commands/node/usev.rs +++ b/src/commands/node/usev.rs @@ -1,4 +1,4 @@ -use crate::utils::file_utils::{check_node_exists, get_home_dir, get_installation_dir}; +use crate::utils::file_utils::{check_node_exists, get_home_dir, run_command, get_installation_dir}; #[cfg(target_os = "windows")] fn usev_util(version: String) { @@ -7,13 +7,19 @@ fn usev_util(version: String) { println!("creating symlink..."); let _ = std::fs::remove_dir_all(&format!("{}/.jvem/node", get_home_dir())); - let res = - std::os::windows::fs::symlink_file(node_path, format!("{}/.jvem/node", get_home_dir())); + let res = run_command( + "powershell", + vec![ + "-Command", + &format!("New-Item -Path {}\\.jvem\\node -ItemType Junction -Value {}", get_home_dir(), node_path), + ], + ); - match res { - Ok(_) => println!("set node version successfully"), - Err(e) => println!("failed: {}", e.to_string()), - }; + if res.status.success() { + println!("done!"); + } else { + println!("failed: {}", String::from_utf8_lossy(&res.stderr)); + } } #[cfg(target_os = "linux")] diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index a6df6d4..636fe80 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -295,6 +295,7 @@ pub fn extract_tarball_macos(name: &str, command: &str) { pub fn extract_zip(temp_dir: &str, name: &str, command: &str) { let output_path = match command { "java" => &get_installation_dir(name, "java"), + "node" => &get_installation_dir(name, "node"), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; From 5be9f2a352bf69ee2b74fef67a0c1b330d8c6516 Mon Sep 17 00:00:00 2001 From: anusikh Date: Wed, 17 Jul 2024 17:49:42 +0530 Subject: [PATCH 17/24] (node) group common unix code --- src/commands/java/deactivate.rs | 12 +----------- src/commands/java/uninstall.rs | 15 +-------------- src/commands/node/deactivate.rs | 12 +----------- src/commands/node/uninstall.rs | 18 +----------------- 4 files changed, 4 insertions(+), 53 deletions(-) diff --git a/src/commands/java/deactivate.rs b/src/commands/java/deactivate.rs index ad3d349..088ae53 100644 --- a/src/commands/java/deactivate.rs +++ b/src/commands/java/deactivate.rs @@ -16,17 +16,7 @@ fn deactivate_util() { } } -#[cfg(target_os = "linux")] -fn deactivate_util() { - let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); - if output.status.success() { - println!("deactivation successful "); - } else { - println!("deactivation failed"); - } -} - -#[cfg(target_os = "macos")] +#[cfg(unix)] fn deactivate_util() { let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]); if output.status.success() { diff --git a/src/commands/java/uninstall.rs b/src/commands/java/uninstall.rs index 21db12f..70e4b68 100644 --- a/src/commands/java/uninstall.rs +++ b/src/commands/java/uninstall.rs @@ -16,20 +16,7 @@ fn uninstall_util(name: &str) { } } -#[cfg(target_os = "linux")] -fn uninstall_util(name: &str) { - let output = run_command( - "rm", - vec!["-rf", &format!("{}", get_installation_dir(&name, "java"))], - ); - if output.status.success() { - println!("uninstall successful "); - } else { - println!("uninstall failed: maybe the mentioned jdk is not installed locally"); - } -} - -#[cfg(target_os = "macos")] +#[cfg(unix)] fn uninstall_util(name: &str) { let output = run_command( "rm", diff --git a/src/commands/node/deactivate.rs b/src/commands/node/deactivate.rs index f667d2f..eafabc0 100644 --- a/src/commands/node/deactivate.rs +++ b/src/commands/node/deactivate.rs @@ -16,7 +16,7 @@ fn deactivate_util() { } } -#[cfg(target_os = "linux")] +#[cfg(unix)] fn deactivate_util() { let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); if output.status.success() { @@ -26,16 +26,6 @@ fn deactivate_util() { } } -#[cfg(target_os = "macos")] -fn deactivate_util() { - let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/node", get_home_dir())]); - if output.status.success() { - println!("deactivation successful"); - } else { - println!("deactivation failed"); - } -} - pub fn deactivate() { let _ = deactivate_util(); } diff --git a/src/commands/node/uninstall.rs b/src/commands/node/uninstall.rs index 9b76011..076b595 100644 --- a/src/commands/node/uninstall.rs +++ b/src/commands/node/uninstall.rs @@ -16,23 +16,7 @@ fn uninstall_util(version: &str) { } } -#[cfg(target_os = "linux")] -fn uninstall_util(version: &str) { - let output = run_command( - "rm", - vec![ - "-rf", - &format!("{}", get_installation_dir(&version, "node")), - ], - ); - if output.status.success() { - println!("uninstall successful "); - } else { - println!("uninstall failed: maybe the mentioned node version is not installed locally"); - } -} - -#[cfg(target_os = "macos")] +#[cfg(unix)] fn uninstall_util(version: &str) { let output = run_command( "rm", From 52deef6eb75fa83a56808c27dd6cd1bef5e72614 Mon Sep 17 00:00:00 2001 From: anusikh Date: Thu, 18 Jul 2024 16:21:37 +0530 Subject: [PATCH 18/24] (node) installation path issue macos --- src/commands/node/usev.rs | 10 ++++++++-- src/utils/file_utils.rs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/commands/node/usev.rs b/src/commands/node/usev.rs index 40bd6e0..9045b2d 100644 --- a/src/commands/node/usev.rs +++ b/src/commands/node/usev.rs @@ -1,4 +1,6 @@ -use crate::utils::file_utils::{check_node_exists, get_home_dir, run_command, get_installation_dir}; +use crate::utils::file_utils::{ + check_node_exists, get_home_dir, get_installation_dir, run_command, +}; #[cfg(target_os = "windows")] fn usev_util(version: String) { @@ -11,7 +13,11 @@ fn usev_util(version: String) { "powershell", vec![ "-Command", - &format!("New-Item -Path {}\\.jvem\\node -ItemType Junction -Value {}", get_home_dir(), node_path), + &format!( + "New-Item -Path {}\\.jvem\\node -ItemType Junction -Value {}", + get_home_dir(), + node_path + ), ], ); diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index 636fe80..b4d9a6e 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -265,7 +265,7 @@ pub fn extract_tarball_macos(name: &str, command: &str) { &format!( "mv {} {}", tmp_path, - &format!("{}/.jvem/node_versions/{}", get_home_dir(), name) + &format!("{}/.jvem/node_versions", get_home_dir()) ), ], ); From b46ec9bb1ca03ad3bd442afc452e5a4e912b86f3 Mon Sep 17 00:00:00 2001 From: anusikh Date: Fri, 19 Jul 2024 01:25:01 +0530 Subject: [PATCH 19/24] (test) added linux shell test --- .github/workflows/build_and_release.yml | 21 +++--- src/tests/test.sh | 96 +++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/tests/test.sh diff --git a/.github/workflows/build_and_release.yml b/.github/workflows/build_and_release.yml index 1d974ab..b8fdd91 100644 --- a/.github/workflows/build_and_release.yml +++ b/.github/workflows/build_and_release.yml @@ -33,11 +33,14 @@ jobs: needs: common-build runs-on: ubuntu-latest steps: - - name: Checkout code + - name: checkout code uses: actions/checkout@v2 - - name: build + - name: build and test run: | cargo build --all --release + bash src/tests/test.sh + - name: build + run: | mv target/release/jvem target/release/jvem_amd64 mkdir source mv target/release/jvem_amd64 source/ @@ -52,7 +55,7 @@ jobs: needs: common-build runs-on: windows-latest steps: - - name: Checkout code + - name: checkout code uses: actions/checkout@v2 - name: build run: | @@ -70,7 +73,7 @@ jobs: needs: common-build runs-on: macos-latest steps: - - name: Checkout code + - name: checkout code uses: actions/checkout@v2 - name: build run: | @@ -78,7 +81,7 @@ jobs: mkdir source mv target/release/jvem source/ tar -czvf ${{ env.MACOS_ARTIFACT }} -C source/ . - - name: Upload Artifact + - name: upload Artifact uses: actions/upload-artifact@v2 with: name: macos-artifact @@ -88,19 +91,19 @@ jobs: needs: [build-linux, build-win, build-macos] runs-on: ubuntu-latest steps: - - name: Checkout code + - name: checkout code uses: actions/checkout@v2 - - name: Download Linux Artifact + - name: download Linux Artifact uses: actions/download-artifact@v2 with: name: linux-artifact path: . - - name: Download Windows Artifact + - name: download Windows Artifact uses: actions/download-artifact@v2 with: name: windows-artifact path: . - - name: Download macOS Artifact + - name: download macOS Artifact uses: actions/download-artifact@v2 with: name: macos-artifact diff --git a/src/tests/test.sh b/src/tests/test.sh new file mode 100644 index 0000000..81c60d0 --- /dev/null +++ b/src/tests/test.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# setting environment variables +export PATH=$M2_HOME/bin:$PATH +export PATH=$PATH:$HOME/.jvem/java/bin +export PATH=$PATH:$HOME/.jvem/node/bin + +assert() { + local output="$1" + local expected_output="$2" + if [ "$output" == "$expected_output" ]; then + echo "[passed]" + else + echo "[failed]" + exit 1 + fi +} + +# node version management test +echo "node version management tests....." +target/release/jvem node install 21.7.3 +target/release/jvem node install 22.5.0 +output=$(target/release/jvem node ls) +expected_output=$( +cat < Date: Fri, 19 Jul 2024 01:30:36 +0530 Subject: [PATCH 20/24] (test) fixed linux test and used direct paths --- src/tests/test.sh | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/tests/test.sh b/src/tests/test.sh index 81c60d0..8c545aa 100644 --- a/src/tests/test.sh +++ b/src/tests/test.sh @@ -1,10 +1,5 @@ #!/bin/bash -# setting environment variables -export PATH=$M2_HOME/bin:$PATH -export PATH=$PATH:$HOME/.jvem/java/bin -export PATH=$PATH:$HOME/.jvem/node/bin - assert() { local output="$1" local expected_output="$2" @@ -30,12 +25,12 @@ EOF assert "$output" "$expected_output" target/release/jvem node usev 21.7.3 -output=$(node -v) +output=$(~/.jvem/node/bin/node -v) expected_output="v21.7.3" assert "$output" "$expected_output" target/release/jvem node usev 22.5.0 -output=$(node -v) +output=$(~/.jvem/node/bin/node -v) expected_output="v22.5.0" assert "$output" "$expected_output" @@ -48,13 +43,12 @@ EOF ) assert "$output" "$expected_output" - # java version management test echo "java version management tests....." target/release/jvem java install openjdk22 target/release/jvem java install openjdk17 target/release/jvem java usev openjdk17 -output=$(java --version) +output=$(~/.jvem/java/bin/java --version) expected_output=$( cat < Date: Fri, 19 Jul 2024 01:35:27 +0530 Subject: [PATCH 21/24] (test) fix maven test for linux and action step rename --- .github/workflows/build_and_release.yml | 2 +- src/tests/test.sh | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_and_release.yml b/.github/workflows/build_and_release.yml index b8fdd91..a5ac517 100644 --- a/.github/workflows/build_and_release.yml +++ b/.github/workflows/build_and_release.yml @@ -39,7 +39,7 @@ jobs: run: | cargo build --all --release bash src/tests/test.sh - - name: build + - name: package run: | mv target/release/jvem target/release/jvem_amd64 mkdir source diff --git a/src/tests/test.sh b/src/tests/test.sh index 8c545aa..225fd6e 100644 --- a/src/tests/test.sh +++ b/src/tests/test.sh @@ -76,11 +76,8 @@ target/release/jvem maven install output=$(~/.jvem/maven/bin/mvn --version) expected_output=$( cat < Date: Fri, 19 Jul 2024 01:48:51 +0530 Subject: [PATCH 22/24] (test) i hope this fixes linux test :( --- src/tests/test.sh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/tests/test.sh b/src/tests/test.sh index 225fd6e..3970b2d 100644 --- a/src/tests/test.sh +++ b/src/tests/test.sh @@ -11,6 +11,16 @@ assert() { fi } +starts_with() { + local output="$1" + local start="$2" + if [[ $output =~ ^"$start" ]]; then + echo "[passed]" + else + echo "[failed]" + fi +} + # node version management test echo "node version management tests....." target/release/jvem node install 21.7.3 @@ -74,13 +84,8 @@ assert "$output" "$expected_output" echo "node version management tests....." target/release/jvem maven install output=$(~/.jvem/maven/bin/mvn --version) -expected_output=$( -cat < Date: Fri, 19 Jul 2024 19:33:44 +0530 Subject: [PATCH 23/24] (chore) README update --- README.md | 134 +++++++++++++----------------------------------------- 1 file changed, 32 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 6307a01..09eeac5 100644 --- a/README.md +++ b/README.md @@ -1,113 +1,43 @@ -# Java Version Manager (JVeM) +# JVeM -![JVeM](/public/final-logo-black.png) +![](/public/final-logo-black.png) -Java Version Manager (JVeM) is a cross-platform tool designed to simplify the management of multiple Java versions on your system. Whether you're a developer working on diverse projects or an enthusiast exploring different Java applications, JVeM makes it easy to switch between and manage various Java versions seamlessly. +a simple version manager for java, node and maven +- *cross-platform support:* consistent experience across different operating systems. +- *simple version switching:* easily switch between different java/node versions. -## Table of Contents +#### getting started: -1. [Features](#features) -2. [Supported Platforms](#supported-platforms) -3. [Getting Started](#getting-started) - - [Windows Installation](#windows-installation) - - [macOS Installation](#macos-installation) - - [Linux Installation](#linux-installation) -4. [Usage](#usage) - - [Commands and Descriptions](#commands-and-descriptions) -5. [Future Scope](#future-scope) +to get started with jvem, refer to the installation instructions and basic usage guidelines below. -## Features: +#### windows installation -- **Cross-Platform Support:** JVeM is compatible with macOS, Linux, and Windows, providing a consistent experience across different operating systems. -- **Simple Version Switching:** Easily switch between different Java versions with a single command, streamlining your development workflow. -- **Effortless Installation:** JVeM offers a straightforward installation process on supported platforms, ensuring quick setup without unnecessary complications. -- **Customizable Configuration:** Tailor JVeM to your specific needs by configuring environment variables and settings to suit your Java development environment. +- download the latest version from [releases](https://github.com/anusikh/jvem/releases) +- add these path to your environment variables: + ``` + C:\Users\\.jvem\java\bin + C:\Users\\.jvem\node + C:\Users\\.jvem\maven + + ``` +#### macos/linux installation -## Supported Platforms: +- download the latest version from [releases](https://github.com/anusikh/jvem/releases) +- add the following lines to `~/.zshrc` or `~/.bashrc` + ``` + PATH="$M2_HOME/bin:$PATH" + PATH=$PATH:$HOME/.jvem/java/bin + PATH=$PATH:$HOME/.jvem/node/bin + JAVA_HOME=$HOME/.jvem/java + JDK_HOME=$HOME/.jvem/java + alias jvem= + ``` -- Windows -- Linux -- macOS +#### usage: -## Getting Started: +- just run the `jvem --help` command -To get started with JVeM, refer to the installation instructions and basic usage guidelines below. +#### note for macos: -#### Windows Installation - -- Download the latest `jvem.zip` file from [Releases](https://github.com/anusikh/jvem/releases) -- Extract this file here. -- Add this path to your environment variables: `C:\Users\\.jvem\java\bin` -- Add the path of the extracted folder to environment variables. - -#### macOS Installation - -- Download the latest `jvem_macos.tar.gz` file from [Releases](https://github.com/anusikh/jvem/releases) -- Extract the tarball file. -- Add this line to your zshrc/bashrc file: `export JAVA_HOME=$HOME/.jvem/java` and source it. -- Add an alias to use jvem easily. `alias jvem=~/Downloads/jvem` - -#### Linux Installation - -- Download the latest `jvem_linux.tar.gz` file from [Releases](https://github.com/anusikh/jvem/releases) -- Extract the tarball file. -- Add this line to your zshrc/bashrc file: `PATH=$PATH:$HOME/.jvem/java/bin` -- Also add this line `JAVA_HOME=$HOME/.jvem/java` and then source the rc file -- Add an alias to use jvem easily. `alias jvem=~/Downloads/jvem` - -## Usage: - -Once installed, use the following commands to manage your Java versions. -1. Check all versions of java supported by JVeM: -``` -jvem lsrem -``` -2. Install a new version on your system: -``` -jvem install zulu17 -``` -3. Start using the java version installed -``` -jvem usev zulu17 -``` -4. Check if java got installed: - - If java versions < 8: -`java -version ` - else: -`java --version` -5. Uninstall java -`jvem uninstall zulu17` - - -## Commands and Descriptions - -| Command | Description | -| ------------------------------ | ---------------------------------------------- | -| `jvem lsrem` | List all JDK versions available for install. | -| `jvem ls` | List locally installed JDK versions. | -| `jvem install zulu21` | Install Zulu JDK version 21. | -| `jvem usev zulu17` | Use a specific JDK version after installation. | -| `jvem current` | Find the currently active JDK version. | -| `jvem deactivate` | Deactivate the currently active JDK. | -| `jvem uninstall zulu17` | Uninstall the specified JDK version. | -| `jvem help` | Display help information. | -| `jvem clean` | Clean empty folders in the .jvem directory. | -| `jvem --V` or `jvem --version` | Display the version of JVeM. | - -## Future Scope: - -JVeM is an evolving project, and future updates may include: - -- Addition of more JDKs -- Addition of support for new platforms. -- Integration of test cases to enhance stability. -- Bug fixes and improvements based on user feedback. - -Explore the full capabilities of JVeM to enhance your Java development experience! - - - -### Note for MacOs: -This might show an error when trying to use it for the first time, you would need to allow it from settings > under privacy and security: -Screenshot 2024-03-09 at 2 14 49 AM +you would need to allow it from settings > under privacy and security: + From cd23118e4d498c3e6a46791e4ebb46334924898d Mon Sep 17 00:00:00 2001 From: anusikh Date: Sun, 21 Jul 2024 18:04:27 +0530 Subject: [PATCH 24/24] (chore) fixed imports and updated .gitignore --- .gitignore | 1 + src/commands/maven/install.rs | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 6172753..33f88cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /vendor .vscode +.idea diff --git a/src/commands/maven/install.rs b/src/commands/maven/install.rs index 4d35ab2..ec23393 100644 --- a/src/commands/maven/install.rs +++ b/src/commands/maven/install.rs @@ -2,7 +2,7 @@ use lazy_static::lazy_static; use crate::utils::file_utils::{ check_maven_exists, create_maven_dir, extract_tarball_linux, extract_tarball_macos, - find_file_in_dir, get_home_dir, run_command, + extract_zip, find_file_in_dir, get_home_dir, run_command, }; lazy_static! { @@ -16,8 +16,6 @@ lazy_static! { #[cfg(target_os = "windows")] fn install_util() { - use crate::utils::file_utils::extract_zip; - match check_maven_exists() { false => { create_maven_dir();