-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
87 lines (72 loc) · 2.58 KB
/
mod.rs
File metadata and controls
87 lines (72 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::io::{Read, Write};
use crate::registry::Registry;
const PACKAGE_DIRECTORY: &str = "fx_modules";
const MODULES_FILE: &str = "modules.json";
pub struct PackageInstall {
package_name: String,
version: String,
}
impl PackageInstall {
pub fn new() -> Self {
PackageInstall {
package_name: String::new(),
version: String::new(),
}
}
pub async fn install(&mut self, working_dir: &str, package: &str) {
if !modules_file_exists(working_dir) {
println!("No modules.json file found in the working directory.");
return;
}
self.package_name = parse_package_name(package);
self.version = parse_version(package);
if self.version.is_empty() {
self.version = "latest".to_string();
}
match Registry::download_package(&self.package_name, &self.version).await {
Ok(_) => println!("Package downloaded successfully"),
Err(_) => eprintln!("Failed to download package"),
}
}
}
pub fn init_fxpkg(working_dir: &str) {
let modules_file_path = format!("{}/{}", working_dir, MODULES_FILE);
if !std::path::Path::new(&modules_file_path).exists() {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let stub_path = format!("{}/{}", manifest_dir, "stubs/modules.json");
let mut stub_file =
std::fs::File::open(stub_path).expect("Failed to open stub modules.json file");
let mut stub_content = String::new();
stub_file
.read_to_string(&mut stub_content)
.expect("Failed to read stub modules.json file");
let file =
std::fs::File::create(&modules_file_path).expect("Failed to create modules.json file");
let mut file = std::io::BufWriter::new(file);
file.write_all(stub_content.as_bytes())
.expect("Failed to write to modules.json file");
println!("Created modules.json file successfully.");
} else {
println!("modules.json file already exists.");
}
}
fn modules_file_exists(working_dir: &str) -> bool {
let path = format!("{}/modules.json", working_dir);
std::fs::metadata(path).is_ok()
}
fn parse_package_name(package: &str) -> String {
let parts: Vec<&str> = package.split('@').collect();
if parts.len() > 1 {
parts[0].to_string()
} else {
package.to_string()
}
}
fn parse_version(package: &str) -> String {
let parts: Vec<&str> = package.split('@').collect();
if parts.len() > 1 {
parts[1].to_string()
} else {
String::new()
}
}