Skip to content

Commit

Permalink
feat: add 'get local' command
Browse files Browse the repository at this point in the history
Now it support load jdk from local folder
  • Loading branch information
bic-potato committed Feb 15, 2022
1 parent 03d2b63 commit 684e39c
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 117 deletions.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
6 changes: 6 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
11 changes: 11 additions & 0 deletions .vs/java-version/project-colors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Version": 1,
"ProjectMap": {
"a2fe74e1-b743-11d0-ae1a-00a0c90fffc3": {
"ProjectGuid": "a2fe74e1-b743-11d0-ae1a-00a0c90fffc3",
"DisplayName": "杂项文件",
"ColorIndex": -1
}
},
"NextColorIndex": 0
}
Binary file added .vs/java-version/v17/.suo
Binary file not shown.
Binary file added .vs/slnx.sqlite
Binary file not shown.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "jvman"
version = "0.1.2"
version = "0.2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -18,10 +18,9 @@ zip = "0.5.13"
dotenv = "0.15.0"
console = "0.15.0"
indicatif = "0.16.2"
winapi = {version="0.3.9", features = ["winuser"]}
# windows-sys = {version = "0.32.0", features = ["Win32_Foundation","Win32_Security", "Win32_System_Threading", "Win32_System_SystemServices"]}
[[bin]]
name="jvman"
path="src/main.rs"


[[test]]
name = "parser_test"
Expand Down
23 changes: 15 additions & 8 deletions src/java_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use download_rs::async_download::Download;
use json;
use reqwest;
use console::Style;
use crate::utils::ziputil;

