Skip to content

Commit 8ffb58f

Browse files
committed
feat: install packages extensions by reading its type and add uninstall extension feature
1 parent ab142d8 commit 8ffb58f

File tree

2 files changed

+152
-5
lines changed

2 files changed

+152
-5
lines changed

src-tauri/src/extensions.rs

+101-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub fn install_themes(extension_path: PathBuf) {
285285
let current_extension_config = storage::read_data("extensions".to_string());
286286
let current_extension_config = match current_extension_config {
287287
Ok(config) => {
288-
if config.status {
288+
if config.status && config.data.is_object() {
289289
config.data
290290
} else {
291291
serde_json::json!({})
@@ -300,13 +300,18 @@ pub fn install_themes(extension_path: PathBuf) {
300300
Some(_) => current_extension_config,
301301
None => {
302302
let mut current_extension_config = current_extension_config.clone();
303-
current_extension_config["themes"] = serde_json::json!([]);
303+
// Create themes key on current extension config
304+
current_extension_config
305+
.as_object_mut()
306+
.unwrap()
307+
.insert("themes".to_string(), serde_json::json!({}));
304308
current_extension_config
305309
}
306310
};
307311

308312
let themes = current_extension_config.get("themes").unwrap();
309-
let themes = themes.as_array().unwrap();
313+
let empty_vec = Vec::new();
314+
let themes = themes.as_array().unwrap_or(&empty_vec);
310315

311316
// Remove theme if already installed
312317
let mut themes = themes
@@ -327,3 +332,96 @@ pub fn install_themes(extension_path: PathBuf) {
327332
.insert("themes".to_string(), serde_json::json!(themes));
328333
storage::write_data("extensions".to_string(), current_extension_config);
329334
}
335+
336+
pub fn install_extensions(extension_path: PathBuf) {
337+
let extension_file: Result<serde_json::Value, serde_json::Error> = serde_json::from_str(
338+
std::fs::read_to_string(extension_path.clone())
339+
.unwrap()
340+
.as_str(),
341+
);
342+
let extension_information = match extension_file {
343+
Ok(file) => file,
344+
Err(_) => {
345+
panic!("Error parsing extension file");
346+
}
347+
};
348+
349+
// Check the type of packaged extension
350+
if extension_information
351+
.get("extensionType")
352+
.unwrap_or(&serde_json::Value::Null)
353+
!= "theme"
354+
{
355+
panic!("Only theme extension is supported right now");
356+
}
357+
install_themes(extension_path);
358+
}
359+
360+
pub fn uninstall_extensions(extension_identifier: String) {
361+
let extensions = storage::read_data("extensions".to_string());
362+
let mut extensions = match extensions {
363+
Ok(config) => {
364+
if config.status {
365+
config.data
366+
} else {
367+
serde_json::json!({})
368+
}
369+
}
370+
Err(_) => {
371+
serde_json::json!({})
372+
}
373+
};
374+
// Iterate on every object of extensions and iterate extensions key of the object and remove the extension
375+
let mut extensions = extensions.as_object_mut().unwrap().clone();
376+
let mut new_extensions = serde_json::json!({}).as_object_mut().unwrap().clone();
377+
for (key, value) in extensions {
378+
let value = value.as_array().unwrap().clone();
379+
let new_value = value
380+
.iter()
381+
.filter(|ext| {
382+
let _empty_string = serde_json::Value::String("".to_string());
383+
let ext_identifier = ext.get("identifier").unwrap_or(&_empty_string);
384+
if key == "themes" && ext_identifier == &extension_identifier {
385+
let theme = storage::read_data("theme".to_string());
386+
let theme = match theme {
387+
Ok(config) => {
388+
if config.status {
389+
config.data
390+
} else {
391+
serde_json::json!({})
392+
}
393+
}
394+
Err(_) => {
395+
serde_json::json!({})
396+
}
397+
};
398+
let theme = theme.as_object().unwrap().clone();
399+
let current_active_theme = theme
400+
.get("theme")
401+
.unwrap_or(&serde_json::Value::String("".to_string()))
402+
.to_string();
403+
404+
let current_active_theme =
405+
current_active_theme.split("@").collect::<Vec<_>>()[0].to_string();
406+
// If the current_active_theme starts with unused " character, remove it
407+
let current_active_theme = if current_active_theme.starts_with("\"") {
408+
current_active_theme.split_at(1).1.to_string()
409+
} else {
410+
current_active_theme
411+
};
412+
let current_active_theme = serde_json::Value::String(current_active_theme);
413+
if &current_active_theme == ext_identifier {
414+
storage::write_data(
415+
"theme".to_string(),
416+
serde_json::json!({"theme": "System Default"}),
417+
);
418+
}
419+
}
420+
ext_identifier != &extension_identifier
421+
})
422+
.cloned()
423+
.collect::<Vec<_>>();
424+
new_extensions.insert(key.to_string(), serde_json::json!(new_value));
425+
}
426+
storage::write_data("extensions".to_string(), serde_json::json!(new_extensions));
427+
}

src-tauri/src/main.rs

+51-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ lazy_static! {
5858
.multiple_values(false),
5959
),
6060
),
61+
)
62+
.subcommand(
63+
App::new("install")
64+
.about("Install extension from packaged json file")
65+
.arg(
66+
Arg::new("extension")
67+
.about("Packaged extension file")
68+
.takes_value(true)
69+
.multiple_values(false),
70+
),
71+
)
72+
.subcommand(
73+
App::new("uninstall").about("Uninstall extension").arg(
74+
Arg::new("extension")
75+
.about("Extension identifier")
76+
.takes_value(true)
77+
.multiple_values(true),
78+
),
6179
),
6280
)
6381
.arg(
@@ -124,8 +142,8 @@ fn change_transparent_effect(effect: String, window: tauri::Window) {
124142
fn main() {
125143
// Extensions stuff
126144
if ARGS_STRUCT.subcommand_matches("extensions").is_some() {
127-
let extension_type = ARGS_STRUCT.subcommand_matches("extensions").unwrap();
128-
match extension_type.subcommand_matches("theme") {
145+
let extension_cmd = ARGS_STRUCT.subcommand_matches("extensions").unwrap();
146+
match extension_cmd.subcommand_matches("theme") {
129147
Some(theme_command) => {
130148
match theme_command.subcommand_matches("build") {
131149
Some(theme_build_info) => {
@@ -183,6 +201,37 @@ fn main() {
183201
}
184202
None => {}
185203
}
204+
match extension_cmd.subcommand_matches("install") {
205+
Some(extension_install_info) => {
206+
let extension = extension_install_info.value_of("extension");
207+
if extension.is_some() {
208+
let extension = extension.unwrap();
209+
let extension = Path::new(extension);
210+
if extension.exists() && extension.is_file() {
211+
extensions::install_extensions(extension.to_path_buf())
212+
} else {
213+
panic!("Extension file not found");
214+
}
215+
} else {
216+
panic!("No extension specified");
217+
}
218+
std::process::exit(0);
219+
}
220+
None => {}
221+
}
222+
match extension_cmd.subcommand_matches("uninstall") {
223+
Some(extension_uninstall_info) => {
224+
let extension = extension_uninstall_info.value_of("extension");
225+
if extension.is_some() {
226+
let extension = extension.unwrap();
227+
extensions::uninstall_extensions(extension.to_string());
228+
} else {
229+
panic!("No extension specified");
230+
}
231+
std::process::exit(0);
232+
}
233+
None => {}
234+
}
186235
}
187236
tauri::Builder::default()
188237
.invoke_handler(tauri::generate_handler![

0 commit comments

Comments
 (0)