pub async fn list_remote() -> json::JsonValue {
let url = String::from("https://api.adoptium.net/v3/info/available_releases?image_type=jdk&os=windows&page=0&release_type=ga&sort_order=ASC");
Expand Down Expand Up @@ -39,8 +40,9 @@ pub async fn get_remote(version: &str) {
{
let file_name = result[i]["binary"]["package"]["name"].to_string();
let version_name = result[i]["release_name"].to_string();
let version_list = java_ver::read_version();
if !version_list.contains(&version_name) {
let mut version_store = java_ver::read_version();

if !version_store.contains(&version_name, "Eclipse Adoptium") {
// println!("{}", file_name);
let download_url = format!(
"https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/{}/jdk/{}/windows/{}",
Expand All @@ -61,14 +63,19 @@ pub async fn get_remote(version: &str) {
java_location.pop();
java_location.push("java/");
java_location.push(&version_name);
let java: java_ver::Java = java_ver::Java::new(
version_name,
String::from("Hotspot"),
String::from("jdk"),
String::from(java_location.to_str().unwrap()),
let java: java_ver::JavaNew = java_ver::JavaNew::new(
"Eclipse Adoptium",
&version_name,
"Hotspot",
"jdk",
java_location.to_str().unwrap(),
);
let mut current_location = std::env::current_exe().unwrap();
current_location.pop();
current_location.push("java/");
let file = String::from(save_location) + &file_name;
java_ver::version_record(std::path::Path::new(&file), java);
ziputil::extract(std::path::Path::new(&file), &current_location);
java_ver::version_record(java);
match std::fs::remove_file(std::path::Path::new(&file)) {
Ok(_) => println!("{}", green.apply_to("jdk install finish!")),
Err(e) => println!("{} temp file delete failed, {}", red.apply_to("Error"), e.to_string())
Expand Down
206 changes: 123 additions & 83 deletions src/java_ver.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use crate::utils::ziputil;
use std::error::Error;
use std::fs::File;
use crate::utils::{uac_utils, ziputil};
use console::Style;
use serde::Deserialize;
use serde::Serialize;
use std::io::Write;
use std::path;
use std::{fs, path, result};
use std::path::Path;
use std::path::PathBuf;
use toml;
// use windows_sys::Win32::Foundation;
// use windows_sys::Win32::Foundation::HANDLE;
// use windows_sys::Win32::Foundation::LUID;
// use windows_sys::Win32::Security;
// use windows_sys::Win32::Security::TOKEN_ADJUST_PRIVILEGES;
// use windows_sys::Win32::System::SystemServices::SE_CREATE_SYMBOLIC_LINK_NAME;
// use windows_sys::Win32::System::Threading;
use crate::utils::release_utils::ReleaseParser;


#[derive(Serialize, Deserialize)]
pub struct Java {
Expand All @@ -23,6 +20,7 @@ pub struct Java {
path: String,
}

#[derive(Serialize, Deserialize)]
pub struct JavaNew {
implementor: String,
full_version: String,
Expand All @@ -32,13 +30,26 @@ pub struct JavaNew {
}

impl JavaNew {
pub fn new ( implementor: String,
full_version: String,
jvm_variant: String,
image_type: String,
path: String)-> JavaNew
pub fn new(implementor: &str,
full_version: &str,
jvm_variant: &str,
image_type: &str,
path: &str) -> JavaNew
{
return JavaNew{implementor, full_version, jvm_variant, image_type, path};
let implementor = implementor.to_string();
let full_version = full_version.to_string();
let jvm_variant = jvm_variant.to_string();
let image_type = image_type.to_string();
let path = path.to_string();
return JavaNew { implementor, full_version, jvm_variant, image_type, path };
}
pub fn get_implementor(&mut self) -> String {
let out = self.implementor.to_owned();
return out;
}
pub fn get_full_version(&mut self) -> String {
let out = self.full_version.to_owned();
return out;
}
}

Expand All @@ -59,109 +70,138 @@ impl Java {
}

#[derive(Serialize, Deserialize)]
struct Store {
pub struct Store {
Java_Version: Option<Vec<Java>>,
}

pub fn read_version() -> Vec<String> {
let mut current_location = std::env::current_exe().unwrap();
current_location.pop();
current_location.push("versions.toml");
let contents = std::fs::read_to_string(&current_location).expect("Unable to load Version Files");
let store: Store = toml::from_str(&contents).unwrap();
let mut versions: Vec<String> = Vec::new();
#[derive(Serialize, Deserialize)]
pub struct StoreNew {
java_version: Option<Vec<JavaNew>>,
}

impl StoreNew {
pub fn new() -> StoreNew {
let mut vec: Vec<JavaNew> = Vec::new();
return StoreNew { java_version: Some(vec) };
}
pub fn add(&mut self, obj: JavaNew) {
let mut list = self.java_version.as_mut().unwrap();
list.push(obj);
}
pub fn get_java_versions(&mut self) -> &mut Vec<JavaNew> {
let mut list = self.java_version.as_mut().unwrap();
return list;
}

pub fn get_full_version_list(&mut self) -> Vec<String> {
let mut list = self.java_version.as_mut().unwrap();
let mut version_list: Vec<String> = Vec::new();
for element in list {
version_list.push(element.full_version.to_string());
}
version_list
}


pub fn contains(&mut self, version: &str, implementor: &str) -> bool {
let list = self.java_version.as_ref().unwrap();

for element in list {
if element.full_version == String::from(version) && element.implementor == String::from(implementor) {
return true;
}
}
return false;
}
}

pub fn old_to_new(store: Store) -> StoreNew {
let mut new = StoreNew::new();
if let Some(list) = store.Java_Version {
for element in list {
let version = element.full_version;
versions.push(version)
let java_new = JavaNew::new("Eclipse Adoptium", &element.full_version, &element.jvm_variant, &element.image_type, &element.path);
new.add(java_new);
}
} else {
panic!("Error on converting record file!");
}
return versions;
return new;
}

pub fn version_record(file: &Path, java_config: Java) {

pub fn read_version() -> StoreNew {
let mut current_location = std::env::current_exe().unwrap();
current_location.pop();
current_location.push("java/");
ziputil::extract(&file, &current_location);
current_location.push("versions.toml");
let contents = std::fs::read_to_string(&current_location).expect("Unable to load Version Files");
let mut result: Store = toml::from_str(&contents).unwrap();
if let Some(store) = result.Java_Version {
let javaverisions = old_to_new( toml::from_str(&contents).unwrap());
let content_string = toml::to_string(&javaverisions).unwrap();
fs::write(&current_location, content_string).unwrap();
return javaverisions;
} else {
let result: StoreNew = toml::from_str(&contents).unwrap();
return result;
}
}


pub fn version_record(java_config: JavaNew) {
let mut version_file = std::env::current_exe().unwrap();
version_file.pop();
version_file.push("versions.toml");
let mut file = std::fs::File::options()
.append(true)
.open(&version_file)
.unwrap();
let mut list: Vec<Java> = Vec::new();
list.push(java_config);
let conf: Store = Store {
Java_Version: Some(list),
};
let mut conf = read_version();
conf.add(java_config);
let config = toml::to_string(&conf).unwrap();
file.write(config.as_bytes()).expect("Err");
fs::write(&version_file, config.as_bytes()).expect("Err");
}

pub fn enable_version(version: &str) {
let mut current_location = std::env::current_exe().unwrap();
current_location.pop();
current_location.push("versions.toml");
let contents = std::fs::read_to_string(&current_location).expect("Unable to load Version Files");
let store: Store = toml::from_str(&contents).unwrap();
if let Some(list) = store.Java_Version {
for element in list {
if version == element.full_version {
pub fn enable_version(implementor: &str, version: &str) {
let store: StoreNew = read_version();
if let Some(lists) = store.java_version {
for element in lists {
if version == element.full_version && implementor == element.implementor {
let mut path = PathBuf::new();
path.push(&element.path);
//path.push("bin/");
let mut current_location = std::env::current_exe().unwrap();
current_location.pop();
current_location.push("OpenJDK/");
// unsafe {
// let hToken: HANDLE;
// let mut token = &mut hToken;
// let mut retn = Threading::OpenProcessToken(
// Threading::GetCurrentProcess(),
// TOKEN_ADJUST_PRIVILEGES,
// token,
// );
// let mut luid: LUID;
// let mut luid_pointer = &mut luid;
// let _ = Security::LookupPrivilegeValueA(
// None,
// &SE_CREATE_SYMBOLIC_LINK_NAME,
// luid_pointer,
// );
// let mut structs = Security::LUID_AND_ATTRIBUTES {
// Luid: luid,
// Attributes: Security::SE_PRIVILEGE_ENABLED,
// };

// let token = Security::TOKEN_PRIVILEGES {
// PrivilegeCount: 1,
// Privileges: [structs],
// };
// let secure = Security::AdjustTokenPrivileges(
// Threading::GetCurrentProcess(),
// 0,
// &mut token,
// 0,
// None,
// None
// );
// uac_utils::get_privilage();
// }
let result = std::fs::remove_dir_all(&current_location);
let _ = std::fs::remove_dir_all(&current_location);

let result = std::os::windows::fs::symlink_dir(&path, &current_location);
match result {
Ok(_) => {
let green = Style::new().green();
println!("{}, JDK VERSION:{}",green.apply_to("Enable SUCCESS"), version)
println!("{}, JDK VERSION:{}", green.apply_to("Enable SUCCESS"), version)
}
Err(e) =>{
Err(e) => {
let red = Style::new().red();
println!("{}, {}", red.apply_to("Enable FAILED") ,e.to_string());
println!("{}, {}", red.apply_to("Enable FAILED"), e.to_string());
}
}
}
}
}
}


pub fn read_local(path: &str) {
let mut record = read_version();
let mut release_parser = ReleaseParser::new(path);
let java = release_parser.parse();

if !record.contains(&java.full_version, &java.implementor) {
version_record(java);
let green = Style::new().green();
println!("{}", green.apply_to("jdk install finish!"))
} else {
let red = Style::new().red();
println!("{}JDK already exist!", red.apply_to("Error"));
}
}

Loading

0 comments on commit 684e39c

Please sign in to comment.